Skip to content

Commit f812dac

Browse files
Merge pull request #43 from HekmatullahAmin/KMPPT-11
feat(core-base): Add core-base/database module
2 parents 9c98af9 + 9dfeb15 commit f812dac

File tree

37 files changed

+209
-239
lines changed

37 files changed

+209
-239
lines changed

build-logic/convention/src/main/kotlin/KMPRoomConventionPlugin.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class KMPRoomConventionPlugin : Plugin<Project> {
2525

2626
dependencies {
2727
// Adding ksp dependencies for multiple platforms
28+
"implementation"(libs.findLibrary("androidx.room.ktx").get())
2829
listOf(
2930
"kspDesktop",
3031
"kspAndroid",
@@ -35,7 +36,7 @@ class KMPRoomConventionPlugin : Plugin<Project> {
3536
).forEach { platform ->
3637
add(platform, libs.findLibrary("androidx.room.compiler").get())
3738
// Kotlin Extensions and Coroutines support for Room
38-
add(platform, libs.findLibrary("androidx.room.ktx").get())
39+
// add(platform, libs.findLibrary("androidx.room.ktx").get())
3940
}
4041
}
4142
}

core-base/database/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

core-base/database/build.gradle.kts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
9+
*/
10+
plugins {
11+
alias(libs.plugins.kmp.library.convention)
12+
}
13+
14+
android {
15+
namespace = "org.mifos.corebase.database"
16+
}
17+
18+
kotlin {
19+
sourceSets {
20+
androidMain.dependencies {
21+
implementation(libs.androidx.room.runtime)
22+
}
23+
24+
desktopMain.dependencies {
25+
implementation(libs.androidx.room.runtime)
26+
}
27+
28+
nativeMain.dependencies {
29+
implementation(libs.androidx.room.runtime)
30+
}
31+
}
32+
}

core/database/src/androidMain/java/org/mifos/core/database/AppDatabaseFactory.kt core-base/database/src/androidMain/kotlin/org/mifos/corebase/AppDatabaseFactory.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import android.content.Context
1313
import androidx.room.Room
@@ -16,13 +16,11 @@ import androidx.room.RoomDatabase
1616
class AppDatabaseFactory(
1717
private val context: Context,
1818
) {
19-
fun createDatabase(): RoomDatabase.Builder<AppDatabase> {
20-
val appContext = context.applicationContext
21-
val dbFile = appContext.getDatabasePath(AppDatabase.DATABASE_NAME)
22-
23-
return Room.databaseBuilder<AppDatabase>(
24-
context = appContext,
25-
name = dbFile.absolutePath,
19+
fun <T : RoomDatabase> createDatabase(databaseClass: Class<T>, databaseName: String): RoomDatabase.Builder<T> {
20+
return Room.databaseBuilder(
21+
context.applicationContext,
22+
databaseClass,
23+
databaseName,
2624
)
2725
}
2826
}

core/database/src/androidMain/java/org/mifos/core/database/Room.android.kt core-base/database/src/androidMain/kotlin/org/mifos/corebase/Room.android.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.Dao
1313
import androidx.room.Entity

core/database/src/androidMain/java/org/mifos/core/database/TypeConverter.android.kt core-base/database/src/androidMain/kotlin/org/mifos/corebase/TypeConverter.android.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.TypeConverter
1313

core/database/src/commonMain/kotlin/org/mifos/core/database/Room.kt core-base/database/src/commonMain/kotlin/org/mifos/corebase/Room.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import kotlin.reflect.KClass
1313

@@ -37,12 +37,12 @@ expect annotation class PrimaryKey(
3737
)
3838

3939
@Suppress("NO_ACTUAL_FOR_EXPECT")
40-
@Target(allowedTargets = []) // Complex annotation target
40+
@Target(allowedTargets = [])
4141
@Retention(AnnotationRetention.BINARY)
4242
expect annotation class ForeignKey
4343

4444
@Suppress("NO_ACTUAL_FOR_EXPECT")
45-
@Target(allowedTargets = []) // Complex annotation target
45+
@Target(allowedTargets = [])
4646
@Retention(AnnotationRetention.BINARY)
4747
expect annotation class Index
4848

core/database/src/commonMain/kotlin/org/mifos/core/database/TypeConverter.kt core-base/database/src/commonMain/kotlin/org/mifos/corebase/TypeConverter.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

12+
@Suppress("NO_ACTUAL_FOR_EXPECT")
1213
@Target(AnnotationTarget.FUNCTION)
1314
@Retention(AnnotationRetention.BINARY)
1415
expect annotation class TypeConverter()

core/database/src/desktopMain/kotlin/org/mifos/core/database/AppDatabaseFactory.kt core-base/database/src/desktopMain/kotlin/org/mifos/corebase/AppDatabaseFactory.kt

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.Room
1313
import androidx.room.RoomDatabase
14+
import androidx.room.util.findAndInstantiateDatabaseImpl
1415
import java.io.File
1516

1617
class AppDatabaseFactory {
17-
fun createDatabase(): RoomDatabase.Builder<AppDatabase> {
18+
inline fun <reified T : RoomDatabase> createDatabase(
19+
databaseName: String,
20+
noinline factory: () -> T = { findAndInstantiateDatabaseImpl(T::class.java) },
21+
): RoomDatabase.Builder<T> {
1822
val os = System.getProperty("os.name").lowercase()
1923
val userHome = System.getProperty("user.home")
2024
val appDataDir = when {
21-
os.contains("win") -> File(System.getenv("APPDATA"), "Bookpedia")
22-
os.contains("mac") -> File(userHome, "Library/Application Support/Bookpedia")
23-
else -> File(userHome, ".local/share/Bookpedia")
25+
os.contains("win") -> File(System.getenv("APPDATA"), "MifosDatabase")
26+
os.contains("mac") -> File(userHome, "Library/Application Support/MifosDatabase")
27+
else -> File(userHome, ".local/share/MifosDatabase")
2428
}
2529

2630
if (!appDataDir.exists()) {
2731
appDataDir.mkdirs()
2832
}
2933

30-
val dbFile = File(appDataDir, AppDatabase.DATABASE_NAME)
31-
return Room.databaseBuilder<AppDatabase>(
34+
val dbFile = File(appDataDir, databaseName)
35+
36+
return Room.databaseBuilder(
3237
name = dbFile.absolutePath,
38+
factory = factory,
3339
)
3440
}
3541
}

core/database/src/desktopMain/kotlin/org/mifos/core/database/Room.desktop.kt core-base/database/src/desktopMain/kotlin/org/mifos/corebase/Room.desktop.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.Dao
1313
import androidx.room.Entity

core/database/src/desktopMain/kotlin/org/mifos/core/database/TypeConverter.desktop.kt core-base/database/src/desktopMain/kotlin/org/mifos/corebase/TypeConverter.desktop.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.TypeConverter
1313

core/database/src/nativeMain/kotlin/org/mifos/core/database/AppDatabaseFactory.kt core-base/database/src/nativeMain/kotlin/org/mifos/corebase/AppDatabaseFactory.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,30 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.Room
1313
import androidx.room.RoomDatabase
14+
import androidx.room.util.findDatabaseConstructorAndInitDatabaseImpl
1415
import kotlinx.cinterop.ExperimentalForeignApi
1516
import platform.Foundation.NSDocumentDirectory
1617
import platform.Foundation.NSFileManager
1718
import platform.Foundation.NSUserDomainMask
1819

1920
class AppDatabaseFactory {
20-
fun createDatabase(): RoomDatabase.Builder<AppDatabase> {
21-
val dbFilePath = documentDirectory() + "/${AppDatabase.DATABASE_NAME}"
22-
return Room.databaseBuilder<AppDatabase>(
21+
inline fun <reified T : RoomDatabase> createDatabase(
22+
databaseName: String,
23+
noinline factory: () -> T = { findDatabaseConstructorAndInitDatabaseImpl(T::class) },
24+
): RoomDatabase.Builder<T> {
25+
val dbFilePath = documentDirectory() + "/$databaseName"
26+
return Room.databaseBuilder(
2327
name = dbFilePath,
28+
factory = factory,
2429
)
2530
}
2631

2732
@OptIn(ExperimentalForeignApi::class)
28-
private fun documentDirectory(): String {
33+
fun documentDirectory(): String {
2934
val documentDirectory = NSFileManager.defaultManager.URLForDirectory(
3035
directory = NSDocumentDirectory,
3136
inDomain = NSUserDomainMask,

core/database/src/nativeMain/kotlin/org/mifos/core/database/Room.native.kt core-base/database/src/nativeMain/kotlin/org/mifos/corebase/Room.native.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.Dao
1313
import androidx.room.Entity

core/database/src/nativeMain/kotlin/org/mifos/core/database/TypeConverter.native.kt core-base/database/src/nativeMain/kotlin/org/mifos/corebase/TypeConverter.native.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE
99
*/
10-
package org.mifos.core.database
10+
package org.mifos.corebase
1111

1212
import androidx.room.TypeConverter
1313

core/database/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ kotlin {
2424
androidMain.dependencies {
2525
implementation(libs.koin.android)
2626
implementation(libs.androidx.room.runtime)
27-
implementation(libs.androidx.sqlite.bundled)
2827
}
2928

3029
nativeMain.dependencies {
@@ -41,6 +40,7 @@ kotlin {
4140
implementation(libs.kotlinx.coroutines.core)
4241
implementation(libs.kotlinx.serialization.json)
4342
api(projects.core.common)
43+
api(projects.coreBase.database)
4444
}
4545
}
4646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "503386f9121daf70e54000a04c4c8b05",
6+
"entities": [
7+
{
8+
"tableName": "samples",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "name",
19+
"columnName": "name",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
}
23+
],
24+
"primaryKey": {
25+
"autoGenerate": true,
26+
"columnNames": [
27+
"id"
28+
]
29+
}
30+
}
31+
],
32+
"setupQueries": [
33+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
34+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '503386f9121daf70e54000a04c4c8b05')"
35+
]
36+
}
37+
}

core/database/src/androidMain/java/org/mifos/core/database/AppDatabase.android.kt core/database/src/androidMain/kotlin/org/mifos/core/database/AppDatabase.android.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@ package org.mifos.core.database
1212
import androidx.room.Database
1313
import androidx.room.RoomDatabase
1414
import androidx.room.TypeConverters
15-
import org.mifos.core.database.dao.ChargeDao
16-
import org.mifos.core.database.entity.ChargeEntity
15+
import org.mifos.core.database.dao.SampleDao
16+
import org.mifos.core.database.entity.SampleEntity
1717
import org.mifos.core.database.utils.ChargeTypeConverters
1818

1919
@Database(
2020
entities = [
21-
ChargeEntity::class,
21+
SampleEntity::class,
2222
],
2323
version = AppDatabase.VERSION,
2424
exportSchema = true,
2525
autoMigrations = [],
2626
)
2727
@TypeConverters(ChargeTypeConverters::class)
2828
actual abstract class AppDatabase : RoomDatabase() {
29-
actual abstract val chargeDao: ChargeDao
29+
30+
actual abstract val sampleDao: SampleDao
3031

3132
companion object {
3233
const val VERSION = 1

core/database/src/androidMain/java/org/mifos/core/database/di/DatabaseModule.android.kt core/database/src/androidMain/kotlin/org/mifos/core/database/di/DatabaseModule.android.kt

+12-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@
99
*/
1010
package org.mifos.core.database.di
1111

12-
import androidx.sqlite.driver.bundled.BundledSQLiteDriver
12+
import kotlinx.coroutines.CoroutineDispatcher
1313
import org.koin.android.ext.koin.androidApplication
1414
import org.koin.core.module.Module
1515
import org.koin.core.qualifier.named
1616
import org.koin.dsl.module
1717
import org.mifos.core.common.di.AppDispatchers
18-
import org.mifos.core.database.AppDatabaseFactory
18+
import org.mifos.core.database.AppDatabase
19+
import org.mifos.corebase.AppDatabaseFactory
20+
import kotlin.coroutines.CoroutineContext
1921

2022
actual val platformModule: Module = module {
2123
single {
22-
AppDatabaseFactory(androidApplication())
23-
.createDatabase()
24+
AppDatabaseFactory(
25+
androidApplication(),
26+
)
27+
.createDatabase(
28+
databaseClass = AppDatabase::class.java,
29+
databaseName = AppDatabase.DATABASE_NAME,
30+
)
2431
.fallbackToDestructiveMigrationOnDowngrade(false)
25-
.setDriver(BundledSQLiteDriver())
26-
.setQueryCoroutineContext(get(named(AppDispatchers.IO.name)))
32+
.setQueryCoroutineContext(get<CoroutineDispatcher>(named(AppDispatchers.IO.name)) as CoroutineContext)
2733
.build()
2834
}
2935
}

core/database/src/commonMain/kotlin/org/mifos/core/database/AppDatabase.kt

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
*/
1010
package org.mifos.core.database
1111

12-
import org.mifos.core.database.dao.ChargeDao
12+
import org.mifos.core.database.dao.SampleDao
1313

14+
@Suppress("NO_ACTUAL_FOR_EXPECT")
1415
expect abstract class AppDatabase {
15-
abstract val chargeDao: ChargeDao
16+
abstract val sampleDao: SampleDao
1617
}
17-
18-
// fun getRoomDatabase(
19-
// appDatabase: AppDatabase,
20-
// ): AppDatabase {
21-
// return appDatabase
22-
// }

0 commit comments

Comments
 (0)