|
1 | 1 | import 'package:powersync/powersync.dart';
|
2 | 2 | import 'package:powersync_flutter_local_only_demo/models/sync_mode.dart';
|
3 | 3 |
|
4 |
| -/// This schema design supports an online/local-only workflow by managing data |
| 4 | +/// This schema design supports a local-only to online workflow by managing data |
5 | 5 | /// across two versions of each table: one for local/offline use and one for
|
6 | 6 | /// online/synced use. This approach simplifies the handling of data in different
|
7 | 7 | /// connectivity states.
|
8 | 8 | ///
|
9 |
| -/// For local only, the views become: |
10 |
| -/// online_todos |
| 9 | +/// For local-only, the views become: |
| 10 | +/// inactive_synced_todos |
11 | 11 | /// todos
|
12 |
| -/// online_lists |
| 12 | +/// inactive_synced_lists |
13 | 13 | /// lists
|
14 | 14 | ///
|
15 | 15 | /// - 'todos' and 'lists' refer to the local-only data.
|
16 |
| -/// - 'online_todos' and 'online_lists' refer to the data that will be synced |
17 |
| -/// once online, making it clear that these are not currently synchronized. |
| 16 | +/// - 'inactive_synced_todos' and 'inactive_synced_lists' refer to the data that will be synced |
| 17 | +/// once online. |
18 | 18 | ///
|
19 | 19 | /// For online, we have these views:
|
20 | 20 | /// todos
|
21 |
| -/// local_todos |
| 21 | +/// inactive_local_todos |
22 | 22 | /// lists
|
23 |
| -/// local_lists |
| 23 | +/// inactive_local_lists |
24 | 24 | ///
|
25 | 25 | /// - 'todos' and 'lists' refer to the synced/online data.
|
26 |
| -/// - local_todos' and 'local_lists' refer to the local-only data, allowing |
| 26 | +/// - `inactive_local_todos' and 'inactive_local_lists' refer to the local-only data, allowing |
27 | 27 | /// for temporary storage or operations before syncing.
|
28 | 28 | ///
|
29 | 29 | /// For an offline-to-online transition [switchToOnlineSchema] copies data so that it ends up in the upload queue.
|
30 | 30 |
|
31 | 31 | const todosTable = 'todos';
|
32 | 32 | const listsTable = 'lists';
|
33 | 33 |
|
34 |
| -Schema makeSchema({online = bool}) { |
| 34 | +Schema makeSchema({synced = bool}) { |
35 | 35 | String onlineName(String table) {
|
36 |
| - if (online) { |
| 36 | + if (synced) { |
37 | 37 | return table;
|
38 | 38 | } else {
|
39 |
| - return "online_$table"; |
| 39 | + return "inactive_synced_$table"; |
40 | 40 | }
|
41 | 41 | }
|
42 | 42 |
|
43 | 43 | String localName(String table) {
|
44 |
| - if (online) { |
45 |
| - return "local_$table"; |
| 44 | + if (synced) { |
| 45 | + return "inactive_local_$table"; |
46 | 46 | } else {
|
47 | 47 | return table;
|
48 | 48 | }
|
@@ -79,20 +79,21 @@ Schema makeSchema({online = bool}) {
|
79 | 79 | }
|
80 | 80 |
|
81 | 81 | switchToOnlineSchema(PowerSyncDatabase db, String userId) async {
|
82 |
| - await db.updateSchema(makeSchema(online: true)); |
| 82 | + await db.updateSchema(makeSchema(synced: true)); |
83 | 83 | await setSyncEnabled(true);
|
84 | 84 |
|
85 | 85 | await db.writeTransaction((tx) async {
|
86 |
| - // Copy local data to the "online" views. |
| 86 | + // Copy local-only data to the "online" views. |
87 | 87 | // This records each operation to the crud queue.
|
88 | 88 | await tx.execute(
|
89 |
| - 'INSERT INTO lists(id, name, created_at, owner_id) SELECT id, name, created_at, ? FROM local_lists', |
| 89 | + 'INSERT INTO lists(id, name, created_at, owner_id) SELECT id, name, created_at, ? FROM inactive_local_lists', |
90 | 90 | [userId]);
|
91 | 91 |
|
92 |
| - await tx.execute('INSERT INTO $todosTable SELECT * FROM local_$todosTable'); |
| 92 | + await tx.execute( |
| 93 | + 'INSERT INTO $todosTable SELECT * FROM inactive_local_$todosTable'); |
93 | 94 |
|
94 | 95 | // Delete the "local-only" data.
|
95 |
| - await tx.execute('DELETE FROM local_$todosTable'); |
96 |
| - await tx.execute('DELETE FROM local_$listsTable'); |
| 96 | + await tx.execute('DELETE FROM inactive_local_$todosTable'); |
| 97 | + await tx.execute('DELETE FROM inactive_local_$listsTable'); |
97 | 98 | });
|
98 | 99 | }
|
0 commit comments