@powersync/common 1.47.0 → 1.48.0

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.
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @internal Not exported from `index.ts`.
3
+ */
4
+ export function encodeTableOptions(options) {
5
+ const trackPrevious = options.trackPrevious;
6
+ return {
7
+ local_only: options.localOnly,
8
+ insert_only: options.insertOnly,
9
+ include_old: trackPrevious && (trackPrevious.columns ?? true),
10
+ include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
11
+ include_metadata: options.trackMetadata,
12
+ ignore_empty_update: options.ignoreEmptyUpdates
13
+ };
14
+ }
15
+ //# sourceMappingURL=internal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.js","sourceRoot":"","sources":["../../../src/db/schema/internal.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAA+B;IAChE,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAE5C,OAAO;QACL,UAAU,EAAE,OAAO,CAAC,SAAS;QAC7B,WAAW,EAAE,OAAO,CAAC,UAAU;QAC/B,WAAW,EAAE,aAAa,IAAI,CAAE,aAAqB,CAAC,OAAO,IAAI,IAAI,CAAC;QACtE,6BAA6B,EAAE,OAAO,aAAa,IAAI,QAAQ,IAAI,aAAa,CAAC,eAAe,IAAI,IAAI;QACxG,gBAAgB,EAAE,OAAO,CAAC,aAAa;QACvC,mBAAmB,EAAE,OAAO,CAAC,kBAAkB;KAChD,CAAC;AACJ,CAAC"}
package/lib/index.d.ts CHANGED
@@ -37,7 +37,7 @@ export * from './db/DBAdapter.js';
37
37
  export * from './db/schema/Column.js';
38
38
  export * from './db/schema/Index.js';
39
39
  export * from './db/schema/IndexedColumn.js';
40
- export * from './db/schema/RawTable.js';
40
+ export { RawTableType, PendingStatementParameter, PendingStatement } from './db/schema/RawTable.js';
41
41
  export * from './db/schema/Schema.js';
42
42
  export * from './db/schema/Table.js';
43
43
  export * from './db/schema/TableV2.js';
package/lib/index.js CHANGED
@@ -37,7 +37,6 @@ export * from './db/DBAdapter.js';
37
37
  export * from './db/schema/Column.js';
38
38
  export * from './db/schema/Index.js';
39
39
  export * from './db/schema/IndexedColumn.js';
40
- export * from './db/schema/RawTable.js';
41
40
  export * from './db/schema/Schema.js';
42
41
  export * from './db/schema/Table.js';
43
42
  export * from './db/schema/TableV2.js';
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,yCAAyC,CAAC;AACxD,cAAc,kCAAkC,CAAC;AACjD,cAAc,oCAAoC,CAAC;AACnD,cAAc,sCAAsC,CAAC;AACrD,cAAc,uCAAuC,CAAC;AACtD,cAAc,yBAAyB,CAAC;AACxC,cAAc,iCAAiC,CAAC;AAChD,cAAc,wCAAwC,CAAC;AAEvD,cAAc,uCAAuC,CAAC;AACtD,cAAc,0CAA0C,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAA+B,MAAM,kCAAkC,CAAC;AACrG,cAAc,kDAAkD,CAAC;AACjE,cAAc,6CAA6C,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8CAA8C,CAAC;AAC7D,cAAc,mCAAmC,CAAC;AAClD,OAAO,EAAE,SAAS,EAAQ,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAChF,cAAc,yCAAyC,CAAC;AACxD,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6CAA6C,CAAC;AAC5D,cAAc,uCAAuC,CAAC;AACtD,cAAc,wCAAwC,CAAC;AACvD,cAAc,wCAAwC,CAAC;AACvD,cAA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c,qCAAqC,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,cAAc,iCAAiC,CAAC;AAChD,cAAc,uDAAuD,CAAC;AACtE,cAAc,4CAA4C,CAAC;AAC3D,cAAc,2DAA2D,CAAC;AAC1E,cAAc,uDAAuD,CAAC;AACtE,cAAc,kCAAkC,CAAC;AAEjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,kBAAkB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/common",
3
- "version": "1.47.0",
3
+ "version": "1.48.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -1,8 +1,23 @@
1
+ import { TableOrRawTableOptions } from './Table.js';
2
+
1
3
  /**
2
- * A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the
3
- * schema).
4
+ * Instructs PowerSync to sync data into a "raw" table.
5
+ *
6
+ * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
7
+ * using client-side table and column constraints.
8
+ *
9
+ * To collect local writes to raw tables with PowerSync, custom triggers are required. See
10
+ * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
11
+ * using raw tables.
12
+ *
13
+ * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
14
+ *
15
+ * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
16
+ * stability guarantees.
4
17
  */
