-
Notifications
You must be signed in to change notification settings - Fork 19
Add SQLDelight driver #231
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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.kotlinter) | ||
alias(libs.plugins.sqldelight) | ||
id("com.powersync.plugins.sharedbuild") | ||
} | ||
|
||
kotlin { | ||
// We don't test on Android devices, JVM tests are enough for the SQLDelight test package since | ||
// it doesn't contain Android-specific code. | ||
powersyncTargets(android = false) | ||
|
||
explicitApi() | ||
applyDefaultHierarchyTemplate() | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
api(libs.sqldelight.runtime) | ||
} | ||
} | ||
} | ||
|
||
sqldelight { | ||
databases { | ||
linkSqlite.set(false) | ||
|
||
create("TestDatabase") { | ||
packageName.set("com.powersync.integrations.sqldelight") | ||
generateAsync.set(true) | ||
deriveSchemaFromMigrations.set(false) | ||
dialect(libs.sqldelight.dialect.sqlite38) | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<LintTask> { | ||
exclude { it.file.path.contains("build/generated") } | ||
} | ||
|
||
tasks.withType<FormatTask> { | ||
exclude { it.file.path.contains("build/generated") } | ||
} |
14 changes: 14 additions & 0 deletions
14
...ht-test-database/src/commonMain/sqldelight/com/powersync/integrations/sqldelight/todos.sq
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,14 @@ | ||
CREATE TABLE todos ( | ||
id TEXT NOT NULL DEFAULT '', | ||
title TEXT, | ||
content TEXT | ||
); | ||
|
||
all: | ||
SELECT * FROM todos; | ||
|
||
create: | ||
INSERT INTO todos (id, title, content) VALUES (uuid(), ?, ?); | ||
|
||
update: | ||
UPDATE todos SET content = content || title RETURNING *; |
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,62 @@ | ||
## PowerSync SQLDelight driver | ||
|
||
This library provides the `PowerSyncDriver` class, which implements an `SqlDriver` for `SQLDelight` | ||
backed by PowerSync. | ||
|
||
## Setup | ||
|
||
Add a dependency on `com.powersync:integration-sqldelight`, using the same version you use for the | ||
PowerSync SDK. | ||
|
||
## Usage | ||
|
||
To get started, ensure that SQLDelight is not linking sqlite3 (the PowerSync SDK takes care of that, | ||
and you don't want to link it twice). Also, ensure the async generator is active because the | ||
PowerSync driver does not support synchronous reads: | ||
|
||
```kotlin | ||
sqldelight { | ||
databases { | ||
linkSqlite.set(false) | ||
|
||
create("MyAppDatabase") { | ||
generateAsync.set(true) | ||
deriveSchemaFromMigrations.set(false) | ||
|
||
dialect("app.cash.sqldelight:sqlite-3-38-dialect") | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Next, define your tables in `.sq` files (but note that the `CREATE TABLE` statement won't be used, | ||
PowerSync creates JSON-backed views for tables instead). | ||
Open a PowerSync database [in the usual way](https://docs.powersync.com/client-sdk-references/kotlin-multiplatform#getting-started) | ||
and finally pass it to the constructor of your generated SQLDelight database: | ||
|
||
```kotlin | ||
val db: PowerSyncDatabase = openPowerSyncDatabase() | ||
val yourSqlDelightDatabase = YourDatabase(PowerSyncDriver(db)) | ||
``` | ||
|
||
Afterwards, writes on both databases (the original `PowerSyncDatabase` instance and the SQLDelight | ||
database) will be visible to each other, update each other's query flows and will get synced | ||
properly. | ||
|
||
## Limitations | ||
|
||
Please note that this library is currently in alpha. It is tested, but API changes are still | ||
possible. | ||
|
||
There are also some limitations to be aware of: | ||
|
||
1. Due to historical reasons, the PowerSync SDK migrates all databases to `user_version` 1 when | ||
created (but it will never downgrade a database). | ||
So if you want to use SQLDelight's schema tools, the first version would have to be `2`. | ||
2. The `CREATE TABLE` statements in your `.sq` files are only used at build time to verify your | ||
queries. At runtime, PowerSync will create tables from your schema as views, the defined | ||
statements are ignored. | ||
If you want to use the schema managed by SQLDelight, configure PowerSync to use | ||
[raw tables](https://docs.powersync.com/usage/use-case-examples/raw-tables). | ||
3. Functions and tables contributed by the PowerSync core extension are not visible to `.sq` files | ||
at the moment. We might revisit this with a custom dialect in the future. |
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,70 @@ | ||
import com.powersync.plugins.utils.powersyncTargets | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.kotlinter) | ||
alias(libs.plugins.kotlin.atomicfu) | ||
id("com.powersync.plugins.sonatype") | ||
id("com.powersync.plugins.sharedbuild") | ||
id("dokka-convention") | ||
} | ||
|
||
kotlin { | ||
powersyncTargets() | ||
explicitApi() | ||
applyDefaultHierarchyTemplate() | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
api(projects.core) | ||
api(libs.sqldelight.runtime) | ||
implementation(libs.kotlinx.coroutines.core) | ||
} | ||
|
||
commonTest.dependencies { | ||
// Separate project because SQLDelight can't generate code in test source sets. | ||
implementation(projects.integrations.sqldelightTestDatabase) | ||
|
||
implementation(libs.kotlin.test) | ||
implementation(libs.kotlinx.io) | ||
implementation(libs.test.turbine) | ||
implementation(libs.test.coroutines) | ||
implementation(libs.test.kotest.assertions) | ||
|
||
implementation(libs.sqldelight.coroutines) | ||
} | ||
|
||
val commonIntegrationTest by creating { | ||
dependsOn(commonTest.get()) | ||
} | ||
|
||
// The PowerSync SDK links the core extension, so we can just run tests as-is. | ||
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) | ||
} | ||
} | ||
|
||
android { | ||
namespace = "com.powersync.drivers.common" | ||
compileSdk = | ||
libs.versions.android.compileSdk | ||
.get() | ||
.toInt() | ||
defaultConfig { | ||
minSdk = | ||
libs.versions.android.minSdk | ||
.get() | ||
.toInt() | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
} | ||
|
||
dokka { | ||
moduleName.set("PowerSync for SQLDelight") | ||
} |
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-sqldelight | ||
POM_NAME=PowerSync SQLDelight driver | ||
POM_DESCRIPTION=Use a PowerSync database for your SQLDelight database. |
5 changes: 5 additions & 0 deletions
5
...elight/src/appleTest/kotlin/com/powersync/integrations/sqldelight/SqlDelightTest.apple.kt
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,5 @@ | ||
package com.powersync.integrations.sqldelight | ||
|
||
import com.powersync.DatabaseDriverFactory | ||
|
||
actual fun databaseDriverFactory(): DatabaseDriverFactory = DatabaseDriverFactory() |
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.