-
-
Save pedrofurla/10801461 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
trait EntityBase { | |
def id: String | |
} | |
trait Repository { | |
type Entity <: EntityBase | |
def get(id: String): Option[Entity] | |
} | |
trait SlickRepository extends Repository { | |
val profile: scala.slick.driver.JdbcProfile | |
def database: profile.simple.Database | |
import profile.simple._ | |
abstract class BaseTable(tag: Tag, tableName: String) extends Table[Entity](tag, tableName) { | |
def id: Column[String] | |
} | |
val table: TableQuery[BaseTable] // needs to be covariant | |
override def get(id: String): Option[Entity] = database.withSession { implicit session => | |
table.filter(_.id === id).list.headOption | |
} | |
} | |
case class User(id: String, name: String, email: String) extends EntityBase | |
abstract class UserRepository extends SlickRepository { | |
type Entity = User | |
import profile.simple._ | |
class UserTable(tag: Tag) extends BaseTable(tag, "user") { | |
def id = column[String]("userid", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("name") | |
def email = column[String]("email") | |
def * = (id, name, email) <> (User.tupled, User.unapply) | |
} | |
val table:TableQuery[BaseTable] = TableQuery[UserTable] | |
} |
Author
pedrofurla
commented
Apr 16, 2014
Have you found a way to get around this? I'm struggling with a way to come up with a base repository like this, running into those same "value table has incompatible type" errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment