Last active
July 1, 2016 12:14
-
-
Save cvogt/6953558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.slick.driver.JdbcProfile | |
import scala.slick.ast.BaseTypedType | |
trait Profile { | |
val profile: JdbcProfile | |
} | |
trait TableComponent extends Profile { | |
import profile.simple._ | |
trait TypedId{ def id:Long } | |
trait Entity[ID]{ | |
def id : Option[ID] | |
} | |
case class AccountId( id:Long ) extends TypedId | |
case class Account( emal:String, id:Option[AccountId] ) extends Entity[AccountId] | |
abstract class PowerTable[ID <: TypedId, E <: Entity[ID]](tag:Tag, tableName: String) | |
extends Table[E](tag, tableName) with HasId[ID] { | |
} | |
trait HasId[ID <: TypedId] { | |
self: Table[_] => | |
protected val idColumnName = "id" | |
implicit val idMapper: BaseTypedType[ID] // changed the type here | |
def id = column[ID](idColumnName, O.AutoInc, O.NotNull) | |
} | |
} | |
trait AccountTable { | |
self: TableComponent => | |
import profile.simple._ | |
val accountQuery = TableQuery[Accounts] | |
class Accounts(tag:Tag) extends PowerTable[AccountId, Account](tag, "accounts") | |
with HasId[AccountId] { | |
implicit lazy val idMapper : BaseTypedType[AccountId] = MappedColumnType.base[AccountId, Long]( | |
i => i.id, | |
l => AccountId(l) | |
) | |
def create = Account.apply _ | |
def extrator = Account.unapply _ | |
def email = column[String]("email", O.NotNull) | |
def * = (email, id.?) <> ( create.tupled, extrator) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In slick 3.x (dunno about 2.x) it's possible to make AccountId successor of MappedTo
and in AccountTable write