5
- export type RawTableType = {
18
+ export type RawTableType = RawTableTypeWithStatements | InferredRawTableType;
19
+
20
+ interface RawTableTypeWithStatements {
6
21
  /**
7
22
  * The statement to run when PowerSync detects that a row needs to be inserted or updated.
8
23
  */
@@ -11,7 +26,44 @@ export type RawTableType = {
11
26
  * The statement to run when PowerSync detects that a row needs to be deleted.
12
27
  */
13
28
  delete: PendingStatement;
14
- };
29
+
30
+ /**
31
+ * An optional statement to run when `disconnectAndClear()` is called on a PowerSync database.
32
+ */
33
+ clear?: string;
34
+ }
35
+
36
+ /**
37
+ * The schema of a {@link RawTableType} in the local database.
38
+ *
39
+ * This information is optional when declaring raw tables. However, providing it allows the sync client to infer `put`
40
+ * and `delete` statements automatically.
41
+ */
42
+ interface RawTableSchema extends TableOrRawTableOptions {
43
+ /**
44
+ * The actual name of the raw table in the local schema.
45
+ *
46
+ * Unlike {@link RawTable.name}, which describes the name of synced tables to match, this reflects the SQLite table
47
+ * name. This is used to infer {@link RawTableType.put} and {@link RawTableType.delete} statements for the sync
48
+ * client. It can also be used to auto-generate triggers forwarding writes on raw tables into the CRUD upload queue
49
+ * (using the `powersync_create_raw_table_crud_trigger` SQL function).
50
+ *
51
+ * When absent, defaults to {@link RawTable.name}.
52
+ */
53
+ tableName?: string;
54
+
55
+ /**
56
+ * An optional filter of columns that should be synced.
57
+ *
58
+ * By default, all columns in a raw table are considered for sync. If a filter is specified, PowerSync treats
59
+ * unmatched columns as local-only and will not attempt to sync them.
60
+ */
61
+ syncedColumns?: string[];
62
+ }
63
+
64
+ interface InferredRawTableType extends Partial<RawTableTypeWithStatements> {
65
+ schema: RawTableSchema;
66
+ }
15
67
 
16
68
  /**
17
69
  * A parameter to use as part of {@link PendingStatement}.
@@ -21,8 +73,10 @@ export type RawTableType = {
21
73
  *
22
74
  * For insert and replace operations, the values of columns in the table are available as parameters through
23
75
  * `{Column: 'name'}`.
76
+ * The `"Rest"` parameter gets resolved to a JSON object covering all values from the synced row that haven't been
77
+ * covered by a `Column` parameter.
24
78
  */
25
- export type PendingStatementParameter = 'Id' | { Column: string };
79
+ export type PendingStatementParameter = 'Id' | { Column: string } | 'Rest';
26
80
 
27
81
  /**
28
82
  * A statement that the PowerSync client should use to insert or delete data into a table managed by the user.
@@ -33,35 +87,16 @@ export type PendingStatement = {
33
87
  };
34
88
 
35
89
  /**
36
- * Instructs PowerSync to sync data into a "raw" table.
37
- *
38
- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
39
- * using client-side table and column constraints.
40
- *
41
- * To collect local writes to raw tables with PowerSync, custom triggers are required. See
42
- * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
43
- * using raw tables.
44
- *
45
- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
46
- *
47
- * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
48
- * stability guarantees.
90
+ * @internal
49
91
  */
50
- export class RawTable implements RawTableType {
92
+ export type RawTable<T extends RawTableType = RawTableType> = T & {
51
93
  /**
52
94
  * The name of the table.
53
95
  *
54
- * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
55
- * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
56
- * appears in the source / backend database) are to be handled specially.
96
+ * This does not have to match the actual table name in the schema - {@link RawTableType.put} and
97
+ * {@link RawTableType.delete} are free to use another table. Instead, this name is used by the sync client to
98
+ * recognize that operations on this table (as it appears in the source / backend database) are to be handled
99
+ * specially.
57
100
  */
58
101
  name: string;
59
- put: PendingStatement;
60
- delete: PendingStatement;
61
-
62
- constructor(name: string, type: RawTableType) {
63
- this.name = name;
64
- this.put = type.put;
65
- this.delete = type.delete;
66
- }
67
- }
102
+ };
@@ -1,3 +1,4 @@
1
+ import { encodeTableOptions } from './internal.js';
1
2
  import { RawTable, RawTableType } from './RawTable.js';
2
3
  import { RowType, Table } from './Table.js';
3
4
 
@@ -57,7 +58,7 @@ export class Schema<S extends SchemaType = SchemaType> {
57
58
  */
58
59
  withRawTables(tables: Record<string, RawTableType>) {
59
60
  for (const [name, rawTableDefinition] of Object.entries(tables)) {
60
- this.rawTables.push(new RawTable(name, rawTableDefinition));
61
+ this.rawTables.push({ name, ...rawTableDefinition });
61
62
  }
62
63
  }
63
64
 
@@ -70,7 +71,31 @@ export class Schema<S extends SchemaType = SchemaType> {
70
71
  toJSON() {
71
72
  return {
72
73
  tables: this.tables.map((t) => t.toJSON()),
73
- raw_tables: this.rawTables
74
+ raw_tables: this.rawTables.map(Schema.rawTableToJson)
74
75
  };
75
76
  }
77
+
78
+ /**
79
+ * Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
80
+ *
81
+ * The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
82
+ * to define triggers for this table.
83
+ */
84
+ static rawTableToJson(table: RawTable): unknown {
85
+ const serialized: any = {
86
+ name: table.name,
87
+ put: table.put,
88
+ delete: table.delete,
89
+ clear: table.clear
90
+ };
91
+ if ('schema' in table) {
92
+ // We have schema options, those are flattened into the outer JSON object for the core extension.
93
+ const schema = table.schema;
94
+ serialized.table_name = schema.tableName ?? table.name;
95
+ serialized.synced_columns = schema.syncedColumns;
96
+ Object.assign(serialized, encodeTableOptions(table.schema));
97
+ }
98
+
99
+ return serialized;
100
+ }
76
101
  }
@@ -8,17 +8,24 @@ import {
8
8
  } from './Column.js';
9
9
  import { Index } from './Index.js';
10
10
  import { IndexedColumn } from './IndexedColumn.js';
11
+ import { encodeTableOptions } from './internal.js';
11
12
  import { TableV2 } from './TableV2.js';
12
13
 
13
- interface SharedTableOptions {
14
+ /**
15
+ * Options that apply both to JSON-based tables and raw tables.
16
+ */
17
+ export interface TableOrRawTableOptions {
14
18
  localOnly?: boolean;
15
19
  insertOnly?: boolean;
16
- viewName?: string;
17
20
  trackPrevious?: boolean | TrackPreviousOptions;
18
21
  trackMetadata?: boolean;
19
22
  ignoreEmptyUpdates?: boolean;
20
23
  }
21
24
 
25
+ interface SharedTableOptions extends TableOrRawTableOptions {
26
+ viewName?: string;
27
+ }
28
+
22
29
  /** Whether to include previous column values when PowerSync tracks local changes.
23
30
  *
24
31
  * Including old values may be helpful for some backend connector implementations, which is
@@ -341,19 +348,12 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
341
348
  }
342
349
 
343
350
  toJSON() {
344
- const trackPrevious = this.trackPrevious;
345
-
346
351
  return {
347
352
  name: this.name,
348
353
  view_name: this.viewName,
349
- local_only: this.localOnly,
350
- insert_only: this.insertOnly,
351
- include_old: trackPrevious && ((trackPrevious as any).columns ?? true),
352
- include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
353
- include_metadata: this.trackMetadata,
354
- ignore_empty_update: this.ignoreEmptyUpdates,
355
354
  columns: this.columns.map((c) => c.toJSON()),
356
- indexes: this.indexes.map((e) => e.toJSON(this))
355
+ indexes: this.indexes.map((e) => e.toJSON(this)),
356
+ ...encodeTableOptions(this)
357
357
  };
358
358
  }
359
359
  }
@@ -0,0 +1,17 @@
1
+ import { TableOrRawTableOptions } from './Table.js';
2
+
3
+ /**
4
+ * @internal Not exported from `index.ts`.
5
+ */
6
+ export function encodeTableOptions(options: TableOrRawTableOptions) {
7
+ const trackPrevious = options.trackPrevious;
8
+
9
+ return {
10
+ local_only: options.localOnly,
11
+ insert_only: options.insertOnly,
12
+ include_old: trackPrevious && ((trackPrevious as any).columns ?? true),
13
+ include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
14
+ include_metadata: options.trackMetadata,
15
+ ignore_empty_update: options.ignoreEmptyUpdates
16
+ };
17
+ }
package/src/index.ts CHANGED
@@ -39,7 +39,7 @@ export * from './db/DBAdapter.js';
39
39
  export * from './db/schema/Column.js';
40
40
  export * from './db/schema/Index.js';
41
41
  export * from './db/schema/IndexedColumn.js';
42
- export * from './db/schema/RawTable.js';
42
+ export { RawTableType, PendingStatementParameter, PendingStatement } from './db/schema/RawTable.js';
43
43
  export * from './db/schema/Schema.js';
44
44
  export * from './db/schema/Table.js';
45
45
  export * from './db/schema/TableV2.js';