Customize slick code generator

2019-08-12 22:58发布

问题:

If I have

abstract class RichTable[T](tableTag: Tag, name: String) extends Table[T](tableTag, name) {
   val tenantId: Column[Int] = column[Int]("tenant_id")
}

And slick generated Table class as follow

 class Dashboard(_tableTag: Tag) extends Table[DashboardRow](_tableTag, "dashboard") {

I want to customize slick code generator to instead generate

class Dashboard(_tableTag: Tag) extends RichTable[DashboardRow](_tableTag, "dashboard") {
 ...
 override val tenantId: Column[Int] = column[Int]("tenant_id") //note override keyword
}

Is this possible to customize slick code generator particular for this case? Note: extends RichTable need to be applied selectively (a list of table classes vs every generated class).

回答1:

For RichTable you need to override def code of Table.

See

https://github.com/slick/slick/blob/2.1/slick-codegen/src/main/scala/scala/slick/codegen/AbstractSourceCodeGenerator.scala#L128



标签: scala slick