@powersync/react-native 1.31.0 → 1.33.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/dist/index.js CHANGED
@@ -3,7 +3,6 @@
3
3
  var common = require('@powersync/common');
4
4
  var react = require('@powersync/react');
5
5
  var reactNative = require('react-native');
6
- var asyncMutex = require('async-mutex');
7
6
 
8
7
  function getDefaultExportFromCjs (x) {
9
8
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -19258,8 +19257,8 @@ class ReactNativeStreamingSyncImplementation extends common.AbstractStreamingSyn
19258
19257
  return;
19259
19258
  }
19260
19259
  this.locks = new Map();
19261
- this.locks.set(common.LockType.CRUD, new asyncMutex.Mutex());
19262
- this.locks.set(common.LockType.SYNC, new asyncMutex.Mutex());
19260
+ this.locks.set(common.LockType.CRUD, new common.Mutex());
19261
+ this.locks.set(common.LockType.SYNC, new common.Mutex());
19263
19262
  if (identifier) {
19264
19263
  LOCKS.set(identifier, this.locks);
19265
19264
  }
@@ -19270,11 +19269,8 @@ class ReactNativeStreamingSyncImplementation extends common.AbstractStreamingSyn
19270
19269
  throw new Error(`Lock type ${lockOptions.type} not found`);
19271
19270
  }
19272
19271
  return lock.runExclusive(async () => {
19273
- if (lockOptions.signal?.aborted) {
19274
- throw new Error('Aborted');
19275
- }
19276
19272
  return lockOptions.callback();
19277
- });
19273
+ }, lockOptions.signal);
19278
19274
  }
19279
19275
  }
19280
19276
 
@@ -19300,15 +19296,9 @@ function uint8ArrayToArrayBuffer(array) {
19300
19296
  }
19301
19297
  }
19302
19298
 
