|
1 | | -import { DB, SQLBatchTuple } from '@op-engineering/op-sqlite'; |
2 | | -import { BaseObserver, DBAdapterListener, QueryResult, RowUpdateType } from '@powersync/common'; |
| 1 | +import { DB, SQLBatchTuple, UpdateHookOperation } from '@op-engineering/op-sqlite'; |
| 2 | +import { |
| 3 | + BaseObserver, |
| 4 | + BatchedUpdateNotification, |
| 5 | + DBAdapterListener, |
| 6 | + QueryResult, |
| 7 | + RowUpdateType, |
| 8 | + UpdateNotification |
| 9 | +} from '@powersync/common'; |
3 | 10 |
|
4 | 11 | export type OPSQLiteConnectionOptions = { |
5 | 12 | baseDB: DB; |
6 | 13 | }; |
7 | 14 |
|
| 15 | +export type OPSQLiteUpdateNotification = { |
| 16 | + table: string; |
| 17 | + operation: UpdateHookOperation; |
| 18 | + row?: any; |
| 19 | + rowId: number; |
| 20 | +}; |
| 21 | + |
8 | 22 | export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> { |
9 | 23 | protected DB: DB; |
| 24 | + private updateBuffer: UpdateNotification[]; |
| 25 | + |
10 | 26 | constructor(protected options: OPSQLiteConnectionOptions) { |
11 | 27 | super(); |
12 | 28 | this.DB = options.baseDB; |
| 29 | + this.updateBuffer = []; |
| 30 | + |
| 31 | + this.DB.rollbackHook(() => { |
| 32 | + this.updateBuffer = []; |
| 33 | + }); |
13 | 34 |
|
14 | | - // link table update commands |
15 | 35 | this.DB.updateHook((update) => { |
16 | | - this.iterateListeners((cb) => { |
17 | | - let opType: RowUpdateType; |
18 | | - switch (update.operation) { |
19 | | - case 'INSERT': |
20 | | - opType = RowUpdateType.SQLITE_INSERT; |
21 | | - break; |
22 | | - case 'DELETE': |
23 | | - opType = RowUpdateType.SQLITE_DELETE; |
24 | | - break; |
25 | | - case 'UPDATE': |
26 | | - opType = RowUpdateType.SQLITE_UPDATE; |
27 | | - break; |
28 | | - } |
29 | | - cb.tablesUpdated?.({ |
30 | | - table: update.table, |
31 | | - opType, |
32 | | - rowId: update.rowId |
33 | | - }); |
34 | | - }); |
| 36 | + this.addTableUpdate(update); |
35 | 37 | }); |
36 | 38 | } |
37 | 39 |
|
| 40 | + addTableUpdate(update: OPSQLiteUpdateNotification) { |
| 41 | + let opType: RowUpdateType; |
| 42 | + switch (update.operation) { |
| 43 | + case 'INSERT': |
| 44 | + opType = RowUpdateType.SQLITE_INSERT; |
| 45 | + break; |
| 46 | + case 'DELETE': |
| 47 | + opType = RowUpdateType.SQLITE_DELETE; |
| 48 | + break; |
| 49 | + case 'UPDATE': |
| 50 | + opType = RowUpdateType.SQLITE_UPDATE; |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + this.updateBuffer.push({ |
| 55 | + table: update.table, |
| 56 | + opType, |
| 57 | + rowId: update.rowId |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + flushUpdates() { |
| 62 | + if (!this.updateBuffer.length) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const groupedUpdates = this.updateBuffer.reduce((grouping: Record<string, UpdateNotification[]>, update) => { |
| 67 | + const { table } = update; |
| 68 | + const updateGroup = grouping[table] || (grouping[table] = []); |
| 69 | + updateGroup.push(update); |
| 70 | + return grouping; |
| 71 | + }, {}); |
| 72 | + |
| 73 | + const batchedUpdate: BatchedUpdateNotification = { |
| 74 | + groupedUpdates, |
| 75 | + rawUpdates: this.updateBuffer, |
| 76 | + tables: Object.keys(groupedUpdates) |
| 77 | + }; |
| 78 | + |
| 79 | + this.updateBuffer = []; |
| 80 | + this.iterateListeners((l) => l.tablesUpdated?.(batchedUpdate)); |
| 81 | + } |
| 82 | + |
38 | 83 | close() { |
39 | 84 | return this.DB.close(); |
40 | 85 | } |
|
0 commit comments