@powersync/common 0.0.0-dev-20260128170746 → 0.0.0-dev-20260202160933

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 (216) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +1 -0
  3. package/src/utils/mutex.ts +1 -1
  4. package/dist/bundle.cjs +0 -14456
  5. package/dist/bundle.cjs.map +0 -1
  6. package/dist/bundle.mjs +0 -14366
  7. package/dist/bundle.mjs.map +0 -1
  8. package/dist/bundle.node.cjs +0 -11934
  9. package/dist/bundle.node.cjs.map +0 -1
  10. package/dist/bundle.node.mjs +0 -11844
  11. package/dist/bundle.node.mjs.map +0 -1
  12. package/dist/index.d.cts +0 -4265
  13. package/lib/attachments/AttachmentContext.d.ts +0 -86
  14. package/lib/attachments/AttachmentContext.js +0 -229
  15. package/lib/attachments/AttachmentContext.js.map +0 -1
  16. package/lib/attachments/AttachmentErrorHandler.d.ts +0 -31
  17. package/lib/attachments/AttachmentErrorHandler.js +0 -2
  18. package/lib/attachments/AttachmentErrorHandler.js.map +0 -1
  19. package/lib/attachments/AttachmentQueue.d.ts +0 -149
  20. package/lib/attachments/AttachmentQueue.js +0 -362
  21. package/lib/attachments/AttachmentQueue.js.map +0 -1
  22. package/lib/attachments/AttachmentService.d.ts +0 -29
  23. package/lib/attachments/AttachmentService.js +0 -56
  24. package/lib/attachments/AttachmentService.js.map +0 -1
  25. package/lib/attachments/LocalStorageAdapter.d.ts +0 -62
  26. package/lib/attachments/LocalStorageAdapter.js +0 -6
  27. package/lib/attachments/LocalStorageAdapter.js.map +0 -1
  28. package/lib/attachments/RemoteStorageAdapter.d.ts +0 -27
  29. package/lib/attachments/RemoteStorageAdapter.js +0 -2
  30. package/lib/attachments/RemoteStorageAdapter.js.map +0 -1
  31. package/lib/attachments/Schema.d.ts +0 -50
  32. package/lib/attachments/Schema.js +0 -62
  33. package/lib/attachments/Schema.js.map +0 -1
  34. package/lib/attachments/SyncingService.d.ts +0 -62
  35. package/lib/attachments/SyncingService.js +0 -168
  36. package/lib/attachments/SyncingService.js.map +0 -1
  37. package/lib/attachments/WatchedAttachmentItem.d.ts +0 -17
  38. package/lib/attachments/WatchedAttachmentItem.js +0 -2
  39. package/lib/attachments/WatchedAttachmentItem.js.map +0 -1
  40. package/lib/client/AbstractPowerSyncDatabase.d.ts +0 -615
  41. package/lib/client/AbstractPowerSyncDatabase.js +0 -983
  42. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  43. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
  44. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
  45. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  46. package/lib/client/ConnectionManager.d.ts +0 -112
  47. package/lib/client/ConnectionManager.js +0 -294
  48. package/lib/client/ConnectionManager.js.map +0 -1
  49. package/lib/client/CustomQuery.d.ts +0 -22
  50. package/lib/client/CustomQuery.js +0 -43
  51. package/lib/client/CustomQuery.js.map +0 -1
  52. package/lib/client/Query.d.ts +0 -97
  53. package/lib/client/Query.js +0 -2
  54. package/lib/client/Query.js.map +0 -1
  55. package/lib/client/SQLOpenFactory.d.ts +0 -42
  56. package/lib/client/SQLOpenFactory.js +0 -20
  57. package/lib/client/SQLOpenFactory.js.map +0 -1
  58. package/lib/client/compilableQueryWatch.d.ts +0 -7
  59. package/lib/client/compilableQueryWatch.js +0 -38
  60. package/lib/client/compilableQueryWatch.js.map +0 -1
  61. package/lib/client/connection/PowerSyncBackendConnector.d.ts +0 -23
  62. package/lib/client/connection/PowerSyncBackendConnector.js +0 -2
  63. package/lib/client/connection/PowerSyncBackendConnector.js.map +0 -1
  64. package/lib/client/connection/PowerSyncCredentials.d.ts +0 -5
  65. package/lib/client/connection/PowerSyncCredentials.js +0 -2
  66. package/lib/client/connection/PowerSyncCredentials.js.map +0 -1
  67. package/lib/client/constants.d.ts +0 -1
  68. package/lib/client/constants.js +0 -2
  69. package/lib/client/constants.js.map +0 -1
  70. package/lib/client/runOnSchemaChange.d.ts +0 -2
  71. package/lib/client/runOnSchemaChange.js +0 -24
  72. package/lib/client/runOnSchemaChange.js.map +0 -1
  73. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -102
  74. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
  75. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  76. package/lib/client/sync/bucket/CrudBatch.d.ts +0 -31
  77. package/lib/client/sync/bucket/CrudBatch.js +0 -26
  78. package/lib/client/sync/bucket/CrudBatch.js.map +0 -1
  79. package/lib/client/sync/bucket/CrudEntry.d.ts +0 -95
  80. package/lib/client/sync/bucket/CrudEntry.js +0 -110
  81. package/lib/client/sync/bucket/CrudEntry.js.map +0 -1
  82. package/lib/client/sync/bucket/CrudTransaction.d.ts +0 -29
  83. package/lib/client/sync/bucket/CrudTransaction.js +0 -25
  84. package/lib/client/sync/bucket/CrudTransaction.js.map +0 -1
  85. package/lib/client/sync/bucket/OpType.d.ts +0 -16
  86. package/lib/client/sync/bucket/OpType.js +0 -23
  87. package/lib/client/sync/bucket/OpType.js.map +0 -1
  88. package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
  89. package/lib/client/sync/bucket/OplogEntry.js +0 -36
  90. package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
  91. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -61
  92. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -324
  93. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  94. package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
  95. package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
  96. package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
  97. package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
  98. package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
  99. package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
  100. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -140
  101. package/lib/client/sync/stream/AbstractRemote.js +0 -506
  102. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  103. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -240
  104. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -990
  105. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  106. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  107. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
  108. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  109. package/lib/client/sync/stream/core-instruction.d.ts +0 -72
  110. package/lib/client/sync/stream/core-instruction.js +0 -27
  111. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  112. package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
  113. package/lib/client/sync/stream/streaming-sync-types.js +0 -26
  114. package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
  115. package/lib/client/sync/sync-streams.d.ts +0 -98
  116. package/lib/client/sync/sync-streams.js +0 -2
  117. package/lib/client/sync/sync-streams.js.map +0 -1
  118. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  119. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  120. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  121. package/lib/client/triggers/TriggerManager.d.ts +0 -459
  122. package/lib/client/triggers/TriggerManager.js +0 -11
  123. package/lib/client/triggers/TriggerManager.js.map +0 -1
  124. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  125. package/lib/client/triggers/TriggerManagerImpl.js +0 -393
  126. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  127. package/lib/client/triggers/sanitizeSQL.d.ts +0 -34
  128. package/lib/client/triggers/sanitizeSQL.js +0 -69
  129. package/lib/client/triggers/sanitizeSQL.js.map +0 -1
  130. package/lib/client/watched/GetAllQuery.d.ts +0 -32
  131. package/lib/client/watched/GetAllQuery.js +0 -25
  132. package/lib/client/watched/GetAllQuery.js.map +0 -1
  133. package/lib/client/watched/WatchedQuery.d.ts +0 -100
  134. package/lib/client/watched/WatchedQuery.js +0 -14
  135. package/lib/client/watched/WatchedQuery.js.map +0 -1
  136. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  137. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  138. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  139. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -121
  140. package/lib/client/watched/processors/DifferentialQueryProcessor.js +0 -173
  141. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +0 -1
  142. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -33
  143. package/lib/client/watched/processors/OnChangeQueryProcessor.js +0 -83
  144. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +0 -1
  145. package/lib/client/watched/processors/comparators.d.ts +0 -30
  146. package/lib/client/watched/processors/comparators.js +0 -35
  147. package/lib/client/watched/processors/comparators.js.map +0 -1
  148. package/lib/db/ConnectionClosedError.d.ts +0 -10
  149. package/lib/db/ConnectionClosedError.js +0 -21
  150. package/lib/db/ConnectionClosedError.js.map +0 -1
  151. package/lib/db/DBAdapter.d.ts +0 -115
  152. package/lib/db/DBAdapter.js +0 -20
  153. package/lib/db/DBAdapter.js.map +0 -1
  154. package/lib/db/crud/SyncProgress.d.ts +0 -68
  155. package/lib/db/crud/SyncProgress.js +0 -61
  156. package/lib/db/crud/SyncProgress.js.map +0 -1
  157. package/lib/db/crud/SyncStatus.d.ts +0 -195
  158. package/lib/db/crud/SyncStatus.js +0 -250
  159. package/lib/db/crud/SyncStatus.js.map +0 -1
  160. package/lib/db/crud/UploadQueueStatus.d.ts +0 -20
  161. package/lib/db/crud/UploadQueueStatus.js +0 -25
  162. package/lib/db/crud/UploadQueueStatus.js.map +0 -1
  163. package/lib/db/schema/Column.d.ts +0 -30
  164. package/lib/db/schema/Column.js +0 -43
  165. package/lib/db/schema/Column.js.map +0 -1
  166. package/lib/db/schema/Index.d.ts +0 -22
  167. package/lib/db/schema/Index.js +0 -30
  168. package/lib/db/schema/Index.js.map +0 -1
  169. package/lib/db/schema/IndexedColumn.d.ts +0 -19
  170. package/lib/db/schema/IndexedColumn.js +0 -30
  171. package/lib/db/schema/IndexedColumn.js.map +0 -1
  172. package/lib/db/schema/RawTable.d.ts +0 -61
  173. package/lib/db/schema/RawTable.js +0 -33
  174. package/lib/db/schema/RawTable.js.map +0 -1
  175. package/lib/db/schema/Schema.d.ts +0 -54
  176. package/lib/db/schema/Schema.js +0 -61
  177. package/lib/db/schema/Schema.js.map +0 -1
  178. package/lib/db/schema/Table.d.ts +0 -157
  179. package/lib/db/schema/Table.js +0 -208
  180. package/lib/db/schema/Table.js.map +0 -1
  181. package/lib/db/schema/TableV2.d.ts +0 -9
  182. package/lib/db/schema/TableV2.js +0 -9
  183. package/lib/db/schema/TableV2.js.map +0 -1
  184. package/lib/index.d.ts +0 -61
  185. package/lib/index.js +0 -62
  186. package/lib/index.js.map +0 -1
  187. package/lib/types/types.d.ts +0 -8
  188. package/lib/types/types.js +0 -2
  189. package/lib/types/types.js.map +0 -1
  190. package/lib/utils/AbortOperation.d.ts +0 -9
  191. package/lib/utils/AbortOperation.js +0 -19
  192. package/lib/utils/AbortOperation.js.map +0 -1
  193. package/lib/utils/BaseObserver.d.ts +0 -18
  194. package/lib/utils/BaseObserver.js +0 -27
  195. package/lib/utils/BaseObserver.js.map +0 -1
  196. package/lib/utils/ControlledExecutor.d.ts +0 -25
  197. package/lib/utils/ControlledExecutor.js +0 -51
  198. package/lib/utils/ControlledExecutor.js.map +0 -1
  199. package/lib/utils/DataStream.d.ts +0 -62
  200. package/lib/utils/DataStream.js +0 -169
  201. package/lib/utils/DataStream.js.map +0 -1
  202. package/lib/utils/Logger.d.ts +0 -31
  203. package/lib/utils/Logger.js +0 -37
  204. package/lib/utils/Logger.js.map +0 -1
  205. package/lib/utils/MetaBaseObserver.d.ts +0 -29
  206. package/lib/utils/MetaBaseObserver.js +0 -51
  207. package/lib/utils/MetaBaseObserver.js.map +0 -1
  208. package/lib/utils/async.d.ts +0 -23
  209. package/lib/utils/async.js +0 -55
  210. package/lib/utils/async.js.map +0 -1
  211. package/lib/utils/mutex.d.ts +0 -7
  212. package/lib/utils/mutex.js +0 -29
  213. package/lib/utils/mutex.js.map +0 -1
  214. package/lib/utils/parseQuery.d.ts +0 -6
  215. package/lib/utils/parseQuery.js +0 -17
  216. package/lib/utils/parseQuery.js.map +0 -1
