38 lines
1.1 KiB
Kotlin
38 lines
1.1 KiB
Kotlin
|
package database
|
||
|
|
||
|
import config.EnvConfig
|
||
|
import com.zaxxer.hikari.*
|
||
|
import kotlinx.coroutines.Dispatchers
|
||
|
import kotlinx.coroutines.withContext
|
||
|
import org.jetbrains.exposed.sql.*
|
||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||
|
|
||
|
object DatabaseFactory {
|
||
|
|
||
|
private val envConfig = EnvConfig()
|
||
|
private val dbUrl = envConfig.dbJdbc
|
||
|
private val dbUser = envConfig.dbUsername
|
||
|
private val dbPassword = envConfig.dbPassowrd
|
||
|
|
||
|
fun init() {
|
||
|
Database.connect(hikari())
|
||
|
}
|
||
|
|
||
|
private fun hikari(): HikariDataSource {
|
||
|
val config = HikariConfig()
|
||
|
config.driverClassName = "org.postgresql.Driver"
|
||
|
config.jdbcUrl = dbUrl
|
||
|
config.username = dbUser
|
||
|
config.password = dbPassword
|
||
|
config.maximumPoolSize = 3
|
||
|
config.isAutoCommit = false
|
||
|
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
|
||
|
config.validate()
|
||
|
return HikariDataSource(config)
|
||
|
}
|
||
|
|
||
|
suspend fun <T> dbQuery(block: () -> T): T =
|
||
|
withContext(Dispatchers.IO) {
|
||
|
transaction { block() }
|
||
|
}
|
||
|
}
|