@powersync/common 1.46.0 → 1.47.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 (55) hide show
  1. package/README.md +5 -1
  2. package/dist/bundle.cjs +1266 -360
  3. package/dist/bundle.cjs.map +1 -1
  4. package/dist/bundle.mjs +1259 -361
  5. package/dist/bundle.mjs.map +1 -1
  6. package/dist/bundle.node.cjs +1266 -360
  7. package/dist/bundle.node.cjs.map +1 -1
  8. package/dist/bundle.node.mjs +1259 -361
  9. package/dist/bundle.node.mjs.map +1 -1
  10. package/dist/index.d.cts +530 -29
  11. package/lib/attachments/AttachmentContext.d.ts +86 -0
  12. package/lib/attachments/AttachmentContext.js +229 -0
  13. package/lib/attachments/AttachmentContext.js.map +1 -0
  14. package/lib/attachments/AttachmentErrorHandler.d.ts +31 -0
  15. package/lib/attachments/AttachmentErrorHandler.js +2 -0
  16. package/lib/attachments/AttachmentErrorHandler.js.map +1 -0
  17. package/lib/attachments/AttachmentQueue.d.ts +149 -0
  18. package/lib/attachments/AttachmentQueue.js +362 -0
  19. package/lib/attachments/AttachmentQueue.js.map +1 -0
  20. package/lib/attachments/AttachmentService.d.ts +29 -0
  21. package/lib/attachments/AttachmentService.js +56 -0
  22. package/lib/attachments/AttachmentService.js.map +1 -0
  23. package/lib/attachments/LocalStorageAdapter.d.ts +62 -0
  24. package/lib/attachments/LocalStorageAdapter.js +6 -0
  25. package/lib/attachments/LocalStorageAdapter.js.map +1 -0
  26. package/lib/attachments/RemoteStorageAdapter.d.ts +27 -0
  27. package/lib/attachments/RemoteStorageAdapter.js +2 -0
  28. package/lib/attachments/RemoteStorageAdapter.js.map +1 -0
  29. package/lib/attachments/Schema.d.ts +50 -0
  30. package/lib/attachments/Schema.js +62 -0
  31. package/lib/attachments/Schema.js.map +1 -0
  32. package/lib/attachments/SyncingService.d.ts +62 -0
  33. package/lib/attachments/SyncingService.js +168 -0
  34. package/lib/attachments/SyncingService.js.map +1 -0
  35. package/lib/attachments/WatchedAttachmentItem.d.ts +17 -0
  36. package/lib/attachments/WatchedAttachmentItem.js +2 -0
  37. package/lib/attachments/WatchedAttachmentItem.js.map +1 -0
  38. package/lib/index.d.ts +10 -0
  39. package/lib/index.js +10 -0
  40. package/lib/index.js.map +1 -1
  41. package/lib/utils/mutex.d.ts +1 -1
  42. package/lib/utils/mutex.js.map +1 -1
  43. package/package.json +1 -1
  44. package/src/attachments/AttachmentContext.ts +279 -0
  45. package/src/attachments/AttachmentErrorHandler.ts +34 -0
  46. package/src/attachments/AttachmentQueue.ts +472 -0
  47. package/src/attachments/AttachmentService.ts +62 -0
  48. package/src/attachments/LocalStorageAdapter.ts +72 -0
  49. package/src/attachments/README.md +718 -0
  50. package/src/attachments/RemoteStorageAdapter.ts +30 -0
  51. package/src/attachments/Schema.ts +87 -0
  52. package/src/attachments/SyncingService.ts +193 -0
  53. package/src/attachments/WatchedAttachmentItem.ts +19 -0
  54. package/src/index.ts +11 -0
  55. package/src/utils/mutex.ts +1 -1
package/dist/bundle.mjs CHANGED
@@ -1,5 +1,1223 @@
1
1
  import { Mutex } from 'async-mutex';
2
2
 
