@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.
@@ -7933,7 +7933,7 @@ function requireDist () {
7933
7933
 
7934
7934
  var distExports = requireDist();
7935
7935
 
7936
- var version = "1.47.0";
7936
+ var version = "1.48.0";
7937
7937
  var PACKAGE = {
7938
7938
  version: version};
7939
7939
 
@@ -9906,8 +9906,9 @@ class TriggerManagerImpl {
9906
9906
  ...config
9907
9907
  };
9908
9908
  }
9909
- generateTriggerName(operation, destinationTable, triggerId) {
9910
- return `__ps_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
9909
+ generateTriggerName(operation, destinationTable, triggerId, managedExternally = false) {
9910
+ const managedTerm = managedExternally ? '_external' : '';
9911
+ return `__ps${managedTerm}_temp_trigger_${operation.toLowerCase()}__${destinationTable}__${triggerId}`;
9911
9912
  }
9912
9913
  /**
9913
9914
  * Cleanup any SQLite triggers or tables that are no longer in use.
@@ -9964,9 +9965,29 @@ class TriggerManagerImpl {
9964
9965
  }
9965
9966
  });
9966
9967
  }
9968
+ /**
9969
+ * Creates a diff trigger destination table on the database with the given configuration.
9970
+ * By default this is invoked internally when creating a diff trigger, but can
9971
+ * be used manually if `manageDestinationExternally` is set to true.
9972
+ */
9973
+ async createDiffDestinationTable(tableName, options) {
9974
+ const tableTriggerTypeClause = !options?.useStorage ? 'TEMP' : '';
9975
+ const onlyIfNotExists = options?.onlyIfNotExists ? 'IF NOT EXISTS' : '';
9976
+ const ctx = options?.lockContext ?? this.db.database;
9977
+ await ctx.execute(/* sql */ `
9978
+ CREATE ${tableTriggerTypeClause} TABLE ${onlyIfNotExists} ${tableName} (
9979
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
9980
+ id TEXT,
9981
+ operation TEXT,
9982
+ timestamp TEXT,
9983
+ value TEXT,
9984
+ previous_value TEXT
9985
+ )
9986
+ `);
9987
+ }
9967
9988
  async createDiffTrigger(options) {
9968
9989
  await this.db.waitForReady();
9969
- const { source, destination, columns, when, hooks, persistDestination = false,
9990
+ const { source, destination, columns, when, hooks, manageDestinationExternally = false,
9970
9991
  // Fall back to the provided default if not given on this level
9971
9992
  useStorage = this.defaultConfig.useStorageByDefault } = options;
9972
9993
  const operations = Object.keys(when);
@@ -10021,11 +10042,11 @@ class TriggerManagerImpl {
10021
10042
  * we need to ensure we can cleanup the created resources.
10022
10043
  * We unfortunately cannot rely on transaction rollback.
10023
10044
  */
10024
- const cleanup = async (force) => {
10045
+ const cleanup = async () => {
10025
10046
  disposeWarningListener();
10026
10047
  return this.db.writeLock(async (tx) => {
10027
10048
  await this.removeTriggers(tx, triggerIds);
10028
- if (!persistDestination || force) {
10049
+ if (!manageDestinationExternally) {
10029
10050
  await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
10030
10051
  }
10031
10052
  await releaseStorageClaim?.();
@@ -10034,18 +10055,15 @@ class TriggerManagerImpl {
10034
10055
  const setup = async (tx) => {
10035
10056
  // Allow user code to execute in this lock context before the trigger is created.
10036
10057
  await hooks?.beforeCreate?.(tx);
10037
- await tx.execute(/* sql */ `
10038
- CREATE ${tableTriggerTypeClause} TABLE ${persistDestination ? 'IF NOT EXISTS ' : ''}${destination} (
10039
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
10040
- id TEXT,
10041
- operation TEXT,
10042
- timestamp TEXT,
10043
- value TEXT,
10044
- previous_value TEXT
10045
- )
10046
- `);
10058
+ if (!manageDestinationExternally) {
10059
+ await this.createDiffDestinationTable(destination, {
10060
+ lockContext: tx,
10061
+ useStorage,
10062
+ onlyIfNotExists: false
10063
+ });
10064
+ }
10047
10065
  if (operations.includes(DiffTriggerOperation.INSERT)) {
10048
- const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id);
10066
+ const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id, manageDestinationExternally);
10049
10067
  triggerIds.push(insertTriggerId);
10050
10068
  await tx.execute(/* sql */ `
10051
10069
  CREATE ${tableTriggerTypeClause} TRIGGER ${insertTriggerId} AFTER INSERT ON ${internalSource} ${whenClauses[DiffTriggerOperation.INSERT]} BEGIN
@@ -10063,7 +10081,7 @@ class TriggerManagerImpl {
10063
10081
  `);
10064
10082
  }
10065
10083
  if (operations.includes(DiffTriggerOperation.UPDATE)) {
10066
- const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id);
10084
+ const updateTriggerId = this.generateTriggerName(DiffTriggerOperation.UPDATE, destination, id, manageDestinationExternally);
10067
10085
  triggerIds.push(updateTriggerId);
10068
10086
  await tx.execute(/* sql */ `
10069
10087
  CREATE ${tableTriggerTypeClause} TRIGGER ${updateTriggerId} AFTER
@@ -10083,7 +10101,7 @@ class TriggerManagerImpl {
10083
10101
  `);
10084
10102
  }
10085
10103
  if (operations.includes(DiffTriggerOperation.DELETE)) {
10086
- const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id);
10104
+ const deleteTriggerId = this.generateTriggerName(DiffTriggerOperation.DELETE, destination, id, manageDestinationExternally);
10087
10105
  triggerIds.push(deleteTriggerId);
10088
10106
  // Create delete trigger for basic JSON
10089
10107
  await tx.execute(/* sql */ `