@powersync/common 1.11.1 → 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.
@@ -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,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;
@@ -315,7 +316,8 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
315
316
  data: {
316
317
  buckets: req,
317
318
  include_checksum: true,
318
- raw_data: true
319
+ raw_data: true,
320
+ parameters: resolvedOptions.params
319
321
  }
320
322
  };
321
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.
@@ -59,6 +65,10 @@ export interface StreamingSyncRequest {
59
65
  * Changes the response to stringified data in each OplogEntry
60
66
  */
61
67
  raw_data: boolean;
68
+ /**
69
+ * Client parameters to be passed to the sync rules.
70
+ */
71
+ parameters?: Record<string, StreamingSyncRequestParameterType>;
62
72
  }
63
73
  export interface StreamingSyncCheckpoint {
64
74
  checkpoint: Checkpoint;
@@ -114,3 +124,4 @@ export interface CrudResponse {
114
124
  */
115
125
  checkpoint?: OpId;
116
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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/common",
3
- "version": "1.11.1",
3
+ "version": "1.13.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"