diff options
author | Jakob Odersky <jakob@driver.xyz> | 2018-09-12 17:30:33 -0700 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2018-10-09 16:19:39 -0700 |
commit | eb6f97b4cac548999cbf192ee83d9ba9a253b7c8 (patch) | |
tree | 6d75a23efc841a6f51e780913387000206d1fe94 /src/main/scala/xyz/driver/core/database/Repository.scala | |
parent | 4d1197099ce4e721c18bf4cacbb2e1980e4210b5 (diff) | |
download | driver-core-eb6f97b4cac548999cbf192ee83d9ba9a253b7c8.tar.gz driver-core-eb6f97b4cac548999cbf192ee83d9ba9a253b7c8.tar.bz2 driver-core-eb6f97b4cac548999cbf192ee83d9ba9a253b7c8.zip |
Move database-related functionality to separate project
This committ includes a breaking change. The database-specific utility
"Converters" trait threw an exception "DatabaseException" defined in
the rest package, thus breaking the dependency graph. The solution was
to move the DatabaseException class from rest to database and not
inherit ServiceExceptio any more.
Unfortunately, the rest classes also require the database exception in
propagating errors so this funtionality has been removed. The
rationale is:
1. Database exceptions are rare and result in 500 errors anyway
making the status code opaque to what actual error caused it.
2. In core 2.0, an improved tracing framework will make diagnosing
and following database errors easier, thereby attenuating the need
to forward details on service exceptions in responses.
Diffstat (limited to 'src/main/scala/xyz/driver/core/database/Repository.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/database/Repository.scala | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/src/main/scala/xyz/driver/core/database/Repository.scala b/src/main/scala/xyz/driver/core/database/Repository.scala deleted file mode 100644 index 5d7f787..0000000 --- a/src/main/scala/xyz/driver/core/database/Repository.scala +++ /dev/null @@ -1,74 +0,0 @@ -package xyz.driver.core.database - -import scalaz.std.scalaFuture._ -import scalaz.{ListT, Monad, OptionT} -import slick.lifted.{AbstractTable, CanBeQueryCondition, RunnableCompiled} -import slick.{lifted => sl} - -import scala.concurrent.{ExecutionContext, Future} -import scala.language.higherKinds - -trait Repository { - type T[D] - implicit def monadT: Monad[T] - - def execute[D](operations: T[D]): Future[D] - def noAction[V](v: V): T[V] - def customAction[R](action: => Future[R]): T[R] - - def customAction[R](action: => OptionT[Future, R]): OptionT[T, R] = - OptionT[T, R](customAction(action.run)) -} - -class FutureRepository(executionContext: ExecutionContext) extends Repository { - implicit val exec: ExecutionContext = executionContext - override type T[D] = Future[D] - implicit val monadT: Monad[Future] = 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 SlickRepository(database: Database, executionContext: ExecutionContext) extends Repository { - import database.profile.api._ - implicit val exec: ExecutionContext = 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)) - - def maybeFilter[V, R: CanBeQueryCondition](data: Option[V])(f: V => E => R): sl.Query[E, U, Seq] = - data.map(v => query.withFilter(f(v))).getOrElse(query) - } - - implicit protected class CompiledQueryOps[U](compiledQuery: RunnableCompiled[_, Seq[U]]) { - def resultT: ListT[T, U] = ListT.listT[T](compiledQuery.result.map(_.toList)) - } - - private val dbioMonad = 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 implicit def monadT: Monad[T] = dbioMonad - - 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) - - def affectsRows(updatesCount: Int): Option[Unit] = { - if (updatesCount > 0) Some(()) else None - } - - def insertReturning[AT <: AbstractTable[_], V](table: TableQuery[AT])( - row: AT#TableElementType): slick.dbio.DBIO[AT#TableElementType] = { - table.returning(table) += row - } -} |