diff --git a/client-sdks/orms/kotlin/room.mdx b/client-sdks/orms/kotlin/room.mdx index 162d16ff..7c20ef8d 100644 --- a/client-sdks/orms/kotlin/room.mdx +++ b/client-sdks/orms/kotlin/room.mdx @@ -54,6 +54,11 @@ To add PowerSync to your Room database, When using PowerSync with Room, PowerSync wraps the Room database instead of opening its own connection. To use an encrypted database with Room, implement `SQLiteDriver` by delegating to `PersistentConnectionFactory` and pass that to `RoomDatabase.Builder.setDriver`: ```kotlin +import androidx.sqlite.SQLiteConnection +import androidx.sqlite.SQLiteDriver +import com.powersync.PersistentConnectionFactory +import kotlinx.io.files.Path + class PowerSyncConnectionFactoryAsSqliteDriver( private val powersyncFactory: PersistentConnectionFactory ) : SQLiteDriver { @@ -62,19 +67,30 @@ class PowerSyncConnectionFactoryAsSqliteDriver( return powersyncFactory.openInMemoryConnection() } - return powersyncFactory.openConnection(fileName, null, false) + val path = Path(fileName) + return powersyncFactory.openConnection( + dbFilename = path.name, + dbDirectory = path.parent?.toString(), + readOnly = false + ) } } ``` -Depending on your target platform, pass an `AndroidEncryptedDatabaseFactory`, `JavaEncryptedDatabaseFactory`, or `NativeEncryptedDatabaseFactory` to the wrapper. The factory automatically installs the PowerSync SQLite extension, so you can skip the `loadPowerSyncExtension()` step. +Depending on your target platform, pass an `AndroidEncryptedDatabaseFactory`, `JavaEncryptedDatabaseFactory`, or `NativeEncryptedDatabaseFactory` to the wrapper. +The factory automatically installs the PowerSync SQLite extension, so you can skip the `loadPowerSyncExtension()` step. +Using the custom driver, follow the [platform-specific instructions](https://developer.android.com/kotlin/multiplatform/room#creating-database) +for opening databases: ```kotlin val driver = PowerSyncConnectionFactoryAsSqliteDriver( AndroidEncryptedDatabaseFactory(context, Key.Passphrase("your encryption key")) ) -val roomDb = Room.databaseBuilder(context, "your_database") +// Example for Android, all other multiplatform targets are supported too. +val appContext = context.applicationContext +val dbFile = appContext.getDatabasePath("my_encrypted_room.db") +val roomDb = Room.databaseBuilder(appContext, dbFile.absolutePath) .setDriver(driver) .build() ```