@@ -1,983 +0,0 @@
1
- import { Mutex } from 'async-mutex';
2
- import { EventIterator } from 'event-iterator';
3
- import Logger from 'js-logger';
4
- import { isBatchedUpdateNotification } from '../db/DBAdapter.js';
5
- import { SyncStatus } from '../db/crud/SyncStatus.js';
6
- import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
7
- import { BaseObserver } from '../utils/BaseObserver.js';
8
- import { ControlledExecutor } from '../utils/ControlledExecutor.js';
9
- import { symbolAsyncIterator, throttleTrailing } from '../utils/async.js';
10
- import { ConnectionManager } from './ConnectionManager.js';
11
- import { CustomQuery } from './CustomQuery.js';
12
- import { isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory.js';
13
- import { PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
14
- import { CrudBatch } from './sync/bucket/CrudBatch.js';
15
- import { CrudEntry } from './sync/bucket/CrudEntry.js';
16
- import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
17
- import { DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_RETRY_DELAY_MS } from './sync/stream/AbstractStreamingSyncImplementation.js';
18
- import { coreStatusToJs } from './sync/stream/core-instruction.js';
19
- import { MEMORY_TRIGGER_CLAIM_MANAGER } from './triggers/MemoryTriggerClaimManager.js';
20
- import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
21
- import { DEFAULT_WATCH_THROTTLE_MS } from './watched/WatchedQuery.js';
22
- import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
23
- const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
24
- const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
25
- clearLocal: true
26
- };
27
- export const DEFAULT_POWERSYNC_CLOSE_OPTIONS = {
28
- disconnect: true
29
- };
30
- export const DEFAULT_POWERSYNC_DB_OPTIONS = {
31
- retryDelayMs: 5000,
32
- crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
33
- };
34
- export const DEFAULT_CRUD_BATCH_LIMIT = 100;
35
- /**
36
- * Requesting nested or recursive locks can block the application in some circumstances.
37
- * This default lock timeout will act as a failsafe to throw an error if a lock cannot
38
- * be obtained.
39
- */
40
- export const DEFAULT_LOCK_TIMEOUT_MS = 120_000; // 2 mins
41
- /**
42
- * Tests if the input is a {@link PowerSyncDatabaseOptionsWithSettings}
43
- * @internal
44
- */
45
- export const isPowerSyncDatabaseOptionsWithSettings = (test) => {
46
- return typeof test == 'object' && isSQLOpenOptions(test.database);
47
- };
48
- export class AbstractPowerSyncDatabase extends BaseObserver {
49
- options;
50
- /**
51
- * Returns true if the connection is closed.
52
- */
53
- closed;
54
- ready;
55
- /**
56
- * Current connection status.
57
- */
58
- currentStatus;
59
- sdkVersion;
60
- bucketStorageAdapter;
61
- _isReadyPromise;
62
- connectionManager;
63
- subscriptions;
64
- get syncStreamImplementation() {
65
- return this.connectionManager.syncStreamImplementation;
66
- }
67
- /**
68
- * The connector used to connect to the PowerSync service.
69
- *
70
- * @returns The connector used to connect to the PowerSync service or null if `connect()` has not been called.
71
- */
72
- get connector() {
73
- return this.connectionManager.connector;
74
- }
75
- /**
76
- * The resolved connection options used to connect to the PowerSync service.
77
- *
78
- * @returns The resolved connection options used to connect to the PowerSync service or null if `connect()` has not been called.
79
- */
80
- get connectionOptions() {
81
- return this.connectionManager.connectionOptions;
82
- }
83
- _schema;
84
- _database;
85
- runExclusiveMutex;
86
- /**
87
- * @experimental
88
- * Allows creating SQLite triggers which can be used to track various operations on SQLite tables.
89
- */
90
- triggers;
91
- triggersImpl;
92
- logger;
93
- constructor(options) {
94
- super();
95
- this.options = options;
96
- const { database, schema } = options;
97
- if (typeof schema?.toJSON != 'function') {
98
- throw new Error('The `schema` option should be provided and should be an instance of `Schema`.');
99
- }
100
- if (isDBAdapter(database)) {
101
- this._database = database;
102
- }
103
- else if (isSQLOpenFactory(database)) {
104
- this._database = database.openDB();
105
- }
106
- else if (isPowerSyncDatabaseOptionsWithSettings(options)) {
107
- this._database = this.openDBAdapter(options);
108
- }
109
- else {
110
- throw new Error('The provided `database` option is invalid.');
111
- }
112
- this.logger = options.logger ?? Logger.get(`PowerSyncDatabase[${this._database.name}]`);
113
- this.bucketStorageAdapter = this.generateBucketStorageAdapter();
114
- this.closed = false;
115
- this.currentStatus = new SyncStatus({});
116
- this.options = { ...DEFAULT_POWERSYNC_DB_OPTIONS, ...options };
117
- this._schema = schema;
118
- this.ready = false;
119
- this.sdkVersion = '';
120
- this.runExclusiveMutex = new Mutex();
121
- // Start async init
122
- this.subscriptions = {
123
- firstStatusMatching: (predicate, abort) => this.waitForStatus(predicate, abort),
124
- resolveOfflineSyncStatus: () => this.resolveOfflineSyncStatus(),
125
- rustSubscriptionsCommand: async (payload) => {
126
- await this.writeTransaction((tx) => {
127
- return tx.execute('select powersync_control(?,?)', ['subscriptions', JSON.stringify(payload)]);
128
- });
129
- }
130
- };
131
- this.connectionManager = new ConnectionManager({
132
- createSyncImplementation: async (connector, options) => {
133
- await this.waitForReady();
134
- return this.runExclusive(async () => {
135
- const sync = this.generateSyncStreamImplementation(connector, this.resolvedConnectionOptions(options));
136
- const onDispose = sync.registerListener({
137
- statusChanged: (status) => {
138
- this.currentStatus = new SyncStatus({
139
- ...status.toJSON(),
140
- hasSynced: this.currentStatus?.hasSynced || !!status.lastSyncedAt
141
- });
142
- this.iterateListeners((cb) => cb.statusChanged?.(this.currentStatus));
143
- }
144
- });
145
- await sync.waitForReady();
146
- return {
147
- sync,
148
- onDispose
149
- };
150
- });
151
- },
152
- logger: this.logger
153
- });
154
- this._isReadyPromise = this.initialize();
155
- this.triggers = this.triggersImpl = new TriggerManagerImpl({
156
- db: this,
157
- schema: this.schema,
158
- ...this.generateTriggerManagerConfig()
159
- });
160
- }
161
- /**
162
- * Schema used for the local database.
163
- */
164
- get schema() {
165
- return this._schema;
166
- }
167
- /**
168
- * The underlying database.
169
- *
170
- * For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
171
- */
172
- get database() {
173
- return this._database;
174
- }
175
- /**
176
- * Whether a connection to the PowerSync service is currently open.
177
- */
178
- get connected() {
179
- return this.currentStatus?.connected || false;
180
- }
181
- get connecting() {
182
- return this.currentStatus?.connecting || false;
183
- }
184
- /**
185
- * Generates a base configuration for {@link TriggerManagerImpl}.
186
- * Implementations should override this if necessary.
187
- */
188
- generateTriggerManagerConfig() {
189
- return {
190
- claimManager: MEMORY_TRIGGER_CLAIM_MANAGER
191
- };
192
- }
193
- /**
194
- * @returns A promise which will resolve once initialization is completed.
195
- */
196
- async waitForReady() {
197
- if (this.ready) {
198
- return;
199
- }
200
- await this._isReadyPromise;
201
- }
202
- /**
203
- * Wait for the first sync operation to complete.
204
- *
205
- * @param request Either an abort signal (after which the promise will complete regardless of
206
- * whether a full sync was completed) or an object providing an abort signal and a priority target.
207
- * When a priority target is set, the promise may complete when all buckets with the given (or higher)
208
- * priorities have been synchronized. This can be earlier than a complete sync.
209
- * @returns A promise which will resolve once the first full sync has completed.
210
- */
211
- async waitForFirstSync(request) {
212
- const signal = request instanceof AbortSignal ? request : request?.signal;
213
- const priority = request && 'priority' in request ? request.priority : undefined;
214
- const statusMatches = priority === undefined
215
- ? (status) => status.hasSynced
216
- : (status) => status.statusForPriority(priority).hasSynced;
217
- return this.waitForStatus(statusMatches, signal);
218
- }
219
- /**
220
- * Waits for the first sync status for which the `status` callback returns a truthy value.
221
- */
222
- async waitForStatus(predicate, signal) {
223
- if (predicate(this.currentStatus)) {
224
- return;
225
- }
226
- return new Promise((resolve) => {
227
- const dispose = this.registerListener({
228
- statusChanged: (status) => {
229
- if (predicate(status)) {
230
- abort();
231
- }
232
- }
233
- });
234
- function abort() {
235
- dispose();
236
- resolve();
237
- }
238
- if (signal?.aborted) {
239
- abort();
240
- }
241
- else {
242
- signal?.addEventListener('abort', abort);
243
- }
244
- });
245
- }
246
- /**
247
- * Entry point for executing initialization logic.
248
- * This is to be automatically executed in the constructor.
249
- */
250
- async initialize() {
251
- await this._initialize();
252
- await this.bucketStorageAdapter.init();
253
- await this.loadVersion();
254
- await this.updateSchema(this.options.schema);
255
- await this.resolveOfflineSyncStatus();
256
- await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
257
- await this.triggersImpl.cleanupResources();
258
- this.ready = true;
259
- this.iterateListeners((cb) => cb.initialized?.());
260
- }
261
- async loadVersion() {
262
- try {
263
- const { version } = await this.database.get('SELECT powersync_rs_version() as version');
264
- this.sdkVersion = version;
265
- }
266
- catch (e) {
267
- throw new Error(`The powersync extension is not loaded correctly. Details: ${e.message}`);
268
- }
269
- let versionInts;
270
- try {
271
- versionInts = this.sdkVersion.split(/[.\/]/)
272
- .slice(0, 3)
273
- .map((n) => parseInt(n));
274
- }
275
- catch (e) {
276
- throw new Error(`Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`);
277
- }
278
- // Validate >=0.4.10 <1.0.0
279
- if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 10)) {
280
- throw new Error(`Unsupported powersync extension version. Need >=0.4.10 <1.0.0, got: ${this.sdkVersion}`);
281
- }
282
- }
283
- async resolveOfflineSyncStatus() {
284
- const result = await this.database.get('SELECT powersync_offline_sync_status() as r');
285
- const parsed = JSON.parse(result.r);
286
- const updatedStatus = new SyncStatus({
287
- ...this.currentStatus.toJSON(),
288
- ...coreStatusToJs(parsed)
289
- });
290
- if (!updatedStatus.isEqual(this.currentStatus)) {
291
- this.currentStatus = updatedStatus;
292
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
293
- }
294
- }
295
- /**
296
- * Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
297
- *
298
- * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
299
- */
300
- async updateSchema(schema) {
301
- if (this.syncStreamImplementation) {
302
- throw new Error('Cannot update schema while connected');
303
- }
304
- /**
305
- * TODO
306
- * Validations only show a warning for now.
307
- * The next major release should throw an exception.
308
- */
309
- try {
310
- schema.validate();
311
- }
312
- catch (ex) {
313
- this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
314
- }
315
- this._schema = schema;
316
- await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
317
- await this.database.refreshSchema();
318
- this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
319
- }
320
- /**
321
- * Wait for initialization to complete.
322
- * While initializing is automatic, this helps to catch and report initialization errors.
323
- */
324
- async init() {
325
- return this.waitForReady();
326
- }
327
- // Use the options passed in during connect, or fallback to the options set during database creation or fallback to the default options
328
- resolvedConnectionOptions(options) {
329
- return {
330
- ...options,
331
- retryDelayMs: options?.retryDelayMs ?? this.options.retryDelayMs ?? this.options.retryDelay ?? DEFAULT_RETRY_DELAY_MS,
332
- crudUploadThrottleMs: options?.crudUploadThrottleMs ?? this.options.crudUploadThrottleMs ?? DEFAULT_CRUD_UPLOAD_THROTTLE_MS
333
- };
334
- }
335
- /**
336
- * @deprecated Use {@link AbstractPowerSyncDatabase#close} instead.
337
- * Clears all listeners registered by {@link AbstractPowerSyncDatabase#registerListener}.
338
- */
339
- dispose() {
340
- return super.dispose();
341
- }
342
- /**
343
- * Locking mechanism for exclusively running critical portions of connect/disconnect operations.
344
- * Locking here is mostly only important on web for multiple tab scenarios.
345
- */
346
- runExclusive(callback) {
347
- return this.runExclusiveMutex.runExclusive(callback);
348
- }
349
- /**
350
- * Connects to stream of events from the PowerSync instance.
351
- */
352
- async connect(connector, options) {
353
- const resolvedOptions = options ?? {};
354
- resolvedOptions.serializedSchema = this.schema.toJSON();
355
- return this.connectionManager.connect(connector, resolvedOptions);
356
- }
357
- /**
358
- * Close the sync connection.
359
- *
360
- * Use {@link connect} to connect again.
361
- */
362
- async disconnect() {
363
- return this.connectionManager.disconnect();
364
- }
365
- /**
366
- * Disconnect and clear the database.
367
- * Use this when logging out.
368
- * The database can still be queried after this is called, but the tables
369
- * would be empty.
370
- *
371
- * To preserve data in local-only tables, set clearLocal to false.
372
- */
373
- async disconnectAndClear(options = DEFAULT_DISCONNECT_CLEAR_OPTIONS) {
374
- await this.disconnect();
375
- await this.waitForReady();
376
- const { clearLocal } = options;
377
- await this.database.writeTransaction(async (tx) => {
378
- await tx.execute('SELECT powersync_clear(?)', [clearLocal ? 1 : 0]);
379
- });
380
- // The data has been deleted - reset the sync status
381
- this.currentStatus = new SyncStatus({});
382
- this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
383
- }
384
- /**
385
- * Create a sync stream to query its status or to subscribe to it.
386
- *
387
- * @param name The name of the stream to subscribe to.
388
- * @param params Optional parameters for the stream subscription.
389
- * @returns A {@link SyncStream} instance that can be subscribed to.
390
- * @experimental Sync streams are currently in alpha.
391
- */
392
- syncStream(name, params) {
393
- return this.connectionManager.stream(this.subscriptions, name, params ?? null);
394
- }
395
- /**
396
- * Close the database, releasing resources.
397
- *
398
- * Also disconnects any active connection.
399
- *
400
- * Once close is called, this connection cannot be used again - a new one
401
- * must be constructed.
402
- */
403
- async close(options = DEFAULT_POWERSYNC_CLOSE_OPTIONS) {
404
- await this.waitForReady();
405
- if (this.closed) {
406
- return;
407
- }
408
- this.triggersImpl.dispose();
409
- await this.iterateAsyncListeners(async (cb) => cb.closing?.());
410
- const { disconnect } = options;
411
- if (disconnect) {
412
- await this.disconnect();
413
- }
414
- await this.connectionManager.close();
415
- await this.database.close();
416
- this.closed = true;
417
- await this.iterateAsyncListeners(async (cb) => cb.closed?.());
418
- }
419
- /**
420
- * Get upload queue size estimate and count.
421
- */
422
- async getUploadQueueStats(includeSize) {
423
- return this.readTransaction(async (tx) => {
424
- if (includeSize) {
425
- const result = await tx.execute(`SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM ${PSInternalTable.CRUD}`);
426
- const row = result.rows.item(0);
427
- return new UploadQueueStats(row?.count ?? 0, row?.size ?? 0);
428
- }
429
- else {
430
- const result = await tx.execute(`SELECT count(*) as count FROM ${PSInternalTable.CRUD}`);
431
- const row = result.rows.item(0);
432
- return new UploadQueueStats(row?.count ?? 0);
433
- }
434
- });
435
- }
436
- /**
437
- * Get a batch of CRUD data to upload.
438
- *
439
- * Returns null if there is no data to upload.
440
- *
441
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
442
- *
443
- * Once the data have been successfully uploaded, call {@link CrudBatch.complete} before
444
- * requesting the next batch.
445
- *
446
- * Use {@link limit} to specify the maximum number of updates to return in a single
447
- * batch.
448
- *
449
- * This method does include transaction ids in the result, but does not group
450
- * data by transaction. One batch may contain data from multiple transactions,
451
- * and a single transaction may be split over multiple batches.
452
- *
453
- * @param limit Maximum number of CRUD entries to include in the batch
454
- * @returns A batch of CRUD operations to upload, or null if there are none
455
- */
456
- async getCrudBatch(limit = DEFAULT_CRUD_BATCH_LIMIT) {
457
- const result = await this.getAll(`SELECT id, tx_id, data FROM ${PSInternalTable.CRUD} ORDER BY id ASC LIMIT ?`, [limit + 1]);
458
- const all = result.map((row) => CrudEntry.fromRow(row)) ?? [];
459
- let haveMore = false;
460
- if (all.length > limit) {
461
- all.pop();
462
- haveMore = true;
463
- }
464
- if (all.length == 0) {
465
- return null;
466
- }
467
- const last = all[all.length - 1];
468
- return new CrudBatch(all, haveMore, async (writeCheckpoint) => this.handleCrudCheckpoint(last.clientId, writeCheckpoint));
469
- }
470
- /**
471
- * Get the next recorded transaction to upload.
472
- *
473
- * Returns null if there is no data to upload.
474
- *
475
- * Use this from the {@link PowerSyncBackendConnector.uploadData} callback.
476
- *
477
- * Once the data have been successfully uploaded, call {@link CrudTransaction.complete} before
478
- * requesting the next transaction.
479
- *
480
- * Unlike {@link getCrudBatch}, this only returns data from a single transaction at a time.
481
- * All data for the transaction is loaded into memory.
482
- *
483
- * @returns A transaction of CRUD operations to upload, or null if there are none
484
- */
485
- async getNextCrudTransaction() {
486
- const iterator = this.getCrudTransactions()[symbolAsyncIterator]();
487
- return (await iterator.next()).value;
488
- }
489
- /**
490
- * Returns an async iterator of completed transactions with local writes against the database.
491
- *
492
- * This is typically used from the {@link PowerSyncBackendConnector.uploadData} callback. Each entry emitted by the
493
- * returned iterator is a full transaction containing all local writes made while that transaction was active.
494
- *
495
- * Unlike {@link getNextCrudTransaction}, which always returns the oldest transaction that hasn't been
496
- * {@link CrudTransaction.complete}d yet, this iterator can be used to receive multiple transactions. Calling
497
- * {@link CrudTransaction.complete} will mark that and all prior transactions emitted by the iterator as completed.
498
- *
499
- * This can be used to upload multiple transactions in a single batch, e.g with:
500
- *
501
- * ```JavaScript
502
- * let lastTransaction = null;
503
- * let batch = [];
504
- *
505
- * for await (const transaction of database.getCrudTransactions()) {
506
- * batch.push(...transaction.crud);
507
- * lastTransaction = transaction;
508
- *
509
- * if (batch.length > 10) {
510
- * break;
511
- * }
512
- * }
513
- * ```
514
- *
515
- * If there is no local data to upload, the async iterator complete without emitting any items.
516
- *
517
- * 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)
518
- * for React Native.
519
- */
520
- getCrudTransactions() {
521
- return {
522
- [symbolAsyncIterator]: () => {
523
- let lastCrudItemId = -1;
524
- const sql = `
525
- WITH RECURSIVE crud_entries AS (
526
- SELECT id, tx_id, data FROM ps_crud WHERE id = (SELECT min(id) FROM ps_crud WHERE id > ?)
527
- UNION ALL
528
- SELECT ps_crud.id, ps_crud.tx_id, ps_crud.data FROM ps_crud
529
- INNER JOIN crud_entries ON crud_entries.id + 1 = rowid
530
- WHERE crud_entries.tx_id = ps_crud.tx_id
531
- )
532
- SELECT * FROM crud_entries;
533
- `;
534
- return {
535
- next: async () => {
536
- const nextTransaction = await this.database.getAll(sql, [lastCrudItemId]);
537
- if (nextTransaction.length == 0) {
538
- return { done: true, value: null };
539
- }
540
- const items = nextTransaction.map((row) => CrudEntry.fromRow(row));
541
- const last = items[items.length - 1];
542
- const txId = last.transactionId;
543
- lastCrudItemId = last.clientId;
544
- return {
545
- done: false,
546
- value: new CrudTransaction(items, async (writeCheckpoint) => this.handleCrudCheckpoint(last.clientId, writeCheckpoint), txId)
547
- };
548
- }
549
- };
550
- }
551
- };
552
- }
553
- /**
554
- * Get an unique client id for this database.
555
- *
556
- * The id is not reset when the database is cleared, only when the database is deleted.
557
- *
558
- * @returns A unique identifier for the database instance
559
- */
560
- async getClientId() {
561
- return this.bucketStorageAdapter.getClientId();
562
- }
563
- async handleCrudCheckpoint(lastClientId, writeCheckpoint) {
564
- return this.writeTransaction(async (tx) => {
565
- await tx.execute(`DELETE FROM ${PSInternalTable.CRUD} WHERE id <= ?`, [lastClientId]);
566
- if (writeCheckpoint) {
567
- const check = await tx.execute(`SELECT 1 FROM ${PSInternalTable.CRUD} LIMIT 1`);
568
- if (!check.rows?.length) {
569
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
570
- writeCheckpoint
571
- ]);
572
- }
573
- }
574
- else {
575
- await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
576
- this.bucketStorageAdapter.getMaxOpId()
577
- ]);
578
- }
579
- });
580
- }
581
- /**
582
- * Execute a SQL write (INSERT/UPDATE/DELETE) query
583
- * and optionally return results.
584
- *
585
- * @param sql The SQL query to execute
586
- * @param parameters Optional array of parameters to bind to the query
587
- * @returns The query result as an object with structured key-value pairs
588
- */
589
- async execute(sql, parameters) {
590
- return this.writeLock((tx) => tx.execute(sql, parameters));
591
- }
592
- /**
593
- * Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing.
594
- * This bypasses certain PowerSync abstractions and is useful for accessing the raw database results.
595
- *
596
- * @param sql The SQL query to execute
597
- * @param parameters Optional array of parameters to bind to the query
598
- * @returns The raw query result from the underlying database as a nested array of raw values, where each row is
599
- * represented as an array of column values without field names.
600
- */
601
- async executeRaw(sql, parameters) {
602
- await this.waitForReady();
603
- return this.database.executeRaw(sql, parameters);
604
- }
605
- /**
606
- * Execute a write query (INSERT/UPDATE/DELETE) multiple times with each parameter set
607
- * and optionally return results.
608
- * This is faster than executing separately with each parameter set.
609
- *
610
- * @param sql The SQL query to execute
611
- * @param parameters Optional 2D array of parameter sets, where each inner array is a set of parameters for one execution
612
- * @returns The query result
613
- */
614
- async executeBatch(sql, parameters) {
615
- await this.waitForReady();
616
- return this.database.executeBatch(sql, parameters);
617
- }
618
- /**
619
- * Execute a read-only query and return results.
620
- *
621
- * @param sql The SQL query to execute
622
- * @param parameters Optional array of parameters to bind to the query
623
- * @returns An array of results
624
- */
625
- async getAll(sql, parameters) {
626
- await this.waitForReady();
627
- return this.database.getAll(sql, parameters);
628
- }
629
- /**
630
- * Execute a read-only query and return the first result, or null if the ResultSet is empty.
631
- *
632
- * @param sql The SQL query to execute
633
- * @param parameters Optional array of parameters to bind to the query
634
- * @returns The first result if found, or null if no results are returned
635
- */
636
- async getOptional(sql, parameters) {
637
- await this.waitForReady();
638
- return this.database.getOptional(sql, parameters);
639
- }
640
- /**
641
- * Execute a read-only query and return the first result, error if the ResultSet is empty.
642
- *
643
- * @param sql The SQL query to execute
644
- * @param parameters Optional array of parameters to bind to the query
645
- * @returns The first result matching the query
646
- * @throws Error if no rows are returned
647
- */
648
- async get(sql, parameters) {
649
- await this.waitForReady();
650
- return this.database.get(sql, parameters);
651
- }
652
- /**
653
- * Takes a read lock, without starting a transaction.
654
- * In most cases, {@link readTransaction} should be used instead.
655
- */
656
- async readLock(callback) {
657
- await this.waitForReady();
658
- return this.database.readLock(callback);
659
- }
660
- /**
661
- * Takes a global lock, without starting a transaction.
662
- * In most cases, {@link writeTransaction} should be used instead.
663
- */
664
- async writeLock(callback) {
665
- await this.waitForReady();
666
- return this.database.writeLock(callback);
667
- }
668
- /**
669
- * Open a read-only transaction.
670
- * Read transactions can run concurrently to a write transaction.
671
- * Changes from any write transaction are not visible to read transactions started before it.
672
- *
673
- * @param callback Function to execute within the transaction
674
- * @param lockTimeout Time in milliseconds to wait for a lock before throwing an error
675
- * @returns The result of the callback
676
- * @throws Error if the lock cannot be obtained within the timeout period
677
- */
678
- async readTransaction(callback, lockTimeout = DEFAULT_LOCK_TIMEOUT_MS) {
679
- await this.waitForReady();
680
- return this.database.readTransaction(async (tx) => {
681
- const res = await callback({ ...tx });
682
- await tx.rollback();
683
- return res;
684
- }, { timeoutMs: lockTimeout });
685
- }
686
- /**
687
- * Open a read-write transaction.
688
- * This takes a global lock - only one write transaction can execute against the database at a time.
689
- * Statements within the transaction must be done on the provided {@link Transaction} interface.
690
- *
691
- * @param callback Function to execute within the transaction
692
- * @param lockTimeout Time in milliseconds to wait for a lock before throwing an error
693
- * @returns The result of the callback
694
- * @throws Error if the lock cannot be obtained within the timeout period
695
- */
696
- async writeTransaction(callback, lockTimeout = DEFAULT_LOCK_TIMEOUT_MS) {
697
- await this.waitForReady();
698
- return this.database.writeTransaction(async (tx) => {
699
- const res = await callback(tx);
700
- await tx.commit();
701
- return res;
702
- }, { timeoutMs: lockTimeout });
703
- }
704
- watch(sql, parameters, handlerOrOptions, maybeOptions) {
705
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onResult' in handlerOrOptions) {
706
- const handler = handlerOrOptions;
707
- const options = maybeOptions;
708
- return this.watchWithCallback(sql, parameters, handler, options);
709
- }
710
- const options = handlerOrOptions;
711
- return this.watchWithAsyncGenerator(sql, parameters, options);
712
- }
713
- /**
714
- * Allows defining a query which can be used to build a {@link WatchedQuery}.
715
- * The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
716
- * An optional mapper function can be provided to transform the results.
717
- *
718
- * @example
719
- * ```javascript
720
- * const watchedTodos = powersync.query({
721
- * sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
722
- * parameters: [],
723
- * mapper: (row) => ({
724
- * ...row,
725
- * created_at: new Date(row.created_at as string)
726
- * })
727
- * })
728
- * .watch()
729
- * // OR use .differentialWatch() for fine-grained watches.
730
- * ```
731
- */
732
- query(query) {
733
- const { sql, parameters = [], mapper } = query;
734
- const compatibleQuery = {
735
- compile: () => ({
736
- sql,
737
- parameters
738
- }),
739
- execute: async ({ sql, parameters }) => {
740
- const result = await this.getAll(sql, parameters);
741
- return mapper ? result.map(mapper) : result;
742
- }
743
- };
744
- return this.customQuery(compatibleQuery);
745
- }
746
- /**
747
- * Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
748
- * The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
749
- *
750
- * @example
751
- * ```javascript
752
- *
753
- * // Potentially a query from an ORM like Drizzle
754
- * const query = db.select().from(lists);
755
- *
756
- * const watchedTodos = powersync.customQuery(query)
757
- * .watch()
758
- * // OR use .differentialWatch() for fine-grained watches.
759
- * ```
760
- */
761
- customQuery(query) {
762
- return new CustomQuery({
763
- db: this,
764
- query
765
- });
766
- }
767
- /**
768
- * Execute a read query every time the source tables are modified.
769
- * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
770
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
771
- *
772
- * Note that the `onChange` callback member of the handler is required.
773
- *
774
- * @param sql The SQL query to execute
775
- * @param parameters Optional array of parameters to bind to the query
776
- * @param handler Callbacks for handling results and errors
777
- * @param options Options for configuring watch behavior
778
- */
779
- watchWithCallback(sql, parameters, handler, options) {
780
- const { onResult, onError = (e) => this.logger.error(e) } = handler ?? {};
781
- if (!onResult) {
782
- throw new Error('onResult is required');
783
- }
784
- const { comparator } = options ?? {};
785
- // This API yields a QueryResult type.
786
- // This is not a standard Array result, which makes it incompatible with the .query API.
787
- const watchedQuery = new OnChangeQueryProcessor({
788
- db: this,
789
- comparator,
790
- placeholderData: null,
791
- watchOptions: {
792
- query: {
793
- compile: () => ({
794
- sql: sql,
795
- parameters: parameters ?? []
796
- }),
797
- execute: () => this.executeReadOnly(sql, parameters)
798
- },
799
- reportFetching: false,
800
- throttleMs: options?.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS,
801
- triggerOnTables: options?.tables
802
- }
803
- });
804
- const dispose = watchedQuery.registerListener({
805
- onData: (data) => {
806
- if (!data) {
807
- // This should not happen. We only use null for the initial data.
808
- return;
809
- }
810
- onResult(data);
811
- },
812
- onError: (error) => {
813
- onError(error);
814
- }
815
- });
816
- options?.signal?.addEventListener('abort', () => {
817
- dispose();
818
- watchedQuery.close();
819
- });
820
- }
821
- /**
822
- * Execute a read query every time the source tables are modified.
823
- * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
824
- * Source tables are automatically detected using `EXPLAIN QUERY PLAN`.
825
- *
826
- * @param sql The SQL query to execute
827
- * @param parameters Optional array of parameters to bind to the query
828
- * @param options Options for configuring watch behavior
829
- * @returns An AsyncIterable that yields QueryResults whenever the data changes
830
- */
831
- watchWithAsyncGenerator(sql, parameters, options) {
832
- return new EventIterator((eventOptions) => {
833
- const handler = {
834
- onResult: (result) => {
835
- eventOptions.push(result);
836
- },
837
- onError: (error) => {
838
- eventOptions.fail(error);
839
- }
840
- };
841
- this.watchWithCallback(sql, parameters, handler, options);
842
- options?.signal?.addEventListener('abort', () => {
843
- eventOptions.stop();
844
- });
845
- });
846
- }
847
- /**
848
- * Resolves the list of tables that are used in a SQL query.
849
- * If tables are specified in the options, those are used directly.
850
- * Otherwise, analyzes the query using EXPLAIN to determine which tables are accessed.
851
- *
852
- * @param sql The SQL query to analyze
853
- * @param parameters Optional parameters for the SQL query
854
- * @param options Optional watch options that may contain explicit table list
855
- * @returns Array of table names that the query depends on
856
- */
857
- async resolveTables(sql, parameters, options) {
858
- const resolvedTables = options?.tables ? [...options.tables] : [];
859
- if (!options?.tables) {
860
- const explained = await this.getAll(`EXPLAIN ${sql}`, parameters);
861
- const rootPages = explained
862
- .filter((row) => row.opcode == 'OpenRead' && row.p3 == 0 && typeof row.p2 == 'number')
863
- .map((row) => row.p2);
864
- const tables = await this.getAll(`SELECT DISTINCT tbl_name FROM sqlite_master WHERE rootpage IN (SELECT json_each.value FROM json_each(?))`, [JSON.stringify(rootPages)]);
865
- for (const table of tables) {
866
- resolvedTables.push(table.tbl_name.replace(POWERSYNC_TABLE_MATCH, ''));
867
- }
868
- }
869
- return resolvedTables;
870
- }
871
- onChange(handlerOrOptions, maybeOptions) {
872
- if (handlerOrOptions && typeof handlerOrOptions === 'object' && 'onChange' in handlerOrOptions) {
873
- const handler = handlerOrOptions;
874
- const options = maybeOptions;
875
- return this.onChangeWithCallback(handler, options);
876
- }
877
- const options = handlerOrOptions;
878
- return this.onChangeWithAsyncGenerator(options);
879
- }
880
- /**
881
- * Invoke the provided callback on any changes to any of the specified tables.
882
- *
883
- * This is preferred over {@link watchWithCallback} when multiple queries need to be performed
884
- * together when data is changed.
885
- *
886
- * Note that the `onChange` callback member of the handler is required.
887
- *
888
- * @param handler Callbacks for handling change events and errors
889
- * @param options Options for configuring watch behavior
890
- * @returns A dispose function to stop watching for changes
891
- */
892
- onChangeWithCallback(handler, options) {
893
- const { onChange, onError = (e) => this.logger.error(e) } = handler ?? {};
894
- if (!onChange) {
895
- throw new Error('onChange is required');
896
- }
897
- const resolvedOptions = options ?? {};
898
- const watchedTables = new Set((resolvedOptions?.tables ?? []).flatMap((table) => [table, `ps_data__${table}`, `ps_data_local__${table}`]));
899
- const changedTables = new Set();
900
- const throttleMs = resolvedOptions.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS;
901
- const executor = new ControlledExecutor(async (e) => {
902
- await onChange(e);
903
- });
904
- const flushTableUpdates = throttleTrailing(() => this.handleTableChanges(changedTables, watchedTables, (intersection) => {
905
- if (resolvedOptions?.signal?.aborted)
906
- return;
907
- executor.schedule({ changedTables: intersection });
908
- }), throttleMs);
909
- if (options?.triggerImmediate) {
910
- executor.schedule({ changedTables: [] });
911
- }
912
- const dispose = this.database.registerListener({
913
- tablesUpdated: async (update) => {
914
- try {
915
- this.processTableUpdates(update, changedTables);
916
- flushTableUpdates();
917
- }
918
- catch (error) {
919
- onError?.(error);
920
- }
921
- }
922
- });
923
- resolvedOptions.signal?.addEventListener('abort', () => {
924
- executor.dispose();
925
- dispose();
926
- });
927
- return () => dispose();
928
- }
929
- /**
930
- * Create a Stream of changes to any of the specified tables.
931
- *
932
- * This is preferred over {@link watchWithAsyncGenerator} when multiple queries need to be performed
933
- * together when data is changed.
934
- *
935
- * Note: do not declare this as `async *onChange` as it will not work in React Native.
936
- *
937
- * @param options Options for configuring watch behavior
938
- * @returns An AsyncIterable that yields change events whenever the specified tables change
939
- */
940
- onChangeWithAsyncGenerator(options) {
941
- const resolvedOptions = options ?? {};
942
- return new EventIterator((eventOptions) => {
943
- const dispose = this.onChangeWithCallback({
944
- onChange: (event) => {
945
- eventOptions.push(event);
946
- },
947
- onError: (error) => {
948
- eventOptions.fail(error);
949
- }
950
- }, options);
951
- resolvedOptions.signal?.addEventListener('abort', () => {
952
- eventOptions.stop();
953
- // Maybe fail?
954
- });
955
- return () => dispose();
956
- });
957
- }
958
- handleTableChanges(changedTables, watchedTables, onDetectedChanges) {
959
- if (changedTables.size > 0) {
960
- const intersection = Array.from(changedTables.values()).filter((change) => watchedTables.has(change));
961
- if (intersection.length) {
962
- onDetectedChanges(intersection);
963
- }
964
- }
965
- changedTables.clear();
966
- }
967
- processTableUpdates(updateNotification, changedTables) {
968
- const tables = isBatchedUpdateNotification(updateNotification)
969
- ? updateNotification.tables
970
- : [updateNotification.table];
971
- for (const table of tables) {
972
- changedTables.add(table);
973
- }
974
- }
975
- /**
976
- * @ignore
977
- */
978
- async executeReadOnly(sql, params) {
979
- await this.waitForReady();
980
- return this.database.readLock((tx) => tx.execute(sql, params));
981
- }
982
- }
983
- //# sourceMappingURL=AbstractPowerSyncDatabase.js.map