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

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 (39) hide show
  1. package/dist/bundle.cjs +48 -53
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +49 -53
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +48 -53
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +49 -53
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +130 -80
  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 +5 -0
  13. package/lib/client/triggers/TriggerManagerImpl.d.ts +1 -1
  14. package/lib/client/triggers/TriggerManagerImpl.js +6 -4
  15. package/lib/client/triggers/TriggerManagerImpl.js.map +1 -1
  16. package/lib/db/schema/RawTable.d.ts +61 -26
  17. package/lib/db/schema/RawTable.js +1 -32
  18. package/lib/db/schema/RawTable.js.map +1 -1
  19. package/lib/db/schema/Schema.d.ts +14 -7
  20. package/lib/db/schema/Schema.js +25 -3
  21. package/lib/db/schema/Schema.js.map +1 -1
  22. package/lib/db/schema/Table.d.ts +13 -8
  23. package/lib/db/schema/Table.js +3 -8
  24. package/lib/db/schema/Table.js.map +1 -1
  25. package/lib/db/schema/internal.d.ts +12 -0
  26. package/lib/db/schema/internal.js +15 -0
  27. package/lib/db/schema/internal.js.map +1 -0
  28. package/lib/index.d.ts +1 -1
  29. package/lib/index.js +0 -1
  30. package/lib/index.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/client/AbstractPowerSyncDatabase.ts +1 -6
  33. package/src/client/triggers/TriggerManager.ts +6 -0
  34. package/src/client/triggers/TriggerManagerImpl.ts +6 -3
  35. package/src/db/schema/RawTable.ts +66 -31
  36. package/src/db/schema/Schema.ts +27 -2
  37. package/src/db/schema/Table.ts +11 -11
  38. package/src/db/schema/internal.ts +17 -0
  39. 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, persistDestination = 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);
@@ -12536,11 +12545,13 @@ class TriggerManagerImpl {
12536
12545
  * we need to ensure we can cleanup the created resources.
12537
12546
  * We unfortunately cannot rely on transaction rollback.
12538
12547
  */
12539
- const cleanup = async () => {
12548
+ const cleanup = async (force) => {
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 (!persistDestination || force) {
12553
+ await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
12554
+ }
12544
12555
  await releaseStorageClaim?.();
12545
12556
  });
12546
12557
  };
@@ -12548,7 +12559,7 @@ class TriggerManagerImpl {
12548
12559
  // Allow user code to execute in this lock context before the trigger is created.
12549
12560
  await hooks?.beforeCreate?.(tx);
12550
12561
  await tx.execute(/* sql */ `
12551
- CREATE ${tableTriggerTypeClause} TABLE ${destination} (
12562
+ CREATE ${tableTriggerTypeClause} TABLE ${persistDestination ? 'IF NOT EXISTS ' : ''}${destination} (
12552
12563
  operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
12553
12564
  id TEXT,
12554
12565
  operation TEXT,
@@ -13024,11 +13035,7 @@ class AbstractPowerSyncDatabase extends BaseObserver {
13024
13035
  this.logger.warn('Schema validation failed. Unexpected behaviour could occur', ex);
13025
13036
  }
13026
13037
  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
- });
13038
+ await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
13032
13039
  await this.database.refreshSchema();
13033
13040
  this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
13034
13041
  }
@@ -14133,39 +14140,6 @@ class ConnectionClosedError extends Error {
14133
14140
  }
14134
14141
  }
14135
14142
 
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
14143
  /**
14170
14144
  * A schema is a collection of tables. It is used to define the structure of a database.
14171
14145
  */
@@ -14210,7 +14184,7 @@ class Schema {
14210
14184
  */
14211
14185
  withRawTables(tables) {
14212
14186
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
14213
- this.rawTables.push(new RawTable(name, rawTableDefinition));
14187
+ this.rawTables.push({ name, ...rawTableDefinition });
14214
14188
  }
14215
14189
  }
14216
14190
  validate() {
@@ -14221,8 +14195,30 @@ class Schema {
14221
14195
  toJSON() {
14222
14196
  return {
14223
14197
  tables: this.tables.map((t) => t.toJSON()),
14224
- raw_tables: this.rawTables
14198
+ raw_tables: this.rawTables.map(Schema.rawTableToJson)
14199
+ };
14200
+ }
14201
+ /**
14202
+ * Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
14203
+ *
14204
+ * The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
14205
+ * to define triggers for this table.
14206
+ */
14207
+ static rawTableToJson(table) {
14208
+ const serialized = {
14209
+ name: table.name,
14210
+ put: table.put,
14211
+ delete: table.delete,
14212
+ clear: table.clear
14225
14213
  };
14214
+ if ('schema' in table) {
14215
+ // We have schema options, those are flattened into the outer JSON object for the core extension.
14216
+ const schema = table.schema;
14217
+ serialized.table_name = schema.tableName ?? table.name;
14218
+ serialized.synced_columns = schema.syncedColumns;
14219
+ Object.assign(serialized, encodeTableOptions(table.schema));
14220
+ }
14221
+ return serialized;
14226
14222
  }
14227
14223
  }
14228
14224
 
@@ -14435,7 +14431,6 @@ exports.MEMORY_TRIGGER_CLAIM_MANAGER = MEMORY_TRIGGER_CLAIM_MANAGER;
14435
14431
  exports.OnChangeQueryProcessor = OnChangeQueryProcessor;
14436
14432
  exports.OpType = OpType;
14437
14433
  exports.OplogEntry = OplogEntry;
14438
- exports.RawTable = RawTable;
14439
14434
  exports.Schema = Schema;
14440
14435
  exports.SqliteBucketStorage = SqliteBucketStorage;
14441
14436
  exports.SyncDataBatch = SyncDataBatch;