@powersync/common 0.0.0-dev-20260226160529 → 0.0.0-dev-20260305124002

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.
Files changed (38) hide show
  1. package/dist/bundle.cjs +58 -61
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +59 -61
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +58 -61
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +59 -61
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +130 -79
  10. package/lib/client/AbstractPowerSyncDatabase.js +1 -5
  11. package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
  12. package/lib/client/triggers/TriggerManager.d.ts +6 -0
  13. package/lib/client/triggers/TriggerManagerImpl.js +16 -12
  14. package/lib/client/triggers/TriggerManagerImpl.js.map +1 -1
  15. package/lib/db/schema/RawTable.d.ts +61 -26
  16. package/lib/db/schema/RawTable.js +1 -32
  17. package/lib/db/schema/RawTable.js.map +1 -1
  18. package/lib/db/schema/Schema.d.ts +14 -7
  19. package/lib/db/schema/Schema.js +25 -3
  20. package/lib/db/schema/Schema.js.map +1 -1
  21. package/lib/db/schema/Table.d.ts +13 -8
  22. package/lib/db/schema/Table.js +3 -8
  23. package/lib/db/schema/Table.js.map +1 -1
  24. package/lib/db/schema/internal.d.ts +12 -0
  25. package/lib/db/schema/internal.js +15 -0
  26. package/lib/db/schema/internal.js.map +1 -0
  27. package/lib/index.d.ts +1 -1
  28. package/lib/index.js +0 -1
  29. package/lib/index.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/client/AbstractPowerSyncDatabase.ts +1 -6
  32. package/src/client/triggers/TriggerManager.ts +7 -0
  33. package/src/client/triggers/TriggerManagerImpl.ts +16 -11
  34. package/src/db/schema/RawTable.ts +66 -31
  35. package/src/db/schema/Schema.ts +27 -2
  36. package/src/db/schema/Table.ts +11 -11
  37. package/src/db/schema/internal.ts +17 -0
  38. package/src/index.ts +1 -1
package/dist/bundle.cjs CHANGED
@@ -103,6 +103,21 @@ class Index {
103
103
  }
104
104
  }
105
105
 
106
+ /**
107
+ * @internal Not exported from `index.ts`.
108
+ */
109
+ function encodeTableOptions(options) {
110
+ const trackPrevious = options.trackPrevious;
111
+ return {
112
+ local_only: options.localOnly,
113
+ insert_only: options.insertOnly,
114
+ include_old: trackPrevious && (trackPrevious.columns ?? true),
115
+ include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
116
+ include_metadata: options.trackMetadata,
117
+ ignore_empty_update: options.ignoreEmptyUpdates
118
+ };
119
+ }
120
+
106
121
  const DEFAULT_TABLE_OPTIONS = {
107
122
  indexes: [],
108
123
  insertOnly: false,
@@ -292,18 +307,12 @@ class Table {
292
307
  }
293
308
  }
294
309
  toJSON() {
295
- const trackPrevious = this.trackPrevious;
296
310
  return {
297
311
  name: this.name,
298
312
  view_name: this.viewName,
299
- local_only: this.localOnly,
300
- insert_only: this.insertOnly,
301
- include_old: trackPrevious && (trackPrevious.columns ?? true),
302
- include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
303
- include_metadata: this.trackMetadata,
304
- ignore_empty_update: this.ignoreEmptyUpdates,
305
313
  columns: this.columns.map((c) => c.toJSON()),
306
- indexes: this.indexes.map((e) => e.toJSON(this))
314
+ indexes: this.indexes.map((e) => e.toJSON(this)),
315
+ ...encodeTableOptions(this)
307
316
  };
308
317
  }
309
318
  }
@@ -12481,7 +12490,7 @@ class TriggerManagerImpl {
12481
12490
  }
