This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val db = Database.forConfig("h2mem1") | |
val queryAction: DBIO[Option[Int]] = ... | |
val result: Future[Option[Int]] = db.run(queryAction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zio.IO | |
trait AssetRepository { | |
def add(name: String, price: BigDecimal): IO[RepositoryFailure, AssetId] | |
def getById(id: AssetId): IO[RepositoryFailure, Option[Asset]] | |
def update(id: AssetId, name: String, price: BigDecimal): IO[RepositoryFailure, Unit] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait DatabaseProvider { | |
def databaseProvider: DatabaseProvider.Service | |
} | |
object DatabaseProvider { | |
trait Service { | |
def db: UIO[BasicBackend#DatabaseDef] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait LiveDatabaseProvider extends DatabaseProvider { | |
override val databaseProvider = new DatabaseProvider.Service { | |
override val db = ZIO.effectTotal(Database.forURL(url = "jdbc:h2:prod")) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait PortfolioAssetRepository { | |
def portfolioAssetRepository: PortfolioAssetRepository.Service | |
} | |
object PortfolioAssetRepository { | |
trait Service { | |
def add(name: String, price: BigDecimal): IO[RepositoryException, AssetId] | |
def getById(id: AssetId): IO[RepositoryException, Option[Asset]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db <- ZIO.accessM[DatabaseProvider](_.databaseProvider.db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.run(action) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result <- ZIO.fromFuture(ec => db.run(dbio)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fromDBIO[R](dbio: DBIO[R]): ZIO[DatabaseProvider, Throwable, R] = | |
for { | |
db <- ZIO.accessM[DatabaseProvider](_.databaseProvider.db) | |
r <- ZIO.fromFuture(ec => db.run(dbio)) | |
} yield r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fromStreamingDBIO[T](dbio: StreamingDBIO[_, T]): ZIO[DatabaseProvider, Throwable, ZStream[Any, Throwable, T]] = | |
for { | |
db <- ZIO.accessM[DatabaseProvider](_.databaseProvider.db) | |
r <- ZIO.effect(db.stream(dbio).toStream()) | |
} yield r |
OlderNewer