Andre Schütz - Learn. Think. Reflect - Tech developer
Code generation with Slick code generator of PostgreSQL database in Scala project

Code generation with Slick code generator of PostgreSQL database in Scala project

If you automatically want to generate the Tables mapping of your database tables for the use within the Slick environment, you can use the Slick code generator to achieve this task.

The groundwork contains:

  • Database tables definitions within the database to generate the mappings
  • Slick code generator integration to your project
  • Execute the code generation from within the sbt console (or intgrate to your project)

Database tables

You can create your database evolutions by using e.g. PgAdmin or any other tool.

Integrate the Slick code generator to your project

You can integrate the generator within your build.sbt or the Build.scala. The following configuration could be an example project configured within the build.sbt.

libraryDependencies += "com.typesafe.slick" %% "slick-codegen" % "3.2.0"

Execute the generation of the code

Open your sbt.

> sbt

Open the console.

scala> console

Execute the generation of the code.

slick.codegen.SourceCodeGenerator.main(
  Array(
    "slick.jdbc.PostgresProfile",
    "org.postgresql.Driver",
    "jdbc:postgresql://localhost:5432/db",
    "./target/generatedSources",
    "com.my.db",
    "USERNAME",
    "PASSWORD"
  )
)

The SourceCodeGenerator can be configured depending on your needs. Additinoal information can be found at http://slick.lightbend.com/doc/3.2.0/code-generation.html.

Leave a Reply