Slick Database Actinism 3.0.0
I started using Slick 3.0.0 and I like the concise syntax. However, I have not been able to find a way to use it in database agnostic mode.
In the following example provided in the documentation: http://slick.typesafe.com/doc/3.0.0/gettingstarted.html
I would like to somehow disable this used database code and avoid importing the specific database in my code (i.e. slick.driver.H2Driver.api._
).
I tried to get rid of it by providing a connection using the cake pattern, but the ".result" member is not available.
A workaround would be to import slick.driver.JdbcDriver.api._
, but it is deprecated and therefore shouldn't be a good starting point.
Has anyone found a way to use Slick 3.0.0 in database agnostic and elegant way?
This question is just around the corner, " How do I write a database aggregation application and do the initial database initialization? ", But this question focuses on Slick 3.0 +0.0. Unfortunately, the answers provided with this previous question do not target Slick 3.0.0 other than those using legacy code.
source to share
The smooth driver class you are looking for is slick.driver.JdbcProfile
.
There is an official example of the project slick-multidb
, which you can get via activator ( github ). Here's the relevant code:
import scala.language.higherKinds
import slick.driver.JdbcProfile
/** All database code goes into the DAO (data access object) class which
* is parameterized by a Slick driver that implements JdbcProfile.
*/
class DAO(val driver: JdbcProfile) {
// Import the Scala API from the driver
import driver.api._
class Props(tag: Tag) extends Table[(String, String)](tag, "PROPS") {
def key = column[String]("KEY", O.PrimaryKey)
def value = column[String]("VALUE")
def * = (key, value)
}
val props = TableQuery[Props]
/** Create the database schema */
def create: DBIO[Unit] =
props.ddl.create
/** Insert a key/value pair */
def insert(k: String, v: String): DBIO[Int] =
props += (k, v)
/** Get the value for the given key */
def get(k: String): DBIO[Option[String]] =
(for(p <- props if p.key === k) yield p.value).result.headOption
/** Get the first element for a Query from this DAO */
def getFirst[M, U, C[_]](q: Query[M, U, C]): DBIO[U] =
q.result.head
}
Client code:
val dao = new DAO(H2Driver)
import dao.driver.api._
db.run(dao.insert("foo", "bar"))
source to share