@powersync/common 0.0.0-dev-20260305124002 → 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.mjs CHANGED
@@ -10455,7 +10455,7 @@ function requireDist () {
10455
10455
 
10456
10456
  var distExports = requireDist();
10457
10457
 
10458
- var version = "1.47.0";
10458
+ var version = "1.48.0";
10459
10459
  var PACKAGE = {
10460
10460
  version: version};
10461
10461
 
@@ -12428,8 +12428,9 @@ class TriggerManagerImpl {
12428
12428
  ...config
12429
12429
  };
12430
12430
  }
12431
- generateTriggerName(operation, destinationTable, triggerId) {
12432
- return `__ps_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12431
+ generateTriggerName(operation, destinationTable, triggerId, managedExternally = false) {
12432
+ const managedTerm = managedExternally ? '_external' : '';
12433
+ return `__ps${managedTerm}_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12433
12434
  }
12434
12435
  /**
12435
12436
  * Cleanup any SQLite triggers or tables that are no longer in use.
@@ -12486,6 +12487,26 @@ class TriggerManagerImpl {
12486
12487
  }
12487
12488
  });
12488
12489
  }
12490
+ /**
12491
+ * Creates a diff trigger destination table on the database with the given configuration.
12492
+ * By default this is invoked internally when creating a diff trigger, but can
12493
+ * be used manually if `manageDestinationExternally` is set to true.
12494
+ */
12495
+ async createDiffDestinationTable(tableName, options) {
12496
+ const tableTriggerTypeClause = !options?.useStorage ? 'TEMP' : '';
12497
+ const onlyIfNotExists = options?.onlyIfNotExists ? 'IF NOT EXISTS' : '';
12498
+ const ctx = options?.lockContext ?? this.db.database;
12499
+ await ctx.execute(/* sql */ `
12500
+ CREATE ${tableTriggerTypeClause} TABLE ${onlyIfNotExists} ${tableName} (
12501
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12502
+ id TEXT,
12503
+ operation TEXT,
12504
+ timestamp TEXT,
12505
+ value TEXT,
12506
+ previous_value TEXT
12507
+ )
12508
+ `);
12509
+ }
12489
12510
  async createDiffTrigger(options) {
12490
12511
  await this.db.waitForReady();
12491
12512
  const { source, destination, columns, when, hooks, manageDestinationExternally = false,
@@ -12557,19 +12578,14 @@ class TriggerManagerImpl {
12557
12578
  // Allow user code to execute in this lock context before the trigger is created.
12558
12579
  await hooks?.beforeCreate?.(tx);
12559
12580
  if (!manageDestinationExternally) {
12560
- await tx.execute(/* sql */ `
12561
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12562
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12563
- id TEXT,
12564
- operation TEXT,
12565
- timestamp TEXT,
12566
- value TEXT,
12567
- previous_value TEXT
12568
- )
12569
- `);
12581
+ await this.createDiffDestinationTable(destination, {
12582
+ lockContext: tx,
12583
+ useStorage,
12584
+ onlyIfNotExists: false
12585
+ });
12570
12586
  }
12571
12587
  if (operations.includes(DiffTriggerOperation.INSERT)) {
12572
- const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id);
12588
+ const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id, manageDestinationExternally);
12573
12589
  triggerIds.push(insertTriggerId);
12574
12590
  await tx.execute(/* sql */ `
12575
12591
  CREATE ${tableTriggerTypeClause} TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[DiffTriggerOperation.INSERT]} BEGIN
@@ -12587,7 +12603,7 @@ class TriggerManagerImpl {
12587
12603
  `);
12588
12604
  }
12589
12605
  if (operations.includes(DiffTriggerOperation.UPDATE)) {
12590
- const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id);
12606
+ const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id, manageDestinationExternally);
12591
12607
  triggerIds.push(updateTriggerId);
12592
12608
  await tx.execute(/* sql */ `
12593
12609
  CREATE ${tableTriggerTypeClause} TRIGGER ${updateTriggerId} AFTER
@@ -12607,7 +12623,7 @@ class TriggerManagerImpl {
12607
12623
  `);
12608
12624
  }
12609
12625
  if (operations.includes(DiffTriggerOperation.DELETE)) {
12610
- const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id);
12626
+ const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id, manageDestinationExternally);
12611
12627
  triggerIds.push(deleteTriggerId);
12612
12628
  // Create delete trigger for basic JSON
12613
12629
  await tx.execute(/* sql */ `