Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions client-sdks/orms/kotlin/room.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<MyDatabase>(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<MyDatabase>(appContext, dbFile.absolutePath)
.setDriver(driver)
.build()
```
Expand Down