@powersync/common 0.0.0-dev-20260305092446 → 0.0.0-dev-20260306114652

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/bundle.cjs CHANGED
@@ -10457,7 +10457,7 @@ function requireDist () {
10457
10457
 
10458
10458
  var distExports = requireDist();
10459
10459
 
10460
- var version = "1.47.0";
10460
+ var version = "1.48.0";
10461
10461
  var PACKAGE = {
10462
10462
  version: version};
10463
10463
 
@@ -12430,8 +12430,9 @@ class TriggerManagerImpl {
12430
12430
  ...config
12431
12431
  };
12432
12432
  }
12433
- generateTriggerName(operation, destinationTable, triggerId) {
12434
- return `__ps_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12433
+ generateTriggerName(operation, destinationTable, triggerId, managedExternally = false) {
12434
+ const managedTerm = managedExternally ? '_external' : '';
12435
+ return `__ps${managedTerm}_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12435
12436
  }
12436
12437
  /**
12437
12438
  * Cleanup any SQLite triggers or tables that are no longer in use.
@@ -12488,9 +12489,29 @@ class TriggerManagerImpl {
12488
12489
  }
12489
12490
  });
12490
12491
  }
12492
+ /**
12493
+ * Creates a diff trigger destination table on the database with the given configuration.
12494
+ * By default this is invoked internally when creating a diff trigger, but can
12495
+ * be used manually if `manageDestinationExternally` is set to true.
12496
+ */
12497
+ async createDiffDestinationTable(tableName, options) {
12498
+ const tableTriggerTypeClause = !options?.useStorage ? 'TEMP' : '';
12499
+ const onlyIfNotExists = options?.onlyIfNotExists ? 'IF NOT EXISTS' : '';
12500
+ const ctx = options?.lockContext ?? this.db.database;
12501
+ await ctx.execute(/* sql */ `
12502
+ CREATE ${tableTriggerTypeClause} TABLE ${onlyIfNotExists} ${tableName} (
12503
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12504
+ id TEXT,
12505
+ operation TEXT,
12506
+ timestamp TEXT,
12507
+ value TEXT,
12508
+ previous_value TEXT
12509
+ )
12510
+ `);
12511
+ }
12491
12512
  async createDiffTrigger(options) {
12492
12513
  await this.db.waitForReady();
12493
- const { source, destination, columns, when, hooks, persistDestination = false,
12514
+ const { source, destination, columns, when, hooks, manageDestinationExternally = false,
12494
12515
  // Fall back to the provided default if not given on this level
12495
12516
  useStorage = this.defaultConfig.useStorageByDefault } = options;
12496
12517
  const operations = Object.keys(when);
@@ -12545,11 +12566,11 @@ class TriggerManagerImpl {
12545
12566
  * we need to ensure we can cleanup the created resources.
12546
12567
  * We unfortunately cannot rely on transaction rollback.
12547
12568
  */
12548
- const cleanup = async (force) => {
12569
+ const cleanup = async () => {
12549
12570
  disposeWarningListener();
12550
12571
  return this.db.writeLock(async (tx) => {
12551
12572
  await this.removeTriggers(tx, triggerIds);
12552
- if (!persistDestination || force) {
12573
+ if (!manageDestinationExternally) {
12553
12574
  await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
12554
12575
  }
12555
12576
  await releaseStorageClaim?.();
@@ -12558,18 +12579,15 @@ class TriggerManagerImpl {
12558
12579
  const setup = async (tx) => {
12559
12580
  // Allow user code to execute in this lock context before the trigger is created.
12560
12581
  await hooks?.beforeCreate?.(tx);
12561
- await tx.execute(/* sql */ `
12562
- CREATE ${tableTriggerTypeClause} TABLE ${persistDestination ? 'IF NOT EXISTS ' : ''}${destination} (
12563
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12564
- id TEXT,
12565
- operation TEXT,
12566
- timestamp TEXT,
12567
- value TEXT,
12568
- previous_value TEXT
12569
- )
12570
- `);
12582
+ if (!manageDestinationExternally) {
12583
+ await this.createDiffDestinationTable(destination, {
12584
+ lockContext: tx,
12585
+ useStorage,
12586
+ onlyIfNotExists: false
12587
+ });
12588
+ }
12571
12589
  if (operations.includes(exports.DiffTriggerOperation.INSERT)) {
12572
- const insertTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.INSERT, destination, id);
12590
+ const insertTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.INSERT, destination, id, manageDestinationExternally);
12573
12591
  triggerIds.push(insertTriggerId);
12574
12592
  await tx.execute(/* sql */ `
12575
12593
  CREATE ${tableTriggerTypeClause} TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[exports.DiffTriggerOperation.INSERT]} BEGIN
@@ -12587,7 +12605,7 @@ class TriggerManagerImpl {
12587
12605
  `);
12588
12606
  }
12589
12607
  if (operations.includes(exports.DiffTriggerOperation.UPDATE)) {
12590
- const updateTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.UPDATE, destination, id);
12608
+ const updateTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.UPDATE, destination, id, manageDestinationExternally);
12591
12609
  triggerIds.push(updateTriggerId);
12592
12610
  await tx.execute(/* sql */ `
12593
12611
  CREATE ${tableTriggerTypeClause} TRIGGER ${updateTriggerId} AFTER
@@ -12607,7 +12625,7 @@ class TriggerManagerImpl {
12607
12625
  `);
12608
12626
  }
12609
12627
  if (operations.includes(exports.DiffTriggerOperation.DELETE)) {
12610
- const deleteTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.DELETE, destination, id);
12628
+ const deleteTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.DELETE, destination, id, manageDestinationExternally);
12611
12629
  triggerIds.push(deleteTriggerId);
12612
12630
  // Create delete trigger for basic JSON
12613
12631
  await tx.execute(/* sql */ `