Skip to content

Commit

Permalink
feat: implement Account SQ file
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Jul 26, 2024
1 parent 160a32f commit 9d8ea00
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 28 deletions.
5 changes: 3 additions & 2 deletions modules/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"))
}
}
}
Expand All @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
}
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions modules/persistence/src/commonMain/sqldelight/Account/Account.sq
Original file line number Diff line number Diff line change
@@ -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 = ?;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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()
actual fun AccountRepository(database: Database): AccountRepository = JvmAccountRepository(database)
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 9d8ea00

Please sign in to comment.