@powersync/common 1.57.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/lib/attachments/AttachmentContext.d.ts +5 -5
  2. package/lib/attachments/AttachmentContext.js +6 -2
  3. package/lib/attachments/AttachmentContext.js.map +1 -1
  4. package/lib/attachments/AttachmentQueue.d.ts +6 -6
  5. package/lib/attachments/AttachmentQueue.js +9 -8
  6. package/lib/attachments/AttachmentQueue.js.map +1 -1
  7. package/lib/attachments/AttachmentService.d.ts +3 -3
  8. package/lib/attachments/AttachmentService.js +4 -3
  9. package/lib/attachments/AttachmentService.js.map +1 -1
  10. package/lib/attachments/Schema.d.ts +2 -2
  11. package/lib/attachments/SyncingService.d.ts +2 -2
  12. package/lib/attachments/SyncingService.js +21 -6
  13. package/lib/attachments/SyncingService.js.map +1 -1
  14. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +56 -232
  15. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  16. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  17. package/lib/client/SQLOpenFactory.d.ts +35 -13
  18. package/lib/client/SQLOpenFactory.js +1 -25
  19. package/lib/client/SQLOpenFactory.js.map +1 -1
  20. package/lib/client/compilableQueryWatch.d.ts +2 -2
  21. package/lib/client/compilableQueryWatch.js +1 -2
  22. package/lib/client/compilableQueryWatch.js.map +1 -1
  23. package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -3
  24. package/lib/client/runOnSchemaChange.d.ts +2 -2
  25. package/lib/client/runOnSchemaChange.js.map +1 -1
  26. package/lib/client/sync/bucket/CrudEntry.d.ts +3 -34
  27. package/lib/client/sync/bucket/CrudEntry.js +0 -99
  28. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  29. package/lib/client/sync/options.d.ts +65 -0
  30. package/lib/client/sync/options.js +25 -0
  31. package/lib/client/sync/options.js.map +1 -0
  32. package/lib/client/sync/sync-streams.d.ts +1 -1
  33. package/lib/client/triggers/TriggerManager.d.ts +9 -38
  34. package/lib/client/watched/GetAllQuery.d.ts +3 -3
  35. package/lib/client/watched/GetAllQuery.js +1 -1
  36. package/lib/client/watched/GetAllQuery.js.map +1 -1
  37. package/lib/client/watched/WatchedQuery.d.ts +2 -10
  38. package/lib/client/watched/WatchedQuery.js +0 -11
  39. package/lib/client/watched/WatchedQuery.js.map +1 -1
  40. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -50
  41. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -176
  42. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  43. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -22
  44. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  45. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  46. package/lib/db/DBAdapter.d.ts +72 -154
  47. package/lib/db/DBAdapter.js +70 -108
  48. package/lib/db/DBAdapter.js.map +1 -1
  49. package/lib/db/QueryResult.d.ts +104 -0
  50. package/lib/db/QueryResult.js +96 -0
  51. package/lib/db/QueryResult.js.map +1 -0
  52. package/lib/db/crud/SyncProgress.d.ts +1 -13
  53. package/lib/db/crud/SyncProgress.js +1 -62
  54. package/lib/db/crud/SyncProgress.js.map +1 -1
  55. package/lib/db/crud/SyncStatus.d.ts +32 -66
  56. package/lib/db/crud/SyncStatus.js +1 -253
  57. package/lib/db/crud/SyncStatus.js.map +1 -1
  58. package/lib/db/schema/Column.d.ts +0 -7
  59. package/lib/db/schema/Column.js +0 -7
  60. package/lib/db/schema/Column.js.map +1 -1
  61. package/lib/db/schema/Index.d.ts +2 -6
  62. package/lib/db/schema/Index.js +1 -4
  63. package/lib/db/schema/Index.js.map +1 -1
  64. package/lib/db/schema/IndexedColumn.d.ts +2 -6
  65. package/lib/db/schema/IndexedColumn.js +1 -4
  66. package/lib/db/schema/IndexedColumn.js.map +1 -1
  67. package/lib/db/schema/Schema.d.ts +4 -28
  68. package/lib/db/schema/Schema.js.map +1 -1
  69. package/lib/db/schema/Table.d.ts +50 -96
  70. package/lib/db/schema/Table.js +83 -91
  71. package/lib/db/schema/Table.js.map +1 -1
  72. package/lib/index.d.ts +5 -21
  73. package/lib/index.js +5 -22
  74. package/lib/index.js.map +1 -1
  75. package/lib/utils/BaseObserver.d.ts +1 -1
  76. package/lib/utils/BaseObserver.js +1 -1
  77. package/lib/utils/Logger.d.ts +52 -23
  78. package/lib/utils/Logger.js +35 -33
  79. package/lib/utils/Logger.js.map +1 -1
  80. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  81. package/lib/utils/MetaBaseObserver.js +1 -50
  82. package/lib/utils/MetaBaseObserver.js.map +1 -1
  83. package/lib/utils/mutex.d.ts +3 -53
  84. package/lib/utils/mutex.js +1 -156
  85. package/lib/utils/mutex.js.map +1 -1
  86. package/package.json +8 -45
  87. package/src/attachments/AttachmentContext.ts +11 -10
  88. package/src/attachments/AttachmentQueue.ts +15 -14
  89. package/src/attachments/AttachmentService.ts +7 -6
  90. package/src/attachments/Schema.ts +2 -2
  91. package/src/attachments/SyncingService.ts +23 -9
  92. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  93. package/src/client/SQLOpenFactory.ts +38 -22
  94. package/src/client/compilableQueryWatch.ts +3 -4
  95. package/src/client/connection/PowerSyncBackendConnector.ts +3 -3
  96. package/src/client/runOnSchemaChange.ts +2 -2
  97. package/src/client/sync/bucket/CrudEntry.ts +4 -104
  98. package/src/client/sync/options.ts +77 -0
  99. package/src/client/sync/sync-streams.ts +1 -1
  100. package/src/client/triggers/TriggerManager.ts +10 -41
  101. package/src/client/watched/GetAllQuery.ts +3 -3
  102. package/src/client/watched/WatchedQuery.ts +2 -15
  103. package/src/client/watched/processors/DifferentialQueryProcessor.ts +0 -223
  104. package/src/client/watched/processors/OnChangeQueryProcessor.ts +0 -111
  105. package/src/db/DBAdapter.ts +141 -226
  106. package/src/db/QueryResult.ts +195 -0
  107. package/src/db/crud/SyncProgress.ts +2 -42
  108. package/src/db/crud/SyncStatus.ts +45 -220
  109. package/src/db/schema/Column.ts +0 -8
  110. package/src/db/schema/Index.ts +3 -6
  111. package/src/db/schema/IndexedColumn.ts +3 -6
  112. package/src/db/schema/Schema.ts +4 -4
  113. package/src/db/schema/Table.ts +111 -186
  114. package/src/index.ts +5 -21
  115. package/src/utils/BaseObserver.ts +1 -1
  116. package/src/utils/Logger.ts +80 -34
  117. package/src/utils/MetaBaseObserver.ts +14 -60
  118. package/src/utils/mutex.ts +4 -201
  119. package/dist/bundle.cjs +0 -14390
  120. package/dist/bundle.cjs.map +0 -1
  121. package/dist/bundle.mjs +0 -14309
  122. package/dist/bundle.mjs.map +0 -1
  123. package/dist/bundle.node.cjs +0 -12021
  124. package/dist/bundle.node.cjs.map +0 -1
  125. package/dist/bundle.node.mjs +0 -11940
  126. package/dist/bundle.node.mjs.map +0 -1
  127. package/dist/index.d.cts +0 -4651
  128. package/legacy/sync_protocol.d.ts +0 -103
  129. package/lib/client/AbstractPowerSyncDatabase.js +0 -990
  130. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  131. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -28
  132. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -28
  133. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  134. package/lib/client/ConnectionManager.d.ts +0 -115
  135. package/lib/client/ConnectionManager.js +0 -294
  136. package/lib/client/ConnectionManager.js.map +0 -1
  137. package/lib/client/CustomQuery.d.ts +0 -22
  138. package/lib/client/CustomQuery.js +0 -43
  139. package/lib/client/CustomQuery.js.map +0 -1
  140. package/lib/client/constants.d.ts +0 -4
  141. package/lib/client/constants.js +0 -5
  142. package/lib/client/constants.js.map +0 -1
  143. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -56
  144. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -29
  145. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  146. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -37
  147. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -165
  148. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  149. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -180
  150. package/lib/client/sync/stream/AbstractRemote.js +0 -505
  151. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  152. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -265
  153. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -705
  154. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  155. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  156. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -62
  157. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  158. package/lib/client/sync/stream/core-instruction.d.ts +0 -77
  159. package/lib/client/sync/stream/core-instruction.js +0 -30
  160. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  161. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  162. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  163. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  164. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  165. package/lib/client/triggers/TriggerManagerImpl.js +0 -410
  166. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  167. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  168. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  169. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  170. package/lib/db/ConnectionClosedError.d.ts +0 -12
  171. package/lib/db/ConnectionClosedError.js +0 -23
  172. package/lib/db/ConnectionClosedError.js.map +0 -1
  173. package/lib/db/schema/TableV2.d.ts +0 -11
  174. package/lib/db/schema/TableV2.js +0 -11
  175. package/lib/db/schema/TableV2.js.map +0 -1
  176. package/lib/utils/AbortOperation.d.ts +0 -11
  177. package/lib/utils/AbortOperation.js +0 -21
  178. package/lib/utils/AbortOperation.js.map +0 -1
  179. package/lib/utils/ControlledExecutor.d.ts +0 -31
  180. package/lib/utils/ControlledExecutor.js +0 -54
  181. package/lib/utils/ControlledExecutor.js.map +0 -1
  182. package/lib/utils/async.d.ts +0 -46
  183. package/lib/utils/async.js +0 -147
  184. package/lib/utils/async.js.map +0 -1
  185. package/lib/utils/compatibility.d.ts +0 -8
  186. package/lib/utils/compatibility.js +0 -9
  187. package/lib/utils/compatibility.js.map +0 -1
  188. package/lib/utils/parseQuery.d.ts +0 -12
  189. package/lib/utils/parseQuery.js +0 -20
  190. package/lib/utils/parseQuery.js.map +0 -1
  191. package/lib/utils/queue.d.ts +0 -16
  192. package/lib/utils/queue.js +0 -42
  193. package/lib/utils/queue.js.map +0 -1
  194. package/lib/utils/stream_transform.d.ts +0 -41
  195. package/lib/utils/stream_transform.js +0 -211
  196. package/lib/utils/stream_transform.js.map +0 -1
  197. package/src/client/AbstractPowerSyncDatabase.ts +0 -1408
  198. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -45
  199. package/src/client/ConnectionManager.ts +0 -405
  200. package/src/client/CustomQuery.ts +0 -56
  201. package/src/client/constants.ts +0 -4
  202. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -65
  203. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -202
  204. package/src/client/sync/stream/AbstractRemote.ts +0 -644
  205. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -970
  206. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -82
  207. package/src/client/sync/stream/core-instruction.ts +0 -109
  208. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  209. package/src/client/triggers/TriggerManagerImpl.ts +0 -501
  210. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  211. package/src/db/ConnectionClosedError.ts +0 -25
  212. package/src/db/schema/TableV2.ts +0 -11
  213. package/src/utils/AbortOperation.ts +0 -19
  214. package/src/utils/ControlledExecutor.ts +0 -78
  215. package/src/utils/async.ts +0 -185
  216. package/src/utils/compatibility.ts +0 -9
  217. package/src/utils/parseQuery.ts +0 -31
  218. package/src/utils/queue.ts +0 -48
  219. package/src/utils/stream_transform.ts +0 -260
