diff --git a/modules/persistence/build.gradle.kts b/modules/persistence/build.gradle.kts index 794a6f96..19944278 100644 --- a/modules/persistence/build.gradle.kts +++ b/modules/persistence/build.gradle.kts @@ -15,8 +15,9 @@ repositories { sqldelight { databases { create("Database") { + dialect("app.cash.sqldelight:postgresql-dialect:2.0.2") packageName.set("com.sphereon.oid.fed.persistence") - dialect("app.cash.sqldelight:postgresql-dialect:2.0.0") + srcDirs.from(project.file("./src/commonMain/sqldelight/")) } } } @@ -35,8 +36,8 @@ kotlin { jvmMain { dependencies { + implementation("com.zaxxer:HikariCP:5.1.0") implementation("org.postgresql:postgresql:42.7.3") - implementation("com.zaxxer:HikariCP:5.0.1") implementation("app.cash.sqldelight:jdbc-driver:2.0.2") } } diff --git a/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.kt b/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.kt new file mode 100644 index 00000000..0c358d34 --- /dev/null +++ b/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.kt @@ -0,0 +1,7 @@ +import com.sphereon.oid.fed.persistence.Database +import com.sphereon.oid.fed.persistence.repositories.AccountRepository + +expect class Persistence { + val database: Database + val accountRepository: AccountRepository +} diff --git a/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/database/DatabaseFactory.kt b/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/database/DatabaseFactory.kt index c31a90bf..5e576dad 100644 --- a/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/database/DatabaseFactory.kt +++ b/modules/persistence/src/commonMain/kotlin/com.sphereon.oid.fed.persistence/database/DatabaseFactory.kt @@ -1,10 +1,11 @@ import app.cash.sqldelight.db.SqlDriver +import com.sphereon.oid.fed.persistence.Database expect class PlatformSqlDriver { fun createPostgresDriver(url: String, username: String, password: String): SqlDriver } -class DatabaseFactory(private val platformSqlDriver: PlatformSqlDriver) { +class DriverFactory(private val platformSqlDriver: PlatformSqlDriver) { fun createDriver(config: DatabaseConfig): SqlDriver { return when (config) { is DatabaseConfig.Postgres -> platformSqlDriver.createPostgresDriver(config.url, config.username, config.password) @@ -13,6 +14,8 @@ class DatabaseFactory(private val platformSqlDriver: PlatformSqlDriver) { } } +expect fun createDatabase(driverFactory: DriverFactory): Database + sealed class DatabaseConfig { data class Postgres(val url: String, val username: String, val password: String) : DatabaseConfig() } \ No newline at end of file diff --git a/modules/persistence/src/commonMain/kotlin/com/sphereon/oid/fed/persistence/repositories/AccountRepository.kt b/modules/persistence/src/commonMain/kotlin/com/sphereon/oid/fed/persistence/repositories/AccountRepository.kt index 22db4110..689075f4 100644 --- a/modules/persistence/src/commonMain/kotlin/com/sphereon/oid/fed/persistence/repositories/AccountRepository.kt +++ b/modules/persistence/src/commonMain/kotlin/com/sphereon/oid/fed/persistence/repositories/AccountRepository.kt @@ -1,10 +1,11 @@ package com.sphereon.oid.fed.persistence.repositories import com.sphereon.oid.fed.openapi.models.Account +import com.sphereon.oid.fed.persistence.Database interface AccountRepository { - fun findById(id: UInt): Account? + fun findById(id: Int): Account? fun findByUsername(username: String): Account? fun create(account: Account) } -expect fun AccountRepository(): AccountRepository +expect fun AccountRepository(database: Database): AccountRepository diff --git a/modules/persistence/src/commonMain/sqldelight/Account/Account.sq b/modules/persistence/src/commonMain/sqldelight/Account/Account.sq new file mode 100644 index 00000000..87a5f868 --- /dev/null +++ b/modules/persistence/src/commonMain/sqldelight/Account/Account.sq @@ -0,0 +1,27 @@ +CREATE TABLE account ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP +); + +CREATE INDEX account_username_index ON account (username); + +selectAll: +SELECT * FROM account; + +create: +INSERT INTO account (username) VALUES (?); + +delete: +UPDATE account SET deleted_at = CURRENT_TIMESTAMP WHERE id = ?; + +findByUsername: +SELECT * FROM account WHERE username = ?; + +findById: +SELECT * FROM account WHERE id = ?; + +update: +UPDATE account SET username = ? WHERE id = ?; \ No newline at end of file diff --git a/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/account/Account.sq b/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/account/Account.sq deleted file mode 100644 index a3492284..00000000 --- a/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/account/Account.sq +++ /dev/null @@ -1,10 +0,0 @@ -CREATE TABLE "account" ( - id SERIAL PRIMARY KEY, - username VARCHAR(255) UNIQUE NOT NULL, - kms "KMS_TYPE" NOT NULL DEFAULT 'LOCAL', - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP -); - -CREATE INDEX "account_username_index" ON "account" (username); diff --git a/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/kms/KmsEnum.sq b/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/kms/KmsEnum.sq deleted file mode 100644 index 7716b162..00000000 --- a/modules/persistence/src/commonMain/sqldelight/com/sphereon/sqldelight/kms/KmsEnum.sq +++ /dev/null @@ -1 +0,0 @@ -CREATE TYPE "KMS_TYPE" AS ENUM ('LOCAL'); diff --git a/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.database/DatabaseFactory.jvm.kt b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.database/DatabaseFactory.jvm.kt index 7cfaeb11..f34001cb 100644 --- a/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.database/DatabaseFactory.jvm.kt +++ b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.database/DatabaseFactory.jvm.kt @@ -2,6 +2,7 @@ import app.cash.sqldelight.db.SqlDriver import app.cash.sqldelight.driver.jdbc.asJdbcDriver import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource +import com.sphereon.oid.fed.persistence.Database actual class PlatformSqlDriver { actual fun createPostgresDriver(url: String, username: String, password: String): SqlDriver { @@ -15,3 +16,8 @@ actual class PlatformSqlDriver { return driver } } + +actual fun createDatabase(driverFactory: DriverFactory): Database { + val driver = driverFactory.createDriver(DatabaseConfig.Postgres("jdbc:postgresql://localhost:5432/oid_fed", "oid_fed", "oid_fed")) + return Database(driver) +} \ No newline at end of file diff --git a/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.repositories/AccountRepository.jvm.kt b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.repositories/AccountRepository.jvm.kt index fe49d109..b59c5993 100644 --- a/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.repositories/AccountRepository.jvm.kt +++ b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence.repositories/AccountRepository.jvm.kt @@ -1,20 +1,16 @@ package com.sphereon.oid.fed.persistence.repositories +import Account.AccountQueries +import com.sphereon.oid.fed.persistence.Database +import app.cash.sqldelight.Query import com.sphereon.oid.fed.openapi.models.Account -private class JvmAccountRepository() : AccountRepository { - - override fun findById(id: UInt): Account? { - return null - } - - override fun findByUsername(username: String): Account? { - return null - } - - override fun create(account: Account) { +private class JvmAccountRepository(database: Database) : AccountRepository { + private val accountQueries: AccountQueries = database.accountQueries + override fun findById(id: Int): Account? { + return accountQueries.findById(id).executeAsOneOrNull() } } -actual fun AccountRepository(): AccountRepository = JvmAccountRepository() \ No newline at end of file +actual fun AccountRepository(database: Database): AccountRepository = JvmAccountRepository(database) \ No newline at end of file diff --git a/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.jvm.kt b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.jvm.kt new file mode 100644 index 00000000..0b232fa2 --- /dev/null +++ b/modules/persistence/src/jvmMain/kotlin/com.sphereon.oid.fed.persistence/Persistence.jvm.kt @@ -0,0 +1,14 @@ +import com.sphereon.oid.fed.persistence.Database +import com.sphereon.oid.fed.persistence.repositories.AccountRepository + +actual class Persistence { + actual val database: Database + actual val accountRepository: AccountRepository + + init { + println("Persistence created") + val driver = PlatformSqlDriver().createPostgresDriver("jdbc:postgresql://localhost:5432/oid_fed", "oid_fed", "oid_fed") + database = Database(driver) + accountRepository = AccountRepository(database) + } +}