Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use PostgreSQL JSON type with Slick 2 code generator

I'm using Slick 2 code generator in my Scala/Play application to generate table classes of my PostgreSQL database. Some of the fields are JSON type though and they are turned into a String by the generator. I was wondering if I can somehow use slick-pg plugin to make the generator recognize Postgres JSON type?

I've tried to extend slick.driver.PostgresDriver directly in Build.scala:

import slick.driver.PostgresDriver
import com.github.tminglei.slickpg._

trait MyPostgresDriver extends PostgresDriver
                          with PgArraySupport
                          with PgDateSupport
                          with PgRangeSupport
                          with PgHStoreSupport
                          with PgPlayJsonSupport
                          with PgSearchSupport
                          with PgPostGISSupport {

  override val Implicit = new ImplicitsPlus {}
  override val simple = new SimpleQLPlus {}

  trait ImplicitsPlus extends Implicits
                        with ArrayImplicits
                        with DateTimeImplicits
                        with RangeImplicits
                        with HStoreImplicits
                        with JsonImplicits
                        with SearchImplicits
                        with PostGISImplicits

  trait SimpleQLPlus extends SimpleQL
                        with ImplicitsPlus
                        with SearchAssistants
                        with PostGISAssistants
}

object MyPostgresDriver extends MyPostgresDriver

But I don't know how to use it with the code generator routine instead of the standard driver:

SourceCodeGenerator.main(
    Array(
        "scala.slick.driver.PostgresDriver", //how do I use MyPostgresDriver here?
        "org.postgresql.Driver", 
        "jdbc:postgresql://localhost:5432/db?user=root&password=root", 
        "app", 
        "db"
    )
)       
like image 762
Caballero Avatar asked Oct 02 '22 07:10

Caballero


1 Answers

You can't make the generator pick up the type this way. It (or rather the Slick code that reverse engineers the Model from the database schema) currently only detects a hand full of types and simply assumes String for all other ones. This will be improved in the future. In order to make it use a different type for a column you have to customize the generator. The corresponding Slick documentation example actually shows how to customize the type:

http://slick.typesafe.com/doc/2.0.0/code-generation.html#customization

like image 167
cvogt Avatar answered Oct 13 '22 09:10

cvogt