12482
12491
  async createDiffTrigger(options) {
12483
12492
  await this.db.waitForReady();
12484
- const { source, destination, columns, when, hooks,
12493
+ const { source, destination, columns, when, hooks, manageDestinationExternally = false,
12485
12494
  // Fall back to the provided default if not given on this level
12486
12495
  useStorage = this.defaultConfig.useStorageByDefault } = options;
12487
12496
  const operations = Object.keys(when);
@@ -12540,23 +12549,27 @@ class TriggerManagerImpl {
12540
12549
  disposeWarningListener();
12541
12550
  return this.db.writeLock(async (tx) => {
12542
12551
  await this.removeTriggers(tx, triggerIds);
12543
- await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
12552
+ if (!manageDestinationExternally) {
12553
+ await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
12554
+ }
12544
12555
  await releaseStorageClaim?.();
12545
12556
  });
12546
12557
  };
12547
12558
  const setup = async (tx) => {
12548
12559
  // Allow user code to execute in this lock context before the trigger is created.
12549
12560
  await hooks?.beforeCreate?.(tx);
12550
- await tx.execute(/* sql */ `
12551
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12552
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12553
- id TEXT,
12554
- operation TEXT,
12555
- timestamp TEXT,
12556
- value TEXT,
12557
- previous_value TEXT
12558
- )
12559
- `);
12561
+ if (!manageDestinationExternally) {
12562
+ await tx.execute(/* sql */ `
12563
+ CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12564
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12565
+ id TEXT,
12566
+ operation TEXT,
12567
+ timestamp TEXT,
12568
+ value TEXT,
12569
+ previous_value TEXT
12570
+ )
12571
+ `);
12572
+ }
12560
12573
  if (operations.includes(exports.DiffTriggerOperation.INSERT)) {
12561
12574
  const insertTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.INSERT, destination, id);
12562
12575
  triggerIds.push(insertTriggerId);
@@ -13024,11 +13037,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
13024
13037
  this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
13025
13038
  }
13026
13039
  this._schema = schema;
13027
- // powersync_replace_schema starts a transaction internally, but we can be more efficient by using BEGIN EXCLUSIVE
13028
- // on some DB adapters.
13029
- this.database.writeTransaction((tx) => {
13030
- return tx.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
13031
- });
13040
+ await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
13032
13041
  await this.database.refreshSchema();
13033
13042
  this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
13034
13043
  }
@@ -14133,39 +14142,6 @@ class ConnectionClosedError extends Error {
14133
14142
  }
14134
14143
  }
14135
14144
 
14136
- /**
14137
- * Instructs PowerSync to sync data into a "raw" table.
14138
- *
14139
- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
14140
- * using client-side table and column constraints.
14141
- *
14142
- * To collect local writes to raw tables with PowerSync, custom triggers are required. See
14143
- * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
14144
- * using raw tables.
14145
- *
14146
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
14147
- *
14148
- * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
14149
- * stability guarantees.
14150
- */
14151
- class RawTable {
14152
- /**
14153
- * The name of the table.
14154
- *
14155
- * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
14156
- * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
14157
- * appears in the source / backend database) are to be handled specially.
14158
- */
14159
- name;
14160
- put;
14161
- delete;
14162
- constructor(name, type) {
14163
- this.name = name;
14164
- this.put = type.put;
14165
- this.delete = type.delete;
14166
- }
14167
- }
14168
-
14169
14145
  /**
14170
14146
  * A schema is a collection of tables. It is used to define the structure of a database.
14171
14147
  */
@@ -14210,7 +14186,7 @@ class Schema {
14210
14186
  */
14211
14187
  withRawTables(tables) {
14212
14188
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
14213
- this.rawTables.push(new RawTable(name, rawTableDefinition));
14189
+ this.rawTables.push({ name, ...rawTableDefinition });
14214
14190
  }
14215
14191
  }
14216
14192
  validate() {
@@ -14221,8 +14197,30 @@ class Schema {
14221
14197
  toJSON() {
14222
14198
  return {
14223
14199
  tables: this.tables.map((t) => t.toJSON()),
14224
- raw_tables: this.rawTables
14200
+ raw_tables: this.rawTables.map(Schema.rawTableToJson)
14201
+ };
14202
+ }
14203
+ /**
14204
+ * Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
14205
+ *
14206
+ * The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
14207
+ * to define triggers for this table.
14208
+ */
14209
+ static rawTableToJson(table) {
14210
+ const serialized = {
14211
+ name: table.name,
14212
+ put: table.put,
14213
+ delete: table.delete,
14214
+ clear: table.clear
14225
14215
  };
14216
+ if ('schema' in table) {
14217
+ // We have schema options, those are flattened into the outer JSON object for the core extension.
14218
+ const schema = table.schema;
14219
+ serialized.table_name = schema.tableName ?? table.name;
14220
+ serialized.synced_columns = schema.syncedColumns;
14221
+ Object.assign(serialized, encodeTableOptions(table.schema));
14222
+ }
14223
+ return serialized;
14226
14224
  }
14227
14225
  }
14228
14226
 
@@ -14435,7 +14433,6 @@ exports.MEMORY_TRIGGER_CLAIM_MANAGER = MEMORY_TRIGGER_CLAIM_MANAGER;
14435
14433
  exports.OnChangeQueryProcessor = OnChangeQueryProcessor;
14436
14434
  exports.OpType = OpType;
14437
14435
  exports.OplogEntry = OplogEntry;
14438
- exports.RawTable = RawTable;
14439
14436
  exports.Schema = Schema;
14440
14437
  exports.SqliteBucketStorage = SqliteBucketStorage;
14441
14438
  exports.SyncDataBatch = SyncDataBatch;