Pablo
12/02/2024, 12:28 PMAlejandro Rios
12/02/2024, 2:48 PMFabioCornelli
12/02/2024, 2:51 PM:database
where you'd like to use room, here is what you have to configure in build.gradle.kts (:database)
plugins {
//..other plugins..
kotlin("multiplatform")
id("com.google.devtools.ksp") version "2.0.21-1.0.28" //ksp version
id("androidx.room") version "2.7.0-alpha11" //room version
//..other plugins..
}
kotlin {
androidTarget()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val commonMain by getting {
dependencies {
//..other commonMain dependencies..
// Room dependencies
val roomVersion = "2.7.0-alpha11"
implementation("androidx.room:room-runtime:$roomVersion")
implementation("androidx.sqlite:sqlite-bundled:2.5.0-alpha11")
//..other commonMain dependencies..
}
}
//.. other sourceSets configuration
}
}
// Ksp configuration for kmp projects (see also: <https://kotlinlang.org/docs/ksp-multiplatform.html>)
dependencies {
val roomVersion = "2.7.0-alpha11"
add("kspAndroid", "androidx.room:room-compiler:$roomVersion")
add("kspIosSimulatorArm64","androidx.room:room-compiler:$roomVersion")
add("kspIosX64","androidx.room:room-compiler:$roomVersion")
add("kspIosArm64", "androidx.room:room-compiler:$roomVersion")
}
// room plugin configuration
room {
schemaDirectory("$projectDir/schemas") //or any folder you'd like to use
}
Now, this should get you started quickly.
You can then improve and clean-up the gradle file by adding toml declaration and use that instead.
See the link posted by AlejandroPablo
12/02/2024, 10:40 PMConfiguration with name 'kspJvm' not found.
Pablo
12/02/2024, 10:40 PMadd("kspJvm", libs.androidx.room.compiler)
Pablo
12/02/2024, 10:41 PMroom {
schemaDirectory("$projectDir/schemas")
}
Pablo
12/02/2024, 10:41 PMPablo
12/02/2024, 10:44 PMkspDesktop
solved the issues, now tomorrow will continue trying make it working thank youPablo
12/03/2024, 2:43 PM@Database(
entities = [CachedGeneralTestModel::class, CachedTestDateModel::class],
version = 1,
exportSchema = true,
)
@TypeConverters(DateConverter::class)
@ConstructedBy(TestDatabaseConstructor::class)
abstract class TestDatabase : RoomDatabase() {
abstract fun cachedGeneralTestDAO(): CachedGeneralTestDAO
}
@Suppress("NO_ACTUAL_FOR_EXPECT")
internal expect object TestDatabaseConstructor : RoomDatabaseConstructor<TestDatabase>
const val dbFileName = "test.db"
fun <T : RoomDatabase> RoomDatabase.Builder<T>.setDefaults(): RoomDatabase.Builder<T> = this.apply {
setJournalMode(RoomDatabase.JournalMode.WRITE_AHEAD_LOGGING) //enabling WAL <https://www.sqlite.org/wal.html>
setDriver(BundledSQLiteDriver())
addCallback(TestDatabaseCallback())
addFallbackInDebugOnly()
setQueryCoroutineContext(Dispatchers.IO)
}
private fun <T : RoomDatabase> RoomDatabase.Builder<T>.addFallbackInDebugOnly(): RoomDatabase.Builder<T> {
if (BuildKonfig.isDebug) { // we are using BuildKonfig as a demonstration reference
fallbackToDestructiveMigration(true)
}
return this
}
Pablo
12/03/2024, 2:43 PM@Database(entities = [MyModel::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun myModelDao(): MyModelDao
}
Alejandro Rios
12/03/2024, 2:45 PMPablo
12/03/2024, 2:46 PMPablo
12/03/2024, 2:46 PMPablo
12/03/2024, 2:46 PMAlejandro Rios
12/03/2024, 4:40 PMsimple abstract definition
with no issues? if so, then you're good to go.
The article might seem complex but is because the setup concept he wants to show, a setup that handles Write-Ahead logging
and a DatabaseBuilder
, here's a different approach