-
Notifications
You must be signed in to change notification settings - Fork 19
Add Room integration #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add Room integration #249
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0d89b62
Room support
simolus3 5f58ffe
Add changelogs
simolus3 beb3beb
Ignore generated code for linting
simolus3 0a24691
Remove android unit tests
simolus3 f4d49e3
Link to docs
simolus3 b70029b
Fix Room tests only running on the JVM
simolus3 b3dc0ac
Review feedback
simolus3 a82f851
Update readme
simolus3 c629745
update top-level readme too
simolus3 ab24db4
JVM fixes
simolus3 e27f4da
Increase test timeout
simolus3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# PowerSync Room integration | ||
|
||
This module provides the ability to use PowerSync with Room databases. This module aims for complete | ||
Room support, meaning that: | ||
|
||
1. Changes synced from PowerSync automatically update your Room `Flow`s. | ||
2. Room and PowerSync cooperate on the write connection, avoiding "database is locked errors". | ||
3. Changes from Room trigger a CRUD upload. | ||
|
||
## Setup | ||
|
||
Add a dependency on `com.powersync:integration-room` with the same version you use for the main | ||
PowerSync SDK. | ||
|
||
PowerSync can use an existing Room database, provided that the PowerSync core SQLite extension has | ||
been loaded. To do that: | ||
|
||
1. Add a dependency on `androidx.sqlite:sqlite-bundled`. Using the SQLite version from the Android | ||
framework will not work as it doesn't support loading extensions. | ||
2. On your `RoomDatabase.Builder`, call `setDriver()` with a PowerSync-enabled driver: | ||
```Kotlin | ||
val driver = BundledSQLiteDriver().also { | ||
it.loadPowerSyncExtension() // Extension method by this module | ||
} | ||
|
||
Room.databaseBuilder(...).setDriver(driver).build() | ||
``` | ||
3. Configure raw tables for your Room databases. | ||
|
||
After these steps, you can open your Room database like you normally would. Then, you can use the | ||
following method to obtain a `PowerSyncDatabase` instance which is backed by Room: | ||
|
||
```Kotlin | ||
// With Room, you need to use raw tables (https://docs.powersync.com/usage/use-case-examples/raw-tables). | ||
// This is because Room verifies your schema at runtime, and PowerSync-managed views will not | ||
// pass those checks. | ||
val schema = Schema(...) | ||
val pool = RoomConnectionPool(yourRoomDatabase, schema) | ||
val powersync = PowerSyncDatabase.opened( | ||
pool = pool, | ||
scope = this, | ||
schema = schema, | ||
identifier = "databaseName", // Prefer to use the same path/name as your Room database | ||
logger = Logger, | ||
) | ||
powersync.connect(...) | ||
``` | ||
|
||
Changes from PowerSync (regardless of whether they've been made with `powersync.execute` or from a | ||
sync operation) will automatically trigger updates in Room. | ||
|
||
To also transfer local writes to PowerSync, you need to | ||
|
||
1. Create triggers on your Room tables to insert into `ps_crud` (see the | ||
[PowerSync documentation on raw tables](https://docs.powersync.com/usage/use-case-examples/raw-tables#capture-local-writes-with-triggers) | ||
for details). | ||
2. Pass the schema as a second parameter to the `RoomConnectionPool` constructor. This will make the | ||
pool notify PowerSync on Room writes for every raw table mentioned in the schema. | ||
Alternatively, call `transferPendingRoomUpdatesToPowerSync` after writes in Room. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import com.powersync.plugins.sonatype.setupGithubRepository | ||
import com.powersync.plugins.utils.powersyncTargets | ||
import org.jmailen.gradle.kotlinter.tasks.FormatTask | ||
import org.jmailen.gradle.kotlinter.tasks.LintTask | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.kotlinter) | ||
alias(libs.plugins.ksp) | ||
alias(libs.plugins.kotlinSerialization) | ||
id("com.powersync.plugins.sonatype") | ||
id("dokka-convention") | ||
id("com.powersync.plugins.sharedbuild") | ||
} | ||
|
||
kotlin { | ||
powersyncTargets() | ||
explicitApi() | ||
applyDefaultHierarchyTemplate() | ||
|
||
sourceSets { | ||
all { | ||
languageSettings { | ||
optIn("com.powersync.ExperimentalPowerSyncAPI") | ||
} | ||
} | ||
|
||
commonMain.dependencies { | ||
api(project(":core")) | ||
api(libs.androidx.room.runtime) | ||
api(libs.androidx.sqlite.bundled) | ||
|
||
implementation(libs.kotlinx.serialization.json) | ||
} | ||
|
||
commonTest.dependencies { | ||
implementation(libs.kotlin.test) | ||
implementation(libs.kotlinx.io) | ||
implementation(libs.test.kotest.assertions) | ||
implementation(libs.test.coroutines) | ||
implementation(libs.test.turbine) | ||
|
||
implementation(libs.androidx.sqlite.bundled) | ||
} | ||
|
||
val commonIntegrationTest by creating { | ||
dependsOn(commonTest.get()) | ||
} | ||
|
||
// We're putting the native libraries into our JAR, so integration tests for the JVM can run as part of the unit | ||
// tests. | ||
jvmTest.get().dependsOn(commonIntegrationTest) | ||
|
||
// We have special setup in this build configuration to make these tests link the PowerSync extension, so they | ||
// can run integration tests along with the executable for unit testing. | ||
nativeTest.orNull?.dependsOn(commonIntegrationTest) | ||
} | ||
} | ||
|
||
dependencies { | ||
// We use a room database for testing, so we apply the symbol processor on the test target. | ||
val targets = listOf( | ||
"jvm", | ||
"macosArm64", | ||
"macosX64", | ||
"iosSimulatorArm64", | ||
"iosX64", | ||
"tvosSimulatorArm64", | ||
"tvosX64", | ||
"watchosSimulatorArm64", | ||
"watchosX64" | ||
) | ||
|
||
targets.forEach { target -> | ||
val capitalized = target.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() } | ||
|
||
add("ksp${capitalized}Test", libs.androidx.room.compiler) | ||
} | ||
} | ||
|
||
android { | ||
namespace = "com.powersync.compose" | ||
compileSdk = | ||
libs.versions.android.compileSdk | ||
.get() | ||
.toInt() | ||
defaultConfig { | ||
minSdk = | ||
libs.versions.android.minSdk | ||
.get() | ||
.toInt() | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
} | ||
|
||
setupGithubRepository() | ||
|
||
dokka { | ||
moduleName.set("PowerSync Room Integration") | ||
} | ||
|
||
tasks.withType<LintTask> { | ||
exclude { it.file.path.contains("build/generated") } | ||
} | ||
|
||
tasks.withType<FormatTask> { | ||
exclude { it.file.path.contains("build/generated") } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
POM_ARTIFACT_ID=integration-room | ||
POM_NAME=Room integration for PowerSync | ||
POM_DESCRIPTION=Use PowerSync to sync data from Room databases. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.