From 46bf532bf379fd8e8bb002272d281a21885c6f93 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 19 Aug 2026 09:27:35 +0200 Subject: [PATCH 1/2] Fix encrypted Room snippet Issue reported in https://github.com/powersync-ja/powersync-kotlin/issues/370 --- client-sdks/orms/kotlin/room.mdx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/client-sdks/orms/kotlin/room.mdx b/client-sdks/orms/kotlin/room.mdx index 162d16ff..5d294ee4 100644 --- a/client-sdks/orms/kotlin/room.mdx +++ b/client-sdks/orms/kotlin/room.mdx @@ -62,19 +62,26 @@ class PowerSyncConnectionFactoryAsSqliteDriver( return powersyncFactory.openInMemoryConnection() } - return powersyncFactory.openConnection(fileName, null, false) + val path = Path(fileName) + return factory.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 KMP 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() ``` From 625ae3ad1448e8409dc78cf2aa7d259f8d10c799 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 19 Aug 2026 09:29:13 +0200 Subject: [PATCH 2/2] Add imports --- client-sdks/orms/kotlin/room.mdx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/client-sdks/orms/kotlin/room.mdx b/client-sdks/orms/kotlin/room.mdx index 5d294ee4..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 { @@ -63,7 +68,11 @@ class PowerSyncConnectionFactoryAsSqliteDriver( } val path = Path(fileName) - return factory.openConnection(dbFilename = path.name, dbDirectory = path.parent?.toString(), readOnly = false) + return powersyncFactory.openConnection( + dbFilename = path.name, + dbDirectory = path.parent?.toString(), + readOnly = false + ) } } ``` @@ -78,7 +87,7 @@ val driver = PowerSyncConnectionFactoryAsSqliteDriver( AndroidEncryptedDatabaseFactory(context, Key.Passphrase("your encryption key")) ) -// Example for Android, all other KMP targets are supported too. +// 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)