@powersync/common 0.0.0-dev-20250711131120 → 0.0.0-dev-20250714151300

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 (32) hide show
  1. package/dist/bundle.cjs +5 -5
  2. package/dist/bundle.mjs +3 -3
  3. package/lib/client/AbstractPowerSyncDatabase.d.ts +59 -12
  4. package/lib/client/AbstractPowerSyncDatabase.js +105 -47
  5. package/lib/client/CustomQuery.d.ts +25 -0
  6. package/lib/client/CustomQuery.js +41 -0
  7. package/lib/client/Query.d.ts +79 -0
  8. package/lib/client/Query.js +1 -0
  9. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +2 -2
  10. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +1 -3
  11. package/lib/client/sync/bucket/SqliteBucketStorage.js +1 -3
  12. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +3 -2
  13. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +16 -12
  14. package/lib/client/watched/GetAllQuery.d.ts +32 -0
  15. package/lib/client/watched/GetAllQuery.js +24 -0
  16. package/lib/client/watched/WatchedQuery.d.ts +93 -0
  17. package/lib/client/watched/WatchedQuery.js +12 -0
  18. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +67 -0
  19. package/lib/client/watched/processors/AbstractQueryProcessor.js +136 -0
  20. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +129 -0
  21. package/lib/client/watched/processors/DifferentialQueryProcessor.js +175 -0
  22. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +27 -0
  23. package/lib/client/watched/processors/OnChangeQueryProcessor.js +74 -0
  24. package/lib/client/watched/processors/comparators.d.ts +24 -0
  25. package/lib/client/watched/processors/comparators.js +33 -0
  26. package/lib/index.d.ts +7 -0
  27. package/lib/index.js +7 -0
  28. package/lib/utils/BaseObserver.d.ts +3 -4
  29. package/lib/utils/BaseObserver.js +3 -0
  30. package/lib/utils/MetaBaseObserver.d.ts +29 -0
  31. package/lib/utils/MetaBaseObserver.js +50 -0
  32. package/package.json +1 -1
@@ -1,17 +1,20 @@
1
1
  import { Mutex } from 'async-mutex';
2
- import { ILogger } from 'js-logger';
2
+ import Logger, { ILogger } from 'js-logger';
3
3
  import { DBAdapter, QueryResult, Transaction } from '../db/DBAdapter.js';
4
4
  import { SyncStatus } from '../db/crud/SyncStatus.js';
5
5
  import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
6
6
  import { Schema } from '../db/schema/Schema.js';
7
7
  import { BaseObserver } from '../utils/BaseObserver.js';
8
8
  import { ConnectionManager } from './ConnectionManager.js';
9
+ import { ArrayQueryDefinition, Query } from './Query.js';
9
10
  import { SQLOpenFactory, SQLOpenOptions } from './SQLOpenFactory.js';
10
11
  import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
11
12
  import { BucketStorageAdapter } from './sync/bucket/BucketStorageAdapter.js';
12
13
  import { CrudBatch } from './sync/bucket/CrudBatch.js';
13
14
  import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
14
15
  import { StreamingSyncImplementation, StreamingSyncImplementationListener, type AdditionalConnectionOptions, type PowerSyncConnectionOptions, type RequiredAdditionalConnectionOptions } from './sync/stream/AbstractStreamingSyncImplementation.js';