3
+ // https://www.sqlite.org/lang_expr.html#castexpr
4
+ var ColumnType;
5
+ (function (ColumnType) {
6
+ ColumnType["TEXT"] = "TEXT";
7
+ ColumnType["INTEGER"] = "INTEGER";
8
+ ColumnType["REAL"] = "REAL";
9
+ })(ColumnType || (ColumnType = {}));
10
+ const text = {
11
+ type: ColumnType.TEXT
12
+ };
13
+ const integer = {
14
+ type: ColumnType.INTEGER
15
+ };
16
+ const real = {
17
+ type: ColumnType.REAL
18
+ };
19
+ // powersync-sqlite-core limits the number of column per table to 1999, due to internal SQLite limits.
20
+ // In earlier versions this was limited to 63.
21
+ const MAX_AMOUNT_OF_COLUMNS = 1999;
22
+ const column = {
23
+ text,
24
+ integer,
25
+ real
26
+ };
27
+ class Column {
28
+ options;
29
+ constructor(options) {
30
+ this.options = options;
31
+ }
32
+ get name() {
33
+ return this.options.name;
34
+ }
35
+ get type() {
36
+ return this.options.type;
37
+ }
38
+ toJSON() {
39
+ return {
40
+ name: this.name,
41
+ type: this.type
42
+ };
43
+ }
44
+ }
45
+
46
+ const DEFAULT_INDEX_COLUMN_OPTIONS = {
47
+ ascending: true
48
+ };
49
+ class IndexedColumn {
50
+ options;
51
+ static createAscending(column) {
52
+ return new IndexedColumn({
53
+ name: column,
54
+ ascending: true
55
+ });
56
+ }
57
+ constructor(options) {
58
+ this.options = { ...DEFAULT_INDEX_COLUMN_OPTIONS, ...options };
59
+ }
60
+ get name() {
61
+ return this.options.name;
62
+ }
63
+ get ascending() {
64
+ return this.options.ascending;
65
+ }
66
+ toJSON(table) {
67
+ return {
68
+ name: this.name,
69
+ ascending: this.ascending,
70
+ type: table.columns.find((column) => column.name === this.name)?.type ?? ColumnType.TEXT
71
+ };
72
+ }
73
+ }
74
+
75
+ const DEFAULT_INDEX_OPTIONS = {
76
+ columns: []
77
+ };
78
+ class Index {
79
+ options;
80
+ static createAscending(options, columnNames) {
81
+ return new Index({
82
+ ...options,
83
+ columns: columnNames.map((name) => IndexedColumn.createAscending(name))
84
+ });
85
+ }
86
+ constructor(options) {
87
+ this.options = options;
88
+ this.options = { ...DEFAULT_INDEX_OPTIONS, ...options };
89
+ }
90
+ get name() {
91
+ return this.options.name;
92
+ }
93
+ get columns() {
94
+ return this.options.columns ?? [];
95
+ }
96
+ toJSON(table) {
97
+ return {
98
+ name: this.name,
99
+ columns: this.columns.map((c) => c.toJSON(table))
100
+ };
101
+ }
102
+ }
103
+
104
+ const DEFAULT_TABLE_OPTIONS = {
105
+ indexes: [],
106
+ insertOnly: false,
107
+ localOnly: false,
108
+ trackPrevious: false,
109
+ trackMetadata: false,
110
+ ignoreEmptyUpdates: false
111
+ };
112
+ const InvalidSQLCharacters = /["'%,.#\s[\]]/;
113
+ class Table {
114
+ options;
115
+ _mappedColumns;
116
+ static createLocalOnly(options) {
117
+ return new Table({ ...options, localOnly: true, insertOnly: false });
118
+ }
119
+ static createInsertOnly(options) {
120
+ return new Table({ ...options, localOnly: false, insertOnly: true });
121
+ }
122
+ /**
123
+ * Create a table.
124
+ * @deprecated This was only only included for TableV2 and is no longer necessary.
125
+ * Prefer to use new Table() directly.
126
+ *
127
+ * TODO remove in the next major release.
128
+ */
129
+ static createTable(name, table) {
130
+ return new Table({
131
+ name,
132
+ columns: table.columns,
133
+ indexes: table.indexes,
134
+ localOnly: table.options.localOnly,
135
+ insertOnly: table.options.insertOnly,
136
+ viewName: table.options.viewName
137
+ });
138
+ }
139
+ constructor(optionsOrColumns, v2Options) {
140
+ if (this.isTableV1(optionsOrColumns)) {
141
+ this.initTableV1(optionsOrColumns);
142
+ }
143
+ else {
144
+ this.initTableV2(optionsOrColumns, v2Options);
145
+ }
146
+ }
147
+ copyWithName(name) {
148
+ return new Table({
149
+ ...this.options,
150
+ name
151
+ });
152
+ }
153
+ isTableV1(arg) {
154
+ return 'columns' in arg && Array.isArray(arg.columns);
155
+ }
156
+ initTableV1(options) {
157
+ this.options = {
158
+ ...options,
159
+ indexes: options.indexes || []
160
+ };
161
+ this.applyDefaultOptions();
162
+ }
163
+ initTableV2(columns, options) {
164
+ const convertedColumns = Object.entries(columns).map(([name, columnInfo]) => new Column({ name, type: columnInfo.type }));
165
+ const convertedIndexes = Object.entries(options?.indexes ?? {}).map(([name, columnNames]) => new Index({
166
+ name,
167
+ columns: columnNames.map((name) => new IndexedColumn({
168
+ name: name.replace(/^-/, ''),
169
+ ascending: !name.startsWith('-')
170
+ }))
171
+ }));
172
+ this.options = {
173
+ name: '',
174
+ columns: convertedColumns,
175
+ indexes: convertedIndexes,
176
+ viewName: options?.viewName,
177
+ insertOnly: options?.insertOnly,
178
+ localOnly: options?.localOnly,
179
+ trackPrevious: options?.trackPrevious,
180
+ trackMetadata: options?.trackMetadata,
181
+ ignoreEmptyUpdates: options?.ignoreEmptyUpdates
182
+ };
183
+ this.applyDefaultOptions();
184
+ this._mappedColumns = columns;
185
+ }
186
+ applyDefaultOptions() {
187
+ this.options.insertOnly ??= DEFAULT_TABLE_OPTIONS.insertOnly;
188
+ this.options.localOnly ??= DEFAULT_TABLE_OPTIONS.localOnly;
189
+ this.options.trackPrevious ??= DEFAULT_TABLE_OPTIONS.trackPrevious;
190
+ this.options.trackMetadata ??= DEFAULT_TABLE_OPTIONS.trackMetadata;
191
+ this.options.ignoreEmptyUpdates ??= DEFAULT_TABLE_OPTIONS.ignoreEmptyUpdates;
192
+ }
193
+ get name() {
194
+ return this.options.name;
195
+ }
196
+ get viewNameOverride() {
197
+ return this.options.viewName;
198
+ }
199
+ get viewName() {
200
+ return this.viewNameOverride ?? this.name;
201
+ }
202
+ get columns() {
203
+ return this.options.columns;
204
+ }
205
+ get columnMap() {
206
+ return (this._mappedColumns ??
207
+ this.columns.reduce((hash, column) => {
208
+ hash[column.name] = { type: column.type ?? ColumnType.TEXT };
209
+ return hash;
210
+ }, {}));
211
+ }
212
+ get indexes() {
213
+ return this.options.indexes ?? [];
214
+ }
215
+ get localOnly() {
216
+ return this.options.localOnly;
217
+ }
218
+ get insertOnly() {
219
+ return this.options.insertOnly;
220
+ }
221
+ get trackPrevious() {
222
+ return this.options.trackPrevious;
223
+ }
224
+ get trackMetadata() {
225
+ return this.options.trackMetadata;
226
+ }
227
+ get ignoreEmptyUpdates() {
228
+ return this.options.ignoreEmptyUpdates;
229
+ }
230
+ get internalName() {
231
+ if (this.options.localOnly) {
232
+ return `ps_data_local__${this.name}`;
233
+ }
234
+ return `ps_data__${this.name}`;
235
+ }
236
+ get validName() {
237
+ if (InvalidSQLCharacters.test(this.name)) {
238
+ return false;
239
+ }
240
+ if (this.viewNameOverride != null && InvalidSQLCharacters.test(this.viewNameOverride)) {
241
+ return false;
242
+ }
243
+ return true;
244
+ }
245
+ validate() {
246
+ if (InvalidSQLCharacters.test(this.name)) {
247
+ throw new Error(`Invalid characters in table name: ${this.name}`);
248
+ }
249
+ if (this.viewNameOverride && InvalidSQLCharacters.test(this.viewNameOverride)) {
250
+ throw new Error(`Invalid characters in view name: ${this.viewNameOverride}`);
251
+ }
252
+ if (this.columns.length > MAX_AMOUNT_OF_COLUMNS) {
253
+ throw new Error(`Table has too many columns. The maximum number of columns is ${MAX_AMOUNT_OF_COLUMNS}.`);
254
+ }
255
+ if (this.trackMetadata && this.localOnly) {
256
+ throw new Error(`Can't include metadata for local-only tables.`);
257
+ }
258
+ if (this.trackPrevious != false && this.localOnly) {
259
+ throw new Error(`Can't include old values for local-only tables.`);
260
+ }
261
+ const columnNames = new Set();
262
+ columnNames.add('id');
263
+ for (const column of this.columns) {
264
+ const { name: columnName } = column;
265
+ if (column.name === 'id') {
266
+ throw new Error(`An id column is automatically added, custom id columns are not supported`);
267
+ }
268
+ if (columnNames.has(columnName)) {
269
+ throw new Error(`Duplicate column ${columnName}`);
270
+ }
271
+ if (InvalidSQLCharacters.test(columnName)) {
272
+ throw new Error(`Invalid characters in column name: ${column.name}`);
273
+ }
274
+ columnNames.add(columnName);
275
+ }
276
+ const indexNames = new Set();
277
+ for (const index of this.indexes) {
278
+ if (indexNames.has(index.name)) {
279
+ throw new Error(`Duplicate index ${index.name}`);
280
+ }
281
+ if (InvalidSQLCharacters.test(index.name)) {
282
+ throw new Error(`Invalid characters in index name: ${index.name}`);
283
+ }
284
+ for (const column of index.columns) {
285
+ if (!columnNames.has(column.name)) {
286
+ throw new Error(`Column ${column.name} not found for index ${index.name}`);
287
+ }
288
+ }
289
+ indexNames.add(index.name);
290
+ }
291
+ }
292
+ toJSON() {
293
+ const trackPrevious = this.trackPrevious;
294
+ return {
295
+ name: this.name,
296
+ view_name: this.viewName,
297
+ local_only: this.localOnly,
298
+ insert_only: this.insertOnly,
299
+ include_old: trackPrevious && (trackPrevious.columns ?? true),
300
+ include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
301
+ include_metadata: this.trackMetadata,
302
+ ignore_empty_update: this.ignoreEmptyUpdates,
303
+ columns: this.columns.map((c) => c.toJSON()),
304
+ indexes: this.indexes.map((e) => e.toJSON(this))
305
+ };
306
+ }
307
+ }
308
+
309
+ const ATTACHMENT_TABLE = 'attachments';
310
+ /**
311
+ * Maps a database row to an AttachmentRecord.
312
+ *
313
+ * @param row - The database row object
314
+ * @returns The corresponding AttachmentRecord
315
+ *
316
+ * @experimental
317
+ */
318
+ function attachmentFromSql(row) {
319
+ return {
320
+ id: row.id,
321
+ filename: row.filename,
322
+ localUri: row.local_uri,
323
+ size: row.size,
324
+ mediaType: row.media_type,
325
+ timestamp: row.timestamp,
326
+ metaData: row.meta_data,
327
+ hasSynced: row.has_synced === 1,
328
+ state: row.state
329
+ };
330
+ }
331
+ /**
332
+ * AttachmentState represents the current synchronization state of an attachment.
333
+ *
334
+ * @experimental
335
+ */
336
+ var AttachmentState;
337
+ (function (AttachmentState) {
338
+ AttachmentState[AttachmentState["QUEUED_UPLOAD"] = 0] = "QUEUED_UPLOAD";
339
+ AttachmentState[AttachmentState["QUEUED_DOWNLOAD"] = 1] = "QUEUED_DOWNLOAD";
340
+ AttachmentState[AttachmentState["QUEUED_DELETE"] = 2] = "QUEUED_DELETE";
341
+ AttachmentState[AttachmentState["SYNCED"] = 3] = "SYNCED";
342
+ AttachmentState[AttachmentState["ARCHIVED"] = 4] = "ARCHIVED"; // Attachment has been orphaned, i.e. the associated record has been deleted
343
+ })(AttachmentState || (AttachmentState = {}));
344
+ /**
345
+ * AttachmentTable defines the schema for the attachment queue table.
346
+ *
347
+ * @internal
348
+ */
349
+ class AttachmentTable extends Table {
350
+ constructor(options) {
351
+ super({
352
+ filename: column.text,
353
+ local_uri: column.text,
354
+ timestamp: column.integer,
355
+ size: column.integer,
356
+ media_type: column.text,
357
+ state: column.integer, // Corresponds to AttachmentState
358
+ has_synced: column.integer,
359
+ meta_data: column.text
360
+ }, {
361
+ ...options,
362
+ viewName: options?.viewName ?? ATTACHMENT_TABLE,
363
+ localOnly: true,
364
+ insertOnly: false
365
+ });
366
+ }
367
+ }
368
+
369
+ /**
370
+ * AttachmentContext provides database operations for managing attachment records.
371
+ *
372
+ * Provides methods to query, insert, update, and delete attachment records with
373
+ * proper transaction management through PowerSync.
374
+ *
375
+ * @internal
376
+ */
377
+ class AttachmentContext {
378
+ /** PowerSync database instance for executing queries */
379
+ db;
380
+ /** Name of the database table storing attachment records */
381
+ tableName;
382
+ /** Logger instance for diagnostic information */
383
+ logger;
384
+ /** Maximum number of archived attachments to keep before cleanup */
385
+ archivedCacheLimit = 100;
386
+ /**
387
+ * Creates a new AttachmentContext instance.
388
+ *
389
+ * @param db - PowerSync database instance
390
+ * @param tableName - Name of the table storing attachment records. Default: 'attachments'
391
+ * @param logger - Logger instance for diagnostic output
392
+ */
393
+ constructor(db, tableName = 'attachments', logger, archivedCacheLimit) {
394
+ this.db = db;
395
+ this.tableName = tableName;
396
+ this.logger = logger;
397
+ this.archivedCacheLimit = archivedCacheLimit;
398
+ }
399
+ /**
400
+ * Retrieves all active attachments that require synchronization.
401
+ * Active attachments include those queued for upload, download, or delete.
402
+ * Results are ordered by timestamp in ascending order.
403
+ *
404
+ * @returns Promise resolving to an array of active attachment records
405
+ */
406
+ async getActiveAttachments() {
407
+ const attachments = await this.db.getAll(
408
+ /* sql */
409
+ `
410
+ SELECT
411
+ *
412
+ FROM
413
+ ${this.tableName}
414
+ WHERE
415
+ state = ?
416
+ OR state = ?
417
+ OR state = ?
418
+ ORDER BY
419
+ timestamp ASC
420
+ `, [AttachmentState.QUEUED_UPLOAD, AttachmentState.QUEUED_DOWNLOAD, AttachmentState.QUEUED_DELETE]);
421
+ return attachments.map(attachmentFromSql);
422
+ }
423
+ /**
424
+ * Retrieves all archived attachments.
425
+ *
426
+ * Archived attachments are no longer referenced but haven't been permanently deleted.
427
+ * These are candidates for cleanup operations to free up storage space.
428
+ *
429
+ * @returns Promise resolving to an array of archived attachment records
430
+ */
431
+ async getArchivedAttachments() {
432
+ const attachments = await this.db.getAll(
433
+ /* sql */
434
+ `
435
+ SELECT
436
+ *
437
+ FROM
438
+ ${this.tableName}
439
+ WHERE
440
+ state = ?
441
+ ORDER BY
442
+ timestamp ASC
443
+ `, [AttachmentState.ARCHIVED]);
444
+ return attachments.map(attachmentFromSql);
445
+ }
446
+ /**
447
+ * Retrieves all attachment records regardless of state.
448
+ * Results are ordered by timestamp in ascending order.
449
+ *
450
+ * @returns Promise resolving to an array of all attachment records
451
+ */
452
+ async getAttachments() {
453
+ const attachments = await this.db.getAll(
454
+ /* sql */
455
+ `
456
+ SELECT
457
+ *
458
+ FROM
459
+ ${this.tableName}
460
+ ORDER BY
461
+ timestamp ASC
462
+ `, []);
463
+ return attachments.map(attachmentFromSql);
464
+ }
465
+ /**
466
+ * Inserts or updates an attachment record within an existing transaction.
467
+ *
468
+ * Performs an upsert operation (INSERT OR REPLACE). Must be called within
469
+ * an active database transaction context.
470
+ *
471
+ * @param attachment - The attachment record to upsert
472
+ * @param context - Active database transaction context
473
+ */
474
+ async upsertAttachment(attachment, context) {
475
+ await context.execute(
476
+ /* sql */
477
+ `
478
+ INSERT
479
+ OR REPLACE INTO ${this.tableName} (
480
+ id,
481
+ filename,
482
+ local_uri,
483
+ size,
484
+ media_type,
485
+ timestamp,
486
+ state,
487
+ has_synced,
488
+ meta_data
489
+ )
490
+ VALUES
491
+ (?, ?, ?, ?, ?, ?, ?, ?, ?)
492
+ `, [
493
+ attachment.id,
494
+ attachment.filename,
495
+ attachment.localUri || null,
496
+ attachment.size || null,
497
+ attachment.mediaType || null,
498
+ attachment.timestamp,
499
+ attachment.state,
500
+ attachment.hasSynced ? 1 : 0,
501
+ attachment.metaData || null
502
+ ]);
503
+ }
504
+ async getAttachment(id) {
505
+ const attachment = await this.db.get(
506
+ /* sql */
507
+ `
508
+ SELECT
509
+ *
510
+ FROM
511
+ ${this.tableName}
512
+ WHERE
513
+ id = ?
514
+ `, [id]);
515
+ return attachment ? attachmentFromSql(attachment) : undefined;
516
+ }
517
+ /**
518
+ * Permanently deletes an attachment record from the database.
519
+ *
520
+ * This operation removes the attachment record but does not delete
521
+ * the associated local or remote files. File deletion should be handled
522
+ * separately through the appropriate storage adapters.
523
+ *
524
+ * @param attachmentId - Unique identifier of the attachment to delete
525
+ */
526
+ async deleteAttachment(attachmentId) {
527
+ await this.db.writeTransaction((tx) => tx.execute(
528
+ /* sql */
529
+ `
530
+ DELETE FROM ${this.tableName}
531
+ WHERE
532
+ id = ?
533
+ `, [attachmentId]));
534
+ }
535
+ async clearQueue() {
536
+ await this.db.writeTransaction((tx) => tx.execute(/* sql */ ` DELETE FROM ${this.tableName} `));
537
+ }
538
+ async deleteArchivedAttachments(callback) {
539
+ const limit = 1000;
540
+ const results = await this.db.getAll(
541
+ /* sql */
542
+ `
543
+ SELECT
544
+ *
545
+ FROM
546
+ ${this.tableName}
547
+ WHERE
548
+ state = ?
549
+ ORDER BY
550
+ timestamp DESC
551
+ LIMIT
552
+ ?
553
+ OFFSET
554
+ ?
555
+ `, [AttachmentState.ARCHIVED, limit, this.archivedCacheLimit]);
556
+ const archivedAttachments = results.map(attachmentFromSql);
557
+ if (archivedAttachments.length === 0)
558
+ return false;
559
+ await callback?.(archivedAttachments);
560
+ this.logger.info(`Deleting ${archivedAttachments.length} archived attachments. Archived attachment exceeds cache archiveCacheLimit of ${this.archivedCacheLimit}.`);
561
+ const ids = archivedAttachments.map((attachment) => attachment.id);
562
+ await this.db.execute(
563
+ /* sql */
564
+ `
565
+ DELETE FROM ${this.tableName}
566
+ WHERE
567
+ id IN (
568
+ SELECT
569
+ json_each.value
570
+ FROM
571
+ json_each (?)
572
+ );
573
+ `, [JSON.stringify(ids)]);
574
+ this.logger.info(`Deleted ${archivedAttachments.length} archived attachments`);
575
+ return archivedAttachments.length < limit;
576
+ }
577
+ /**
578
+ * Saves multiple attachment records in a single transaction.
579
+ *
580
+ * All updates are saved in a single batch after processing.
581
+ * If the attachments array is empty, no database operations are performed.
582
+ *
583
+ * @param attachments - Array of attachment records to save
584
+ */
585
+ async saveAttachments(attachments) {
586
+ if (attachments.length === 0) {
587
+ return;
588
+ }
589
+ await this.db.writeTransaction(async (tx) => {
590
+ for (const attachment of attachments) {
591
+ await this.upsertAttachment(attachment, tx);
592
+ }
593
+ });
594
+ }
595
+ }
596
+
597
+ var WatchedQueryListenerEvent;
598
+ (function (WatchedQueryListenerEvent) {
599
+ WatchedQueryListenerEvent["ON_DATA"] = "onData";
600
+ WatchedQueryListenerEvent["ON_ERROR"] = "onError";
601
+ WatchedQueryListenerEvent["ON_STATE_CHANGE"] = "onStateChange";
602
+ WatchedQueryListenerEvent["SETTINGS_WILL_UPDATE"] = "settingsWillUpdate";
603
+ WatchedQueryListenerEvent["CLOSED"] = "closed";
604
+ })(WatchedQueryListenerEvent || (WatchedQueryListenerEvent = {}));
605
+ const DEFAULT_WATCH_THROTTLE_MS = 30;
606
+ const DEFAULT_WATCH_QUERY_OPTIONS = {
607
+ throttleMs: DEFAULT_WATCH_THROTTLE_MS,
608
+ reportFetching: true
609
+ };
610
+
611
+ /**
612
+ * Orchestrates attachment synchronization between local and remote storage.
613
+ * Handles uploads, downloads, deletions, and state transitions.
614
+ *
615
+ * @internal
616
+ */
617
+ class SyncingService {
618
+ attachmentService;
619
+ localStorage;
620
+ remoteStorage;
621
+ logger;
622
+ errorHandler;
623
+ constructor(attachmentService, localStorage, remoteStorage, logger, errorHandler) {
624
+ this.attachmentService = attachmentService;
625
+ this.localStorage = localStorage;
626
+ this.remoteStorage = remoteStorage;
627
+ this.logger = logger;
628
+ this.errorHandler = errorHandler;
629
+ }
630
+ /**
631
+ * Processes attachments based on their state (upload, download, or delete).
632
+ * All updates are saved in a single batch after processing.
633
+ *
634
+ * @param attachments - Array of attachment records to process
635
+ * @param context - Attachment context for database operations
636
+ * @returns Promise that resolves when all attachments have been processed and saved
637
+ */
638
+ async processAttachments(attachments, context) {
639
+ const updatedAttachments = [];
640
+ for (const attachment of attachments) {
641
+ switch (attachment.state) {
642
+ case AttachmentState.QUEUED_UPLOAD:
643
+ const uploaded = await this.uploadAttachment(attachment);
644
+ updatedAttachments.push(uploaded);
645
+ break;
646
+ case AttachmentState.QUEUED_DOWNLOAD:
647
+ const downloaded = await this.downloadAttachment(attachment);
648
+ updatedAttachments.push(downloaded);
649
+ break;
650
+ case AttachmentState.QUEUED_DELETE:
651
+ const deleted = await this.deleteAttachment(attachment);
652
+ updatedAttachments.push(deleted);
653
+ break;
654
+ }
655
+ }
656
+ await context.saveAttachments(updatedAttachments);
657
+ }
658
+ /**
659
+ * Uploads an attachment from local storage to remote storage.
660
+ * On success, marks as SYNCED. On failure, defers to error handler or archives.
661
+ *
662
+ * @param attachment - The attachment record to upload
663
+ * @returns Updated attachment record with new state
664
+ * @throws Error if the attachment has no localUri
665
+ */
666
+ async uploadAttachment(attachment) {
667
+ this.logger.info(`Uploading attachment ${attachment.filename}`);
668
+ try {
669
+ if (attachment.localUri == null) {
670
+ throw new Error(`No localUri for attachment ${attachment.id}`);
671
+ }
672
+ const fileBlob = await this.localStorage.readFile(attachment.localUri);
673
+ await this.remoteStorage.uploadFile(fileBlob, attachment);
674
+ return {
675
+ ...attachment,
676
+ state: AttachmentState.SYNCED,
677
+ hasSynced: true
678
+ };
679
+ }
680
+ catch (error) {
681
+ const shouldRetry = (await this.errorHandler?.onUploadError(attachment, error)) ?? true;
682
+ if (!shouldRetry) {
683
+ return {
684
+ ...attachment,
685
+ state: AttachmentState.ARCHIVED
686
+ };
687
+ }
688
+ return attachment;
689
+ }
690
+ }
691
+ /**
692
+ * Downloads an attachment from remote storage to local storage.
693
+ * Retrieves the file, converts to base64, and saves locally.
694
+ * On success, marks as SYNCED. On failure, defers to error handler or archives.
695
+ *
696
+ * @param attachment - The attachment record to download
697
+ * @returns Updated attachment record with local URI and new state
698
+ */
699
+ async downloadAttachment(attachment) {
700
+ this.logger.info(`Downloading attachment ${attachment.filename}`);
701
+ try {
702
+ const fileData = await this.remoteStorage.downloadFile(attachment);
703
+ const localUri = this.localStorage.getLocalUri(attachment.filename);
704
+ await this.localStorage.saveFile(localUri, fileData);
705
+ return {
706
+ ...attachment,
707
+ state: AttachmentState.SYNCED,
708
+ localUri: localUri,
709
+ hasSynced: true
710
+ };
711
+ }
712
+ catch (error) {
713
+ const shouldRetry = (await this.errorHandler?.onDownloadError(attachment, error)) ?? true;
714
+ if (!shouldRetry) {
715
+ return {
716
+ ...attachment,
717
+ state: AttachmentState.ARCHIVED
718
+ };
719
+ }
720
+ return attachment;
721
+ }
722
+ }
723
+ /**
724
+ * Deletes an attachment from both remote and local storage.
725
+ * Removes the remote file, local file (if exists), and the attachment record.
726
+ * On failure, defers to error handler or archives.
727
+ *
728
+ * @param attachment - The attachment record to delete
729
+ * @returns Updated attachment record
730
+ */
731
+ async deleteAttachment(attachment) {
732
+ try {
733
+ await this.remoteStorage.deleteFile(attachment);
734
+ if (attachment.localUri) {
735
+ await this.localStorage.deleteFile(attachment.localUri);
736
+ }
737
+ await this.attachmentService.withContext(async (ctx) => {
738
+ await ctx.deleteAttachment(attachment.id);
739
+ });
740
+ return {
741
+ ...attachment,
742
+ state: AttachmentState.ARCHIVED
743
+ };
744
+ }
745
+ catch (error) {
746
+ const shouldRetry = (await this.errorHandler?.onDeleteError(attachment, error)) ?? true;
747
+ if (!shouldRetry) {
748
+ return {
749
+ ...attachment,
750
+ state: AttachmentState.ARCHIVED
751
+ };
752
+ }
753
+ return attachment;
754
+ }
755
+ }
756
+ /**
757
+ * Performs cleanup of archived attachments by removing their local files and records.
758
+ * Errors during local file deletion are logged but do not prevent record deletion.
759
+ */
760
+ async deleteArchivedAttachments(context) {
761
+ return await context.deleteArchivedAttachments(async (archivedAttachments) => {
762
+ for (const attachment of archivedAttachments) {
763
+ if (attachment.localUri) {
764
+ try {
765
+ await this.localStorage.deleteFile(attachment.localUri);
766
+ }
767
+ catch (error) {
768
+ this.logger.error('Error deleting local file for archived attachment', error);
769
+ }
770
+ }
771
+ }
772
+ });
773
+ }
774
+ }
775
+
776
+ /**
777
+ * Wrapper for async-mutex runExclusive, which allows for a timeout on each exclusive lock.
778
+ */
779
+ async function mutexRunExclusive(mutex, callback, options) {
780
+ return new Promise((resolve, reject) => {
781
+ const timeout = options?.timeoutMs;
782
+ let timedOut = false;
783
+ const timeoutId = timeout
784
+ ? setTimeout(() => {
785
+ timedOut = true;
786
+ reject(new Error('Timeout waiting for lock'));
787
+ }, timeout)
788
+ : undefined;
789
+ mutex.runExclusive(async () => {
790
+ if (timeoutId) {
791
+ clearTimeout(timeoutId);
792
+ }
793
+ if (timedOut)
794
+ return;
795
+ try {
796
+ resolve(await callback());
797
+ }
798
+ catch (ex) {
799
+ reject(ex);
800
+ }
801
+ });
802
+ });
803
+ }
804
+
805
+ /**
806
+ * Service for querying and watching attachment records in the database.
807
+ *
808
+ * @internal
809
+ */
810
+ class AttachmentService {
811
+ db;
812
+ logger;
813
+ tableName;
814
+ mutex = new Mutex();
815
+ context;
816
+ constructor(db, logger, tableName = 'attachments', archivedCacheLimit = 100) {
817
+ this.db = db;
818
+ this.logger = logger;
819
+ this.tableName = tableName;
820
+ this.context = new AttachmentContext(db, tableName, logger, archivedCacheLimit);
821
+ }
822
+ /**
823
+ * Creates a differential watch query for active attachments requiring synchronization.
824
+ * @returns Watch query that emits changes for queued uploads, downloads, and deletes
825
+ */
826
+ watchActiveAttachments({ throttleMs } = {}) {
827
+ this.logger.info('Watching active attachments...');
828
+ const watch = this.db
829
+ .query({
830
+ sql: /* sql */ `
831
+ SELECT
832
+ *
833
+ FROM
834
+ ${this.tableName}
835
+ WHERE
836
+ state = ?
837
+ OR state = ?
838
+ OR state = ?
839
+ ORDER BY
840
+ timestamp ASC
841
+ `,
842
+ parameters: [AttachmentState.QUEUED_UPLOAD, AttachmentState.QUEUED_DOWNLOAD, AttachmentState.QUEUED_DELETE]
843
+ })
844
+ .differentialWatch({ throttleMs });
845
+ return watch;
846
+ }
847
+ /**
848
+ * Executes a callback with exclusive access to the attachment context.
849
+ */
850
+ async withContext(callback) {
851
+ return mutexRunExclusive(this.mutex, async () => {
852
+ return callback(this.context);
853
+ });
854
+ }
855
+ }
856
+
857
+ /**
858
+ * AttachmentQueue manages the lifecycle and synchronization of attachments
859
+ * between local and remote storage.
860
+ * Provides automatic synchronization, upload/download queuing, attachment monitoring,
861
+ * verification and repair of local files, and cleanup of archived attachments.
862
+ *
863
+ * @experimental
864
+ * @alpha This is currently experimental and may change without a major version bump.
865
+ */
866
+ class AttachmentQueue {
867
+ /** Timer for periodic synchronization operations */
868
+ periodicSyncTimer;
869
+ /** Service for synchronizing attachments between local and remote storage */
870
+ syncingService;
871
+ /** Adapter for local file storage operations */
872
+ localStorage;
873
+ /** Adapter for remote file storage operations */
874
+ remoteStorage;
875
+ /**
876
+ * Callback function to watch for changes in attachment references in your data model.
877
+ *
878
+ * This should be implemented by the user of AttachmentQueue to monitor changes in your application's
879
+ * data that reference attachments. When attachments are added, removed, or modified,
880
+ * this callback should trigger the onUpdate function with the current set of attachments.
881
+ */
882
+ watchAttachments;
883
+ /** Name of the database table storing attachment records */
884
+ tableName;
885
+ /** Logger instance for diagnostic information */
886
+ logger;
887
+ /** Interval in milliseconds between periodic sync operations. Default: 30000 (30 seconds) */
888
+ syncIntervalMs = 30 * 1000;
889
+ /** Duration in milliseconds to throttle sync operations */
890
+ syncThrottleDuration;
891
+ /** Whether to automatically download remote attachments. Default: true */
892
+ downloadAttachments = true;
893
+ /** Maximum number of archived attachments to keep before cleanup. Default: 100 */
894
+ archivedCacheLimit;
895
+ /** Service for managing attachment-related database operations */
896
+ attachmentService;
897
+ /** PowerSync database instance */
898
+ db;
899
+ /** Cleanup function for status change listener */
900
+ statusListenerDispose;
901
+ watchActiveAttachments;
902
+ watchAttachmentsAbortController;
903
+ /**
904
+ * Creates a new AttachmentQueue instance.
905
+ *
906
+ * @param options - Configuration options
907
+ * @param options.db - PowerSync database instance
908
+ * @param options.remoteStorage - Remote storage adapter for upload/download operations
909
+ * @param options.localStorage - Local storage adapter for file persistence
910
+ * @param options.watchAttachments - Callback for monitoring attachment changes in your data model
911
+ * @param options.tableName - Name of the table to store attachment records. Default: 'ps_attachment_queue'
912
+ * @param options.logger - Logger instance. Defaults to db.logger
913
+ * @param options.syncIntervalMs - Interval between automatic syncs in milliseconds. Default: 30000
914
+ * @param options.syncThrottleDuration - Throttle duration for sync operations in milliseconds. Default: 1000
915
+ * @param options.downloadAttachments - Whether to automatically download remote attachments. Default: true
916
+ * @param options.archivedCacheLimit - Maximum archived attachments before cleanup. Default: 100
917
+ */
918
+ constructor({ db, localStorage, remoteStorage, watchAttachments, logger, tableName = ATTACHMENT_TABLE, syncIntervalMs = 30 * 1000, syncThrottleDuration = DEFAULT_WATCH_THROTTLE_MS, downloadAttachments = true, archivedCacheLimit = 100, errorHandler }) {
919
+ this.db = db;
920
+ this.remoteStorage = remoteStorage;
921
+ this.localStorage = localStorage;
922
+ this.watchAttachments = watchAttachments;
923
+ this.tableName = tableName;
924
+ this.syncIntervalMs = syncIntervalMs;
925
+ this.syncThrottleDuration = syncThrottleDuration;
926
+ this.archivedCacheLimit = archivedCacheLimit;
927
+ this.downloadAttachments = downloadAttachments;
928
+ this.logger = logger ?? db.logger;
929
+ this.attachmentService = new AttachmentService(db, this.logger, tableName, archivedCacheLimit);
930
+ this.syncingService = new SyncingService(this.attachmentService, localStorage, remoteStorage, this.logger, errorHandler);
931
+ }
932
+ /**
933
+ * Generates a new attachment ID using a SQLite UUID function.
934
+ *
935
+ * @returns Promise resolving to the new attachment ID
936
+ */
937
+ async generateAttachmentId() {
938
+ return this.db.get('SELECT uuid() as id').then((row) => row.id);
939
+ }
940
+ /**
941
+ * Starts the attachment synchronization process.
942
+ *
943
+ * This method:
944
+ * - Stops any existing sync operations
945
+ * - Sets up periodic synchronization based on syncIntervalMs
946
+ * - Registers listeners for active attachment changes
947
+ * - Processes watched attachments to queue uploads/downloads
948
+ * - Handles state transitions for archived and new attachments
949
+ */
950
+ async startSync() {
951
+ await this.stopSync();
952
+ this.watchActiveAttachments = this.attachmentService.watchActiveAttachments({
953
+ throttleMs: this.syncThrottleDuration
954
+ });
955
+ // immediately invoke the sync storage to initialize local storage
956
+ await this.localStorage.initialize();
957
+ await this.verifyAttachments();
958
+ // Sync storage periodically
959
+ this.periodicSyncTimer = setInterval(async () => {
960
+ await this.syncStorage();
961
+ }, this.syncIntervalMs);
962
+ // Sync storage when there is a change in active attachments
963
+ this.watchActiveAttachments.registerListener({
964
+ onDiff: async () => {
965
+ await this.syncStorage();
966
+ }
967
+ });
968
+ this.statusListenerDispose = this.db.registerListener({
969
+ statusChanged: (status) => {
970
+ if (status.connected) {
971
+ // Device came online, process attachments immediately
972
+ this.syncStorage().catch((error) => {
973
+ this.logger.error('Error syncing storage on connection:', error);
974
+ });
975
+ }
976
+ }
977
+ });
978
+ this.watchAttachmentsAbortController = new AbortController();
979
+ const signal = this.watchAttachmentsAbortController.signal;
980
+ // Process attachments when there is a change in watched attachments
981
+ this.watchAttachments(async (watchedAttachments) => {
982
+ // Skip processing if sync has been stopped
983
+ if (signal.aborted) {
984
+ return;
985
+ }
986
+ await this.attachmentService.withContext(async (ctx) => {
987
+ // Need to get all the attachments which are tracked in the DB.
988
+ // We might need to restore an archived attachment.
989
+ const currentAttachments = await ctx.getAttachments();
990
+ const attachmentUpdates = [];
991
+ for (const watchedAttachment of watchedAttachments) {
992
+ const existingQueueItem = currentAttachments.find((a) => a.id === watchedAttachment.id);
993
+ if (!existingQueueItem) {
994
+ // Item is watched but not in the queue yet. Need to add it.
995
+ if (!this.downloadAttachments) {
996
+ continue;
997
+ }
998
+ const filename = watchedAttachment.filename ?? `${watchedAttachment.id}.${watchedAttachment.fileExtension}`;
999
+ attachmentUpdates.push({
1000
+ id: watchedAttachment.id,
1001
+ filename,
1002
+ state: AttachmentState.QUEUED_DOWNLOAD,
1003
+ hasSynced: false,
1004
+ metaData: watchedAttachment.metaData,
1005
+ timestamp: new Date().getTime()
1006
+ });
1007
+ continue;
1008
+ }
1009
+ if (existingQueueItem.state === AttachmentState.ARCHIVED) {
1010
+ // The attachment is present again. Need to queue it for sync.
1011
+ // We might be able to optimize this in future
1012
+ if (existingQueueItem.hasSynced === true) {
1013
+ // No remote action required, we can restore the record (avoids deletion)
1014
+ attachmentUpdates.push({
1015
+ ...existingQueueItem,
1016
+ state: AttachmentState.SYNCED
1017
+ });
1018
+ }
1019
+ else {
1020
+ // The localURI should be set if the record was meant to be uploaded
1021
+ // and hasSynced is false then
1022
+ // it must be an upload operation
1023
+ const newState = existingQueueItem.localUri == null ? AttachmentState.QUEUED_DOWNLOAD : AttachmentState.QUEUED_UPLOAD;
1024
+ attachmentUpdates.push({
1025
+ ...existingQueueItem,
1026
+ state: newState
1027
+ });
1028
+ }
1029
+ }
1030
+ }
1031
+ for (const attachment of currentAttachments) {
1032
+ const notInWatchedItems = watchedAttachments.find((i) => i.id === attachment.id) == null;
1033
+ if (notInWatchedItems) {
1034
+ switch (attachment.state) {
1035
+ case AttachmentState.QUEUED_DELETE:
1036
+ case AttachmentState.QUEUED_UPLOAD:
1037
+ // Only archive if it has synced
1038
+ if (attachment.hasSynced === true) {
1039
+ attachmentUpdates.push({
1040
+ ...attachment,
1041
+ state: AttachmentState.ARCHIVED
1042
+ });
1043
+ }
1044
+ break;
1045
+ default:
1046
+ // Archive other states such as QUEUED_DOWNLOAD
1047
+ attachmentUpdates.push({
1048
+ ...attachment,
1049
+ state: AttachmentState.ARCHIVED
1050
+ });
1051
+ }
1052
+ }
1053
+ }
1054
+ if (attachmentUpdates.length > 0) {
1055
+ await ctx.saveAttachments(attachmentUpdates);
1056
+ }
1057
+ });
1058
+ }, signal);
1059
+ }
1060
+ /**
1061
+ * Synchronizes all active attachments between local and remote storage.
1062
+ *
1063
+ * This is called automatically at regular intervals when sync is started,
1064
+ * but can also be called manually to trigger an immediate sync.
1065
+ */
1066
+ async syncStorage() {
1067
+ await this.attachmentService.withContext(async (ctx) => {
1068
+ const activeAttachments = await ctx.getActiveAttachments();
1069
+ await this.localStorage.initialize();
1070
+ await this.syncingService.processAttachments(activeAttachments, ctx);
1071
+ await this.syncingService.deleteArchivedAttachments(ctx);
1072
+ });
1073
+ }
1074
+ /**
1075
+ * Stops the attachment synchronization process.
1076
+ *
1077
+ * Clears the periodic sync timer and closes all active attachment watchers.
1078
+ */
1079
+ async stopSync() {
1080
+ clearInterval(this.periodicSyncTimer);
1081
+ this.periodicSyncTimer = undefined;
1082
+ if (this.watchActiveAttachments)
1083
+ await this.watchActiveAttachments.close();
1084
+ if (this.watchAttachmentsAbortController) {
1085
+ this.watchAttachmentsAbortController.abort();
1086
+ }
1087
+ if (this.statusListenerDispose) {
1088
+ this.statusListenerDispose();
1089
+ this.statusListenerDispose = undefined;
1090
+ }
1091
+ }
1092
+ /**
1093
+ * Saves a file to local storage and queues it for upload to remote storage.
1094
+ *
1095
+ * @param options - File save options
1096
+ * @param options.data - The file data as ArrayBuffer, Blob, or base64 string
1097
+ * @param options.fileExtension - File extension (e.g., 'jpg', 'pdf')
1098
+ * @param options.mediaType - MIME type of the file (e.g., 'image/jpeg')
1099
+ * @param options.metaData - Optional metadata to associate with the attachment
1100
+ * @param options.id - Optional custom ID. If not provided, a UUID will be generated
1101
+ * @param options.updateHook - Optional callback to execute additional database operations
1102
+ * within the same transaction as the attachment creation
1103
+ * @returns Promise resolving to the created attachment record
1104
+ */
1105
+ async saveFile({ data, fileExtension, mediaType, metaData, id, updateHook }) {
1106
+ const resolvedId = id ?? (await this.generateAttachmentId());
1107
+ const filename = `${resolvedId}.${fileExtension}`;
1108
+ const localUri = this.localStorage.getLocalUri(filename);
1109
+ const size = await this.localStorage.saveFile(localUri, data);
1110
+ const attachment = {
1111
+ id: resolvedId,
1112
+ filename,
1113
+ mediaType,
1114
+ localUri,
1115
+ state: AttachmentState.QUEUED_UPLOAD,
1116
+ hasSynced: false,
1117
+ size,
1118
+ timestamp: new Date().getTime(),
1119
+ metaData
1120
+ };
1121
+ await this.attachmentService.withContext(async (ctx) => {
1122
+ await ctx.db.writeTransaction(async (tx) => {
1123
+ await updateHook?.(tx, attachment);
1124
+ await ctx.upsertAttachment(attachment, tx);
1125
+ });
1126
+ });
1127
+ return attachment;
1128
+ }
1129
+ async deleteFile({ id, updateHook }) {
1130
+ await this.attachmentService.withContext(async (ctx) => {
1131
+ const attachment = await ctx.getAttachment(id);
1132
+ if (!attachment) {
1133
+ throw new Error(`Attachment with id ${id} not found`);
1134
+ }
1135
+ await ctx.db.writeTransaction(async (tx) => {
1136
+ await updateHook?.(tx, attachment);
1137
+ await ctx.upsertAttachment({
1138
+ ...attachment,
1139
+ state: AttachmentState.QUEUED_DELETE,
1140
+ hasSynced: false
1141
+ }, tx);
1142
+ });
1143
+ });
1144
+ }
1145
+ async expireCache() {
1146
+ let isDone = false;
1147
+ while (!isDone) {
1148
+ await this.attachmentService.withContext(async (ctx) => {
1149
+ isDone = await this.syncingService.deleteArchivedAttachments(ctx);
1150
+ });
1151
+ }
1152
+ }
1153
+ async clearQueue() {
1154
+ await this.attachmentService.withContext(async (ctx) => {
1155
+ await ctx.clearQueue();
1156
+ });
1157
+ await this.localStorage.clear();
1158
+ }
1159
+ /**
1160
+ * Verifies the integrity of all attachment records and repairs inconsistencies.
1161
+ *
1162
+ * This method checks each attachment record against the local filesystem and:
1163
+ * - Updates localUri if the file exists at a different path
1164
+ * - Archives attachments with missing local files that haven't been uploaded
1165
+ * - Requeues synced attachments for download if their local files are missing
1166
+ */
1167
+ async verifyAttachments() {
1168
+ await this.attachmentService.withContext(async (ctx) => {
1169
+ const attachments = await ctx.getAttachments();
1170
+ const updates = [];
1171
+ for (const attachment of attachments) {
1172
+ if (attachment.localUri == null) {
1173
+ continue;
1174
+ }
1175
+ const exists = await this.localStorage.fileExists(attachment.localUri);
1176
+ if (exists) {
1177
+ // The file exists, this is correct
1178
+ continue;
1179
+ }
1180
+ const newLocalUri = this.localStorage.getLocalUri(attachment.filename);
1181
+ const newExists = await this.localStorage.fileExists(newLocalUri);
1182
+ if (newExists) {
1183
+ // The file exists locally but the localUri is broken, we update it.
1184
+ updates.push({
1185
+ ...attachment,
1186
+ localUri: newLocalUri
1187
+ });
1188
+ }
1189
+ else {
1190
+ // the file doesn't exist locally.
1191
+ if (attachment.state === AttachmentState.SYNCED) {
1192
+ // the file has been successfully synced to remote storage but is missing
1193
+ // we download it again
1194
+ updates.push({
1195
+ ...attachment,
1196
+ state: AttachmentState.QUEUED_DOWNLOAD,
1197
+ localUri: undefined
1198
+ });
1199
+ }
1200
+ else {
1201
+ // the file wasn't successfully synced to remote storage, we archive it
1202
+ updates.push({
1203
+ ...attachment,
1204
+ state: AttachmentState.ARCHIVED,
1205
+ localUri: undefined // Clears the value
1206
+ });
1207
+ }
1208
+ }
1209
+ }
1210
+ await ctx.saveAttachments(updates);
1211
+ });
1212
+ }
1213
+ }
1214
+
1215
+ var EncodingType;
1216
+ (function (EncodingType) {
1217
+ EncodingType["UTF8"] = "utf8";
1218
+ EncodingType["Base64"] = "base64";
1219
+ })(EncodingType || (EncodingType = {}));
1220
+
3
1221
  function getDefaultExportFromCjs (x) {
4
1222
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5
1223
  }
@@ -1316,20 +2534,6 @@ class MetaBaseObserver extends BaseObserver {
1316
2534
  }
1317
2535
  }
