@powersync/common 0.0.0-dev-20260309101613 → 0.0.0-dev-20260311081226

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
@@ -12428,9 +12428,8 @@ class TriggerManagerImpl {
12428
12428
  ...config
12429
12429
  };
12430
12430
  }
12431
- generateTriggerName(operation, destinationTable, triggerId, managedExternally = false) {
12432
- const managedTerm = managedExternally ? '_external' : '';
12433
- return `__ps${managedTerm}_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12431
+ generateTriggerName(operation, destinationTable, triggerId) {
12432
+ return `__ps_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
12434
12433
  }
12435
12434
  /**
12436
12435
  * Cleanup any SQLite triggers or tables that are no longer in use.
@@ -12487,24 +12486,9 @@ class TriggerManagerImpl {
12487
12486
  }
12488
12487
  });
12489
12488
  }
12490
- async createDiffDestinationTable(tableName, options) {
12491
- const { temporary = true, onlyIfNotExists = false } = options ?? {};
12492
- const tableTriggerTypeClause = temporary ? 'TEMP' : '';
12493
- const onlyIfNotExistsClause = onlyIfNotExists ? 'IF NOT EXISTS' : '';
12494
- await this.db.execute(/* sql */ `
12495
- CREATE ${tableTriggerTypeClause} TABLE ${onlyIfNotExistsClause} ${tableName} (
12496
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12497
- id TEXT,
12498
- operation TEXT,
12499
- timestamp TEXT,
12500
- value TEXT,
12501
- previous_value TEXT
12502
- )
12503
- `);
12504
- }
12505
12489
  async createDiffTrigger(options) {
12506
12490
  await this.db.waitForReady();
12507
- const { source, destination, columns, when, hooks, manageDestinationExternally = false,
12491
+ const { source, destination, columns, when, hooks, setupContext,
12508
12492
  // Fall back to the provided default if not given on this level
12509
12493
  useStorage = this.defaultConfig.useStorageByDefault } = options;
12510
12494
  const operations = Object.keys(when);
@@ -12531,7 +12515,7 @@ class TriggerManagerImpl {
12531
12515
  const internalSource = sourceDefinition.internalName;
12532
12516
  const triggerIds = [];
12533
12517
  const id = await this.getUUID();
12534
- const releaseStorageClaim = useStorage && !manageDestinationExternally ? await this.options.claimManager.obtainClaim(id) : null;
12518
+ const releaseStorageClaim = useStorage ? await this.options.claimManager.obtainClaim(id) : null;
12535
12519
  /**
12536
12520
  * We default to replicating all columns if no columns array is provided.
12537
12521
  */
@@ -12559,33 +12543,35 @@ class TriggerManagerImpl {
12559
12543
  * we need to ensure we can cleanup the created resources.
12560
12544
  * We unfortunately cannot rely on transaction rollback.
12561
12545
  */
12562
- const cleanup = async () => {
12546
+ const cleanup = async (context) => {
12563
12547
  disposeWarningListener();
12564
- return this.db.writeLock(async (tx) => {
12548
+ const doCleanup = async (tx) => {
12565
12549
  await this.removeTriggers(tx, triggerIds);
12566
- if (!manageDestinationExternally) {
12567
- await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
12568
- }
12550
+ await tx.execute(`DROP TABLE IF EXISTS ${destination};`);
12569
12551
  await releaseStorageClaim?.();
12570
- });
12552
+ };
12553
+ if (context) {
12554
+ await doCleanup(context);
12555
+ }
12556
+ else {
12557
+ await this.db.writeLock(doCleanup);
12558
+ }
12571
12559
  };
12572
12560
  const setup = async (tx) => {
12573
12561
  // Allow user code to execute in this lock context before the trigger is created.
12574
12562
  await hooks?.beforeCreate?.(tx);
12575
- if (!manageDestinationExternally) {
12576
- await tx.execute(/* sql */ `
12577
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12578
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12579
- id TEXT,
12580
- operation TEXT,
12581
- timestamp TEXT,
12582
- value TEXT,
12583
- previous_value TEXT
12584
- )
12585
- `);
12586
- }
12563
+ await tx.execute(/* sql */ `
12564
+ CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12565
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12566
+ id TEXT,
12567
+ operation TEXT,
12568
+ timestamp TEXT,
12569
+ value TEXT,
12570
+ previous_value TEXT
12571
+ )
12572
+ `);
12587
12573
  if (operations.includes(DiffTriggerOperation.INSERT)) {
12588
- const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id, manageDestinationExternally);
12574
+ const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id);
12589
12575
  triggerIds.push(insertTriggerId);
12590
12576
  await tx.execute(/* sql */ `
12591
12577
  CREATE ${tableTriggerTypeClause} TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[DiffTriggerOperation.INSERT]} BEGIN
@@ -12603,7 +12589,7 @@ class TriggerManagerImpl {
12603
12589
  `);
12604
12590
  }
12605
12591
  if (operations.includes(DiffTriggerOperation.UPDATE)) {
12606
- const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id, manageDestinationExternally);
12592
+ const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id);
12607
12593
  triggerIds.push(updateTriggerId);
12608
12594
  await tx.execute(/* sql */ `
12609
12595
  CREATE ${tableTriggerTypeClause} TRIGGER ${updateTriggerId} AFTER
@@ -12623,7 +12609,7 @@ class TriggerManagerImpl {
12623
12609
  `);
12624
12610
  }
12625
12611
  if (operations.includes(DiffTriggerOperation.DELETE)) {
12626
- const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id, manageDestinationExternally);
12612
+ const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id);
12627
12613
  triggerIds.push(deleteTriggerId);
12628
12614
  // Create delete trigger for basic JSON
12629
12615
  await tx.execute(/* sql */ `
@@ -12643,7 +12629,12 @@ class TriggerManagerImpl {
12643
12629
  }
12644
12630
  };
12645
12631
  try {
12646
- await this.db.writeLock(setup);
12632
+ if (setupContext) {
12633
+ await setup(setupContext);
12634
+ }
12635
+ else {
12636
+ await this.db.writeLock(setup);
12637
+ }
12647
12638
  return cleanup;
12648
12639
  }
12649
12640
  catch (error) {