16
+ import { WatchCompatibleQuery } from './watched/WatchedQuery.js';
17
+ import { WatchedQueryComparator } from './watched/processors/comparators.js';
15
18
  export interface DisconnectAndClearOptions {
16
19
  /** When set to false, data in local-only tables is preserved. */
17
20
  clearLocal?: boolean;
@@ -44,7 +47,7 @@ export interface PowerSyncDatabaseOptionsWithOpenFactory extends BasePowerSyncDa
44
47
  export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatabaseOptions {
45
48
  database: SQLOpenOptions;
46
49
  }
47
- export interface SQLWatchOptions {
50
+ export interface SQLOnChangeOptions {
48
51
  signal?: AbortSignal;
49
52
  tables?: string[];
50
53
  /** The minimum interval between queries. */
@@ -56,6 +59,17 @@ export interface SQLWatchOptions {
56
59
  * by not removing PowerSync table name prefixes
57
60
  */
58
61
  rawTableNames?: boolean;
62
+ /**
63
+ * Emits an empty result set immediately
64
+ */
65
+ triggerImmediate?: boolean;
66
+ }
67
+ export interface SQLWatchOptions extends SQLOnChangeOptions {
68
+ /**
69
+ * Optional comparator which will be used to compare the results of the query.
70
+ * The watched query will only yield results if the comparator returns false.
71
+ */
72
+ comparator?: WatchedQueryComparator<QueryResult>;
59
73
  }
60
74
  export interface WatchOnChangeEvent {
61
75
  changedTables: string[];
@@ -71,6 +85,8 @@ export interface WatchOnChangeHandler {
71
85
  export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
72
86
  initialized: () => void;
73
87
  schemaChanged: (schema: Schema) => void;
88
+ closing: () => Promise<void> | void;
89
+ closed: () => Promise<void> | void;
74
90
  }
75
91
  export interface PowerSyncCloseOptions {
76
92
  /**
@@ -81,9 +97,9 @@ export interface PowerSyncCloseOptions {
81
97
  disconnect?: boolean;
82
98
  }
83
99
  export declare const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions;
84
- export declare const DEFAULT_WATCH_THROTTLE_MS = 30;
85
100
  export declare const DEFAULT_POWERSYNC_DB_OPTIONS: {
86
101
  retryDelayMs: number;
102
+ logger: Logger.ILogger;
87
103
  crudUploadThrottleMs: number;
88
104
  };
89
105
  export declare const DEFAULT_CRUD_BATCH_LIMIT = 100;
@@ -100,11 +116,6 @@ export declare const DEFAULT_LOCK_TIMEOUT_MS = 120000;
100
116
  export declare const isPowerSyncDatabaseOptionsWithSettings: (test: any) => test is PowerSyncDatabaseOptionsWithSettings;
101
117
  export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDBListener> {
102
118
  protected options: PowerSyncDatabaseOptions;
103
- /**
104
- * Transactions should be queued in the DBAdapter, but we also want to prevent
105
- * calls to `.execute` while an async transaction is running.
106
- */
107
- protected static transactionMutex: Mutex;
108
119
  /**
109
120
  * Returns true if the connection is closed.
110
121
  */
@@ -122,7 +133,6 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
122
133
  protected _schema: Schema;
123
134
  private _database;
124
135
  protected runExclusiveMutex: Mutex;
125
- logger: ILogger;
126
136
  constructor(options: PowerSyncDatabaseOptionsWithDBAdapter);
127
137
  constructor(options: PowerSyncDatabaseOptionsWithOpenFactory);
128
138
  constructor(options: PowerSyncDatabaseOptionsWithSettings);
@@ -185,6 +195,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
185
195
  * Cannot be used while connected - this should only be called before {@link AbstractPowerSyncDatabase.connect}.
186
196
  */
187
197
  updateSchema(schema: Schema): Promise<void>;
198
+ get logger(): Logger.ILogger;
188
199
  /**
189
200
  * Wait for initialization to complete.
190
201
  * While initializing is automatic, this helps to catch and report initialization errors.
@@ -394,6 +405,42 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
394
405
  * ```
395
406
  */
396
407
  watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
408
+ /**
409
+ * Allows defining a query which can be used to build a {@link WatchedQuery}.
410
+ * The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
411
+ * An optional mapper function can be provided to transform the results.
412
+ *
413
+ * @example
414
+ * ```javascript
415
+ * const watchedTodos = powersync.query({
416
+ * sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
417
+ * parameters: [],
418
+ * mapper: (row) => ({
419
+ * ...row,
420
+ * created_at: new Date(row.created_at as string)
421
+ * })
422
+ * })
423
+ * .watch()
424
+ * // OR use .differentialWatch() for fine-grained watches.
425
+ * ```
426
+ */
427
+ query<RowType>(query: ArrayQueryDefinition<RowType>): Query<RowType>;
428
+ /**
429
+ * Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
430
+ * The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
431
+ *
432
+ * @example
433
+ * ```javascript
434
+ *
435
+ * // Potentially a query from an ORM like Drizzle
436
+ * const query = db.select().from(lists);
437
+ *
438
+ * const watchedTodos = powersync.customQuery(query)
439
+ * .watch()
440
+ * // OR use .differentialWatch() for fine-grained watches.
441
+ * ```
442
+ */
443
+ customQuery<RowType>(query: WatchCompatibleQuery<RowType[]>): Query<RowType>;
397
444
  /**
398
445
  * Execute a read query every time the source tables are modified.
399
446
  * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
@@ -442,7 +489,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
442
489
  * }
443
490
  * ```
444
491
  */
445
- onChange(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent>;
492
+ onChange(options?: SQLOnChangeOptions): AsyncIterable<WatchOnChangeEvent>;
446
493
  /**
447
494
  * See {@link onChangeWithCallback}.
448
495
  *
@@ -457,7 +504,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
457
504
  * }
458
505
  * ```
459
506
  */
460
- onChange(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
507
+ onChange(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
461
508
  /**
462
509
  * Invoke the provided callback on any changes to any of the specified tables.
463
510
  *
@@ -470,7 +517,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
470
517
  * @param options Options for configuring watch behavior
471
518
  * @returns A dispose function to stop watching for changes
472
519
  */
473
- onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
520
+ onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
474
521
  /**
475
522
  * Create a Stream of changes to any of the specified tables.
476
523
  *
@@ -8,15 +8,16 @@ import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
8
8
  import { BaseObserver } from '../utils/BaseObserver.js';
9
9
  import { ControlledExecutor } from '../utils/ControlledExecutor.js';
10
10
  import { throttleTrailing } from '../utils/async.js';
11
- import { mutexRunExclusive } from '../utils/mutex.js';
12
11
  import { ConnectionManager } from './ConnectionManager.js';
12
+ import { CustomQuery } from './CustomQuery.js';
13
13
  import { isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory.js';
14
- import { runOnSchemaChange } from './runOnSchemaChange.js';
15
14
  import { PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
16
15
  import { CrudBatch } from './sync/bucket/CrudBatch.js';
17
16
  import { CrudEntry } from './sync/bucket/CrudEntry.js';
18
17
  import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
19
18
  import { DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_RETRY_DELAY_MS } from './sync/stream/AbstractStreamingSyncImplementation.js';
19
+ import { DEFAULT_WATCH_THROTTLE_MS } from './watched/WatchedQuery.js';
20
+ import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
20
21
  const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
21
22
  const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
22
23
  clearLocal: true
@@ -24,9 +25,9 @@ const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
24
25
  export const DEFAULT_POWERSYNC_CLOSE_OPTIONS = {
25
26
  disconnect: true
26
27
  };
27
- export const DEFAULT_WATCH_THROTTLE_MS = 30;
28
28
  export const DEFAULT_POWERSYNC_DB_OPTIONS = {
29
29
  retryDelayMs: 5000,
30
+ logger: Logger.get('PowerSyncDatabase'),
30
31
  crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
31
32
  };
32
33
  export const DEFAULT_CRUD_BATCH_LIMIT = 100;
@@ -45,11 +46,6 @@ export const isPowerSyncDatabaseOptionsWithSettings = (test) => {
45
46
  };
46
47
  export class AbstractPowerSyncDatabase extends BaseObserver {
47
48
  options;
48
- /**
49
- * Transactions should be queued in the DBAdapter, but we also want to prevent
50
- * calls to `.execute` while an async transaction is running.
51
- */
52
- static transactionMutex = new Mutex();
53
49
  /**
54
50
  * Returns true if the connection is closed.
55
51
  */
@@ -69,7 +65,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
69
65
  _schema;
70
66
  _database;
71
67
  runExclusiveMutex;
72
- logger;
73
68
  constructor(options) {
74
69
  super();
75
70
  this.options = options;
@@ -89,7 +84,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
89
84
  else {
90
85
  throw new Error('The provided `database` option is invalid.');
91
86
  }
92
- this.logger = options.logger ?? Logger.get(`PowerSyncDatabase[${this._database.name}]`);
93
87
  this.bucketStorageAdapter = this.generateBucketStorageAdapter();
94
88
  this.closed = false;
95
89
  this.currentStatus = new SyncStatus({});
@@ -269,13 +263,16 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
269
263
  schema.validate();
270
264
  }
271
265
  catch (ex) {
272
- this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
266
+ this.options.logger?.warn('Schema validation failed. Unexpected behaviour could occur', ex);
273
267
  }
274
268
  this._schema = schema;
275
269
  await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
276
270
  await this.database.refreshSchema();
277
271
  this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
278
272
  }
273
+ get logger() {
274
+ return this.options.logger;
275
+ }
279
276
  /**
280
277
  * Wait for initialization to complete.
281
278
  * While initializing is automatic, this helps to catch and report initialization errors.
@@ -345,6 +342,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
345
342
  if (this.closed) {
346
343
  return;
347
344
  }
345
+ await this.iterateAsyncListeners(async (cb) => cb.closing?.());
348
346
  const { disconnect } = options;
349
347
  if (disconnect) {
350
348
  await this.disconnect();
@@ -352,6 +350,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
352
350
  await this.connectionManager.close();
353
351
  await this.database.close();
354
352
  this.closed = true;
353
+ await this.iterateAsyncListeners(async (cb) => cb.closed?.());
355
354
  }
356
355
  /**
357
356
  * Get upload queue size estimate and count.
@@ -475,8 +474,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
475
474
  * @returns The query result as an object with structured key-value pairs
476
475
  */
477
476
  async execute(sql, parameters) {
478
- await this.waitForReady();
479
- return this.database.execute(sql, parameters);
477
+ return this.writeLock((tx) => tx.execute(sql, parameters));
480
478
  }
481
479
  /**
482
480
  * Execute a SQL write (INSERT/UPDATE/DELETE) query directly on the database without any PowerSync processing.
@@ -544,7 +542,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
544
542
  */
545
543
  async readLock(callback) {
546
544
  await this.waitForReady();
547
- return mutexRunExclusive(AbstractPowerSyncDatabase.transactionMutex, () => callback(this.database));
545
+ return this.database.readLock(callback);
548
546
  }
549
547
  /**
550
548
  * Takes a global lock, without starting a transaction.
@@ -552,10 +550,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
552
550
  */
553
551
  async writeLock(callback) {
554
552
  await this.waitForReady();
555
- return mutexRunExclusive(AbstractPowerSyncDatabase.transactionMutex, async () => {
556
- const res = await callback(this.database);
557
- return res;
558
- });
553
+ return this.database.writeLock(callback);
559
554
  }
560
555
  /**
561
556
  * Open a read-only transaction.
@@ -602,6 +597,60 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
602
597
  const options = handlerOrOptions;
603
598
  return this.watchWithAsyncGenerator(sql, parameters, options);
604
599
  }
600
+ /**
601
+ * Allows defining a query which can be used to build a {@link WatchedQuery}.
602
+ * The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
603
+ * An optional mapper function can be provided to transform the results.
604
+ *
605
+ * @example
606
+ * ```javascript
607
+ * const watchedTodos = powersync.query({
608
+ * sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
609
+ * parameters: [],
610
+ * mapper: (row) => ({
611
+ * ...row,
612
+ * created_at: new Date(row.created_at as string)
613
+ * })
614
+ * })
615
+ * .watch()
616
+ * // OR use .differentialWatch() for fine-grained watches.
617
+ * ```
618
+ */
619
+ query(query) {
620
+ const { sql, parameters = [], mapper } = query;
621
+ const compatibleQuery = {
622
+ compile: () => ({
623
+ sql,
624
+ parameters
625
+ }),
626
+ execute: async ({ sql, parameters }) => {
627
+ const result = await this.getAll(sql, parameters);
628
+ return mapper ? result.map(mapper) : result;
629
+ }
630
+ };
631
+ return this.customQuery(compatibleQuery);
632
+ }
633
+ /**
634
+ * Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
635
+ * The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
636
+ *
637
+ * @example
638
+ * ```javascript
639
+ *
640
+ * // Potentially a query from an ORM like Drizzle
641
+ * const query = db.select().from(lists);
642
+ *
643
+ * const watchedTodos = powersync.customQuery(query)
644
+ * .watch()
645
+ * // OR use .differentialWatch() for fine-grained watches.
646
+ * ```
647
+ */
648
+ customQuery(query) {
649
+ return new CustomQuery({
650
+ db: this,
651
+ query
652
+ });
653
+ }
605
654
  /**
606
655
  * Execute a read query every time the source tables are modified.
607
656
  * Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
@@ -615,39 +664,45 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
615
664
  * @param options Options for configuring watch behavior
616
665
  */
617
666
  watchWithCallback(sql, parameters, handler, options) {
618
- const { onResult, onError = (e) => this.logger.error(e) } = handler ?? {};
667
+ const { onResult, onError = (e) => this.options.logger?.error(e) } = handler ?? {};
619
668
  if (!onResult) {
620
669
  throw new Error('onResult is required');
621
670
  }
622
- const watchQuery = async (abortSignal) => {
623
- try {
624
- const resolvedTables = await this.resolveTables(sql, parameters, options);
625
- // Fetch initial data
626
- const result = await this.executeReadOnly(sql, parameters);
627
- onResult(result);
628
- this.onChangeWithCallback({
629
- onChange: async () => {
630
- try {
631
- const result = await this.executeReadOnly(sql, parameters);
632
- onResult(result);
633
- }
634
- catch (error) {
635
- onError?.(error);
636
- }
637
- },
638
- onError
639
- }, {
640
- ...(options ?? {}),
641
- tables: resolvedTables,
642
- // Override the abort signal since we intercept it
643
- signal: abortSignal
644
- });
671
+ const { comparator } = options ?? {};
672
+ // This API yields a QueryResult type.
673
+ // This is not a standard Array result, which makes it incompatible with the .query API.
674
+ const watchedQuery = new OnChangeQueryProcessor({
675
+ db: this,
676
+ comparator,
677
+ placeholderData: null,
678
+ watchOptions: {
679
+ query: {
680
+ compile: () => ({
681
+ sql: sql,
682
+ parameters: parameters ?? []
683
+ }),
684
+ execute: () => this.executeReadOnly(sql, parameters)
685
+ },
686
+ reportFetching: false,
687
+ throttleMs: options?.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS
645
688
  }
646
- catch (error) {
647
- onError?.(error);
689
+ });
690
+ const dispose = watchedQuery.registerListener({
691
+ onData: (data) => {
692
+ if (!data) {
693
+ // This should not happen. We only use null for the initial data.
694
+ return;
695
+ }
696
+ onResult(data);
697
+ },
698
+ onError: (error) => {
699
+ onError(error);
648
700
  }
649
- };
650
- runOnSchemaChange(watchQuery, this, options);
701
+ });
702
+ options?.signal?.addEventListener('abort', () => {
703
+ dispose();
704
+ watchedQuery.close();
705
+ });
651
706
  }
652
707
  /**
653
708
  * Execute a read query every time the source tables are modified.
@@ -721,7 +776,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
721
776
  * @returns A dispose function to stop watching for changes
722
777
  */
723
778
  onChangeWithCallback(handler, options) {
724
- const { onChange, onError = (e) => this.logger.error(e) } = handler ?? {};
779
+ const { onChange, onError = (e) => this.options.logger?.error(e) } = handler ?? {};
725
780
  if (!onChange) {
726
781
  throw new Error('onChange is required');
727
782
  }
@@ -737,6 +792,9 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
737
792
  return;
738
793
  executor.schedule({ changedTables: intersection });
739
794
  }), throttleMs);
795
+ if (options?.triggerImmediate) {
796
+ executor.schedule({ changedTables: [] });
797
+ }
740
798
  const dispose = this.database.registerListener({
741
799
  tablesUpdated: async (update) => {
742
800
  try {
@@ -0,0 +1,25 @@
1
+ import { AbstractPowerSyncDatabase } from './AbstractPowerSyncDatabase.js';
2
+ import { Query, StandardWatchedQueryOptions } from './Query.js';
3
+ import { DifferentialQueryProcessor, DifferentialWatchedQueryOptions } from './watched/processors/DifferentialQueryProcessor.js';
4
+ import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
5
+ import { WatchCompatibleQuery, WatchedQueryOptions } from './watched/WatchedQuery.js';
6
+ /**
7
+ * @internal
8
+ */
9
+ export interface CustomQueryOptions<RowType> {
10
+ db: AbstractPowerSyncDatabase;
11
+ query: WatchCompatibleQuery<RowType[]>;
12
+ }
13
+ /**
14
+ * @internal
15
+ */
16
+ export declare class CustomQuery<RowType> implements Query<RowType> {
17
+ protected options: CustomQueryOptions<RowType>;
18
+ constructor(options: CustomQueryOptions<RowType>);
19
+ protected resolveOptions(options: WatchedQueryOptions): {
20
+ reportFetching: boolean | undefined;
21
+ throttleMs: number | undefined;
22
+ };
23
+ watch(watchOptions: StandardWatchedQueryOptions<RowType>): OnChangeQueryProcessor<RowType[]>;
24
+ differentialWatch(differentialWatchOptions: DifferentialWatchedQueryOptions<RowType>): DifferentialQueryProcessor<RowType>;
25
+ }
@@ -0,0 +1,41 @@
1
+ import { FalsyComparator } from './watched/processors/comparators.js';
2
+ import { DifferentialQueryProcessor } from './watched/processors/DifferentialQueryProcessor.js';
3
+ import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
4
+ import { DEFAULT_WATCH_QUERY_OPTIONS } from './watched/WatchedQuery.js';
5
+ /**
6
+ * @internal
7
+ */
8
+ export class CustomQuery {
9
+ options;
10
+ constructor(options) {
11
+ this.options = options;
12
+ }
13
+ resolveOptions(options) {
14
+ return {
15
+ reportFetching: options?.reportFetching ?? DEFAULT_WATCH_QUERY_OPTIONS.reportFetching,
16
+ throttleMs: options?.throttleMs ?? DEFAULT_WATCH_QUERY_OPTIONS.throttleMs
17
+ };
18
+ }
19
+ watch(watchOptions) {
20
+ return new OnChangeQueryProcessor({
21
+ db: this.options.db,
22
+ comparator: watchOptions?.comparator ?? FalsyComparator,
23
+ placeholderData: watchOptions?.placeholderData ?? [],
24
+ watchOptions: {
25
+ ...this.resolveOptions(watchOptions),
26
+ query: this.options.query
27
+ }
28
+ });
29
+ }
30
+ differentialWatch(differentialWatchOptions) {
31
+ return new DifferentialQueryProcessor({
32
+ db: this.options.db,
33
+ differentiator: differentialWatchOptions?.differentiator,
34
+ placeholderData: differentialWatchOptions?.placeholderData ?? [],
35
+ watchOptions: {
36
+ ...this.resolveOptions(differentialWatchOptions),
37
+ query: this.options.query
38
+ }
39
+ });
40
+ }
41
+ }
@@ -0,0 +1,79 @@
1
+ import { ArrayComparator } from './watched/processors/comparators.js';
2
+ import { DifferentialWatchedQuery, DifferentialWatchedQueryOptions } from './watched/processors/DifferentialQueryProcessor.js';
3
+ import { ComparisonWatchedQuery } from './watched/processors/OnChangeQueryProcessor.js';
4
+ import { WatchedQueryOptions } from './watched/WatchedQuery.js';
5
+ /**
6
+ * Query parameters for {@link ArrayQueryDefinition#parameters}
7
+ */
8
+ export type QueryParam = string | number | boolean | null | undefined | bigint | Uint8Array;
9
+ /**
10
+ * Options for building a query with {@link AbstractPowerSyncDatabase#query}.
11
+ * This query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
12
+ */
13
+ export interface ArrayQueryDefinition<RowType = unknown> {
14
+ sql: string;
15
+ parameters?: ReadonlyArray<Readonly<QueryParam>>;
16
+ /**
17
+ * Maps the raw SQLite row to a custom typed object.
18
+ * @example
19
+ * ```javascript
20
+ * mapper: (row) => ({
21
+ * ...row,
22
+ * created_at: new Date(row.created_at as string),
23
+ * })
24
+ * ```
25
+ */
26
+ mapper?: (row: Record<string, unknown>) => RowType;
27
+ }
28
+ /**
29
+ * Options for {@link Query.watch}.
30
+ */
31
+ export interface StandardWatchedQueryOptions<RowType> extends WatchedQueryOptions {
32
+ /**
33
+ * Optional comparator which processes the items of an array of rows.
34
+ * The comparator compares the result set rows by index using the {@link ArrayComparatorOptions#compareBy} function.
35
+ * The comparator reports a changed result set as soon as a row does not match the previous result set.
36
+ *
37
+ * @example
38
+ * ```javascript
39
+ * comparator: new ArrayComparator({
40
+ * compareBy: (item) => JSON.stringify(item)
41
+ * })
42
+ * ```
43
+ */
44
+ comparator?: ArrayComparator<RowType>;
45
+ /**
46
+ * The initial data state reported while the query is loading for the first time.
47
+ * @default []
48
+ */
49
+ placeholderData?: RowType[];
50
+ }
51
+ export interface Query<RowType> {
52
+ /**
53
+ * Creates a {@link WatchedQuery} which watches and emits results of the linked query.
54
+ *
55
+ * By default the returned watched query will emit changes whenever a change to the underlying SQLite tables is made.
56
+ * These changes might not be relevant to the query, but the query will emit a new result set.
57
+ *
58
+ * A {@link StandardWatchedQueryOptions#comparator} can be provided to limit the data emissions. The watched query will still
59
+ * query the underlying DB on underlying table changes, but the result will only be emitted if the comparator detects a change in the results.
60
+ *
61
+ * The comparator in this method is optimized and returns early as soon as it detects a change. Each data emission will correlate to a change in the result set,
62
+ * but note that the result set will not maintain internal object references to the previous result set. If internal object references are needed,
63
+ * consider using {@link Query#differentialWatch} instead.
64
+ */
65
+ watch(options?: StandardWatchedQueryOptions<RowType>): ComparisonWatchedQuery<ReadonlyArray<Readonly<RowType>>>;
66
+ /**
67
+ * Creates a {@link WatchedQuery} which watches and emits results of the linked query.
68
+ *
69
+ * This query method watches for changes in the underlying SQLite tables and runs the query on each table change.
70
+ * The difference between the current and previous result set is computed.
71
+ * The watched query will not emit changes if the result set is identical to the previous result set.
72
+ * If the result set is different, the watched query will emit the new result set and provide a detailed diff of the changes.
73
+ *
74
+ * The deep differentiation allows maintaining result set object references between result emissions.
75
+ * The {@link DifferentialWatchedQuery#state} `data` array will contain the previous row references for unchanged rows.
76
+ * A detailed diff of the changes can be accessed via {@link DifferentialWatchedQuery#state} `diff`.
77
+ */
78
+ differentialWatch(options?: DifferentialWatchedQueryOptions<RowType>): DifferentialWatchedQuery<RowType>;
79
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { BaseListener, BaseObserver, Disposable } from '../../../utils/BaseObserver.js';
1
+ import { BaseListener, BaseObserverInterface, Disposable } from '../../../utils/BaseObserver.js';
2
2
  import { CrudBatch } from './CrudBatch.js';
3
3
  import { CrudEntry, OpId } from './CrudEntry.js';
4
4
  import { SyncDataBatch } from './SyncDataBatch.js';
@@ -62,7 +62,7 @@ export declare enum PowerSyncControlCommand {
62
62
  export interface BucketStorageListener extends BaseListener {
63
63
  crudUpdate: () => void;
64
64
  }
65
- export interface BucketStorageAdapter extends BaseObserver<BucketStorageListener>, Disposable {
65
+ export interface BucketStorageAdapter extends BaseObserverInterface<BucketStorageListener>, Disposable {
66
66
  init(): Promise<void>;
67
67
  saveSyncData(batch: SyncDataBatch, fixedKeyFormat?: boolean): Promise<void>;
68
68
  removeBuckets(buckets: string[]): Promise<void>;
@@ -1,4 +1,3 @@
1
- import { Mutex } from 'async-mutex';
2
1
  import { ILogger } from 'js-logger';
3
2
  import { DBAdapter, Transaction } from '../../../db/DBAdapter.js';
4
3
  import { BaseObserver } from '../../../utils/BaseObserver.js';
@@ -8,13 +7,12 @@ import { CrudEntry } from './CrudEntry.js';
8
7
  import { SyncDataBatch } from './SyncDataBatch.js';
9
8
  export declare class SqliteBucketStorage extends BaseObserver<BucketStorageListener> implements BucketStorageAdapter {
10
9
  private db;
11
- private mutex;
12
10
  private logger;
13
11
  tableNames: Set<string>;
14
12
  private _hasCompletedSync;
15
13
  private updateListener;
16
14
  private _clientId?;
17
- constructor(db: DBAdapter, mutex: Mutex, logger?: ILogger);
15
+ constructor(db: DBAdapter, logger?: ILogger);
18
16
  init(): Promise<void>;
19
17
  dispose(): Promise<void>;
20
18
  _getClientId(): Promise<string>;
@@ -6,16 +6,14 @@ import { PSInternalTable } from './BucketStorageAdapter.js';
6
6
  import { CrudEntry } from './CrudEntry.js';
7
7
  export class SqliteBucketStorage extends BaseObserver {
8
8
  db;
9
- mutex;
10
9
  logger;
11
10
  tableNames;
12
11
  _hasCompletedSync;
13
12
  updateListener;
14
13
  _clientId;
15
- constructor(db, mutex, logger = Logger.get('SqliteBucketStorage')) {
14
+ constructor(db, logger = Logger.get('SqliteBucketStorage')) {
16
15
  super();
17
16
  this.db = db;
18
- this.mutex = mutex;
19
17
  this.logger = logger;
20
18
  this._hasCompletedSync = false;
21
19
  this.tableNames = new Set();
@@ -1,6 +1,6 @@
1
1
  import Logger, { ILogger } from 'js-logger';
2
2
  import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus.js';
3
- import { BaseListener, BaseObserver, Disposable } from '../../../utils/BaseObserver.js';
3
+ import { BaseListener, BaseObserver, BaseObserverInterface, Disposable } from '../../../utils/BaseObserver.js';
4
4
  import { BucketStorageAdapter } from '../bucket/BucketStorageAdapter.js';
5
5
  import { AbstractRemote, FetchStrategy } from './AbstractRemote.js';
6
6
  import { StreamingSyncRequestParameterType } from './streaming-sync-types.js';
@@ -131,7 +131,7 @@ export interface AdditionalConnectionOptions {
131
131
  }
132
132
  /** @internal */
133
133
  export type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
134
- export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener>, Disposable {
134
+ export interface StreamingSyncImplementation extends BaseObserverInterface<StreamingSyncImplementationListener>, Disposable {
135
135
  /**
136
136
  * Connects to the sync service
137
137
  */
@@ -164,6 +164,7 @@ export declare abstract class AbstractStreamingSyncImplementation extends BaseOb
164
164
  protected _lastSyncedAt: Date | null;
165
165
  protected options: AbstractStreamingSyncImplementationOptions;
166
166
  protected abortController: AbortController | null;
167
+ protected uploadAbortController: AbortController | null;
167
168
  protected crudUpdateListener?: () => void;
168
169
  protected streamingSyncPromise?: Promise<void>;
169
170
  private isUploadingCrud;