19303
- /**
19304
- * Adapter for React Native Quick SQLite
19305
- */
19306
- class RNQSDBAdapter extends common.BaseObserver {
19299
+ class RNQSConnectionPool extends common.BaseObserver {
19307
19300
  baseDB;
19308
19301
  name;
19309
- getAll;
19310
- getOptional;
19311
- get;
19312
19302
  constructor(baseDB, name) {
19313
19303
  super();
19314
19304
  this.baseDB = baseDB;
@@ -19317,41 +19307,48 @@ class RNQSDBAdapter extends common.BaseObserver {
19317
19307
  baseDB.registerTablesChangedHook((update) => {
19318
19308
  this.iterateListeners((cb) => cb.tablesUpdated?.(update));
19319
19309
  });
19320
- const topLevelUtils = this.generateDBHelpers({
19321
- // Arrow function binds `this` for use in readOnlyExecute
19322
- execute: (sql, params) => this.readOnlyExecute(sql, params)
19323
- });
19324
- // Only assigning get helpers
19325
- this.getAll = topLevelUtils.getAll;
19326
- this.getOptional = topLevelUtils.getOptional;
19327
- this.get = topLevelUtils.get;
19328
19310
  }
19329
19311
  close() {
19330
19312
  return this.baseDB.close();
19331
19313
  }
19332
19314
  readLock(fn, options) {
19333
- return this.baseDB.readLock((dbTx) => fn(this.generateDBHelpers(this.generateContext(dbTx))), options);
19334
- }
19335
- readTransaction(fn, options) {
19336
- return this.baseDB.readTransaction((dbTx) => fn(this.generateDBHelpers(this.generateContext(dbTx))), options);
19315
+ return this.baseDB.readLock((dbTx) => fn(this.generateContext(dbTx)), options);
19337
19316
  }
19338
19317
  writeLock(fn, options) {
19339
- return this.baseDB.writeLock((dbTx) => fn(this.generateDBHelpers(this.generateContext(dbTx))), options);
19318
+ return this.baseDB.writeLock((dbTx) => fn(this.generateContext(dbTx)), options);
19340
19319
  }
19341
- writeTransaction(fn, options) {
19342
- return this.baseDB.writeTransaction((dbTx) => fn(this.generateDBHelpers(this.generateContext(dbTx))), options);
19320
+ generateContext(ctx) {
19321
+ return new QuickSqliteContext(ctx);
19322
+ }
19323
+ async refreshSchema() {
19324
+ await this.baseDB.refreshSchema();
19325
+ }
19326
+ }
19327
+ class QuickSqliteExecutor {
19328
+ context;
19329
+ constructor(context) {
19330
+ this.context = context;
19343
19331
  }
19344
19332
  execute(query, params) {
19345
- return this.baseDB.execute(query, params);
19333
+ return this.context.execute(query, params);
19346
19334
  }
19347
19335
  /**
19348
19336
  * 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
19349
19337
  */
19350
19338
  async executeRaw(query, params) {
19351
- const result = await this.baseDB.execute(query, params);
19339
+ const result = await this.context.execute(query, params);
19352
19340
  const rows = result.rows?._array ?? [];
19353
19341
  return rows.map((row) => Object.values(row));
19354
19342
  }
19343
+ }
19344
+ class QuickSqliteContext extends common.DBGetUtilsDefaultMixin(QuickSqliteExecutor) {
19345
+ }
19346
+ /**
19347
+ * Adapter for React Native Quick SQLite
19348
+ */
19349
+ class RNQSDBAdapter extends common.DBAdapterDefaultMixin(RNQSConnectionPool) {
19350
+ // We don't want the default implementation here, RNQS does not support executeBatch for lock contexts so that would
19351
+ // be less efficient.
19355
19352
  async executeBatch(query, params = []) {
19356
19353
  const commands = [];
19357
19354
  for (let i = 0; i < params.length; i++) {
@@ -19362,64 +19359,6 @@ class RNQSDBAdapter extends common.BaseObserver {
19362
19359
  rowsAffected: result.rowsAffected ? result.rowsAffected : 0
19363
19360
  };
19364
19361
  }
19365
- generateContext(ctx) {
19366
- return {
19367
- ...ctx,
19368
- // 'executeRaw' is not implemented in RNQS, this falls back to 'execute'.
19369
- executeRaw: async (sql, params) => {
19370
- const result = await ctx.execute(sql, params);
19371
- const rows = result.rows?._array ?? [];
19372
- return rows.map((row) => Object.values(row));
19373
- }
19374
- };
19375
- }
19376
- /**
19377
- * This provides a top-level read only execute method which is executed inside a read-lock.
19378
- * This is necessary since the high level `execute` method uses a write-lock under
19379
- * the hood. Helper methods such as `get`, `getAll` and `getOptional` are read only,
19380
- * and should use this method.
19381
- */
19382
- readOnlyExecute(sql, params) {
19383
- return this.baseDB.readLock((ctx) => ctx.execute(sql, params));
19384
- }
19385
- /**
19386
- * Adds DB get utils to lock contexts and transaction contexts
19387
- * @param tx
19388
- * @returns
19389
- */
19390
- generateDBHelpers(tx) {
19391
- return {
19392
- ...tx,
19393
- /**
19394
- * Execute a read-only query and return results
19395
- */
19396
- getAll: async (sql, parameters) => {
19397
- const res = await tx.execute(sql, parameters);
19398
- return res.rows?._array ?? [];
19399
- },
19400
- /**
19401
- * Execute a read-only query and return the first result, or null if the ResultSet is empty.
19402
- */
19403
- getOptional: async (sql, parameters) => {
19404
- const res = await tx.execute(sql, parameters);
19405
- return res.rows?.item(0) ?? null;
19406
- },
19407
- /**
19408
- * Execute a read-only query and return the first result, error if the ResultSet is empty.
19409
- */
19410
- get: async (sql, parameters) => {
19411
- const res = await tx.execute(sql, parameters);
19412
- const first = res.rows?.item(0);
19413
- if (!first) {
19414
- throw new Error('Result set is empty');
19415
- }
19416
- return first;
19417
- }
19418
- };
19419
- }
19420
- async refreshSchema() {
19421
- await this.baseDB.refreshSchema();
19422
- }
19423
19362
  }
19424
19363
 
19425
19364
  /**