Skip to content

Commit

Permalink
Add clean up for DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
bartma11 authored Apr 5, 2023
1 parent 03f55f5 commit 972d586
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/scala/com/softwaremill/realworld/db/Db.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package com.softwaremill.realworld.db

import com.zaxxer.hikari.{HikariConfig, HikariDataSource}
import io.getquill.{SnakeCase, SqliteZioJdbcContext}
import zio.ZLayer
import zio.{ZIO, ZLayer}

import java.io.Closeable
import javax.sql.DataSource

object Db:

private def create(dbConfig: DbConfig): DataSource = {
private def create(dbConfig: DbConfig): HikariDataSource = {
val poolConfig = new HikariConfig()
poolConfig.setJdbcUrl(dbConfig.jdbcUrl)
poolConfig.setConnectionInitSql(dbConfig.connectionInitSql)
new HikariDataSource(poolConfig)
}

// Used for migration and executing queries.
val dataSourceLive: ZLayer[DbConfig, Nothing, DataSource] = ZLayer.fromFunction(create(_))
val dataSourceLive: ZLayer[DbConfig, Nothing, DataSource] =
ZLayer
.service[DbConfig]
.flatMap { env =>
val dbConfig = env.get
ZLayer.scoped {
ZIO.fromAutoCloseable(ZIO.succeed(create(dbConfig)))
}
}

// Quill framework object used for specifying sql queries.
def quillLive: ZLayer[Any, Nothing, SqliteZioJdbcContext[SnakeCase]] = ZLayer.succeed(new SqliteZioJdbcContext[SnakeCase](SnakeCase))

0 comments on commit 972d586

Please sign in to comment.