@powersync/common 0.0.0-dev-20250722092404 → 0.0.0-dev-20250728083821
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.
- package/dist/bundle.cjs +5 -5
- package/dist/bundle.mjs +3 -3
- package/lib/client/AbstractPowerSyncDatabase.d.ts +5 -56
- package/lib/client/AbstractPowerSyncDatabase.js +29 -95
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +2 -2
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +2 -3
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +5 -15
- package/lib/index.d.ts +1 -7
- package/lib/index.js +1 -7
- package/lib/utils/BaseObserver.d.ts +4 -3
- package/lib/utils/BaseObserver.js +0 -3
- package/package.json +1 -1
- package/lib/client/CustomQuery.d.ts +0 -25
- package/lib/client/CustomQuery.js +0 -41
- package/lib/client/Query.d.ts +0 -97
- package/lib/client/Query.js +0 -1
- package/lib/client/watched/GetAllQuery.d.ts +0 -32
- package/lib/client/watched/GetAllQuery.js +0 -24
- package/lib/client/watched/WatchedQuery.d.ts +0 -93
- package/lib/client/watched/WatchedQuery.js +0 -12
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -67
- package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -136
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +0 -121
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +0 -164
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +0 -33
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +0 -74
- package/lib/client/watched/processors/comparators.d.ts +0 -30
- package/lib/client/watched/processors/comparators.js +0 -34
- package/lib/utils/MetaBaseObserver.d.ts +0 -29
- package/lib/utils/MetaBaseObserver.js +0 -50
|
@@ -6,15 +6,12 @@ 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';
|
|
10
9
|
import { SQLOpenFactory, SQLOpenOptions } from './SQLOpenFactory.js';
|
|
11
10
|
import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
|
|
12
11
|
import { BucketStorageAdapter } from './sync/bucket/BucketStorageAdapter.js';
|
|
13
12
|
import { CrudBatch } from './sync/bucket/CrudBatch.js';
|
|
14
13
|
import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
|
|
15
14
|
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';
|
|
18
15
|
export interface DisconnectAndClearOptions {
|
|
19
16
|
/** When set to false, data in local-only tables is preserved. */
|
|
20
17
|
clearLocal?: boolean;
|
|
@@ -47,7 +44,7 @@ export interface PowerSyncDatabaseOptionsWithOpenFactory extends BasePowerSyncDa
|
|
|
47
44
|
export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatabaseOptions {
|
|
48
45
|
database: SQLOpenOptions;
|
|
49
46
|
}
|
|
50
|
-
export interface
|
|
47
|
+
export interface SQLWatchOptions {
|
|
51
48
|
signal?: AbortSignal;
|
|
52
49
|
tables?: string[];
|
|
53
50
|
/** The minimum interval between queries. */
|
|
@@ -59,17 +56,6 @@ export interface SQLOnChangeOptions {
|
|
|
59
56
|
* by not removing PowerSync table name prefixes
|
|
60
57
|
*/
|
|
61
58
|
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>;
|
|
73
59
|
}
|
|
74
60
|
export interface WatchOnChangeEvent {
|
|
75
61
|
changedTables: string[];
|
|
@@ -85,8 +71,6 @@ export interface WatchOnChangeHandler {
|
|
|
85
71
|
export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
|
|
86
72
|
initialized: () => void;
|
|
87
73
|
schemaChanged: (schema: Schema) => void;
|
|
88
|
-
closing: () => Promise<void> | void;
|
|
89
|
-
closed: () => Promise<void> | void;
|
|
90
74
|
}
|
|
91
75
|
export interface PowerSyncCloseOptions {
|
|
92
76
|
/**
|
|
@@ -97,6 +81,7 @@ export interface PowerSyncCloseOptions {
|
|
|
97
81
|
disconnect?: boolean;
|
|
98
82
|
}
|
|
99
83
|
export declare const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions;
|
|
84
|
+
export declare const DEFAULT_WATCH_THROTTLE_MS = 30;
|
|
100
85
|
export declare const DEFAULT_POWERSYNC_DB_OPTIONS: {
|
|
101
86
|
retryDelayMs: number;
|
|
102
87
|
crudUploadThrottleMs: number;
|
|
@@ -404,42 +389,6 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
404
389
|
* ```
|
|
405
390
|
*/
|
|
406
391
|
watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
|
|
407
|
-
/**
|
|
408
|
-
* Allows defining a query which can be used to build a {@link WatchedQuery}.
|
|
409
|
-
* The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
|
|
410
|
-
* An optional mapper function can be provided to transform the results.
|
|
411
|
-
*
|
|
412
|
-
* @example
|
|
413
|
-
* ```javascript
|
|
414
|
-
* const watchedTodos = powersync.query({
|
|
415
|
-
* sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
|
|
416
|
-
* parameters: [],
|
|
417
|
-
* mapper: (row) => ({
|
|
418
|
-
* ...row,
|
|
419
|
-
* created_at: new Date(row.created_at as string)
|
|
420
|
-
* })
|
|
421
|
-
* })
|
|
422
|
-
* .watch()
|
|
423
|
-
* // OR use .differentialWatch() for fine-grained watches.
|
|
424
|
-
* ```
|
|
425
|
-
*/
|
|
426
|
-
query<RowType>(query: ArrayQueryDefinition<RowType>): Query<RowType>;
|
|
427
|
-
/**
|
|
428
|
-
* Allows building a {@link WatchedQuery} using an existing {@link WatchCompatibleQuery}.
|
|
429
|
-
* The watched query will use the provided {@link WatchCompatibleQuery.execute} method to query results.
|
|
430
|
-
*
|
|
431
|
-
* @example
|
|
432
|
-
* ```javascript
|
|
433
|
-
*
|
|
434
|
-
* // Potentially a query from an ORM like Drizzle
|
|
435
|
-
* const query = db.select().from(lists);
|
|
436
|
-
*
|
|
437
|
-
* const watchedTodos = powersync.customQuery(query)
|
|
438
|
-
* .watch()
|
|
439
|
-
* // OR use .differentialWatch() for fine-grained watches.
|
|
440
|
-
* ```
|
|
441
|
-
*/
|
|
442
|
-
customQuery<RowType>(query: WatchCompatibleQuery<RowType[]>): Query<RowType>;
|
|
443
392
|
/**
|
|
444
393
|
* Execute a read query every time the source tables are modified.
|
|
445
394
|
* Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
|
|
@@ -488,7 +437,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
488
437
|
* }
|
|
489
438
|
* ```
|
|
490
439
|
*/
|
|
491
|
-
onChange(options?:
|
|
440
|
+
onChange(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent>;
|
|
492
441
|
/**
|
|
493
442
|
* See {@link onChangeWithCallback}.
|
|
494
443
|
*
|
|
@@ -503,7 +452,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
503
452
|
* }
|
|
504
453
|
* ```
|
|
505
454
|
*/
|
|
506
|
-
onChange(handler?: WatchOnChangeHandler, options?:
|
|
455
|
+
onChange(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
|
|
507
456
|
/**
|
|
508
457
|
* Invoke the provided callback on any changes to any of the specified tables.
|
|
509
458
|
*
|
|
@@ -516,7 +465,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
516
465
|
* @param options Options for configuring watch behavior
|
|
517
466
|
* @returns A dispose function to stop watching for changes
|
|
518
467
|
*/
|
|
519
|
-
onChangeWithCallback(handler?: WatchOnChangeHandler, options?:
|
|
468
|
+
onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLWatchOptions): () => void;
|
|
520
469
|
/**
|
|
521
470
|
* Create a Stream of changes to any of the specified tables.
|
|
522
471
|
*
|
|
@@ -9,15 +9,13 @@ import { BaseObserver } from '../utils/BaseObserver.js';
|
|
|
9
9
|
import { ControlledExecutor } from '../utils/ControlledExecutor.js';
|
|
10
10
|
import { throttleTrailing } from '../utils/async.js';
|
|
11
11
|
import { ConnectionManager } from './ConnectionManager.js';
|
|
12
|
-
import { CustomQuery } from './CustomQuery.js';
|
|
13
12
|
import { isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory.js';
|
|
13
|
+
import { runOnSchemaChange } from './runOnSchemaChange.js';
|
|
14
14
|
import { PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
|
|
15
15
|
import { CrudBatch } from './sync/bucket/CrudBatch.js';
|
|
16
16
|
import { CrudEntry } from './sync/bucket/CrudEntry.js';
|
|
17
17
|
import { CrudTransaction } from './sync/bucket/CrudTransaction.js';
|
|
18
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';
|
|
21
19
|
const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
|
|
22
20
|
const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
|
|
23
21
|
clearLocal: true
|
|
@@ -25,6 +23,7 @@ const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
|
|
|
25
23
|
export const DEFAULT_POWERSYNC_CLOSE_OPTIONS = {
|
|
26
24
|
disconnect: true
|
|
27
25
|
};
|
|
26
|
+
export const DEFAULT_WATCH_THROTTLE_MS = 30;
|
|
28
27
|
export const DEFAULT_POWERSYNC_DB_OPTIONS = {
|
|
29
28
|
retryDelayMs: 5000,
|
|
30
29
|
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
|
|
@@ -342,7 +341,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
342
341
|
if (this.closed) {
|
|
343
342
|
return;
|
|
344
343
|
}
|
|
345
|
-
await this.iterateAsyncListeners(async (cb) => cb.closing?.());
|
|
346
344
|
const { disconnect } = options;
|
|
347
345
|
if (disconnect) {
|
|
348
346
|
await this.disconnect();
|
|
@@ -350,7 +348,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
350
348
|
await this.connectionManager.close();
|
|
351
349
|
await this.database.close();
|
|
352
350
|
this.closed = true;
|
|
353
|
-
await this.iterateAsyncListeners(async (cb) => cb.closed?.());
|
|
354
351
|
}
|
|
355
352
|
/**
|
|
356
353
|
* Get upload queue size estimate and count.
|
|
@@ -597,60 +594,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
597
594
|
const options = handlerOrOptions;
|
|
598
595
|
return this.watchWithAsyncGenerator(sql, parameters, options);
|
|
599
596
|
}
|
|
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
|
-
}
|
|
654
597
|
/**
|
|
655
598
|
* Execute a read query every time the source tables are modified.
|
|
656
599
|
* Use {@link SQLWatchOptions.throttleMs} to specify the minimum interval between queries.
|
|
@@ -668,41 +611,35 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
668
611
|
if (!onResult) {
|
|
669
612
|
throw new Error('onResult is required');
|
|
670
613
|
}
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
614
|
+
const watchQuery = async (abortSignal) => {
|
|
615
|
+
try {
|
|
616
|
+
const resolvedTables = await this.resolveTables(sql, parameters, options);
|
|
617
|
+
// Fetch initial data
|
|
618
|
+
const result = await this.executeReadOnly(sql, parameters);
|
|
619
|
+
onResult(result);
|
|
620
|
+
this.onChangeWithCallback({
|
|
621
|
+
onChange: async () => {
|
|
622
|
+
try {
|
|
623
|
+
const result = await this.executeReadOnly(sql, parameters);
|
|
624
|
+
onResult(result);
|
|
625
|
+
}
|
|
626
|
+
catch (error) {
|
|
627
|
+
onError?.(error);
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
onError
|
|
631
|
+
}, {
|
|
632
|
+
...(options ?? {}),
|
|
633
|
+
tables: resolvedTables,
|
|
634
|
+
// Override the abort signal since we intercept it
|
|
635
|
+
signal: abortSignal
|
|
636
|
+
});
|
|
688
637
|
}
|
|
689
|
-
|
|
690
|
-
|
|
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);
|
|
638
|
+
catch (error) {
|
|
639
|
+
onError?.(error);
|
|
700
640
|
}
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
dispose();
|
|
704
|
-
watchedQuery.close();
|
|
705
|
-
});
|
|
641
|
+
};
|
|
642
|
+
runOnSchemaChange(watchQuery, this, options);
|
|
706
643
|
}
|
|
707
644
|
/**
|
|
708
645
|
* Execute a read query every time the source tables are modified.
|
|
@@ -792,9 +729,6 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
792
729
|
return;
|
|
793
730
|
executor.schedule({ changedTables: intersection });
|
|
794
731
|
}), throttleMs);
|
|
795
|
-
if (options?.triggerImmediate) {
|
|
796
|
-
executor.schedule({ changedTables: [] });
|
|
797
|
-
}
|
|
798
732
|
const dispose = this.database.registerListener({
|
|
799
733
|
tablesUpdated: async (update) => {
|
|
800
734
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseListener,
|
|
1
|
+
import { BaseListener, BaseObserver, 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
|
|
65
|
+
export interface BucketStorageAdapter extends BaseObserver<BucketStorageListener>, Disposable {
|
|
66
66
|
init(): Promise<void>;
|
|
67
67
|
saveSyncData(batch: SyncDataBatch, fixedKeyFormat?: boolean): Promise<void>;
|
|
68
68
|
removeBuckets(buckets: string[]): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ILogger } from 'js-logger';
|
|
2
2
|
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus.js';
|
|
3
|
-
import { BaseListener, BaseObserver,
|
|
3
|
+
import { BaseListener, BaseObserver, 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';
|
|
@@ -136,7 +136,7 @@ export interface AdditionalConnectionOptions {
|
|
|
136
136
|
}
|
|
137
137
|
/** @internal */
|
|
138
138
|
export type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
|
|
139
|
-
export interface StreamingSyncImplementation extends
|
|
139
|
+
export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener>, Disposable {
|
|
140
140
|
/**
|
|
141
141
|
* Connects to the sync service
|
|
142
142
|
*/
|
|
@@ -168,7 +168,6 @@ export declare abstract class AbstractStreamingSyncImplementation extends BaseOb
|
|
|
168
168
|
protected _lastSyncedAt: Date | null;
|
|
169
169
|
protected options: AbstractStreamingSyncImplementationOptions;
|
|
170
170
|
protected abortController: AbortController | null;
|
|
171
|
-
protected uploadAbortController: AbortController | null;
|
|
172
171
|
protected crudUpdateListener?: () => void;
|
|
173
172
|
protected streamingSyncPromise?: Promise<void>;
|
|
174
173
|
protected logger: ILogger;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Logger from 'js-logger';
|
|
2
|
-
import { FULL_SYNC_PRIORITY } from '../../../db/crud/SyncProgress.js';
|
|
3
2
|
import { SyncStatus } from '../../../db/crud/SyncStatus.js';
|
|
3
|
+
import { FULL_SYNC_PRIORITY } from '../../../db/crud/SyncProgress.js';
|
|
4
4
|
import { AbortOperation } from '../../../utils/AbortOperation.js';
|
|
5
5
|
import { BaseObserver } from '../../../utils/BaseObserver.js';
|
|
6
6
|
import { throttleLeadingTrailing } from '../../../utils/async.js';
|
|
@@ -83,9 +83,6 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
83
83
|
_lastSyncedAt;
|
|
84
84
|
options;
|
|
85
85
|
abortController;
|
|
86
|
-
// In rare cases, mostly for tests, uploads can be triggered without being properly connected.
|
|
87
|
-
// This allows ensuring that all upload processes can be aborted.
|
|
88
|
-
uploadAbortController;
|
|
89
86
|
crudUpdateListener;
|
|
90
87
|
streamingSyncPromise;
|
|
91
88
|
logger;
|
|
@@ -161,10 +158,8 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
161
158
|
return this.syncStatus.connected;
|
|
162
159
|
}
|
|
163
160
|
async dispose() {
|
|
164
|
-
super.dispose();
|
|
165
161
|
this.crudUpdateListener?.();
|
|
166
162
|
this.crudUpdateListener = undefined;
|
|
167
|
-
this.uploadAbortController?.abort();
|
|
168
163
|
}
|
|
169
164
|
async hasCompletedSync() {
|
|
170
165
|
return this.options.adapter.hasCompletedSync();
|
|
@@ -185,12 +180,7 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
185
180
|
* Keep track of the first item in the CRUD queue for the last `uploadCrud` iteration.
|
|
186
181
|
*/
|
|
187
182
|
let checkedCrudItem;
|
|
188
|
-
|
|
189
|
-
this.uploadAbortController = controller;
|
|
190
|
-
this.abortController?.signal.addEventListener('abort', () => {
|
|
191
|
-
controller.abort();
|
|
192
|
-
}, { once: true });
|
|
193
|
-
while (!controller.signal.aborted) {
|
|
183
|
+
while (true) {
|
|
194
184
|
try {
|
|
195
185
|
/**
|
|
196
186
|
* This is the first item in the FIFO CRUD queue.
|
|
@@ -235,7 +225,7 @@ The next upload iteration will be delayed.`);
|
|
|
235
225
|
uploadError: ex
|
|
236
226
|
}
|
|
237
227
|
});
|
|
238
|
-
await this.delayRetry(
|
|
228
|
+
await this.delayRetry();
|
|
239
229
|
if (!this.isConnected) {
|
|
240
230
|
// Exit the upload loop if the sync stream is no longer connected
|
|
241
231
|
break;
|
|
@@ -250,7 +240,6 @@ The next upload iteration will be delayed.`);
|
|
|
250
240
|
});
|
|
251
241
|
}
|
|
252
242
|
}
|
|
253
|
-
this.uploadAbortController = null;
|
|
254
243
|
}
|
|
255
244
|
});
|
|
256
245
|
}
|
|
@@ -450,7 +439,8 @@ The next upload iteration will be delayed.`);
|
|
|
450
439
|
});
|
|
451
440
|
}
|
|
452
441
|
async legacyStreamingSyncIteration(signal, resolvedOptions) {
|
|
453
|
-
|
|
442
|
+
const rawTables = resolvedOptions.serializedSchema?.raw_tables;
|
|
443
|
+
if (rawTables != null && rawTables.length) {
|
|
454
444
|
this.logger.warn('Raw tables require the Rust-based sync client. The JS client will ignore them.');
|
|
455
445
|
}
|
|
456
446
|
this.logger.debug('Streaming sync iteration started');
|
package/lib/index.d.ts
CHANGED
|
@@ -29,15 +29,9 @@ export * from './db/schema/IndexedColumn.js';
|
|
|
29
29
|
export * from './db/schema/Schema.js';
|
|
30
30
|
export * from './db/schema/Table.js';
|
|
31
31
|
export * from './db/schema/TableV2.js';
|
|
32
|
-
export * from './client/Query.js';
|
|
33
|
-
export * from './client/watched/GetAllQuery.js';
|
|
34
|
-
export * from './client/watched/processors/AbstractQueryProcessor.js';
|
|
35
|
-
export * from './client/watched/processors/comparators.js';
|
|
36
|
-
export * from './client/watched/processors/DifferentialQueryProcessor.js';
|
|
37
|
-
export * from './client/watched/processors/OnChangeQueryProcessor.js';
|
|
38
|
-
export * from './client/watched/WatchedQuery.js';
|
|
39
32
|
export * from './utils/AbortOperation.js';
|
|
40
33
|
export * from './utils/BaseObserver.js';
|
|
34
|
+
export * from './utils/ControlledExecutor.js';
|
|
41
35
|
export * from './utils/DataStream.js';
|
|
42
36
|
export * from './utils/Logger.js';
|
|
43
37
|
export * from './utils/parseQuery.js';
|
package/lib/index.js
CHANGED
|
@@ -29,15 +29,9 @@ export * from './db/schema/IndexedColumn.js';
|
|
|
29
29
|
export * from './db/schema/Schema.js';
|
|
30
30
|
export * from './db/schema/Table.js';
|
|
31
31
|
export * from './db/schema/TableV2.js';
|
|
32
|
-
export * from './client/Query.js';
|
|
33
|
-
export * from './client/watched/GetAllQuery.js';
|
|
34
|
-
export * from './client/watched/processors/AbstractQueryProcessor.js';
|
|
35
|
-
export * from './client/watched/processors/comparators.js';
|
|
36
|
-
export * from './client/watched/processors/DifferentialQueryProcessor.js';
|
|
37
|
-
export * from './client/watched/processors/OnChangeQueryProcessor.js';
|
|
38
|
-
export * from './client/watched/WatchedQuery.js';
|
|
39
32
|
export * from './utils/AbortOperation.js';
|
|
40
33
|
export * from './utils/BaseObserver.js';
|
|
34
|
+
export * from './utils/ControlledExecutor.js';
|
|
41
35
|
export * from './utils/DataStream.js';
|
|
42
36
|
export * from './utils/Logger.js';
|
|
43
37
|
export * from './utils/parseQuery.js';
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
export interface Disposable {
|
|
2
|
-
dispose: () => Promise<void
|
|
2
|
+
dispose: () => Promise<void>;
|
|
3
3
|
}
|
|
4
|
-
export type BaseListener = Record<string, ((...event: any) => any) | undefined>;
|
|
5
4
|
export interface BaseObserverInterface<T extends BaseListener> {
|
|
6
5
|
registerListener(listener: Partial<T>): () => void;
|
|
7
6
|
}
|
|
7
|
+
export type BaseListener = {
|
|
8
|
+
[key: string]: ((...event: any) => any) | undefined;
|
|
9
|
+
};
|
|
8
10
|
export declare class BaseObserver<T extends BaseListener = BaseListener> implements BaseObserverInterface<T> {
|
|
9
11
|
protected listeners: Set<Partial<T>>;
|
|
10
12
|
constructor();
|
|
11
|
-
dispose(): void;
|
|
12
13
|
/**
|
|
13
14
|
* Register a listener for updates to the PowerSync client.
|
|
14
15
|
*/
|
package/package.json
CHANGED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
comparator: differentialWatchOptions?.comparator,
|
|
34
|
-
placeholderData: differentialWatchOptions?.placeholderData ?? [],
|
|
35
|
-
watchOptions: {
|
|
36
|
-
...this.resolveOptions(differentialWatchOptions),
|
|
37
|
-
query: this.options.query
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
package/lib/client/Query.d.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { WatchedQueryComparator } from './watched/processors/comparators.js';
|
|
2
|
-
import { DifferentialWatchedQuery, DifferentialWatchedQueryOptions } from './watched/processors/DifferentialQueryProcessor.js';
|
|
3
|
-
import { StandardWatchedQuery } 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
|
-
* The underlying watched query implementation (re)evaluates the query on any SQLite table change.
|
|
34
|
-
*
|
|
35
|
-
* Providing this optional comparator can be used to filter duplicate result set emissions when the result set is unchanged.
|
|
36
|
-
* The comparator compares the previous and current result set.
|
|
37
|
-
*
|
|
38
|
-
* For an efficient comparator see {@link ArrayComparator}.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```javascript
|
|
42
|
-
* comparator: new ArrayComparator({
|
|
43
|
-
* compareBy: (item) => JSON.stringify(item)
|
|
44
|
-
* })
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
comparator?: WatchedQueryComparator<RowType[]>;
|
|
48
|
-
/**
|
|
49
|
-
* The initial data state reported while the query is loading for the first time.
|
|
50
|
-
* @default []
|
|
51
|
-
*/
|
|
52
|
-
placeholderData?: RowType[];
|
|
53
|
-
}
|
|
54
|
-
export interface Query<RowType> {
|
|
55
|
-
/**
|
|
56
|
-
* Creates a {@link WatchedQuery} which watches and emits results of the linked query.
|
|
57
|
-
*
|
|
58
|
-
* By default the returned watched query will emit changes whenever a change to the underlying SQLite tables is made.
|
|
59
|
-
* These changes might not be relevant to the query, but the query will emit a new result set.
|
|
60
|
-
*
|
|
61
|
-
* A {@link StandardWatchedQueryOptions#comparator} can be provided to limit the data emissions. The watched query will still
|
|
62
|
-
* query the underlying DB on underlying table changes, but the result will only be emitted if the comparator detects a change in the results.
|
|
63
|
-
*
|
|
64
|
-
* 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,
|
|
65
|
-
* but note that the result set will not maintain internal object references to the previous result set. If internal object references are needed,
|
|
66
|
-
* consider using {@link Query#differentialWatch} instead.
|
|
67
|
-
*/
|
|
68
|
-
watch(options?: StandardWatchedQueryOptions<RowType>): StandardWatchedQuery<ReadonlyArray<Readonly<RowType>>>;
|
|
69
|
-
/**
|
|
70
|
-
* Creates a {@link WatchedQuery} which watches and emits results of the linked query.
|
|
71
|
-
*
|
|
72
|
-
* This query method watches for changes in the underlying SQLite tables and runs the query on each table change.
|
|
73
|
-
* The difference between the current and previous result set is computed.
|
|
74
|
-
* The watched query will not emit changes if the result set is identical to the previous result set.
|
|
75
|
-
*
|
|
76
|
-
* If the result set is different, the watched query will emit the new result set and emit a detailed diff of the changes via the `onData` and `onDiff` listeners.
|
|
77
|
-
*
|
|
78
|
-
* The deep differentiation allows maintaining result set object references between result emissions.
|
|
79
|
-
* The {@link DifferentialWatchedQuery#state} `data` array will contain the previous row references for unchanged rows.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```javascript
|
|
83
|
-
* const watchedLists = powerSync.query({sql: 'SELECT * FROM lists'})
|
|
84
|
-
* .differentialWatch();
|
|
85
|
-
*
|
|
86
|
-
* const disposeListener = watchedLists.registerListener({
|
|
87
|
-
* onData: (lists) => {
|
|
88
|
-
* console.log('The latest result set for the query is', lists);
|
|
89
|
-
* },
|
|
90
|
-
* onDiff: (diff) => {
|
|
91
|
-
* console.log('The lists result set has changed since the last emission', diff.added, diff.removed, diff.updated, diff.all)
|
|
92
|
-
* }
|
|
93
|
-
* })
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
differentialWatch(options?: DifferentialWatchedQueryOptions<RowType>): DifferentialWatchedQuery<RowType>;
|
|
97
|
-
}
|
package/lib/client/Query.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|