@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
@@ -105,6 +105,21 @@ class Index {
105
105
  }
106
106
  }
107
107
 
108
+ /**
109
+ * @internal Not exported from `index.ts`.
110
+ */
111
+ function encodeTableOptions(options) {
112
+ const trackPrevious = options.trackPrevious;
113
+ return {
114
+ local_only: options.localOnly,
115
+ insert_only: options.insertOnly,
116
+ include_old: trackPrevious && (trackPrevious.columns ?? true),
117
+ include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
118
+ include_metadata: options.trackMetadata,
119
+ ignore_empty_update: options.ignoreEmptyUpdates
120
+ };
121
+ }
122
+
108
123
  const DEFAULT_TABLE_OPTIONS = {
109
124
  indexes: [],
110
125
  insertOnly: false,
@@ -294,18 +309,12 @@ class Table {
294
309
  }
295
310
  }
296
311
  toJSON() {
297
- const trackPrevious = this.trackPrevious;
298
312
  return {
299
313
  name: this.name,
300
314
  view_name: this.viewName,
301
- local_only: this.localOnly,
302
- insert_only: this.insertOnly,
303
- include_old: trackPrevious && (trackPrevious.columns ?? true),
304
- include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
305
- include_metadata: this.trackMetadata,
306
- ignore_empty_update: this.ignoreEmptyUpdates,
307
315
  columns: this.columns.map((c) => c.toJSON()),
308
- indexes: this.indexes.map((e) => e.toJSON(this))
316
+ indexes: this.indexes.map((e) => e.toJSON(this)),
317
+ ...encodeTableOptions(this)
309
318
  };
310
319
  }
311
320
  }
@@ -9959,7 +9968,7 @@ class TriggerManagerImpl {
9959
9968
  }
9960
9969
  async createDiffTrigger(options) {
9961
9970
  await this.db.waitForReady();
9962
- const { source, destination, columns, when, hooks,
9971
+ const { source, destination, columns, when, hooks, manageDestinationExternally = false,
9963
9972
  // Fall back to the provided default if not given on this level
9964
9973
  useStorage = this.defaultConfig.useStorageByDefault } = options;
9965
9974
  const operations = Object.keys(when);
@@ -10018,23 +10027,27 @@ class TriggerManagerImpl {
10018
10027
  disposeWarningListener();
10019
10028
  return this.db.writeLock(async (tx) => {
10020
10029
  await this.removeTriggers(tx, triggerIds);
10021
- await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
10030
+ if (!manageDestinationExternally) {
10031
+ await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
10032
+ }
10022
10033
  await releaseStorageClaim?.();
10023
10034
  });
10024
10035
  };
10025
10036
  const setup = async (tx) => {
10026
10037
  // Allow user code to execute in this lock context before the trigger is created.
10027
10038
  await hooks?.beforeCreate?.(tx);
10028
- await tx.execute(/* sql */ `
10029
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
10030
- operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
10031
- id TEXT,
10032
- operation TEXT,
10033
- timestamp TEXT,
10034
- value TEXT,
10035
- previous_value TEXT
10036
- )
10037
- `);
10039
+ if (!manageDestinationExternally) {
10040
+ await tx.execute(/* sql */ `
10041
+ CREATE ${tableTriggerTypeClause} TABLE ${destination} (
10042
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
10043
+ id TEXT,
10044
+ operation TEXT,
10045
+ timestamp TEXT,
10046
+ value TEXT,
10047
+ previous_value TEXT
10048
+ )
10049
+ `);
10050
+ }
10038
10051
  if (operations.includes(exports.DiffTriggerOperation.INSERT)) {
10039
10052
  const insertTriggerId = this.generateTriggerName(exports.DiffTriggerOperation.INSERT, destination, id);
10040
10053
  triggerIds.push(insertTriggerId);
@@ -10502,11 +10515,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
10502
10515
  this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
10503
10516
  }
10504
10517
  this._schema = schema;
10505
- // powersync_replace_schema starts a transaction internally, but we can be more efficient by using BEGIN EXCLUSIVE
10506
- // on some DB adapters.
10507
- this.database.writeTransaction((tx) => {
10508
- return tx.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
10509
- });
10518
+ await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
10510
10519
  await this.database.refreshSchema();
10511
10520
  this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
10512
10521
  }
@@ -11611,39 +11620,6 @@ class ConnectionClosedError extends Error {
11611
11620
  }
11612
11621
  }
11613
11622
 
11614
- /**
11615
- * Instructs PowerSync to sync data into a "raw" table.
11616
- *
11617
- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
11618
- * using client-side table and column constraints.
11619
- *
11620
- * To collect local writes to raw tables with PowerSync, custom triggers are required. See
11621
- * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
11622
- * using raw tables.
11623
- *
11624
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
11625
- *
11626
- * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
11627
- * stability guarantees.
11628
- */
11629
- class RawTable {
11630
- /**
11631
- * The name of the table.
11632
- *
11633
- * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
11634
- * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
11635
- * appears in the source / backend database) are to be handled specially.
11636
- */
11637
- name;
11638
- put;
11639
- delete;
11640
- constructor(name, type) {
11641
- this.name = name;
11642
- this.put = type.put;
11643
- this.delete = type.delete;
11644
- }
11645
- }
11646
-
11647
11623
  /**
11648
11624
  * A schema is a collection of tables. It is used to define the structure of a database.
11649
11625
  */
@@ -11688,7 +11664,7 @@ class Schema {
11688
11664
  */
11689
11665
  withRawTables(tables) {
11690
11666
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
11691
- this.rawTables.push(new RawTable(name, rawTableDefinition));
11667
+ this.rawTables.push({ name, ...rawTableDefinition });
11692
11668
  }
11693
11669
  }
11694
11670
  validate() {
@@ -11699,8 +11675,30 @@ class Schema {
11699
11675
  toJSON() {
11700
11676
  return {
11701
11677
  tables: this.tables.map((t) => t.toJSON()),
11702
- raw_tables: this.rawTables
11678
+ raw_tables: this.rawTables.map(Schema.rawTableToJson)
11679
+ };
11680
+ }
11681
+ /**
11682
+ * Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
11683
+ *
11684
+ * The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
11685
+ * to define triggers for this table.
11686
+ */
11687
+ static rawTableToJson(table) {
11688
+ const serialized = {
11689
+ name: table.name,
11690
+ put: table.put,
11691
+ delete: table.delete,
11692
+ clear: table.clear
11703
11693
  };
11694
+ if ('schema' in table) {
11695
+ // We have schema options, those are flattened into the outer JSON object for the core extension.
11696
+ const schema = table.schema;
11697
+ serialized.table_name = schema.tableName ?? table.name;
11698
+ serialized.synced_columns = schema.syncedColumns;
11699
+ Object.assign(serialized, encodeTableOptions(table.schema));
11700
+ }
11701
+ return serialized;
11704
11702
  }
11705
11703
  }
11706
11704
 
@@ -11913,7 +11911,6 @@ exports.MEMORY_TRIGGER_CLAIM_MANAGER = MEMORY_TRIGGER_CLAIM_MANAGER;
11913
11911
  exports.OnChangeQueryProcessor = OnChangeQueryProcessor;
11914
11912
  exports.OpType = OpType;
11915
11913
  exports.OplogEntry = OplogEntry;
11916
- exports.RawTable = RawTable;
11917
11914
  exports.Schema = Schema;
11918
11915
  exports.SqliteBucketStorage = SqliteBucketStorage;
11919
11916
  exports.SyncDataBatch = SyncDataBatch;