Skip to content

Commit

Permalink
Add TableModule and EntityTableModule
Browse files Browse the repository at this point in the history
To save some boilerplate when defining slick table schemas
  • Loading branch information
nafg committed Mar 12, 2020
1 parent b38ada8 commit f78a339
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/scala/slick/additions/AdditionsProfile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,35 @@ trait AdditionsProfile { this: JdbcProfile =>
trait AutoNameSnakify extends AutoName { this: Table[_] =>
protected implicit def nameStyle: NameStyle = NameStyle.Snakify
}

trait TableModule {
type Rec
type Row <: Table[Rec]
val Q: TableQuery[Row]
}

abstract class EntityTableModule[K: BaseColumnType, V](tableName: String) extends TableModule {
type Rec = KeyedEntity[K, V]

abstract class BaseEntRow(tag: Tag) extends EntityTable[K, V](tag, tableName) with AutoNameSnakify {
def tableQuery = EntityTableModule.this.Q
}

type Row <: BaseEntRow

val mkRow: Tag => Row = {
import scala.reflect.runtime.universe._
val m = runtimeMirror(this.getClass.getClassLoader)
val thisAsSymbol = m.moduleSymbol(this.getClass)
val rowClassMirror = m.reflectClass(thisAsSymbol.info.member(TypeName("Row")).asClass)
val ctor = rowClassMirror.reflectConstructor(rowClassMirror.symbol.primaryConstructor.asMethod)

tag => ctor.apply(tag).asInstanceOf[Row]
}

class TableQuery extends EntTableQuery[K, V, Row](mkRow)

val Q: TableQuery = new TableQuery
}
}
}

0 comments on commit f78a339

Please sign in to comment.