@powersync/common 1.12.0 → 1.13.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.
- package/lib/client/AbstractPowerSyncDatabase.d.ts +29 -2
- 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/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
|
@@ -10,14 +10,14 @@ 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
|
-
export interface
|
|
18
|
+
export interface BasePowerSyncDatabaseOptions {
|
|
18
19
|
/** Schema used for the local database. */
|
|
19
20
|
schema: Schema;
|
|
20
|
-
database: DBAdapter;
|
|
21
21
|
/**
|
|
22
22
|
* Delay for retrying sync streaming operations
|
|
23
23
|
* from the PowerSync backend after an error occurs.
|
|
@@ -31,6 +31,25 @@ export interface PowerSyncDatabaseOptions {
|
|
|
31
31
|
crudUploadThrottleMs?: number;
|
|
32
32
|
logger?: ILogger;
|
|
33
33
|
}
|
|
34
|
+
export interface PowerSyncDatabaseOptions extends BasePowerSyncDatabaseOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Source for a SQLite database connection.
|
|
37
|
+
* This can be either:
|
|
38
|
+
* - A {@link DBAdapter} if providing an instantiated SQLite connection
|
|
39
|
+
* - A {@link SQLOpenFactory} which will be used to open a SQLite connection
|
|
40
|
+
* - {@link SQLOpenOptions} for opening a SQLite connection with a default {@link SQLOpenFactory}
|
|
41
|
+
*/
|
|
42
|
+
database: DBAdapter | SQLOpenFactory | SQLOpenOptions;
|
|
43
|
+
}
|
|
44
|
+
export interface PowerSyncDatabaseOptionsWithDBAdapter extends BasePowerSyncDatabaseOptions {
|
|
45
|
+
database: DBAdapter;
|
|
46
|
+
}
|
|
47
|
+
export interface PowerSyncDatabaseOptionsWithOpenFactory extends BasePowerSyncDatabaseOptions {
|
|
48
|
+
database: SQLOpenFactory;
|
|
49
|
+
}
|
|
50
|
+
export interface PowerSyncDatabaseOptionsWithSettings extends BasePowerSyncDatabaseOptions {
|
|
51
|
+
database: SQLOpenOptions;
|
|
52
|
+
}
|
|
34
53
|
export interface SQLWatchOptions {
|
|
35
54
|
signal?: AbortSignal;
|
|
36
55
|
tables?: string[];
|
|
@@ -100,6 +119,10 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
100
119
|
protected _isReadyPromise: Promise<void>;
|
|
101
120
|
private hasSyncedWatchDisposer?;
|
|
102
121
|
protected _schema: Schema;
|
|
122
|
+
private _database;
|
|
123
|
+
constructor(options: PowerSyncDatabaseOptionsWithDBAdapter);
|
|
124
|
+
constructor(options: PowerSyncDatabaseOptionsWithOpenFactory);
|
|
125
|
+
constructor(options: PowerSyncDatabaseOptionsWithSettings);
|
|
103
126
|
constructor(options: PowerSyncDatabaseOptions);
|
|
104
127
|
/**
|
|
105
128
|
* Schema used for the local database.
|
|
@@ -117,6 +140,10 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
117
140
|
* Whether a connection to the PowerSync service is currently open.
|
|
118
141
|
*/
|
|
119
142
|
get connected(): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Opens the DBAdapter given open options using a default open factory
|
|
145
|
+
*/
|
|
146
|
+
protected abstract openDBAdapter(options: SQLOpenOptions): DBAdapter;
|
|
120
147
|
protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector): AbstractStreamingSyncImplementation;
|
|
121
148
|
protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
|
|
122
149
|
/**
|
|
@@ -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
|
+
};
|
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';
|