@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.
@@ -9906,9 +9906,8 @@ class TriggerManagerImpl {
9906
9906
  ...config
9907
9907
  };
9908
9908
  }
9909
- generateTriggerName(operation, destinationTable, triggerId, managedExternally = false) {
9910
- const managedTerm = managedExternally ? '_external' : '';
9911
- return `__ps${managedTerm}_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
9909
+ generateTriggerName(operation, destinationTable, triggerId) {
9910
+ return `__ps_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
9912
9911
  }
9913
9912
  /**
9914
9913
  * Cleanup any SQLite triggers or tables that are no longer in use.
@@ -9965,24 +9964,9 @@ class TriggerManagerImpl {
9965
9964
  }
9966
9965
  });
9967
9966
  }
9968
- async createDiffDestinationTable(tableName, options) {
9969
- const { temporary = true, onlyIfNotExists = false } = options ?? {};
9970
- const tableTriggerTypeClause = temporary ? 'TEMP' : '';
9971
- const onlyIfNotExistsClause = onlyIfNotExists ? 'IF NOT EXISTS' : '';
9972
- await this.db.execute(/* sql */ `
9973
- CREATE ${tableTriggerTypeClause} TABLE ${onlyIfNotExistsClause} ${tableName} (
9974
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
9975
- id TEXT,
9976
- operation TEXT,
9977
- timestamp TEXT,
9978
- value TEXT,
9979
- previous_value TEXT
9980
- )
9981
- `);
9982
- }
9983
9967
  async createDiffTrigger(options) {
9984
9968
  await this.db.waitForReady();
9985
- const { source, destination, columns, when, hooks, manageDestinationExternally = false,
9969
+ const { source, destination, columns, when, hooks, setupContext,
9986
9970
  // Fall back to the provided default if not given on this level
9987
9971
  useStorage = this.defaultConfig.useStorageByDefault } = options;
9988
9972
  const operations = Object.keys(when);
@@ -10009,7 +9993,7 @@ class TriggerManagerImpl {
10009
9993
  const internalSource = sourceDefinition.internalName;
10010
9994
  const triggerIds = [];
10011
9995
  const id = await this.getUUID();
10012
- const releaseStorageClaim = useStorage && !manageDestinationExternally ? await this.options.claimManager.obtainClaim(id) : null;
9996
+ const releaseStorageClaim = useStorage ? await this.options.claimManager.obtainClaim(id) : null;
10013
9997
  /**
10014
9998
  * We default to replicating all columns if no columns array is provided.
10015
9999
  */
@@ -10037,33 +10021,35 @@ class TriggerManagerImpl {
10037
10021
  * we need to ensure we can cleanup the created resources.
10038
10022
  * We unfortunately cannot rely on transaction rollback.
10039
10023
  */
10040
- const cleanup = async () => {
10024
+ const cleanup = async (context) => {
10041
10025
  disposeWarningListener();
10042
- return this.db.writeLock(async (tx) => {
10026
+ const doCleanup = async (tx) => {
10043
10027
  await this.removeTriggers(tx, triggerIds);
10044
- if (!manageDestinationExternally) {
10045
- await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
10046
- }
10028
+ await tx.execute(`DROP TABLE IF EXISTS ${destination};`);
10047
10029
  await releaseStorageClaim?.();
10048
- });
10030
+ };
10031
+ if (context) {
10032
+ await doCleanup(context);
10033
+ }
10034
+ else {
10035
+ await this.db.writeLock(doCleanup);
10036
+ }
10049
10037
  };
10050
10038
  const setup = async (tx) => {
10051
10039
  // Allow user code to execute in this lock context before the trigger is created.
10052
10040
  await hooks?.beforeCreate?.(tx);
10053
- if (!manageDestinationExternally) {
10054
- await tx.execute(/* sql */ `
10055
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
10056
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
10057
- id TEXT,
10058
- operation TEXT,
10059
- timestamp TEXT,
10060
- value TEXT,
10061
- previous_value TEXT
10062
- )
10063
- `);
10064
- }
10041
+ await tx.execute(/* sql */ `
10042
+ CREATE ${tableTriggerTypeClause} TABLE ${destination} (
10043
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
10044
+ id TEXT,
10045
+ operation TEXT,
10046
+ timestamp TEXT,
10047
+ value TEXT,
10048
+ previous_value TEXT
10049
+ )
10050
+ `);
10065
10051
  if (operations.includes(DiffTriggerOperation.INSERT)) {
10066
- const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id, manageDestinationExternally);
10052
+ const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id);
10067
10053
  triggerIds.push(insertTriggerId);
10068
10054
  await tx.execute(/* sql */ `
10069
10055
  CREATE ${tableTriggerTypeClause} TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[DiffTriggerOperation.INSERT]} BEGIN
@@ -10081,7 +10067,7 @@ class TriggerManagerImpl {
10081
10067
  `);
10082
10068
  }
10083
10069
  if (operations.includes(DiffTriggerOperation.UPDATE)) {
10084
- const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id, manageDestinationExternally);
10070
+ const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id);
10085
10071
  triggerIds.push(updateTriggerId);
10086
10072
  await tx.execute(/* sql */ `
10087
10073
  CREATE ${tableTriggerTypeClause} TRIGGER ${updateTriggerId} AFTER
@@ -10101,7 +10087,7 @@ class TriggerManagerImpl {
10101
10087
  `);
10102
10088
  }
10103
10089
  if (operations.includes(DiffTriggerOperation.DELETE)) {
10104
- const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id, manageDestinationExternally);
10090
+ const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id);
10105
10091
  triggerIds.push(deleteTriggerId);
10106
10092
  // Create delete trigger for basic JSON
10107
10093
  await tx.execute(/* sql */ `
@@ -10121,7 +10107,12 @@ class TriggerManagerImpl {
10121
10107
  }
10122
10108
  };
10123
10109
  try {
10124
- await this.db.writeLock(setup);
10110
+ if (setupContext) {
10111
+ await setup(setupContext);
10112
+ }
10113
+ else {
10114
+ await this.db.writeLock(setup);
10115
+ }
10125
10116
  return cleanup;
10126
10117
  }
10127
10118
  catch (error) {