@@ -1,1408 +0,0 @@
1
- import Logger, { ILogger } from 'js-logger';
2
- import {
3
- BatchedUpdateNotification,
4
- DBAdapter,
5
- LockContext,
6
- QueryResult,
7
- Transaction,
8
- UpdateNotification,
9
- isBatchedUpdateNotification
10
- } from '../db/DBAdapter.js';
11
- import { SyncStatus } from '../db/crud/SyncStatus.js';
12
- import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
13
- import { Schema } from '../db/schema/Schema.js';
14
- import { BaseObserver } from '../utils/BaseObserver.js';
15
- import { ControlledExecutor } from '../utils/ControlledExecutor.js';
16
- import { EventQueue, throttleTrailing } from '../utils/async.js';
17
- import {
18
- ConnectionManager,
19
- CreateSyncImplementationOptions,
20
- InternalSubscriptionAdapter
21
- } from './ConnectionManager.js';
22
- import { CustomQuery } from './CustomQuery.js';
23
- import { ArrayQueryDefinition, Query } from './Query.js';
24
- import { SQLOpenFactory, SQLOpenOptions, isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory.js';
25
- import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
26
- import { BucketStorageAdapter, PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
27
- import { CrudBatch } from './sync/bucket/CrudBatch.js';
28
- import { CrudEntry, CrudEntryJSON } from './sync/bucket/CrudEntry.js';
29
- import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
30
- import {
31
- DEFAULT_CRUD_UPLOAD_THROTTLE_MS,
32
- DEFAULT_RETRY_DELAY_MS,
33
- InternalConnectionOptions,
34
- StreamingSyncImplementation,
35
- StreamingSyncImplementationListener,
36
- type AdditionalConnectionOptions,
37
- type PowerSyncConnectionOptions,
38
- type RequiredAdditionalConnectionOptions
39
- } from './sync/stream/AbstractStreamingSyncImplementation.js';
40
- import { CoreSyncStatus, coreStatusToJs } from './sync/stream/core-instruction.js';
41
- import { SyncStream } from './sync/sync-streams.js';
42
- import { MEMORY_TRIGGER_CLAIM_MANAGER } from './triggers/MemoryTriggerClaimManager.js';
43
- import { TriggerManager, TriggerManagerConfig } from './triggers/TriggerManager.js';
44
- import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
45
- import { DEFAULT_WATCH_THROTTLE_MS, WatchCompatibleQuery } from './watched/WatchedQuery.js';
46
- import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
47
- import { WatchedQueryComparator } from './watched/processors/comparators.js';
48
- import { Mutex } from '../utils/mutex.js';
49
- import { symbolAsyncIterator } from '../utils/compatibility.js';
50
-
51
- /**
52
- * @public
53
- */
54
- export interface DisconnectAndClearOptions {
55
- /** When set to false, data in local-only tables is preserved. */
56
- clearLocal?: boolean;
57
- }
58
-
59
- /**
60
- * @public
61
- */
62
- export interface BasePowerSyncDatabaseOptions extends AdditionalConnectionOptions {
63
- /** Schema used for the local database. */
64
- schema: Schema;
65
- /**
66
- * @deprecated Use {@link AdditionalConnectionOptions.retryDelayMs} instead as this will be removed in future
67
- * releases.
68
- */
69
- retryDelay?: number;
70
- logger?: ILogger;
71
- }
72
-
73
- /**
74
- * @public
75
- */
76
- export interface PowerSyncDatabaseOptions extends BasePowerSyncDatabaseOptions {
77
- /**
78
- * Source for a SQLite database connection.
79
- * This can be either:
80
- * - A {@link DBAdapter} if providing an instantiated SQLite connection
81
- * - A {@link SQLOpenFactory} which will be used to open a SQLite connection
82
- * - {@link SQLOpenOptions} for opening a SQLite connection with a default {@link SQLOpenFactory}
83
- */
84
- database: DBAdapter | SQLOpenFactory | SQLOpenOptions;
85
- }
86
-
87
- /**
88
- * @public
89
- */
90
- export interface PowerSyncDatabaseOptionsWithDBAdapter extends BasePowerSyncDatabaseOptions {
91
- database: DBAdapter;
92
- }
93
-
94
- /**
95
- * @public
96
- */
97
- export interface PowerSyncDatabaseOptionsWithOpenFactory extends BasePowerSyncDatabaseOptions {
98
- database: SQLOpenFactory;
99
- }
100
-
101
- /**
102
- * @public
103
- */
104
- export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatabaseOptions {
105
- database: SQLOpenOptions;
106
- }
107
-
108
- /**
109
- * @public
110
- */
111
- export interface SQLOnChangeOptions {
112
- signal?: AbortSignal;
113
- tables?: string[];
114
- /** The minimum interval between queries. */
115
- throttleMs?: number;
116
- /**
117
- * @deprecated All tables specified in {@link SQLOnChangeOptions.tables} will be watched, including PowerSync tables
118
- * with prefixes.
119
- *
120
- * Allows for watching any SQL table
121
- * by not removing PowerSync table name prefixes
122
- */
123
- rawTableNames?: boolean;
124
- /**
125
- * Emits an empty result set immediately
126
- */
127
- triggerImmediate?: boolean;
128
- }
129
-
130
- /**
131
- * @public
132
- */
133
- export interface SQLWatchOptions extends SQLOnChangeOptions {
134
- /**
135
- * Optional comparator which will be used to compare the results of the query.
136
- * The watched query will only yield results if the comparator returns false.
137
- */
138
- comparator?: WatchedQueryComparator<QueryResult>;
139
- }
140
-
141
- /**
142
- * @public
143
- */
144
- export interface WatchOnChangeEvent {
145
- changedTables: string[];
146
- }
147
-
148
- /**
149
- * @public
150
- */
151
- export interface WatchHandler {
152
- onResult: (results: QueryResult) => void;
153
- onError?: (error: Error) => void;
154
- }
155
-
156
- /**
157
- * @public
158
- */
159
- export interface WatchOnChangeHandler {
160
- onChange: (event: WatchOnChangeEvent) => Promise<void> | void;
161
- onError?: (error: Error) => void;
162
- }
163
-
164
- /**
165
- * @public
166
- */
167
- export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
168
- initialized: () => void;
169
- schemaChanged: (schema: Schema) => void;
170
- closing: () => Promise<void> | void;
171
- closed: () => Promise<void> | void;
172
- }
173
-
174
- /**
175
- * @public
176
- */
177
- export interface PowerSyncCloseOptions {
178
- /**
179
- * Disconnect the sync stream client if connected.
180
- * This is usually true, but can be false for Web when using
181
- * multiple tabs and a shared sync provider.
182
- */
183
- disconnect?: boolean;
184
- }
185
-
186
- const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
187
-
188
- const DEFAULT_DISCONNECT_CLEAR_OPTIONS: DisconnectAndClearOptions = {
189
- clearLocal: true
190
- };
191
-
192
- /**
193
- * @internal
194
- */
195
- export const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions = {
196
- disconnect: true
197
- };
198
-
199
- /**
200
- * @internal
201
- */
202
- export const DEFAULT_POWERSYNC_DB_OPTIONS = {
203
- retryDelayMs: 5000,
204
- crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
205
- };
206
-
207
- /**
208
- * @internal
209
- */
210
- export const DEFAULT_CRUD_BATCH_LIMIT = 100;
211
-
212
- /**
213
- * Requesting nested or recursive locks can block the application in some circumstances.
214
- * This default lock timeout will act as a failsafe to throw an error if a lock cannot
215
- * be obtained.
216
- *
217
- * @internal
218
- */
219
- export const DEFAULT_LOCK_TIMEOUT_MS = 120_000; // 2 mins
220
-
221
- /**
222
- * Tests if the input is a {@link PowerSyncDatabaseOptionsWithSettings}
223
- * @internal
224
- */
225
- export const isPowerSyncDatabaseOptionsWithSettings = (test: any): test is PowerSyncDatabaseOptionsWithSettings => {
226
- return typeof test == 'object' && isSQLOpenOptions(test.database);
227
- };
228
-
229
- /**
230
- * @public
231
- */
232
- export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDBListener> {
233
- /**
234
- * Returns true if the connection is closed.
235
- */
236
- closed: boolean;
237
- ready: boolean;
238
-
239
- /**
240
- * Current connection status.
241
- */
242
- currentStatus: SyncStatus;
243
-
244
- sdkVersion: string;
245
-
246
- protected bucketStorageAdapter: BucketStorageAdapter;
247
- protected _isReadyPromise: Promise<void>;
248
- protected connectionManager: ConnectionManager;
249
- private subscriptions: InternalSubscriptionAdapter;
250
-
251
- get syncStreamImplementation() {
252
- return this.connectionManager.syncStreamImplementation;
253
- }
254
-
255
- /**
256
- * The connector used to connect to the PowerSync service.
257
- *
258
- * @returns The connector used to connect to the PowerSync service or null if `connect()` has not been called.
259
- */
260
- get connector() {
261
- return this.connectionManager.connector;
262
- }
263
-
264
- /**
265
- * The resolved connection options used to connect to the PowerSync service.
266
- *
267
- * @returns The resolved connection options used to connect to the PowerSync service or null if `connect()` has not been called.
268
- */
269
- get connectionOptions() {
270
- return this.connectionManager.connectionOptions;
271
- }
272
-
273
- protected _schema: Schema;
274
-
275
- private _database: DBAdapter;
276
-
277
- protected runExclusiveMutex: Mutex;
278
-
279
- /**
280
- * @experimental
281
- * Allows creating SQLite triggers which can be used to track various operations on SQLite tables.
282
- */
283
- readonly triggers: TriggerManager;
284
- protected triggersImpl: TriggerManagerImpl;
285
-
286
- logger: ILogger;
287
-
288
- constructor(options: PowerSyncDatabaseOptionsWithDBAdapter);
289
- constructor(options: PowerSyncDatabaseOptionsWithOpenFactory);
290
- constructor(options: PowerSyncDatabaseOptionsWithSettings);
291
- constructor(options: PowerSyncDatabaseOptions); // Note this is important for extending this class and maintaining API compatibility
292
- constructor(protected options: PowerSyncDatabaseOptions) {
293
- super();
294
-
295
- const { database, schema } = options;
296
-
297
- if (typeof schema?.toJSON != 'function') {
298
- throw new Error('The `schema` option should be provided and should be an instance of `Schema`.');
299
- }
300
-
301
- if (isDBAdapter(database)) {
302
- this._database = database;
303
- } else if (isSQLOpenFactory(database)) {
304
- this._database = database.openDB();
305
- } else if (isPowerSyncDatabaseOptionsWithSettings(options)) {
306
- this._database = this.openDBAdapter(options);
307
- } else {
308
- throw new Error('The provided `database` option is invalid.');
309
- }
310
-
311
- this.logger = options.logger ?? Logger.get(`PowerSyncDatabase[${this._database.name}]`);
312
-
313
- this.bucketStorageAdapter = this.generateBucketStorageAdapter();
314
- this.closed = false;
315
- this.currentStatus = new SyncStatus({});
316
- this.options = { ...DEFAULT_POWERSYNC_DB_OPTIONS, ...options };
317
- this._schema = schema;
318
- this.ready = false;
319
- this.sdkVersion = '';
320
- this.runExclusiveMutex = new Mutex();
321
-
322
- // Start async init
323
- this.subscriptions = {
324
- firstStatusMatching: (predicate, abort) => this.waitForStatus(predicate, abort),
325
- resolveOfflineSyncStatus: () => this.resolveOfflineSyncStatus(),
326
- rustSubscriptionsCommand: async (payload) => {
327
- await this.writeTransaction((tx) => {
328
- return tx.execute('select powersync_control(?,?)', ['subscriptions', JSON.stringify(payload)]);
329
- });
330
- }
331
- };
332
- this.connectionManager = new ConnectionManager({
333
- createSyncImplementation: async (connector, options) => {
334
- await this.waitForReady();
335
- return this.runExclusive(async () => {
336
- const sync = this.generateSyncStreamImplementation(connector, this.resolvedConnectionOptions(options));
337
- const onDispose = sync.registerListener({
338
- statusChanged: (status) => {
339
- this.currentStatus = new SyncStatus({
340
- ...status.toJSON(),
341
- hasSynced: this.currentStatus?.hasSynced || !!status.lastSyncedAt
342
- });
343
- this.iterateListeners((cb) => cb.statusChanged?.(this.currentStatus));
344
- }
345
- });
346
- await sync.waitForReady();
347
-
348
- return {
349
- sync,
350
- onDispose
351
- };
352
- });
353
- },
354
- logger: this.logger
355
- });
356
-
357
- this._isReadyPromise = this.initialize();
358
-
359
- this.triggers = this.triggersImpl = new TriggerManagerImpl({
360
- db: this,
361
- schema: this.schema,
362
- ...this.generateTriggerManagerConfig()
363
- });
364
- }
365
-
366
- /**
367
- * Schema used for the local database.
368
- */
369
- get schema() {
370
- return this._schema;
371
- }
372
-
373
- /**
374
- * The underlying database.
375
- *
376
- * For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
377
- */
378
- get database() {
379
- return this._database;
380
- }
381
-
382
- /**
383
- * Whether a connection to the PowerSync service is currently open.
384
- */
385
- get connected() {
386
- return this.currentStatus?.connected || false;
387
- }
388
-
389
- get connecting() {
390
- return this.currentStatus?.connecting || false;
391
- }
392
-
393
- /**
394
- * Opens the DBAdapter given open options using a default open factory
395
- */
396
- protected abstract openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter;
397
-
398
- /**
399
- * Generates a base configuration for {@link TriggerManagerImpl}.
400
- * Implementations should override this if necessary.
401
- */
402
- protected generateTriggerManagerConfig(): TriggerManagerConfig {
403
- return {
404
- claimManager: MEMORY_TRIGGER_CLAIM_MANAGER
405
- };
406
- }
407
-
408
- protected abstract generateSyncStreamImplementation(
409
- connector: PowerSyncBackendConnector,
410
- options: CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions
411
- ): StreamingSyncImplementation;
412
-
413
- protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
414
-
415
- /**
416
- * @returns A promise which will resolve once initialization is completed.
417
- */
418
- async waitForReady(): Promise<void> {
419
- if (this.ready) {
420
- return;
421
- }
422
-
423
- await this._isReadyPromise;
424
- }
425
-
426
- /**
427
- * Wait for the first sync operation to complete.
428
- *
429
- * @param request - Either an abort signal (after which the promise will complete regardless of
430
- * whether a full sync was completed) or an object providing an abort signal and a priority target.
431
- * When a priority target is set, the promise may complete when all buckets with the given (or higher)
432
- * priorities have been synchronized. This can be earlier than a complete sync.
433
- * @returns A promise which will resolve once the first full sync has completed.
434
- */
435
- async waitForFirstSync(request?: AbortSignal | { signal?: AbortSignal; priority?: number }): Promise<void> {
436
- const signal = request instanceof AbortSignal ? request : request?.signal;
437
- const priority = request && 'priority' in request ? request.priority : undefined;
438
-
439
- const statusMatches =
440
- priority === undefined
441
- ? (status: SyncStatus) => status.hasSynced
442
- : (status: SyncStatus) => status.statusForPriority(priority).hasSynced;
443
-
444
- return this.waitForStatus(statusMatches, signal);
445
- }
446
-
447
- /**
448
- * Waits for the first sync status for which the `status` callback returns a truthy value.
449
- */
450
- async waitForStatus(predicate: (status: SyncStatus) => any, signal?: AbortSignal): Promise<void> {
451
- if (predicate(this.currentStatus)) {
452
- return;
453
- }
454
-
455
- return new Promise((resolve) => {
456
- const dispose = this.registerListener({
457
- statusChanged: (status) => {
458
- if (predicate(status)) {
459
- abort();
460
- }
461
- }
462
- });
463
-
464
- function abort() {
465
- dispose();
466
- resolve();
467
- }
468
-
469
- if (signal?.aborted) {
470
- abort();
471
- } else {
472
- signal?.addEventListener('abort', abort);
473
- }
474
- });
475
- }
476
-
477
- /**
478
- * Allows for extended implementations to execute custom initialization
479
- * logic as part of the total init process
480
- */
481
- abstract _initialize(): Promise<void>;
482
-
483
- /**
484
- * Entry point for executing initialization logic.
485
- * This is to be automatically executed in the constructor.
486
- */
487
- protected async initialize() {
488
- await this._initialize();
489
- await this.bucketStorageAdapter.init();
490
- await this.loadVersion();
491
- await this.updateSchema(this.options.schema);
492
- await this.resolveOfflineSyncStatus();
493
- await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
494
- await this.triggersImpl.cleanupResources();
495
- this.ready = true;
496
- this.iterateListeners((cb) => cb.initialized?.());
497
- }
498
-
499
- protected async loadVersion() {
500
- try {
501
- const { version } = await this.database.get<{ version: string }>('SELECT powersync_rs_version() as version');
502
- this.sdkVersion = version;
503
- } catch (e: any) {
504
- throw new Error(`The powersync extension is not loaded correctly. Details: ${e.message}`);
505
- }
506
- let versionInts: number[];
507
- try {
508
- versionInts = this.sdkVersion!.split(/[.\/]/)
509
- .slice(0, 3)
510
- .map((n) => parseInt(n));
511
- } catch (e: any) {
512
- throw new Error(
513
- `Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`
514
- );
515
- }
516
-
517
- // Validate >=0.4.10 <1.0.0
518
- if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 10)) {
519
- throw new Error(`Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}`);
520
- }
521
- }
522
-
523
- protected async resolveOfflineSyncStatus() {
524
- const result = await this.database.get<{ r: string }>('SELECT powersync_offline_sync_status() as r');
525
- const parsed = JSON.parse(result.r) as CoreSyncStatus;
526
-
527
- const updatedStatus = new SyncStatus({
528
- ...this.currentStatus.toJSON(),
529
- ...coreStatusToJs(parsed)
530
- });
531
-
532
- if (!updatedStatus.isEqual(this.currentStatus)) {
533
- this.currentStatus = updatedStatus;
534
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
535
- }
536
- }
537
-
538
- /**
539
- * Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
540
- *
541
- * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
542
- */
543
- async updateSchema(schema: Schema) {
544
- if (this.syncStreamImplementation) {
545
- throw new Error('Cannot update schema while connected');
546
- }
547
-
548
- /**
549
- * TODO
550
- * Validations only show a warning for now.
551
- * The next major release should throw an exception.
552
- */
553
- try {
554
- schema.validate();
555
- } catch (ex) {
556
- this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
557
- }
558
- this._schema = schema;
559
-
560
- await this.database.writeTransaction((tx) =>
561
- tx.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())])
562
- );
563
- await this.database.refreshSchema();
564
- this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
565
- }
566
-
567
- /**
568
- * Wait for initialization to complete.
569
- * While initializing is automatic, this helps to catch and report initialization errors.
570
- */
571
- async init() {
572
- return this.waitForReady();
573
- }
574
-
575
- // Use the options passed in during connect, or fallback to the options set during database creation or fallback to the default options
576
- protected resolvedConnectionOptions(
577
- options: CreateSyncImplementationOptions
578
- ): CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions {
579
- return {
580
- ...options,
581
- retryDelayMs:
582
- options?.retryDelayMs ?? this.options.retryDelayMs ?? this.options.retryDelay ?? DEFAULT_RETRY_DELAY_MS,
583
- crudUploadThrottleMs:
584
- options?.crudUploadThrottleMs ?? this.options.crudUploadThrottleMs ?? DEFAULT_CRUD_UPLOAD_THROTTLE_MS
585
- };
586
- }
587
-
588
- /**
589
- * @deprecated Use {@link AbstractPowerSyncDatabase#close} instead.
590
- * Clears all listeners registered by {@link AbstractPowerSyncDatabase#registerListener}.
591
- */
592
- dispose(): void {
593
- return super.dispose();
594
- }
595
-
596
- /**
597
- * Locking mechanism for exclusively running critical portions of connect/disconnect operations.
598
- * Locking here is mostly only important on web for multiple tab scenarios.
599
- */
600
- protected runExclusive<T>(callback: () => Promise<T>): Promise<T> {
601
- return this.runExclusiveMutex.runExclusive(callback);
602
- }
603
-
604
- /**
605
- * Connects to stream of events from the PowerSync instance.
606
- */
607
- async connect(connector: PowerSyncBackendConnector, options?: PowerSyncConnectionOptions) {
608
- const resolvedOptions: InternalConnectionOptions = options ?? {};
609
- resolvedOptions.serializedSchema = this.schema.toJSON();
610
-
611
- return this.connectionManager.connect(connector, resolvedOptions);
612
- }
613
-
614
- /**
615
- * Close the sync connection.
616
- *
617
- * Use {@link AbstractPowerSyncDatabase.connect} to connect again.
618
- */
619
- async disconnect() {
620
- return this.connectionManager.disconnect();
621
- }
622
-
623
- /**
624
- * Disconnect and clear the database.
625
- * Use this when logging out.
626
- * The database can still be queried after this is called, but the tables
627
- * would be empty.
628
- *
629
- * To preserve data in local-only tables, set clearLocal to false.
630
- */
631
- async disconnectAndClear(options = DEFAULT_DISCONNECT_CLEAR_OPTIONS) {
632
- await this.disconnect();
633
- await this.waitForReady();
634
-
635
- const { clearLocal } = options;
636
-
637
- await this.database.writeTransaction(async (tx) => {
638
- await tx.execute('SELECT powersync_clear(?)', [clearLocal ? 1 : 0]);
639
- });
640
-
641
- // The data has been deleted - reset the sync status
642
- this.currentStatus = new SyncStatus({});
643
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
644
- }
645
-
646
- /**
647
- * Create a sync stream to query its status or to subscribe to it.
648
- *
649
- * @param name - The name of the stream to subscribe to.
650
- * @param params - Optional parameters for the stream subscription.
651
- * @returns A {@link SyncStream} instance that can be subscribed to.
652
- * @experimental Sync streams are currently in alpha.
653
- */
654
- syncStream(name: string, params?: Record<string, any>): SyncStream {
655
- return this.connectionManager.stream(this.subscriptions, name, params ?? null);
656
- }
657
-
658
- /**
659
- * Close the database, releasing resources.
660
- *
661
- * Also disconnects any active connection.
662
- *
663
- * Once close is called, this connection cannot be used again - a new one
664
- * must be constructed.
665
- */
666
- async close(options: PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS) {
667
- await this.waitForReady();
668
-
669
- if (this.closed) {
670
- return;
671
- }
672
-
673
- this.triggersImpl.dispose();
674
-
675
- await this.iterateAsyncListeners(async (cb) => cb.closing?.());
676
-
677
- const { disconnect } = options;
678
- if (disconnect) {
679
- await this.disconnect();
680
- }
681
-
682
- await this.connectionManager.close();
683
- await this.database.close();
684
- this.closed = true;
685
- await this.iterateAsyncListeners(async (cb) => cb.closed?.());
686
- }
687
-
688
- /**
689
- * Get upload queue size estimate and count.
690
- */
691
- async getUploadQueueStats(includeSize?: boolean): Promise<UploadQueueStats> {
692
- return this.readTransaction(async (tx) => {
693
- if (includeSize) {
694
- const result = await tx.execute(
695
- `SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM ${PSInternalTable.CRUD}`
696
- );
697
-
698
- const row = result.rows!.item(0);
699
- return new UploadQueueStats(row?.count ?? 0, row?.size ?? 0);
700
- } else {
701
- const result = await tx.execute(`SELECT count(*) as count FROM ${PSInternalTable.CRUD}`);
702
- const row = result.rows!.item(0);
703
- return new UploadQueueStats(row?.count ?? 0);
704
- }
705
- });
706
- }
707
-
708
- /**
709
- * Get a batch of CRUD data to upload.
710
- *
711
- * Returns null if there is no data to upload.
712
- *
713
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
714
- *
715
- * Once the data have been successfully uploaded, call {@link CrudBatch.complete} before
716
- * requesting the next batch.
717
- *
718
- * Use the `limit` parameter to specify the maximum number of updates to return in a single
719
- * batch.
720
- *
721
- * This method does include transaction ids in the result, but does not group
722
- * data by transaction. One batch may contain data from multiple transactions,
723
- * and a single transaction may be split over multiple batches.
724
- *
725
- * @param limit - Maximum number of CRUD entries to include in the batch
726
- * @returns A batch of CRUD operations to upload, or null if there are none
727
- */
728
- async getCrudBatch(limit: number = DEFAULT_CRUD_BATCH_LIMIT): Promise<CrudBatch | null> {
729
- const result = await this.getAll<CrudEntryJSON>(
730
- `SELECT id, tx_id, data FROM ${PSInternalTable.CRUD} ORDER BY id ASC LIMIT ?`,
731
- [limit + 1]
732
- );
733
-
734
- const all: CrudEntry[] = result.map((row) => CrudEntry.fromRow(row)) ?? [];
735
-
736
- let haveMore = false;
737
- if (all.length > limit) {
738
- all.pop();
739
- haveMore = true;
740
- }
741
- if (all.length == 0) {
742
- return null;
743
- }
744
-
745
- const last = all[all.length - 1];
746
- return new CrudBatch(all, haveMore, async (writeCheckpoint?: string) =>
747
- this.handleCrudCheckpoint(last.clientId, writeCheckpoint)
748
- );
749
- }
750
-
751
- /**
752
- * Get the next recorded transaction to upload.
753
- *
754
- * Returns null if there is no data to upload.
755
- *
756
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
757
- *
758
- * Once the data have been successfully uploaded, call {@link CrudTransaction.complete} before
759
- * requesting the next transaction.
760
- *
761
- * Unlike {@link AbstractPowerSyncDatabase.getCrudBatch}, this only returns data from a single transaction at a time.
762
- * All data for the transaction is loaded into memory.
763
- *
764
- * @returns A transaction of CRUD operations to upload, or null if there are none
765
- */
766
- async getNextCrudTransaction(): Promise<CrudTransaction | null> {
767
- const iterator = this.getCrudTransactions()[symbolAsyncIterator]();
768
- return (await iterator.next()).value;
769
- }
770
-
771
- /**
772
- * Returns an async iterator of completed transactions with local writes against the database.
773
- *
774
- * This is typically used from the {@link PowerSyncBackendConnector.uploadData} callback. Each entry emitted by the
775
- * returned iterator is a full transaction containing all local writes made while that transaction was active.
776
- *
777
- * Unlike {@link AbstractPowerSyncDatabase.getNextCrudTransaction}, which always returns the oldest transaction that hasn't been
778
- * {@link CrudTransaction.complete}d yet, this iterator can be used to receive multiple transactions. Calling
779
- * {@link CrudTransaction.complete} will mark that and all prior transactions emitted by the iterator as completed.
780
- *
781
- * This can be used to upload multiple transactions in a single batch, e.g with:
782
- *
783
- * ```JavaScript
784
- * let lastTransaction = null;
785
- * let batch = [];
786
- *
787
- * for await (const transaction of database.getCrudTransactions()) {
788
- * batch.push(...transaction.crud);
789
- * lastTransaction = transaction;
790
- *
791
- * if (batch.length > 10) {
792
- * break;
793
- * }
794
- * }
795
- * ```
796
- *
797
- * If there is no local data to upload, the async iterator complete without emitting any items.
798
- *
799
- * Note that iterating over async iterables requires a [polyfill](https://github.com/powersync-ja/powersync-js/tree/main/packages/react-native#babel-plugins-watched-queries)
800
- * for React Native.
801
- */
802
- getCrudTransactions(): AsyncIterable<CrudTransaction, null> {
803
- return {
804
- [symbolAsyncIterator]: () => {
805
- let lastCrudItemId = -1;
806
- const sql = `
807
- WITH RECURSIVE crud_entries AS (
808
- SELECT id, tx_id, data FROM ps_crud WHERE id = (SELECT min(id) FROM ps_crud WHERE id > ?)
809
- UNION ALL
810
- SELECT ps_crud.id, ps_crud.tx_id, ps_crud.data FROM ps_crud
811
- INNER JOIN crud_entries ON crud_entries.id + 1 = rowid
812
- WHERE crud_entries.tx_id = ps_crud.tx_id
813
- )
814
- SELECT * FROM crud_entries;
815
- `;
816
-
817
- return {
818
- next: async () => {
819
- const nextTransaction = await this.database.getAll<CrudEntryJSON>(sql, [lastCrudItemId]);
820
- if (nextTransaction.length == 0) {
821
- return { done: true, value: null };
822
- }
823
-
824
- const items = nextTransaction.map((row) => CrudEntry.fromRow(row));
825
- const last = items[items.length - 1];
826
- const txId = last.transactionId;
827
- lastCrudItemId = last.clientId;
828
-
829
- return {
830
- done: false,
831
- value: new CrudTransaction(
832
- items,
833
- async (writeCheckpoint?: string) => this.handleCrudCheckpoint(last.clientId, writeCheckpoint),
834
- txId
835
- )
836
- };
837
- }
838
- };
839
- }
840
- };
841
- }
842
-
843
- /**
844
- * Get an unique client id for this database.
845
- *
846
- * The id is not reset when the database is cleared, only when the database is deleted.
847
- *
848
- * @returns A unique identifier for the database instance
849
- */
850
- async getClientId(): Promise<string> {
851
- return this.bucketStorageAdapter.getClientId();
852
- }
853
-
854
- private async handleCrudCheckpoint(lastClientId: number, writeCheckpoint?: string) {
855
- return this.writeTransaction(async (tx) => {
856
- await tx.execute(`DELETE FROM ${PSInternalTable.CRUD} WHERE id <= ?`, [lastClientId]);
857
- if (writeCheckpoint) {
858
- const check = await tx.execute(`SELECT 1 FROM ${PSInternalTable.CRUD} LIMIT 1`);
859
- if (!check.rows?.length) {
860
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
861
- writeCheckpoint
862
- ]);
863
- }
864
- } else {
865
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
866
- this.bucketStorageAdapter.getMaxOpId()
867
- ]);
868
- }
869
- });
870
- }
871
-
872
- /**
873
- * Execute a SQL write (INSERT/UPDATE/DELETE) query
874
- * and optionally return results.
875
- *
876
- * When using the default client-side [JSON-based view system](https://docs.powersync.com/architecture/client-architecture#client-side-schema-and-sqlite-database-structure),
877
- * the returned result's `rowsAffected` may be `0` for successful `UPDATE` and `DELETE` statements.
878
- * Use a `RETURNING` clause and inspect `result.rows` when you need to confirm which rows changed.
879
- *
880
- * @param sql - The SQL query to execute
881
- * @param parameters - Optional array of parameters to bind to the query
882
- * @returns The query result as an object with structured key-value pairs
883
- */
884
- async execute(sql: string, parameters?: any[]) {
885
- return this.writeLock((tx) => tx.execute(sql, parameters));
886
- }
887
-
888
- /**
889
- * Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing.
890
- * This bypasses certain PowerSync abstractions and is useful for accessing the raw database results.
891
- *
892
- * @param sql - The SQL query to execute
893
- * @param parameters - Optional array of parameters to bind to the query
894
- * @returns The raw query result from the underlying database as a nested array of raw values, where each row is
895
- * represented as an array of column values without field names.
896
- */
897
- async executeRaw(sql: string, parameters?: any[]) {
898
- await this.waitForReady();
899
- return this.database.executeRaw(sql, parameters);
900
- }
901
-
902
- /**
903
- * Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
904
- * and optionally return results.
905
- * This is faster than executing separately with each parameter set.
906
- *
907
- * @param sql - The SQL query to execute
908
- * @param parameters - Optional 2D array of parameter sets, where each inner array is a set of parameters for one execution
909
- * @returns The query result
910
- */
911
- async executeBatch(sql: string, parameters?: any[][]) {
912
- await this.waitForReady();
913
- return this.database.executeBatch(sql, parameters);
914
- }
915
-
916
- /**
917
- * Execute a read-only query and return results.
918
- *
919
- * @param sql - The SQL query to execute
920
- * @param parameters - Optional array of parameters to bind to the query
921
- * @returns An array of results
922
- */
923
- async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
924
- await this.waitForReady();
925
- return this.database.getAll(sql, parameters);
926
- }
927
-
928
- /**
929
- * Execute a read-only query and return the first result, or null if the ResultSet is empty.
930
- *
931
- * @param sql - The SQL query to execute
932
- * @param parameters - Optional array of parameters to bind to the query
933
- * @returns The first result if found, or null if no results are returned
934
- */
935
- async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
936
- await this.waitForReady();
937
- return this.database.getOptional(sql, parameters);
938
- }
939
-
940
- /**
941
- * Execute a read-only query and return the first result, error if the ResultSet is empty.
942
- *
943
- * @param sql - The SQL query to execute
944
- * @param parameters - Optional array of parameters to bind to the query
945
- * @returns The first result matching the query
946
- * @throws Error if no rows are returned
947
- */
948
- async get<T>(sql: string, parameters?: any[]): Promise<T> {
949
- await this.waitForReady();
950
- return this.database.get(sql, parameters);
951
- }
952
-
953
- /**
954
- * Takes a read lock, without starting a transaction.
955
- * In most cases, {@link AbstractPowerSyncDatabase.readTransaction} should be used instead.
956
- */
957
- async readLock<T>(callback: (db: LockContext) => Promise<T>) {
958
- await this.waitForReady();
959
- return this.database.readLock(callback);
960
- }
961
-
962
- /**
963
- * Takes a global lock, without starting a transaction.
964
- * In most cases, {@link AbstractPowerSyncDatabase.writeTransaction} should be used instead.
965
- */
966
- async writeLock<T>(callback: (db: LockContext) => Promise<T>) {
967
- await this.waitForReady();
968
- return this.database.writeLock(callback);
969
- }
970
-
971
- /**
972
- * Open a read-only transaction.
973
- * Read transactions can run concurrently to a write transaction.
974
- * Changes from any write transaction are not visible to read transactions started before it.
975
- *
976
- * @param callback - Function to execute within the transaction
977
- * @param lockTimeout - Time in milliseconds to wait for a lock before throwing an error
978
- * @returns The result of the callback
979
- * @throws Error if the lock cannot be obtained within the timeout period
980
- */
981
- async readTransaction<T>(
982
- callback: (tx: Transaction) => Promise<T>,
983
- lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
984
- ): Promise<T> {
985
- await this.waitForReady();
986
- return this.database.readTransaction(
987
- async (tx) => {
988
- const res = await callback(tx);
989
- await tx.rollback();
990
- return res;
991
- },
992
- { timeoutMs: lockTimeout }
993
- );
994
- }
995
-
996
- /**
997
- * Open a read-write transaction.
998
- * This takes a global lock - only one write transaction can execute against the database at a time.
999
- * Statements within the transaction must be done on the provided {@link Transaction} interface.
1000
- *
1001
- * @param callback - Function to execute within the transaction
1002
- * @param lockTimeout - Time in milliseconds to wait for a lock before throwing an error
1003
- * @returns The result of the callback
1004
- * @throws Error if the lock cannot be obtained within the timeout period
1005
- */
1006
- async writeTransaction<T>(
1007
- callback: (tx: Transaction) => Promise<T>,
1008
- lockTimeout: number = DEFAULT_LOCK_TIMEOUT_MS
1009
- ): Promise<T> {
1010
- await this.waitForReady();
1011
- return this.database.writeTransaction(
1012
- async (tx) => {
1013
- const res = await callback(tx);
1014
- await tx.commit();
1015
- return res;
1016
- },
1017
- { timeoutMs: lockTimeout }
1018
- );
1019
- }
1020
-
1021
- /**
1022
- * This version of `watch` uses `AsyncGenerator`, for documentation see {@link AbstractPowerSyncDatabase.watchWithAsyncGenerator}.
1023
- * Can be overloaded to use a callback handler instead, for documentation see {@link AbstractPowerSyncDatabase.watchWithCallback}.
1024
- *
1025
- * @example
1026
- * ```javascript
1027
- * async *attachmentIds() {
1028
- * for await (const result of this.powersync.watch(
1029
- * `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
1030
- * []
1031
- * )) {
1032
- * yield result.rows?._array.map((r) => r.id) ?? [];
1033
- * }
1034
- * }
1035
- * ```
1036
- */
1037
- watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
1038
- /**
1039
- * See {@link AbstractPowerSyncDatabase.watchWithCallback}.
1040
- *
1041
- * @example
1042
- * ```javascript
1043
- * onAttachmentIdsChange(onResult) {
1044
- * this.powersync.watch(
1045
- * `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
1046
- * [],
1047
- * {
1048
- * onResult: (result) => onResult(result.rows?._array.map((r) => r.id) ?? [])
1049
- * }
1050
- * );
1051
- * }
1052
- * ```
1053
- */
1054
- watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
1055
-
1056
- watch(
1057
- sql: string,
1058
- parameters?: any[],
1059
- handlerOrOptions?: WatchHandler | SQLWatchOptions,
1060
- maybeOptions?: SQLWatchOptions
1061
- ): void | AsyncIterable<QueryResult> {
1062
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onResult' in handlerOrOptions) {
1063
- const handler = handlerOrOptions as WatchHandler;
1064
- const options = maybeOptions;
1065
-
1066
- return this.watchWithCallback(sql, parameters, handler, options);
1067
- }
1068
-
1069
- const options = handlerOrOptions as SQLWatchOptions | undefined;
1070
- return this.watchWithAsyncGenerator(sql, parameters, options);
1071
- }
1072
-
1073
- /**
1074
- * Allows defining a query which can be used to build a {@link WatchedQuery}.
1075
- * The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
1076
- * An optional mapper function can be provided to transform the results.
1077
- *
1078
- * @example
1079
- * ```javascript
1080
- * const watchedTodos = powersync.query({
1081
- * sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
1082
- * parameters: [],
1083
- * mapper: (row) => ({
1084
- * ...row,
1085
- * created_at: new Date(row.created_at as string)
1086
- * })
1087
- * })
1088
- * .watch()
1089
- * // OR use .differentialWatch() for fine-grained watches.
1090
- * ```
1091
- */
1092
- query<RowType>(query: ArrayQueryDefinition<RowType>): Query<RowType> {
1093
- const { sql, parameters = [], mapper } = query;
1094
- const compatibleQuery: WatchCompatibleQuery<RowType[]> = {
1095
- compile: () => ({
1096
- sql,
1097
- parameters
1098
- }),
1099
- execute: async ({ sql, parameters }) => {
1100
- const result = await this.getAll<Record<string, any>>(sql, parameters);
1101
- return mapper ? result.map(mapper) : (result as RowType[]);
1102
- }
1103
- };
1104
- return this.customQuery(compatibleQuery);
1105
- }
1106
-
1107
- /**
1108
- * Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
1109
- * The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
1110
- *
1111
- * @example
1112
- * ```javascript
1113
- *
1114
- * // Potentially a query from an ORM like Drizzle
1115
- * const query = db.select().from(lists);
1116
- *
1117
- * const watchedTodos = powersync.customQuery(query)
1118
- * .watch()
1119
- * // OR use .differentialWatch() for fine-grained watches.
1120
- * ```
1121
- */
1122
- customQuery<RowType>(query: WatchCompatibleQuery<RowType[]>): Query<RowType> {
1123
- return new CustomQuery({
1124
- db: this,
1125
- query
1126
- });
1127
- }
1128
-
1129
- /**
1130
- * Execute a read query every time the source tables are modified.
1131
- * Use {@link SQLOnChangeOptions.throttleMs} to specify the minimum interval between queries.
1132
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
1133
- *
1134
- * Note that the `onChange` callback member of the handler is required.
1135
- *
1136
- * @param sql - The SQL query to execute
1137
- * @param parameters - Optional array of parameters to bind to the query
1138
- * @param handler - Callbacks for handling results and errors
1139
- * @param options - Options for configuring watch behavior
1140
- */
1141
- watchWithCallback(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void {
1142
- const { onResult, onError = (e: Error) => this.logger.error(e) } = handler ?? {};
1143
- if (!onResult) {
1144
- throw new Error('onResult is required');
1145
- }
1146
- const { comparator } = options ?? {};
1147
-
1148
- // This API yields a QueryResult type.
1149
- // This is not a standard Array result, which makes it incompatible with the .query API.
1150
- const watchedQuery = new OnChangeQueryProcessor({
1151
- db: this,
1152
- comparator,
1153
- placeholderData: null as unknown as QueryResult, // FIXME
1154
- watchOptions: {
1155
- query: {
1156
- compile: () => ({
1157
- sql: sql,
1158
- parameters: parameters ?? []
1159
- }),
1160
- execute: () => this.executeReadOnly(sql, parameters)
1161
- },
1162
- reportFetching: false,
1163
- throttleMs: options?.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS,
1164
- triggerOnTables: options?.tables
1165
- }
1166
- });
1167
-
1168
- const dispose = watchedQuery.registerListener({
1169
- onData: (data) => {
1170
- if (!data) {
1171
- // This should not happen. We only use null for the initial data.
1172
- return;
1173
- }
1174
- onResult(data);
1175
- },
1176
- onError: (error) => {
1177
- onError(error);
1178
- }
1179
- });
1180
-
1181
- options?.signal?.addEventListener('abort', () => {
1182
- dispose();
1183
- watchedQuery.close();
1184
- });
1185
- }
1186
-
1187
- /**
1188
- * Execute a read query every time the source tables are modified.
1189
- * Use {@link SQLOnChangeOptions.throttleMs} to specify the minimum interval between queries.
1190
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
1191
- *
1192
- * @param sql - The SQL query to execute
1193
- * @param parameters - Optional array of parameters to bind to the query
1194
- * @param options - Options for configuring watch behavior
1195
- * @returns An AsyncIterable that yields QueryResults whenever the data changes
1196
- */
1197
- watchWithAsyncGenerator(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult> {
1198
- return EventQueue.queueBasedAsyncIterable((queue, abort) => {
1199
- const handler: WatchHandler = {
1200
- onResult: (result) => {
1201
- queue.notify(result);
1202
- },
1203
- onError: (error) => {
1204
- queue.notifyError(error);
1205
- }
1206
- };
1207
-
1208
- this.watchWithCallback(sql, parameters, handler, { ...options, signal: abort });
1209
- }, options?.signal);
1210
- }
1211
-
1212
- /**
1213
- * Resolves the list of tables that are used in a SQL query.
1214
- * If tables are specified in the options, those are used directly.
1215
- * Otherwise, analyzes the query using EXPLAIN to determine which tables are accessed.
1216
- *
1217
- * @param sql - The SQL query to analyze
1218
- * @param parameters - Optional parameters for the SQL query
1219
- * @param options - Optional watch options that may contain explicit table list
1220
- * @returns Array of table names that the query depends on
1221
- */
1222
- async resolveTables(sql: string, parameters?: any[], options?: SQLWatchOptions): Promise<string[]> {
1223
- const resolvedTables = options?.tables ? [...options.tables] : [];
1224
- if (!options?.tables) {
1225
- const explained = await this.getAll<{ opcode: string; p3: number; p2: number }>(`EXPLAIN ${sql}`, parameters);
1226
- const rootPages = explained
1227
- .filter((row) => row.opcode == 'OpenRead' && row.p3 == 0 && typeof row.p2 == 'number')
1228
- .map((row) => row.p2);
1229
- const tables = await this.getAll<{ tbl_name: string }>(
1230
- `SELECT DISTINCT tbl_name FROM sqlite_master WHERE rootpage IN (SELECT json_each.value FROM json_each(?))`,
1231
- [JSON.stringify(rootPages)]
1232
- );
1233
- for (const table of tables) {
1234
- resolvedTables.push(table.tbl_name.replace(POWERSYNC_TABLE_MATCH, ''));
1235
- }
1236
- }
1237
-
1238
- return resolvedTables;
1239
- }
1240
-
1241
- /**
1242
- * This version of `onChange` uses `AsyncGenerator`, for documentation see {@link AbstractPowerSyncDatabase.onChangeWithAsyncGenerator}.
1243
- * Can be overloaded to use a callback handler instead, for documentation see {@link AbstractPowerSyncDatabase.onChangeWithCallback}.
1244
- *
1245
- * @example
1246
- * ```javascript
1247
- * async monitorChanges() {
1248
- * for await (const event of this.powersync.onChange({tables: ['todos']})) {
1249
- * console.log('Detected change event:', event);
1250
- * }
1251
- * }
1252
- * ```
1253
- */
1254
- onChange(options?: SQLOnChangeOptions): AsyncIterable<WatchOnChangeEvent>;
1255
- /**
1256
- * See {@link AbstractPowerSyncDatabase.onChangeWithCallback}.
1257
- *
1258
- * @example
1259
- * ```javascript
1260
- * monitorChanges() {
1261
- * this.powersync.onChange({
1262
- * onChange: (event) => {
1263
- * console.log('Change detected:', event);
1264
- * }
1265
- * }, { tables: ['todos'] });
1266
- * }
1267
- * ```
1268
- */
1269
- onChange(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
1270
-
1271
- onChange(
1272
- handlerOrOptions?: WatchOnChangeHandler | SQLOnChangeOptions,
1273
- maybeOptions?: SQLOnChangeOptions
1274
- ): (() => void) | AsyncIterable<WatchOnChangeEvent> {
1275
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onChange' in handlerOrOptions) {
1276
- const handler = handlerOrOptions as WatchOnChangeHandler;
1277
- const options = maybeOptions;
1278
-
1279
- return this.onChangeWithCallback(handler, options);
1280
- }
1281
-
1282
- const options = handlerOrOptions as SQLWatchOptions | undefined;
1283
- return this.onChangeWithAsyncGenerator(options);
1284
- }
1285
-
1286
- /**
1287
- * Invoke the provided callback on any changes to any of the specified tables.
1288
- *
1289
- * This is preferred over {@link AbstractPowerSyncDatabase.watchWithCallback} when multiple queries need to be performed
1290
- * together when data is changed.
1291
- *
1292
- * Note that the `onChange` callback member of the handler is required.
1293
- *
1294
- * @param handler - Callbacks for handling change events and errors
1295
- * @param options - Options for configuring watch behavior
1296
- * @returns A dispose function to stop watching for changes
1297
- */
1298
- onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void {
1299
- const { onChange, onError = (e: Error) => this.logger.error(e) } = handler ?? {};
1300
- if (!onChange) {
1301
- throw new Error('onChange is required');
1302
- }
1303
-
1304
- const resolvedOptions = options ?? {};
1305
- const watchedTables = new Set<string>(
1306
- (resolvedOptions?.tables ?? []).flatMap((table) => [table, `ps_data__${table}`, `ps_data_local__${table}`])
1307
- );
1308
-
1309
- const changedTables = new Set<string>();
1310
- const throttleMs = resolvedOptions.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS;
1311
-
1312
- const executor = new ControlledExecutor(async (e: WatchOnChangeEvent) => {
1313
- await onChange(e);
1314
- });
1315
-
1316
- const flushTableUpdates = throttleTrailing(
1317
- () =>
1318
- this.handleTableChanges(changedTables, watchedTables, (intersection) => {
1319
- if (resolvedOptions?.signal?.aborted) return;
1320
- executor.schedule({ changedTables: intersection });
1321
- }),
1322
- throttleMs
1323
- );
1324
-
1325
- if (options?.triggerImmediate) {
1326
- executor.schedule({ changedTables: [] });
1327
- }
1328
-
1329
- const dispose = this.database.registerListener({
1330
- tablesUpdated: async (update) => {
1331
- try {
1332
- this.processTableUpdates(update, changedTables);
1333
- flushTableUpdates();
1334
- } catch (error: any) {
1335
- onError?.(error);
1336
- }
1337
- }
1338
- });
1339
-
1340
- resolvedOptions.signal?.addEventListener('abort', () => {
1341
- executor.dispose();
1342
- dispose();
1343
- });
1344
-
1345
- return () => dispose();
1346
- }
1347
-
1348
- /**
1349
- * Create a Stream of changes to any of the specified tables.
1350
- *
1351
- * This is preferred over {@link AbstractPowerSyncDatabase.watchWithAsyncGenerator} when multiple queries need to be
1352
- * performed together when data is changed.
1353
- *
1354
- * @param options - Options for configuring watch behavior
1355
- * @returns An AsyncIterable that yields change events whenever the specified tables change
1356
- */
1357
- // Note: do not declare this as `async *onChange` as it will not work in React Native.
1358
- onChangeWithAsyncGenerator(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent> {
1359
- return EventQueue.queueBasedAsyncIterable((queue, abort) => {
1360
- this.onChangeWithCallback(
1361
- {
1362
- onChange: (event): void => {
1363
- queue.notify(event);
1364
- },
1365
- onError: (error) => {
1366
- queue.notifyError(error);
1367
- }
1368
- },
1369
- { ...options, signal: abort }
1370
- );
1371
-
1372
- // Note: We don't have to track the dispose function returned by onChangeWithCallback, it cleans up
1373
- // after the abort signal completes.
1374
- }, options?.signal);
1375
- }
1376
-
1377
- private handleTableChanges(
1378
- changedTables: Set<string>,
1379
- watchedTables: Set<string>,
1380
- onDetectedChanges: (changedTables: string[]) => void
1381
- ): void {
1382
- if (changedTables.size > 0) {
1383
- const intersection = Array.from(changedTables.values()).filter((change) => watchedTables.has(change));
1384
- if (intersection.length) {
1385
- onDetectedChanges(intersection);
1386
- }
1387
- }
1388
- changedTables.clear();
1389
- }
1390
-
1391
- private processTableUpdates(
1392
- updateNotification: BatchedUpdateNotification | UpdateNotification,
1393
- changedTables: Set<string>
1394
- ): void {
1395
- const tables = isBatchedUpdateNotification(updateNotification)
1396
- ? updateNotification.tables
1397
- : [updateNotification.table];
1398
-
1399
- for (const table of tables) {
1400
- changedTables.add(table);
1401
- }
1402
- }
1403
-
1404
- private async executeReadOnly(sql: string, params?: any[]) {
1405
- await this.waitForReady();
1406
- return this.database.readLock((tx) => tx.execute(sql, params));
1407
- }
1408
- }