-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eca6bee
commit 8cf9a4a
Showing
13 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
core/database/src/androidMain/kotlin/club/nito/core/database/Database.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package club.nito.core.database | ||
|
||
import android.content.Context | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
|
||
internal actual class DriverFactory(private val context: Context) { | ||
actual fun createDriver(): SqlDriver { | ||
val schema = Database.Schema | ||
|
||
return AndroidSqliteDriver( | ||
schema = schema, | ||
context = context, | ||
name = DATABASE_NAME, | ||
callback = object : AndroidSqliteDriver.Callback(schema) { | ||
override fun onConfigure(db: SupportSQLiteDatabase) { | ||
db.enableWriteAheadLogging() | ||
db.setForeignKeyConstraintsEnabled(true) | ||
} | ||
}, | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
core/database/src/androidMain/kotlin/club/nito/core/database/di/DatabaseModule.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package club.nito.core.database.di | ||
|
||
import club.nito.core.database.DriverFactory | ||
import org.koin.core.scope.Scope | ||
|
||
internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory( | ||
context = get(), | ||
) |
Empty file.
23 changes: 23 additions & 0 deletions
23
core/database/src/commonMain/kotlin/club/nito/core/database/Database.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package club.nito.core.database | ||
|
||
import app.cash.sqldelight.EnumColumnAdapter | ||
import app.cash.sqldelight.db.SqlDriver | ||
|
||
internal expect class DriverFactory { | ||
fun createDriver(): SqlDriver | ||
} | ||
|
||
internal const val DATABASE_NAME = "nito.db" | ||
|
||
internal fun createDatabase(driverFactory: DriverFactory): Database { | ||
val driver = driverFactory.createDriver() | ||
val database = Database( | ||
driver = driver, | ||
participantsAdapter = Participants.Adapter( | ||
statusAdapter = EnumColumnAdapter(), | ||
), | ||
) | ||
|
||
// Do more work with the database (see below). | ||
return database | ||
} |
10 changes: 10 additions & 0 deletions
10
core/database/src/commonMain/kotlin/club/nito/core/database/adapter/ParticipantStatus.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package club.nito.core.database.adapter | ||
|
||
import app.cash.sqldelight.ColumnAdapter | ||
import club.nito.core.model.participant.ParticipantStatus | ||
|
||
internal data object ParticipantStatusAdapter : ColumnAdapter<ParticipantStatus, String> { | ||
override fun decode(databaseValue: String): ParticipantStatus = ParticipantStatus.valueOf(databaseValue) | ||
|
||
override fun encode(value: ParticipantStatus): String = value.name | ||
} |
17 changes: 17 additions & 0 deletions
17
core/database/src/commonMain/kotlin/club/nito/core/database/di/DatabaseModule.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package club.nito.core.database.di | ||
|
||
import club.nito.core.database.DriverFactory | ||
import club.nito.core.database.createDatabase | ||
import org.koin.core.module.Module | ||
import org.koin.core.scope.Scope | ||
import org.koin.dsl.module | ||
|
||
public val databaseModule: Module = module { | ||
single { | ||
createDatabase( | ||
driverFactory = createDriverFactory(), | ||
) | ||
} | ||
} | ||
|
||
internal expect fun Scope.createDriverFactory(): DriverFactory |
37 changes: 37 additions & 0 deletions
37
core/database/src/commonMain/sqldelight/club/nito/core/database/Participants.sq
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import club.nito.core.model.participant.ParticipantStatus; | ||
|
||
CREATE TABLE `participants` ( | ||
`schedule_id` TEXT NOT NULL, | ||
`user_id` TEXT NOT NULL, | ||
`status` TEXT AS ParticipantStatus NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX `participants_pkey` ON `participants` (`schedule_id`, `user_id`); | ||
|
||
participantsByScheduleId: | ||
SELECT * | ||
FROM participants | ||
WHERE schedule_id = ?; | ||
|
||
participantsByUserId: | ||
SELECT * | ||
FROM participants | ||
WHERE user_id = ?; | ||
|
||
upsert { | ||
UPDATE participants SET | ||
`schedule_id` = :schedule_id, | ||
`user_id` = :user_id, | ||
`status` = :status | ||
WHERE schedule_id = :schedule_id AND user_id = :user_id; | ||
|
||
INSERT OR IGNORE INTO participants ( | ||
`schedule_id`, | ||
`user_id`, | ||
`status` | ||
) VALUES ( | ||
:schedule_id, | ||
:user_id, | ||
:status | ||
); | ||
} |
File renamed without changes.
Empty file.
11 changes: 11 additions & 0 deletions
11
core/database/src/iosMain/kotlin/club/nito/core/database/Database.ios.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package club.nito.core.database | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.native.NativeSqliteDriver | ||
|
||
internal actual class DriverFactory { | ||
actual fun createDriver(): SqlDriver = NativeSqliteDriver( | ||
schema = Database.Schema, | ||
name = DATABASE_NAME, | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
core/database/src/iosMain/kotlin/club/nito/core/database/di/DatabaseModule.ios.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package club.nito.core.database.di | ||
|
||
import club.nito.core.database.DriverFactory | ||
import org.koin.core.scope.Scope | ||
|
||
internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory() |
15 changes: 15 additions & 0 deletions
15
core/database/src/jsMain/kotlin/club/nito/core/database/Database.js.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package club.nito.core.database | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.worker.WebWorkerDriver | ||
import org.w3c.dom.Worker | ||
|
||
internal actual class DriverFactory { | ||
actual fun createDriver(): SqlDriver { | ||
return WebWorkerDriver( | ||
Worker( | ||
js("""new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url)""") | ||
) | ||
).also { Database.Schema.create(it) } | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
core/database/src/jsMain/kotlin/club/nito/core/database/di/DatabaseModule.js.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package club.nito.core.database.di | ||
|
||
import club.nito.core.database.DriverFactory | ||
import org.koin.core.scope.Scope | ||
|
||
internal actual fun Scope.createDriverFactory(): DriverFactory = DriverFactory() |