|
| 1 | +## PowerSync SQLDelight driver |
| 2 | + |
| 3 | +This library provides the `PowerSyncDriver` class, which implements an `SqlDriver` for `SQLDelight` |
| 4 | +backed by PowerSync. |
| 5 | + |
| 6 | +## Setup |
| 7 | + |
| 8 | +Add a dependency on `com.powersync:integration-sqldelight`, using the same version you use for the |
| 9 | +PowerSync SDK. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +To get started, ensure that SQLDelight is not linking sqlite3 (the PowerSync SDK takes care of that, |
| 14 | +and you don't want to link it twice). Also, ensure the async generator is active because the |
| 15 | +PowerSync driver does not support synchronous reads: |
| 16 | + |
| 17 | +```kotlin |
| 18 | +sqldelight { |
| 19 | + databases { |
| 20 | + linkSqlite.set(false) |
| 21 | + |
| 22 | + create("MyAppDatabase") { |
| 23 | + generateAsync.set(true) |
| 24 | + deriveSchemaFromMigrations.set(false) |
| 25 | + |
| 26 | + dialect("app.cash.sqldelight:sqlite-3-38-dialect") |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Next, define your tables in `.sq` files (but note that the `CREATE TABLE` statement won't be used, |
| 33 | +PowerSync creates JSON-backed views for tables instead). |
| 34 | +Open a PowerSync database [in the usual way](https://docs.powersync.com/client-sdk-references/kotlin-multiplatform#getting-started) |
| 35 | +and finally pass it to the constructor of your generated SQLDelight database: |
| 36 | + |
| 37 | +```kotlin |
| 38 | +val db: PowerSyncDatabase = openPowerSyncDatabase() |
| 39 | +val yourSqlDelightDatabase = YourDatabase(PowerSyncDriver(db)) |
| 40 | +``` |
| 41 | + |
| 42 | +Afterwards, writes on both databases (the original `PowerSyncDatabase` instance and the SQLDelight |
| 43 | +database) will be visible to each other, update each other's query flows and will get synced |
| 44 | +properly. |
| 45 | + |
| 46 | +## Limitations |
| 47 | + |
| 48 | +Please note that this library is currently in alpha. It is tested, but API changes are still |
| 49 | +possible. |
| 50 | + |
| 51 | +There are also some limitations to be aware of: |
| 52 | + |
| 53 | +1. Due to historical reasons, the PowerSync SDK migrates all databases to `user_version` 1 when |
| 54 | + created (but it will never downgrade a database). |
| 55 | + So if you want to use SQLDelight's schema tools, the first version would have to be `2`. |
| 56 | +2. The `CREATE TABLE` statements in your `.sq` files are only used at build time to verify your |
| 57 | + queries. At runtime, PowerSync will create tables from your schema as views, the defined |
| 58 | + statements are ignored. |
| 59 | + If you want to use the schema managed by SQLDelight, configure PowerSync to use |
| 60 | + [raw tables](https://docs.powersync.com/usage/use-case-examples/raw-tables). |
| 61 | +3. Functions and tables contributed by the PowerSync core extension are not visible to `.sq` files |
| 62 | + at the moment. We might revisit this with a custom dialect in the future. |
0 commit comments