@powersync/common 0.0.0-dev-20240606141637 → 0.0.0-dev-20240626101022
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/lib/client/AbstractPowerSyncDatabase.d.ts +14 -1
- package/lib/client/AbstractPowerSyncDatabase.js +14 -2
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +2 -9
- package/lib/client/SQLOpenFactory.d.ts +29 -0
- package/lib/client/SQLOpenFactory.js +18 -0
- package/lib/client/connection/PowerSyncCredentials.d.ts +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +5 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +8 -5
- package/lib/client/sync/stream/streaming-sync-types.d.ts +8 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
|
@@ -10,14 +10,22 @@ import { BucketStorageAdapter } from './sync/bucket/BucketStorageAdapter';
|
|
|
10
10
|
import { CrudBatch } from './sync/bucket/CrudBatch';
|
|
11
11
|
import { CrudTransaction } from './sync/bucket/CrudTransaction';
|
|
12
12
|
import { AbstractStreamingSyncImplementation, StreamingSyncImplementationListener, StreamingSyncImplementation, PowerSyncConnectionOptions } from './sync/stream/AbstractStreamingSyncImplementation';
|
|
13
|
+
import { SQLOpenFactory, SQLOpenOptions } from './SQLOpenFactory';
|
|
13
14
|
export interface DisconnectAndClearOptions {
|
|
14
15
|
/** When set to false, data in local-only tables is preserved. */
|
|
15
16
|
clearLocal?: boolean;
|
|
16
17
|
}
|
|
17
18
|
export interface PowerSyncDatabaseOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Source for a SQLite database connection.
|
|
21
|
+
* This can be either:
|
|
22
|
+
* - A {@link DBAdapter} if providing an instantiated SQLite connection
|
|
23
|
+
* - A {@link SQLOpenFactory} which will be used to open a SQLite connection
|
|
24
|
+
* - {@link SQLOpenOptions} for opening a SQLite connection with a default {@link SQLOpenFactory}
|
|
25
|
+
*/
|
|
26
|
+
database: DBAdapter | SQLOpenFactory | SQLOpenOptions;
|
|
18
27
|
/** Schema used for the local database. */
|
|
19
28
|
schema: Schema;
|
|
20
|
-
database: DBAdapter;
|
|
21
29
|
/**
|
|
22
30
|
* Delay for retrying sync streaming operations
|
|
23
31
|
* from the PowerSync backend after an error occurs.
|
|
@@ -100,6 +108,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
100
108
|
protected _isReadyPromise: Promise<void>;
|
|
101
109
|
private hasSyncedWatchDisposer?;
|
|
102
110
|
protected _schema: Schema;
|
|
111
|
+
private _database;
|
|
103
112
|
constructor(options: PowerSyncDatabaseOptions);
|
|
104
113
|
/**
|
|
105
114
|
* Schema used for the local database.
|
|
@@ -117,6 +126,10 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
117
126
|
* Whether a connection to the PowerSync service is currently open.
|
|
118
127
|
*/
|
|
119
128
|
get connected(): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Opens the DBAdapter given open options using a default open factory
|
|
131
|
+
*/
|
|
132
|
+
protected abstract openDBAdapter(options: SQLOpenOptions): DBAdapter;
|
|
120
133
|
protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector): AbstractStreamingSyncImplementation;
|
|
121
134
|
protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
|
|
122
135
|
/**
|
|
@@ -14,6 +14,7 @@ import { CrudEntry } from './sync/bucket/CrudEntry';
|
|
|
14
14
|
import { CrudTransaction } from './sync/bucket/CrudTransaction';
|
|
15
15
|
import { DEFAULT_CRUD_UPLOAD_THROTTLE_MS } from './sync/stream/AbstractStreamingSyncImplementation';
|
|
16
16
|
import { ControlledExecutor } from '../utils/ControlledExecutor';
|
|
17
|
+
import { isDBAdapter, isSQLOpenFactory, isSQLOpenOptions } from './SQLOpenFactory';
|
|
17
18
|
const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
|
|
18
19
|
const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
|
|
19
20
|
clearLocal: true
|
|
@@ -56,9 +57,20 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
56
57
|
_isReadyPromise;
|
|
57
58
|
hasSyncedWatchDisposer;
|
|
58
59
|
_schema;
|
|
60
|
+
_database;
|
|
59
61
|
constructor(options) {
|
|
60
62
|
super();
|
|
61
63
|
this.options = options;
|
|
64
|
+
const { database } = options;
|
|
65
|
+
if (isDBAdapter(database)) {
|
|
66
|
+
this._database = database;
|
|
67
|
+
}
|
|
68
|
+
else if (isSQLOpenFactory(database)) {
|
|
69
|
+
this._database = database.openDB();
|
|
70
|
+
}
|
|
71
|
+
else if (isSQLOpenOptions(database)) {
|
|
72
|
+
this._database = this.openDBAdapter(database);
|
|
73
|
+
}
|
|
62
74
|
this.bucketStorageAdapter = this.generateBucketStorageAdapter();
|
|
63
75
|
this.closed = false;
|
|
64
76
|
this.currentStatus = new SyncStatus({});
|
|
@@ -81,7 +93,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
81
93
|
* For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
|
|
82
94
|
*/
|
|
83
95
|
get database() {
|
|
84
|
-
return this.
|
|
96
|
+
return this._database;
|
|
85
97
|
}
|
|
86
98
|
/**
|
|
87
99
|
* Whether a connection to the PowerSync service is currently open.
|
|
@@ -127,7 +139,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
127
139
|
async initialize() {
|
|
128
140
|
await this._initialize();
|
|
129
141
|
await this.bucketStorageAdapter.init();
|
|
130
|
-
const version = await this.
|
|
142
|
+
const version = await this.database.execute('SELECT powersync_rs_version()');
|
|
131
143
|
this.sdkVersion = version.rows?.item(0)['powersync_rs_version()'] ?? '';
|
|
132
144
|
await this.updateSchema(this.options.schema);
|
|
133
145
|
this.updateHasSynced();
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { DBAdapter } from '../db/DBAdapter';
|
|
2
2
|
import { Schema } from '../db/schema/Schema';
|
|
3
3
|
import { AbstractPowerSyncDatabase, PowerSyncDatabaseOptions } from './AbstractPowerSyncDatabase';
|
|
4
|
-
|
|
4
|
+
import { SQLOpenOptions } from './SQLOpenFactory';
|
|
5
|
+
export interface PowerSyncOpenFactoryOptions extends Partial<PowerSyncDatabaseOptions>, SQLOpenOptions {
|
|
5
6
|
/** Schema used for the local database. */
|
|
6
7
|
schema: Schema;
|
|
7
|
-
/**
|
|
8
|
-
* Filename for the database.
|
|
9
|
-
*/
|
|
10
|
-
dbFilename: string;
|
|
11
|
-
/**
|
|
12
|
-
* Directory where the database file is located.
|
|
13
|
-
*/
|
|
14
|
-
dbLocation?: string;
|
|
15
8
|
}
|
|
16
9
|
export declare abstract class AbstractPowerSyncDatabaseOpenFactory {
|
|
17
10
|
protected options: PowerSyncOpenFactoryOptions;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DBAdapter } from '../db/DBAdapter';
|
|
2
|
+
export interface SQLOpenOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Filename for the database.
|
|
5
|
+
*/
|
|
6
|
+
dbFilename: string;
|
|
7
|
+
/**
|
|
8
|
+
* Directory where the database file is located.
|
|
9
|
+
*/
|
|
10
|
+
dbLocation?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface SQLOpenFactory {
|
|
13
|
+
/**
|
|
14
|
+
* Opens a connection adapter to a SQLite DB
|
|
15
|
+
*/
|
|
16
|
+
openDB(): DBAdapter;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Tests if the input is a {@link SQLOpenOptions}
|
|
20
|
+
*/
|
|
21
|
+
export declare const isSQLOpenOptions: (test: any) => test is SQLOpenOptions;
|
|
22
|
+
/**
|
|
23
|
+
* Tests if input is a {@link SQLOpenFactory}
|
|
24
|
+
*/
|
|
25
|
+
export declare const isSQLOpenFactory: (test: any) => test is SQLOpenFactory;
|
|
26
|
+
/**
|
|
27
|
+
* Tests if input is a {@link DBAdapter}
|
|
28
|
+
*/
|
|
29
|
+
export declare const isDBAdapter: (test: any) => test is DBAdapter;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests if the input is a {@link SQLOpenOptions}
|
|
3
|
+
*/
|
|
4
|
+
export const isSQLOpenOptions = (test) => {
|
|
5
|
+
return typeof test == 'object' && 'dbFilename' in test;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Tests if input is a {@link SQLOpenFactory}
|
|
9
|
+
*/
|
|
10
|
+
export const isSQLOpenFactory = (test) => {
|
|
11
|
+
return typeof test?.openDB == 'function';
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Tests if input is a {@link DBAdapter}
|
|
15
|
+
*/
|
|
16
|
+
export const isDBAdapter = (test) => {
|
|
17
|
+
return typeof test?.writeTransaction == 'function';
|
|
18
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ILogger } from 'js-logger';
|
|
2
|
+
import { StreamingSyncRequestParameterType } from './streaming-sync-types';
|
|
2
3
|
import { AbstractRemote } from './AbstractRemote';
|
|
3
4
|
import { BucketStorageAdapter } from '../bucket/BucketStorageAdapter';
|
|
4
5
|
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus';
|
|
@@ -54,6 +55,10 @@ export interface PowerSyncConnectionOptions {
|
|
|
54
55
|
* Defaults to a HTTP streaming connection.
|
|
55
56
|
*/
|
|
56
57
|
connectionMethod?: SyncStreamConnectionMethod;
|
|
58
|
+
/**
|
|
59
|
+
* These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
|
|
60
|
+
*/
|
|
61
|
+
params?: Record<string, StreamingSyncRequestParameterType>;
|
|
57
62
|
}
|
|
58
63
|
export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener>, Disposable {
|
|
59
64
|
/**
|
|
@@ -22,7 +22,8 @@ export const DEFAULT_STREAMING_SYNC_OPTIONS = {
|
|
|
22
22
|
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
|
|
23
23
|
};
|
|
24
24
|
export const DEFAULT_STREAM_CONNECTION_OPTIONS = {
|
|
25
|
-
connectionMethod: SyncStreamConnectionMethod.HTTP
|
|
25
|
+
connectionMethod: SyncStreamConnectionMethod.HTTP,
|
|
26
|
+
params: {}
|
|
26
27
|
};
|
|
27
28
|
export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
28
29
|
_lastSyncedAt;
|
|
@@ -116,13 +117,16 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
116
117
|
}
|
|
117
118
|
catch (ex) {
|
|
118
119
|
this.updateSyncStatus({
|
|
119
|
-
connected: false,
|
|
120
120
|
dataFlow: {
|
|
121
121
|
uploading: false
|
|
122
122
|
}
|
|
123
123
|
});
|
|
124
124
|
await this.delayRetry();
|
|
125
|
-
|
|
125
|
+
if (!this.isConnected) {
|
|
126
|
+
// Exit the upload loop if the sync stream is no longer connected
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
this.logger.debug(`Caught exception when uploading. Upload will retry after a delay. Exception: ${ex.message}`);
|
|
126
130
|
}
|
|
127
131
|
finally {
|
|
128
132
|
this.updateSyncStatus({
|
|
@@ -305,7 +309,6 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
305
309
|
let validatedCheckpoint = null;
|
|
306
310
|
let appliedCheckpoint = null;
|
|
307
311
|
let bucketSet = new Set(initialBuckets.keys());
|
|
308
|
-
const { params = undefined } = (await this.options.remote.getCredentials()) ?? {};
|
|
309
312
|
this.logger.debug('Requesting stream from server');
|
|
310
313
|
const syncOptions = {
|
|
311
314
|
path: '/sync/stream',
|
|
@@ -314,7 +317,7 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
314
317
|
buckets: req,
|
|
315
318
|
include_checksum: true,
|
|
316
319
|
raw_data: true,
|
|
317
|
-
parameters: params
|
|
320
|
+
parameters: resolvedOptions.params
|
|
318
321
|
}
|
|
319
322
|
};
|
|
320
323
|
const stream = resolvedOptions?.connectionMethod == SyncStreamConnectionMethod.HTTP
|
|
@@ -42,6 +42,12 @@ export interface SyncResponse {
|
|
|
42
42
|
checkpoint_token?: string;
|
|
43
43
|
checkpoint?: Checkpoint;
|
|
44
44
|
}
|
|
45
|
+
type JSONValue = string | number | boolean | null | undefined | JSONObject | JSONArray;
|
|
46
|
+
interface JSONObject {
|
|
47
|
+
[key: string]: JSONValue;
|
|
48
|
+
}
|
|
49
|
+
type JSONArray = JSONValue[];
|
|
50
|
+
export type StreamingSyncRequestParameterType = JSONValue;
|
|
45
51
|
export interface StreamingSyncRequest {
|
|
46
52
|
/**
|
|
47
53
|
* Existing bucket states.
|
|
@@ -62,7 +68,7 @@ export interface StreamingSyncRequest {
|
|
|
62
68
|
/**
|
|
63
69
|
* Client parameters to be passed to the sync rules.
|
|
64
70
|
*/
|
|
65
|
-
parameters?: Record<string,
|
|
71
|
+
parameters?: Record<string, StreamingSyncRequestParameterType>;
|
|
66
72
|
}
|
|
67
73
|
export interface StreamingSyncCheckpoint {
|
|
68
74
|
checkpoint: Checkpoint;
|
|
@@ -118,3 +124,4 @@ export interface CrudResponse {
|
|
|
118
124
|
*/
|
|
119
125
|
checkpoint?: OpId;
|
|
120
126
|
}
|
|
127
|
+
export {};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './client/AbstractPowerSyncDatabase';
|
|
2
2
|
export * from './client/AbstractPowerSyncOpenFactory';
|
|
3
|
+
export * from './client/SQLOpenFactory';
|
|
3
4
|
export * from './client/connection/PowerSyncBackendConnector';
|
|
4
5
|
export * from './client/connection/PowerSyncCredentials';
|
|
5
6
|
export * from './client/sync/bucket/BucketStorageAdapter';
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './client/AbstractPowerSyncDatabase';
|
|
2
2
|
export * from './client/AbstractPowerSyncOpenFactory';
|
|
3
|
+
export * from './client/SQLOpenFactory';
|
|
3
4
|
export * from './client/connection/PowerSyncBackendConnector';
|
|
4
5
|
export * from './client/connection/PowerSyncCredentials';
|
|
5
6
|
export * from './client/sync/bucket/BucketStorageAdapter';
|