1318
2536
 
1319
- var WatchedQueryListenerEvent;
1320
- (function (WatchedQueryListenerEvent) {
1321
- WatchedQueryListenerEvent["ON_DATA"] = "onData";
1322
- WatchedQueryListenerEvent["ON_ERROR"] = "onError";
1323
- WatchedQueryListenerEvent["ON_STATE_CHANGE"] = "onStateChange";
1324
- WatchedQueryListenerEvent["SETTINGS_WILL_UPDATE"] = "settingsWillUpdate";
1325
- WatchedQueryListenerEvent["CLOSED"] = "closed";
1326
- })(WatchedQueryListenerEvent || (WatchedQueryListenerEvent = {}));
1327
- const DEFAULT_WATCH_THROTTLE_MS = 30;
1328
- const DEFAULT_WATCH_QUERY_OPTIONS = {
1329
- throttleMs: DEFAULT_WATCH_THROTTLE_MS,
1330
- reportFetching: true
1331
- };
1332
-
1333
2537
  /**
1334
2538
  * Performs underlying watching and yields a stream of results.
1335
2539
  * @internal
@@ -9242,7 +10446,7 @@ function requireDist () {
9242
10446
 
9243
10447
  var distExports = requireDist();
9244
10448
 
9245
- var version = "1.46.0";
10449
+ var version = "1.47.0";
9246
10450
  var PACKAGE = {
9247
10451
  version: version};
9248
10452
 
@@ -12876,151 +14080,50 @@ class SqliteBucketStorage extends BaseObserver {
12876
14080
  ]);
12877
14081
  return r != 0;
12878
14082
  }
12879
- async migrateToFixedSubkeys() {
12880
- await this.writeTransaction(async (tx) => {
12881
- await tx.execute('UPDATE ps_oplog SET key = powersync_remove_duplicate_key_encoding(key);');
12882
- await tx.execute('INSERT OR REPLACE INTO ps_kv (key, value) VALUES (?, ?);', [
12883
- SqliteBucketStorage._subkeyMigrationKey,
12884
- '1'
12885
- ]);
12886
- });
12887
- }
12888
- static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
12889
- }
12890
- function hasMatchingPriority(priority, bucket) {
12891
- return bucket.priority != null && bucket.priority <= priority;
12892
- }
12893
-
12894
- // TODO JSON
12895
- class SyncDataBatch {
12896
- buckets;
12897
- static fromJSON(json) {
12898
- return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
12899
- }
12900
- constructor(buckets) {
12901
- this.buckets = buckets;
12902
- }
12903
- }
12904
-
12905
- /**
12906
- * Thrown when an underlying database connection is closed.
12907
- * This is particularly relevant when worker connections are marked as closed while
12908
- * operations are still in progress.
12909
- */
12910
- class ConnectionClosedError extends Error {
12911
- static NAME = 'ConnectionClosedError';
12912
- static MATCHES(input) {
12913
- /**
12914
- * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
12915
- * check might fail. This also performs a failsafe check.
12916
- * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
12917
- */
12918
- return (input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME));
12919
- }
12920
- constructor(message) {
12921
- super(message);
12922
- this.name = ConnectionClosedError.NAME;
12923
- }
12924
- }
12925
-
12926
- // https://www.sqlite.org/lang_expr.html#castexpr
12927
- var ColumnType;
12928
- (function (ColumnType) {
12929
- ColumnType["TEXT"] = "TEXT";
12930
- ColumnType["INTEGER"] = "INTEGER";
12931
- ColumnType["REAL"] = "REAL";
12932
- })(ColumnType || (ColumnType = {}));
12933
- const text = {
12934
- type: ColumnType.TEXT
12935
- };
12936
- const integer = {
12937
- type: ColumnType.INTEGER
12938
- };
12939
- const real = {
12940
- type: ColumnType.REAL
12941
- };
12942
- // powersync-sqlite-core limits the number of column per table to 1999, due to internal SQLite limits.
12943
- // In earlier versions this was limited to 63.
12944
- const MAX_AMOUNT_OF_COLUMNS = 1999;
12945
- const column = {
12946
- text,
12947
- integer,
12948
- real
12949
- };
12950
- class Column {
12951
- options;
12952
- constructor(options) {
12953
- this.options = options;
12954
- }
12955
- get name() {
12956
- return this.options.name;
12957
- }
12958
- get type() {
12959
- return this.options.type;
12960
- }
12961
- toJSON() {
12962
- return {
12963
- name: this.name,
12964
- type: this.type
12965
- };
12966
- }
12967
- }
12968
-
12969
- const DEFAULT_INDEX_COLUMN_OPTIONS = {
12970
- ascending: true
12971
- };
12972
- class IndexedColumn {
12973
- options;
12974
- static createAscending(column) {
12975
- return new IndexedColumn({
12976
- name: column,
12977
- ascending: true
12978
- });
12979
- }
12980
- constructor(options) {
12981
- this.options = { ...DEFAULT_INDEX_COLUMN_OPTIONS, ...options };
12982
- }
12983
- get name() {
12984
- return this.options.name;
12985
- }
12986
- get ascending() {
12987
- return this.options.ascending;
12988
- }
12989
- toJSON(table) {
12990
- return {
12991
- name: this.name,
12992
- ascending: this.ascending,
12993
- type: table.columns.find((column) => column.name === this.name)?.type ?? ColumnType.TEXT
12994
- };
12995
- }
12996
- }
12997
-
12998
- const DEFAULT_INDEX_OPTIONS = {
12999
- columns: []
13000
- };
13001
- class Index {
13002
- options;
13003
- static createAscending(options, columnNames) {
13004
- return new Index({
13005
- ...options,
13006
- columns: columnNames.map((name) => IndexedColumn.createAscending(name))
14083
+ async migrateToFixedSubkeys() {
14084
+ await this.writeTransaction(async (tx) => {
14085
+ await tx.execute('UPDATE ps_oplog SET key = powersync_remove_duplicate_key_encoding(key);');
14086
+ await tx.execute('INSERT OR REPLACE INTO ps_kv (key, value) VALUES (?, ?);', [
14087
+ SqliteBucketStorage._subkeyMigrationKey,
14088
+ '1'
14089
+ ]);
13007
14090
  });
13008
14091
  }
13009
- constructor(options) {
13010
- this.options = options;
13011
- this.options = { ...DEFAULT_INDEX_OPTIONS, ...options };
14092
+ static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
14093
+ }
14094
+ function hasMatchingPriority(priority, bucket) {
14095
+ return bucket.priority != null && bucket.priority <= priority;
14096
+ }
14097
+
14098
+ // TODO JSON
14099
+ class SyncDataBatch {
14100
+ buckets;
14101
+ static fromJSON(json) {
14102
+ return new SyncDataBatch(json.buckets.map((bucket) => SyncDataBucket.fromRow(bucket)));
13012
14103
  }
13013
- get name() {
13014
- return this.options.name;
14104
+ constructor(buckets) {
14105
+ this.buckets = buckets;
13015
14106
  }
13016
- get columns() {
13017
- return this.options.columns ?? [];
14107
+ }
14108
+
14109
+ /**
14110
+ * Thrown when an underlying database connection is closed.
14111
+ * This is particularly relevant when worker connections are marked as closed while
14112
+ * operations are still in progress.
14113
+ */
14114
+ class ConnectionClosedError extends Error {
14115
+ static NAME = 'ConnectionClosedError';
14116
+ static MATCHES(input) {
14117
+ /**
14118
+ * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
14119
+ * check might fail. This also performs a failsafe check.
14120
+ * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
14121
+ */
14122
+ return (input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME));
13018
14123
  }
13019
- toJSON(table) {
13020
- return {
13021
- name: this.name,
13022
- columns: this.columns.map((c) => c.toJSON(table))
13023
- };
14124
+ constructor(message) {
14125
+ super(message);
14126
+ this.name = ConnectionClosedError.NAME;
13024
14127
  }
13025
14128
  }
13026
14129
 
@@ -13117,211 +14220,6 @@ class Schema {
13117
14220
  }
13118
14221
  }
13119
14222
 
13120
- const DEFAULT_TABLE_OPTIONS = {
13121
- indexes: [],
13122
- insertOnly: false,
13123
- localOnly: false,
13124
- trackPrevious: false,
13125
- trackMetadata: false,
13126
- ignoreEmptyUpdates: false
13127
- };
13128
- const InvalidSQLCharacters = /["'%,.#\s[\]]/;
13129
- class Table {
13130
- options;
13131
- _mappedColumns;
13132
- static createLocalOnly(options) {
13133
- return new Table({ ...options, localOnly: true, insertOnly: false });
13134
- }
13135
- static createInsertOnly(options) {
13136
- return new Table({ ...options, localOnly: false, insertOnly: true });
13137
- }
13138
- /**
13139
- * Create a table.
13140
- * @deprecated This was only only included for TableV2 and is no longer necessary.
13141
- * Prefer to use new Table() directly.
13142
- *
13143
- * TODO remove in the next major release.
13144
- */
13145
- static createTable(name, table) {
13146
- return new Table({
13147
- name,
13148
- columns: table.columns,
13149
- indexes: table.indexes,
13150
- localOnly: table.options.localOnly,
13151
- insertOnly: table.options.insertOnly,
13152
- viewName: table.options.viewName
13153
- });
13154
- }
13155
- constructor(optionsOrColumns, v2Options) {
13156
- if (this.isTableV1(optionsOrColumns)) {
13157
- this.initTableV1(optionsOrColumns);
13158
- }
13159
- else {
13160
- this.initTableV2(optionsOrColumns, v2Options);
13161
- }
13162
- }
13163
- copyWithName(name) {
13164
- return new Table({
13165
- ...this.options,
13166
- name
13167
- });
13168
- }
13169
- isTableV1(arg) {
13170
- return 'columns' in arg && Array.isArray(arg.columns);
13171
- }
13172
- initTableV1(options) {
13173
- this.options = {
13174
- ...options,
13175
- indexes: options.indexes || []
13176
- };
13177
- this.applyDefaultOptions();
13178
- }
13179
- initTableV2(columns, options) {
13180
- const convertedColumns = Object.entries(columns).map(([name, columnInfo]) => new Column({ name, type: columnInfo.type }));
13181
- const convertedIndexes = Object.entries(options?.indexes ?? {}).map(([name, columnNames]) => new Index({
13182
- name,
13183
- columns: columnNames.map((name) => new IndexedColumn({
13184
- name: name.replace(/^-/, ''),
13185
- ascending: !name.startsWith('-')
13186
- }))
13187
- }));
13188
- this.options = {
13189
- name: '',
13190
- columns: convertedColumns,
13191
- indexes: convertedIndexes,
13192
- viewName: options?.viewName,
13193
- insertOnly: options?.insertOnly,
13194
- localOnly: options?.localOnly,
13195
- trackPrevious: options?.trackPrevious,
13196
- trackMetadata: options?.trackMetadata,
13197
- ignoreEmptyUpdates: options?.ignoreEmptyUpdates
13198
- };
13199
- this.applyDefaultOptions();
13200
- this._mappedColumns = columns;
13201
- }
13202
- applyDefaultOptions() {
13203
- this.options.insertOnly ??= DEFAULT_TABLE_OPTIONS.insertOnly;
13204
- this.options.localOnly ??= DEFAULT_TABLE_OPTIONS.localOnly;
13205
- this.options.trackPrevious ??= DEFAULT_TABLE_OPTIONS.trackPrevious;
13206
- this.options.trackMetadata ??= DEFAULT_TABLE_OPTIONS.trackMetadata;
13207
- this.options.ignoreEmptyUpdates ??= DEFAULT_TABLE_OPTIONS.ignoreEmptyUpdates;
13208
- }
13209
- get name() {
13210
- return this.options.name;
13211
- }
13212
- get viewNameOverride() {
13213
- return this.options.viewName;
13214
- }
13215
- get viewName() {
13216
- return this.viewNameOverride ?? this.name;
13217
- }
13218
- get columns() {
13219
- return this.options.columns;
13220
- }
13221
- get columnMap() {
13222
- return (this._mappedColumns ??
13223
- this.columns.reduce((hash, column) => {
13224
- hash[column.name] = { type: column.type ?? ColumnType.TEXT };
13225
- return hash;
13226
- }, {}));
13227
- }
13228
- get indexes() {
13229
- return this.options.indexes ?? [];
13230
- }
13231
- get localOnly() {
13232
- return this.options.localOnly;
13233
- }
13234
- get insertOnly() {
13235
- return this.options.insertOnly;
13236
- }
13237
- get trackPrevious() {
13238
- return this.options.trackPrevious;
13239
- }
13240
- get trackMetadata() {
13241
- return this.options.trackMetadata;
13242
- }
13243
- get ignoreEmptyUpdates() {
13244
- return this.options.ignoreEmptyUpdates;
13245
- }
13246
- get internalName() {
13247
- if (this.options.localOnly) {
13248
- return `ps_data_local__${this.name}`;
13249
- }
13250
- return `ps_data__${this.name}`;
13251
- }
13252
- get validName() {
13253
- if (InvalidSQLCharacters.test(this.name)) {
13254
- return false;
13255
- }
13256
- if (this.viewNameOverride != null && InvalidSQLCharacters.test(this.viewNameOverride)) {
13257
- return false;
13258
- }
13259
- return true;
13260
- }
13261
- validate() {
13262
- if (InvalidSQLCharacters.test(this.name)) {
13263
- throw new Error(`Invalid characters in table name: ${this.name}`);
13264
- }
13265
- if (this.viewNameOverride && InvalidSQLCharacters.test(this.viewNameOverride)) {
13266
- throw new Error(`Invalid characters in view name: ${this.viewNameOverride}`);
13267
- }
13268
- if (this.columns.length > MAX_AMOUNT_OF_COLUMNS) {
13269
- throw new Error(`Table has too many columns. The maximum number of columns is ${MAX_AMOUNT_OF_COLUMNS}.`);
13270
- }
13271
- if (this.trackMetadata && this.localOnly) {
13272
- throw new Error(`Can't include metadata for local-only tables.`);
13273
- }
13274
- if (this.trackPrevious != false && this.localOnly) {
13275
- throw new Error(`Can't include old values for local-only tables.`);
13276
- }
13277
- const columnNames = new Set();
13278
- columnNames.add('id');
13279
- for (const column of this.columns) {
13280
- const { name: columnName } = column;
13281
- if (column.name === 'id') {
13282
- throw new Error(`An id column is automatically added, custom id columns are not supported`);
13283
- }
13284
- if (columnNames.has(columnName)) {
13285
- throw new Error(`Duplicate column ${columnName}`);
13286
- }
13287
- if (InvalidSQLCharacters.test(columnName)) {
13288
- throw new Error(`Invalid characters in column name: ${column.name}`);
13289
- }
13290
- columnNames.add(columnName);
13291
- }
13292
- const indexNames = new Set();
13293
- for (const index of this.indexes) {
13294
- if (indexNames.has(index.name)) {
13295
- throw new Error(`Duplicate index ${index.name}`);
13296
- }
13297
- if (InvalidSQLCharacters.test(index.name)) {
13298
- throw new Error(`Invalid characters in index name: ${index.name}`);
13299
- }
13300
- for (const column of index.columns) {
13301
- if (!columnNames.has(column.name)) {
13302
- throw new Error(`Column ${column.name} not found for index ${index.name}`);
13303
- }
13304
- }
13305
- indexNames.add(index.name);
13306
- }
13307
- }
13308
- toJSON() {
13309
- const trackPrevious = this.trackPrevious;
13310
- return {
13311
- name: this.name,
13312
- view_name: this.viewName,
13313
- local_only: this.localOnly,
13314
- insert_only: this.insertOnly,
13315
- include_old: trackPrevious && (trackPrevious.columns ?? true),
13316
- include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
13317
- include_metadata: this.trackMetadata,
13318
- ignore_empty_update: this.ignoreEmptyUpdates,
13319
- columns: this.columns.map((c) => c.toJSON()),
13320
- indexes: this.indexes.map((e) => e.toJSON(this))
13321
- };
13322
- }
13323
- }
13324
-
13325
14223
  /**
13326
14224
  Generate a new table from the columns and indexes
13327
14225
  @deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
@@ -13477,5 +14375,5 @@ const parseQuery = (query, parameters) => {
13477
14375
  return { sqlStatement, parameters: parameters };
13478
14376
  };
13479
14377
 
13480
- export { AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_PRESSURE_LIMITS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DataStream, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, MEMORY_TRIGGER_CLAIM_MANAGER, OnChangeQueryProcessor, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RawTable, RowUpdateType, Schema, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID };
14378
+ export { ATTACHMENT_TABLE, AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, AttachmentContext, AttachmentQueue, AttachmentService, AttachmentState, AttachmentTable, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_PRESSURE_LIMITS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DataStream, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, EncodingType, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, MEMORY_TRIGGER_CLAIM_MANAGER, OnChangeQueryProcessor, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RawTable, RowUpdateType, Schema, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, SyncingService, Table, TableV2, TriggerManagerImpl, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, attachmentFromSql, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, mutexRunExclusive, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID };
13481
14379
  //# sourceMappingURL=bundle.mjs.map