Slick 3.0.0 Mapping Existing Models

隐身守侯 提交于 2019-12-25 11:57:22

问题


I have a model that is composed of three case classes as below:

case class MyModel(myId: MyIdType, name: MyNameType)

case class MyIdType(id: Long)
case class MyNameType(name: String)
object MyNameType(name: String) {
  val NAME1 = MyNameType("name1")
  val NAME2 = MyNameType("name2")
  val NAME3 = MyNameType("name3")
}

Let's say these are the existing models. I have a Slick table mapping that goes like this:

  class MyTable(tag: Tag) extends Table[MyTableElem](tag, "myTable") {
    def id                  = column[Long]("id", O.PrimaryKey)
    def name                = column[String]("name")

    def * = (id, name) <> (MyTableElem.tupled, MyTableElem.unapply)
  }

As it can be seen that I have to have another type to map my table MyTable first to MyTableElem and then during every read, I transform the MyTableElem to MyModel. Is there a way to avoid this and go directly to the MyModel? I guess I have to implement the tupled and unapply methods or?


回答1:


I think you may have to define your own column type. In Slick 3.0 documentation Using Custom Scalar Types in Queries, MappedTo could be used in your case.

If you have a wrapper class (which can optionally be a case class and/or value class) for an underlying value of some supported type, you can make it extend MappedTo to get a macro-generated implicit ColumnType for free.

Following is how I try to get your problem done.

import slick.lifted.Tag
// I am using PostgreSQL
import slick.driver.PostgresDriver.api._

case class MyModel(myId: MyIdType, name: MyNameType)

case class MyIdType(id: Long) extends MappedTo[Long] {
  override def value: Long = id
}
case class MyNameType(name: String) extends MappedTo[String] {
  override def value: String = name
}

class MyTable(tag: Tag) extends Table[MyModel](tag, "myTable") {
  def id                  = column[MyIdType]("id", O.PrimaryKey)
  def name                = column[MyNameType]("name")

  def * = (id, name) <> (MyModel.tupled, MyModel.unapply)
}

Anyway, hope it helps.



来源:https://stackoverflow.com/questions/32628389/slick-3-0-0-mapping-existing-models

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!