From 6fde5b828ee0f131536d0df0caf68e712742de53 Mon Sep 17 00:00:00 2001 From: Vlad Uspensky Date: Thu, 3 May 2018 10:32:06 -0700 Subject: Renaming `Dal` to `Repository` (#161) * Renaming `Dal` to `Repository` --- README.md | 2 +- src/main/scala/xyz/driver/core/database/Dal.scala | 73 ---------------------- .../xyz/driver/core/database/Repository.scala | 73 ++++++++++++++++++++++ .../scala/xyz/driver/core/database/package.scala | 9 +++ 4 files changed, 83 insertions(+), 74 deletions(-) delete mode 100644 src/main/scala/xyz/driver/core/database/Dal.scala create mode 100644 src/main/scala/xyz/driver/core/database/Repository.scala diff --git a/README.md b/README.md index f773405..19202b6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Core library is used to provide ways to implement practices established in [Driv * `config` Contains method `loadDefaultConfig` with default way of providing config to the application, * `domain` Common generic domain objects, e.g., `Email` and `PhoneNumber`, * `messages` Localization messages supporting different locales and methods to read from config, - * `database` Method for database initialization from config, `Id`, `Name`, `Time`, `Date` etc. mapping, schema lifecycle and base classes to implement and test `Dal` (data access layer objects), + * `database` Method for database initialization from config, `Id`, `Name`, `Time`, `Date` etc. mapping, schema lifecycle and base classes to implement and test `Repository` (data access objects), * `rest` Wrapper over call to external REST API, authorization, context headers, XSS protection, does logging and allows to add Swagger to a service, * `auth` Basic entities for authentication and authorization: `User`, `Role` `Permission` `AuthToken`, `AuthCredentials` etc., * `swagger` Contains custom `AbstractModelConverter` to customize Swagger JSON with any Scala JSON formats created by, for instance, Spray JSON, diff --git a/src/main/scala/xyz/driver/core/database/Dal.scala b/src/main/scala/xyz/driver/core/database/Dal.scala deleted file mode 100644 index bcde0de..0000000 --- a/src/main/scala/xyz/driver/core/database/Dal.scala +++ /dev/null @@ -1,73 +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} - -trait Dal { - 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 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)) - - 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 - } -} diff --git a/src/main/scala/xyz/driver/core/database/Repository.scala b/src/main/scala/xyz/driver/core/database/Repository.scala new file mode 100644 index 0000000..31c79ad --- /dev/null +++ b/src/main/scala/xyz/driver/core/database/Repository.scala @@ -0,0 +1,73 @@ +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} + +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 + } +} diff --git a/src/main/scala/xyz/driver/core/database/package.scala b/src/main/scala/xyz/driver/core/database/package.scala index 9385c8f..98fd5bd 100644 --- a/src/main/scala/xyz/driver/core/database/package.scala +++ b/src/main/scala/xyz/driver/core/database/package.scala @@ -36,4 +36,13 @@ package object database { cal.set(date.year, date.month, date.day, 0, 0, 0) new SqlDate(cal.getTime.getTime) } + + @deprecated("Dal is deprecated. Please use Repository trait instead!", "1.8.26") + type Dal = Repository + + @deprecated("SlickDal is deprecated. Please use SlickRepository class instead!", "1.8.26") + type SlickDal = SlickRepository + + @deprecated("FutureDal is deprecated. Please use FutureRepository class instead!", "1.8.26") + type FutureDal = FutureRepository } -- cgit v1.2.3