diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-20 01:54:57 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-20 01:54:57 -0500 |
commit | 687919e7c163e1ff8a002f1a2c24e4b37f75f20d (patch) | |
tree | b185f9e8c9d0bf515b989ba8c13dc0f264e6fd25 /src/main/scala/xyz/driver/core/database/Dal.scala | |
parent | dd8cbb3ada14a4f05d5683b90a5b83dc4b3b35f5 (diff) | |
parent | f0a5e41ec45d2420ba7173e156806f81701f9796 (diff) | |
download | driver-core-687919e7c163e1ff8a002f1a2c24e4b37f75f20d.tar.gz driver-core-687919e7c163e1ff8a002f1a2c24e4b37f75f20d.tar.bz2 driver-core-687919e7c163e1ff8a002f1a2c24e4b37f75f20d.zip |
Merge pull request #8 from drivergroup/dal-improvementsv0.9.29
Dal improvements
Diffstat (limited to 'src/main/scala/xyz/driver/core/database/Dal.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/database/Dal.scala | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/database/Dal.scala b/src/main/scala/xyz/driver/core/database/Dal.scala new file mode 100644 index 0000000..e920392 --- /dev/null +++ b/src/main/scala/xyz/driver/core/database/Dal.scala @@ -0,0 +1,47 @@ +package xyz.driver.core.database + +import scala.concurrent.{ExecutionContext, Future} + +import scalaz.{ListT, Monad} +import scalaz.std.scalaFuture._ + +trait Dal { + protected type T[D] + protected implicit val monadT: Monad[T] + + protected def execute[D](operations: T[D]): Future[D] + protected def noAction[V](v: V): T[V] + protected def customAction[R](action: => Future[R]): T[R] +} + +class FutureDal(executionContext: ExecutionContext) extends Dal { + implicit val exec = executionContext + override type T[D] = Future[D] + implicit val monadT = implicitly[Monad[Future]] + + def execute[D](operations: T[D]): Future[D] = operations + def noAction[V](v: V): T[V] = Future.successful(v) + def customAction[R](action: => Future[R]): T[R] = action +} + +class SlickDal(database: Database, executionContext: ExecutionContext) extends Dal { + import database.profile.api._ + implicit val exec = executionContext + override type T[D] = slick.dbio.DBIO[D] + + implicit protected class QueryOps[+E, U](query: Query[E, U, Seq]) { + def resultT: ListT[T, U] = ListT[T, U](query.result.map(_.toList)) + } + + override implicit val monadT: Monad[T] = new Monad[T] { + override def point[A](a: => A): T[A] = DBIO.successful(a) + override def bind[A, B](fa: T[A])(f: A => T[B]): T[B] = fa.flatMap(f) + } + + override def execute[D](readOperations: T[D]): Future[D] = { + database.database.run(readOperations.transactionally) + } + + override def noAction[V](v: V): T[V] = DBIO.successful(v) + override def customAction[R](action: => Future[R]): T[R] = DBIO.from(action) +} |