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