|
3 | 3 | > [!NOTE] |
4 | 4 | > Note that this package is currently in alpha. |
5 | 5 |
|
6 | | -This module provides the ability to use PowerSync with Room databases. This module aims for complete |
7 | | -Room support, meaning that: |
| 6 | +This module integrates PowerSync with Room databases. This allows you run typed queries against the local database with compile-time validation. |
8 | 7 |
|
9 | | -1. Changes synced from PowerSync automatically update your Room `Flow`s. |
10 | | -2. Room and PowerSync cooperate on the write connection, avoiding "database is locked errors". |
11 | | -3. Changes from Room trigger a CRUD upload. |
12 | | - |
13 | | -For more details on using this module, see its page on the [PowerSync documentation](https://docs.powersync.com/client-sdk-references/kotlin-multiplatform/libraries/room). |
14 | | - |
15 | | -## Setup |
16 | | - |
17 | | -Add a dependency on `com.powersync:integration-room` with the same version you use for the main |
18 | | -PowerSync SDK. |
19 | | - |
20 | | -PowerSync can use an existing Room database, provided that the PowerSync core SQLite extension has |
21 | | -been loaded. To do that: |
22 | | - |
23 | | -1. Add a dependency on `androidx.sqlite:sqlite-bundled`. Using the SQLite version from the Android |
24 | | - framework will not work as it doesn't support loading extensions. |
25 | | -2. On your `RoomDatabase.Builder`, call `setDriver()` with a PowerSync-enabled driver: |
26 | | - ```Kotlin |
27 | | - val driver = BundledSQLiteDriver().also { |
28 | | - it.loadPowerSyncExtension() // Extension method by this module |
29 | | - } |
30 | | - |
31 | | - Room.databaseBuilder(...).setDriver(driver).build() |
32 | | - ``` |
33 | | -3. Configure raw tables for your Room databases. |
34 | | - |
35 | | -After these steps, you can open your Room database like you normally would. Then, you can use the |
36 | | -following method to obtain a `PowerSyncDatabase` instance which is backed by Room: |
37 | | - |
38 | | -```Kotlin |
39 | | -// With Room, you need to use raw tables (https://docs.powersync.com/usage/use-case-examples/raw-tables). |
40 | | -// This is because Room verifies your schema at runtime, and PowerSync-managed views will not |
41 | | -// pass those checks. |
42 | | -val schema = Schema(...) |
43 | | -val pool = RoomConnectionPool(yourRoomDatabase, schema) |
44 | | -val powersync = PowerSyncDatabase.opened( |
45 | | - pool = pool, |
46 | | - scope = this, |
47 | | - schema = schema, |
48 | | - identifier = "databaseName", // Prefer to use the same path/name as your Room database |
49 | | - logger = Logger, |
50 | | -) |
51 | | -powersync.connect(...) |
52 | | -``` |
53 | | - |
54 | | -Changes from PowerSync (regardless of whether they've been made with `powersync.execute` or from a |
55 | | -sync operation) will automatically trigger updates in Room. |
56 | | - |
57 | | -To also transfer local writes to PowerSync, you need to |
58 | | - |
59 | | -1. Create triggers on your Room tables to insert into `ps_crud` (see the |
60 | | - [PowerSync documentation on raw tables](https://docs.powersync.com/usage/use-case-examples/raw-tables#capture-local-writes-with-triggers) |
61 | | - for details). |
62 | | -2. Pass the schema as a second parameter to the `RoomConnectionPool` constructor. This will make the |
63 | | - pool notify PowerSync on Room writes for every raw table mentioned in the schema. |
64 | | - Alternatively, call `transferPendingRoomUpdatesToPowerSync` after writes in Room. |
| 8 | +For details on using this integration, see its page on the [PowerSync documentation](https://docs.powersync.com/client-sdk-references/kotlin-multiplatform/libraries/room). |
0 commit comments