Skip to content

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

Draft
wants to merge 4 commits into
base: refactor-drivers
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ and API documentation [here](https://powersync-ja.github.io/powersync-kotlin/).
1. Retrieve a token to connect to the PowerSync service.
2. Apply local changes on your backend application server (and from there, to your backend database).

- [integrations](./integrations/)
- [sqldelight](./integrations/sqldelight/): An experimental SQLDelight driver backed by PowerSync databases.
Changes to the PowerSync database, including those from the server, update SQLDelight queries.

## Demo Apps / Example Projects

The easiest way to test the PowerSync KMP SDK is to run one of our demo applications.
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {
alias(libs.plugins.kotlinter) apply false
alias(libs.plugins.keeper) apply false
alias(libs.plugins.kotlin.atomicfu) apply false
alias(libs.plugins.sqldelight) apply false
id("org.jetbrains.dokka") version libs.versions.dokkaBase
id("dokka-convention")
}
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ powersync-core = "0.4.2"
sqlite-jdbc = "3.50.3.0"
turbine = "1.2.0"
kotest = "5.9.1"
sqldelight = "2.1.0"

stately = "2.1.0"
supabase = "3.0.1"
Expand Down Expand Up @@ -95,6 +96,10 @@ supabase-storage = { module = "io.github.jan-tennert.supabase:storage-kt", versi
androidx-sqlite = { module = "androidx.sqlite:sqlite", version.ref = "androidxSqlite" }
androidx-sqliteFramework = { module = "androidx.sqlite:sqlite-framework", version.ref = "androidxSqlite" }

sqldelight-coroutines = { module = "app.cash.sqldelight:coroutines-extensions", version.ref = "sqldelight" }
sqldelight-runtime = { module = "app.cash.sqldelight:runtime", version.ref = "sqldelight" }
sqldelight-dialect-sqlite38 = { module = "app.cash.sqldelight:sqlite-3-38-dialect", version.ref = "sqldelight" }

# Sample - Android
androidx-core = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
Expand All @@ -121,3 +126,4 @@ kotlinter = { id = "org.jmailen.kotlinter", version.ref = "kotlinter" }
keeper = { id = "com.slack.keeper", version.ref = "keeper" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
34 changes: 34 additions & 0 deletions integrations/sqldelight-test-database/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import com.powersync.plugins.utils.powersyncTargets

plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinter)
alias(libs.plugins.sqldelight)
id("com.powersync.plugins.sonatype")
}

kotlin {
// Disabling Android for simplicity, we're only testing the common driver anyway
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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(), ?, ?);
55 changes: 55 additions & 0 deletions integrations/sqldelight/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## PowerSync SQLDelight driver

This library provides the `PowerSyncDriver` class, which implements an `SqlDriver` for `SQLDelight`
backed by PowerSync.

## 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. While you can write `CREATE TABLE` statements in your `.sq` files, note that these aren't
actually used - you still have to define your PowerSync schema and the SDK will auto-create the
tables from there.
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.
57 changes: 57 additions & 0 deletions integrations/sqldelight/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import com.powersync.plugins.utils.powersyncTargets

plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinter)
alias(libs.plugins.kotlin.atomicfu)
id("com.powersync.plugins.sonatype")
id("dokka-convention")
}

kotlin {
powersyncTargets()
explicitApi()
applyDefaultHierarchyTemplate()

sourceSets {
commonMain.dependencies {
api(projects.core)
api(libs.sqldelight.runtime)
implementation(libs.kotlinx.coroutines.core)
}

jvmTest.dependencies {
// Separate project because SQLDelight can't generate code in test source sets.
implementation(projects.integrations.sqldelightTestDatabase)

implementation(libs.kotlin.test)
implementation(libs.test.turbine)
implementation(libs.test.coroutines)
implementation(libs.test.kotest.assertions)

implementation(libs.sqldelight.coroutines)
}
}
}

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")
}
Loading