@powersync/common 1.12.0 → 1.13.1

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.
@@ -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 PowerSyncDatabaseOptions {
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.options.database;
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.options.database.execute('SELECT powersync_rs_version()');
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
- export interface PowerSyncOpenFactoryOptions extends Partial<PowerSyncDatabaseOptions> {
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,3 +1,4 @@
1
+ import { OpId } from './CrudEntry';
1
2
  import { OplogEntry, OplogEntryJSON } from './OplogEntry';
2
3
  export type SyncDataBucketJSON = {
3
4
  bucket: string;
@@ -17,11 +18,11 @@ export declare class SyncDataBucket {
17
18
  /**
18
19
  * The `after` specified in the request.
19
20
  */
20
- after?: string | undefined;
21
+ after?: OpId | undefined;
21
22
  /**
22
23
  * Use this for the next request.
23
24
  */
24
- next_after?: string | undefined;
25
+ next_after?: OpId | undefined;
25
26
  static fromRow(row: SyncDataBucketJSON): SyncDataBucket;
26
27
  constructor(bucket: string, data: OplogEntry[],
27
28
  /**
@@ -31,10 +32,10 @@ export declare class SyncDataBucket {
31
32
  /**
32
33
  * The `after` specified in the request.
33
34
  */
34
- after?: string | undefined,
35
+ after?: OpId | undefined,
35
36
  /**
36
37
  * Use this for the next request.
37
38
  */
38
- next_after?: string | undefined);
39
+ next_after?: OpId | undefined);
39
40
  toJSON(): SyncDataBucketJSON;
40
41
  }
@@ -180,8 +180,16 @@ export class AbstractRemote {
180
180
  socketIsClosed = true;
181
181
  rsocket.close();
182
182
  };
183
+ // Helps to prevent double close scenarios
184
+ rsocket.onClose(() => (socketIsClosed = true));
183
185
  // We initially request this amount and expect these to arrive eventually
184
186
  let pendingEventsCount = SYNC_QUEUE_REQUEST_N;
187
+ const disposeClosedListener = stream.registerListener({
188
+ closed: () => {
189
+ closeSocket();
190
+ disposeClosedListener();
191
+ }
192
+ });
185
193
  const socket = await new Promise((resolve, reject) => {
186
194
  let connectionEstablished = false;
187
195
  const res = rsocket.requestStream({
@@ -196,9 +204,8 @@ export class AbstractRemote {
196
204
  if (e.message !== 'Closed. ') {
197
205
  this.logger.error(e);
198
206
  }
199
- // RSocket will close this automatically
200
- // Attempting to close multiple times causes a console warning
201
- socketIsClosed = true;
207
+ // RSocket will close the RSocket stream automatically
208
+ // Close the downstream stream as well - this will close the RSocket connection and WebSocket
202
209
  stream.close();
203
210
  // Handles cases where the connection failed e.g. auth error or connection error
204
211
  if (!connectionEstablished) {
@@ -236,8 +243,7 @@ export class AbstractRemote {
236
243
  }
237
244
  },
238
245
  closed: () => {
239
- closeSocket();
240
- l?.();
246
+ l();
241
247
  }
242
248
  });
243
249
  /**
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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/common",
3
- "version": "1.12.0",
3
+ "version": "1.13.1",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -20,7 +20,7 @@
20
20
  "bugs": {
21
21
  "url": "https://github.com/powersync-ja/powersync-js/issues"
22
22
  },
23
- "homepage": "https://docs.powersync.com/resources/api-reference",
23
+ "homepage": "https://docs.powersync.com",
24
24
  "dependencies": {
25
25
  "async-mutex": "^0.4.0",
26
26
  "buffer": "^6.0.3",
@@ -36,7 +36,7 @@
36
36
  "@types/lodash": "^4.14.197",
37
37
  "@types/node": "^20.5.9",
38
38
  "@types/uuid": "^9.0.1",
39
- "typescript": "^5.1.3",
39
+ "typescript": "^5.5.3",
40
40
  "vitest": "^1.5.2",
41
41
  "bson": "^6.6.0"
42
42
  },