0

I'm trying to implement this: http://reactore.com/repository-patterngeneric-dao-implementation-in-scala-using-slick-3/

The example uses an explicit postgres driver. The application I'm working on uses config-based api generation, meaning that I need

XXXX  extends HasDatabaseConfigProvider[JdbcProfile] {

    import profile.api._

before I can use a lot of slick functionality, e.g. Table. This is a problem, because I need to have access to this:

 abstract class BaseTable[E: ClassTag](tag: Tag, schemaName: [String], tableName: String)
    extends Table[E](tag, schemaName, tableName) {
    val classOfEntity = classTag[E].runtimeClass
    val id: Rep[Long] = column[Long]("id", O.PrimaryKey, O.AutoInc)
  }

but Tag and Table are both part of the config API.

I like this implementation and would like to use something as close to it as possible. I found a similar solution here https://stackoverflow.com/a/39504536/1676006 but it's a little harder to follow and I'm not as huge a fan of the result.

Can I implement the pattern I want, or should I start looking for alternative solutions?

ChickenWing
  • 639
  • 9
  • 24

1 Answers1

0

you can try this sampled

BaseTable.scala

  abstract class BaseTable(tag: Tag) extends Table[basetable](tag, "tableName") {
      def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
      def name = column[String]("name")
      def * = (id, name) <>((basetable.apply _).tupled, basetable.unapply)
}

basetable.scala

case class basetable(id:Long,name:String)

object basetable{
  implicit val basetableFormat = Json.format[basetable]
}

baseRepo.scala

class baseRepo @Inject()(@NamedDatabase("databases name") dbConfigProvider: DatabaseConfigProvider) {

  private val  baseTable = TableQuery[BaseTable]
  private val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig._

  /**
    * search id 
    * */
  def FindById(id:Long): Future[Seq[basetable]] = db.run(
    baseTable.filter(_.id === id).result
  )
}
Neil_TW
  • 97
  • 5
  • Unfortunately, this approach doesn't work, as I don't have access to the db profile api at the inheritance level unless I wrap everything in a trait, at which point I lose visibility on all the components – ChickenWing Aug 07 '18 at 19:32