@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.
- package/dist/bundle.cjs +58 -61
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +59 -61
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +58 -61
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +59 -61
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +130 -79
- package/lib/client/AbstractPowerSyncDatabase.js +1 -5
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/triggers/TriggerManager.d.ts +6 -0
- package/lib/client/triggers/TriggerManagerImpl.js +16 -12
- package/lib/client/triggers/TriggerManagerImpl.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +61 -26
- package/lib/db/schema/RawTable.js +1 -32
- package/lib/db/schema/RawTable.js.map +1 -1
- package/lib/db/schema/Schema.d.ts +14 -7
- package/lib/db/schema/Schema.js +25 -3
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +13 -8
- package/lib/db/schema/Table.js +3 -8
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/db/schema/internal.d.ts +12 -0
- package/lib/db/schema/internal.js +15 -0
- package/lib/db/schema/internal.js.map +1 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/AbstractPowerSyncDatabase.ts +1 -6
- package/src/client/triggers/TriggerManager.ts +7 -0
- package/src/client/triggers/TriggerManagerImpl.ts +16 -11
- package/src/db/schema/RawTable.ts +66 -31
- package/src/db/schema/Schema.ts +27 -2
- package/src/db/schema/Table.ts +11 -11
- package/src/db/schema/internal.ts +17 -0
- package/src/index.ts +1 -1
|
@@ -1,33 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Instructs PowerSync to sync data into a "raw" table.
|
|
3
|
-
*
|
|
4
|
-
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
|
|
5
|
-
* using client-side table and column constraints.
|
|
6
|
-
*
|
|
7
|
-
* To collect local writes to raw tables with PowerSync, custom triggers are required. See
|
|
8
|
-
* {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
|
|
9
|
-
* using raw tables.
|
|
10
|
-
*
|
|
11
|
-
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
|
|
12
|
-
*
|
|
13
|
-
* @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
|
|
14
|
-
* stability guarantees.
|
|
15
|
-
*/
|
|
16
|
-
export class RawTable {
|
|
17
|
-
/**
|
|
18
|
-
* The name of the table.
|
|
19
|
-
*
|
|
20
|
-
* This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
|
|
21
|
-
* another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
|
|
22
|
-
* appears in the source / backend database) are to be handled specially.
|
|
23
|
-
*/
|
|
24
|
-
name;
|
|
25
|
-
put;
|
|
26
|
-
delete;
|
|
27
|
-
constructor(name, type) {
|
|
28
|
-
this.name = name;
|
|
29
|
-
this.put = type.put;
|
|
30
|
-
this.delete = type.delete;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
1
|
+
export {};
|
|
33
2
|
//# sourceMappingURL=RawTable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RawTable.js","sourceRoot":"","sources":["../../../src/db/schema/RawTable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"RawTable.js","sourceRoot":"","sources":["../../../src/db/schema/RawTable.ts"],"names":[],"mappings":""}
|
|
@@ -27,14 +27,14 @@ export declare class Schema<S extends SchemaType = SchemaType> {
|
|
|
27
27
|
validate(): void;
|
|
28
28
|
toJSON(): {
|
|
29
29
|
tables: {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
local_only: boolean;
|
|
33
|
-
insert_only: boolean;
|
|
30
|
+
local_only: boolean | undefined;
|
|
31
|
+
insert_only: boolean | undefined;
|
|
34
32
|
include_old: any;
|
|
35
33
|
include_old_only_when_changed: boolean;
|
|
36
|
-
include_metadata: boolean;
|
|
37
|
-
ignore_empty_update: boolean;
|
|
34
|
+
include_metadata: boolean | undefined;
|
|
35
|
+
ignore_empty_update: boolean | undefined;
|
|
36
|
+
name: string;
|
|
37
|
+
view_name: string;
|
|
38
38
|
columns: {
|
|
39
39
|
name: string;
|
|
40
40
|
type: import("./Column.js").ColumnType | undefined;
|
|
@@ -48,7 +48,14 @@ export declare class Schema<S extends SchemaType = SchemaType> {
|
|
|
48
48
|
}[];
|
|
49
49
|
}[];
|
|
50
50
|
}[];
|
|
51
|
-
raw_tables:
|
|
51
|
+
raw_tables: unknown[];
|
|
52
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
|
|
55
|
+
*
|
|
56
|
+
* The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
|
|
57
|
+
* to define triggers for this table.
|
|
58
|
+
*/
|
|
59
|
+
static rawTableToJson(table: RawTable): unknown;
|
|
53
60
|
}
|
|
54
61
|
export {};
|
package/lib/db/schema/Schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { encodeTableOptions } from './internal.js';
|
|
2
2
|
/**
|
|
3
3
|
* A schema is a collection of tables. It is used to define the structure of a database.
|
|
4
4
|
*/
|
|
@@ -43,7 +43,7 @@ export class Schema {
|
|
|
43
43
|
*/
|
|
44
44
|
withRawTables(tables) {
|
|
45
45
|
for (const [name, rawTableDefinition] of Object.entries(tables)) {
|
|
46
|
-
this.rawTables.push(
|
|
46
|
+
this.rawTables.push({ name, ...rawTableDefinition });
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
validate() {
|
|
@@ -54,8 +54,30 @@ export class Schema {
|
|
|
54
54
|
toJSON() {
|
|
55
55
|
return {
|
|
56
56
|
tables: this.tables.map((t) => t.toJSON()),
|
|
57
|
-
raw_tables: this.rawTables
|
|
57
|
+
raw_tables: this.rawTables.map(Schema.rawTableToJson)
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns a representation of the raw table that is understood by the PowerSync SQLite core extension.
|
|
62
|
+
*
|
|
63
|
+
* The output of this can be passed through `JSON.serialize` and then used in `powersync_create_raw_table_crud_trigger`
|
|
64
|
+
* to define triggers for this table.
|
|
65
|
+
*/
|
|
66
|
+
static rawTableToJson(table) {
|
|
67
|
+
const serialized = {
|
|
68
|
+
name: table.name,
|
|
69
|
+
put: table.put,
|
|
70
|
+
delete: table.delete,
|
|
71
|
+
clear: table.clear
|
|
72
|
+
};
|
|
73
|
+
if ('schema' in table) {
|
|
74
|
+
// We have schema options, those are flattened into the outer JSON object for the core extension.
|
|
75
|
+
const schema = table.schema;
|
|
76
|
+
serialized.table_name = schema.tableName ?? table.name;
|
|
77
|
+
serialized.synced_columns = schema.syncedColumns;
|
|
78
|
+
Object.assign(serialized, encodeTableOptions(table.schema));
|
|
79
|
+
}
|
|
80
|
+
return serialized;
|
|
81
|
+
}
|
|
60
82
|
}
|
|
61
83
|
//# sourceMappingURL=Schema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.js","sourceRoot":"","sources":["../../../src/db/schema/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Schema.js","sourceRoot":"","sources":["../../../src/db/schema/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAUnD;;GAEG;AACH,MAAM,OAAO,MAAM;IACjB;;MAEE;IACO,KAAK,CAAqB;IAC1B,KAAK,CAAI;IACT,MAAM,CAAU;IAChB,SAAS,CAAa;IAE/B,YAAY,MAAmB;Q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C,CAAC,MAAM,EAAE,CAAC;YAC1C,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,cAAc,CAAC;SACtD,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,cAAc,CAAC,KAAe;QACnC,MAAM,UAAU,GAAQ;YACtB,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,GAAG,EAAE,KAAK,CAAC,GAAG;YACd,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC;QACF,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;YACtB,iGAAiG;YACjG,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;YAC5B,UAAU,CAAC,UAAU,GAAG,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC;YACvD,UAAU,CAAC,cAAc,GAAG,MAAM,CAAC,aAAa,CAAC;YACjD,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,kBAAkB,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;QAC9D,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;CACF"}
|
package/lib/db/schema/Table.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { Column, ColumnsType, ColumnType, ExtractColumnValueType } from './Column.js';
|
|
2
2
|
import { Index } from './Index.js';
|
|
3
3
|
import { TableV2 } from './TableV2.js';
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Options that apply both to JSON-based tables and raw tables.
|
|
6
|
+
*/
|
|
7
|
+
export interface TableOrRawTableOptions {
|
|
5
8
|
localOnly?: boolean;
|
|
6
9
|
insertOnly?: boolean;
|
|
7
|
-
viewName?: string;
|
|
8
10
|
trackPrevious?: boolean | TrackPreviousOptions;
|
|
9
11
|
trackMetadata?: boolean;
|
|
10
12
|
ignoreEmptyUpdates?: boolean;
|
|
11
13
|
}
|
|
14
|
+
interface SharedTableOptions extends TableOrRawTableOptions {
|
|
15
|
+
viewName?: string;
|
|
16
|
+
}
|
|
12
17
|
/** Whether to include previous column values when PowerSync tracks local changes.
|
|
13
18
|
*
|
|
14
19
|
* Including old values may be helpful for some backend connector implementations, which is
|
|
@@ -132,14 +137,14 @@ export declare class Table<Columns extends ColumnsType = ColumnsType> {
|
|
|
132
137
|
get validName(): boolean;
|
|
133
138
|
validate(): void;
|
|
134
139
|
toJSON(): {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
local_only: boolean;
|
|
138
|
-
insert_only: boolean;
|
|
140
|
+
local_only: boolean | undefined;
|
|
141
|
+
insert_only: boolean | undefined;
|
|
139
142
|
include_old: any;
|
|
140
143
|
include_old_only_when_changed: boolean;
|
|
141
|
-
include_metadata: boolean;
|
|
142
|
-
ignore_empty_update: boolean;
|
|
144
|
+
include_metadata: boolean | undefined;
|
|
145
|
+
ignore_empty_update: boolean | undefined;
|
|
146
|
+
name: string;
|
|
147
|
+
view_name: string;
|
|
143
148
|
columns: {
|
|
144
149
|
name: string;
|
|
145
150
|
type: ColumnType | undefined;
|
package/lib/db/schema/Table.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Column, ColumnType, MAX_AMOUNT_OF_COLUMNS } from './Column.js';
|
|
2
2
|
import { Index } from './Index.js';
|
|
3
3
|
import { IndexedColumn } from './IndexedColumn.js';
|
|
4
|
+
import { encodeTableOptions } from './internal.js';
|
|
4
5
|
export const DEFAULT_TABLE_OPTIONS = {
|
|
5
6
|
indexes: [],
|
|
6
7
|
insertOnly: false,
|
|
@@ -190,18 +191,12 @@ export class Table {
|
|
|
190
191
|
}
|
|
191
192
|
}
|
|
192
193
|
toJSON() {
|
|
193
|
-
const trackPrevious = this.trackPrevious;
|
|
194
194
|
return {
|
|
195
195
|
name: this.name,
|
|
196
196
|
view_name: this.viewName,
|
|
197
|
-
local_only: this.localOnly,
|
|
198
|
-
insert_only: this.insertOnly,
|
|
199
|
-
include_old: trackPrevious && (trackPrevious.columns ?? true),
|
|
200
|
-
include_old_only_when_changed: typeof trackPrevious == 'object' && trackPrevious.onlyWhenChanged == true,
|
|
201
|
-
include_metadata: this.trackMetadata,
|
|
202
|
-
ignore_empty_update: this.ignoreEmptyUpdates,
|
|
203
197
|
columns: this.columns.map((c) => c.toJSON()),
|
|
204
|
-
indexes: this.indexes.map((e) => e.toJSON(this))
|
|
198
|
+
indexes: this.indexes.map((e) => e.toJSON(this)),
|
|
199
|
+
...encodeTableOptions(this)
|
|
205
200
|
};
|
|
206
201
|
}
|
|
207
202
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Table.js","sourceRoot":"","sources":["../../../src/db/schema/Table.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EAEN,UAAU,EAEV,qBAAqB,EACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"Table.js","sourceRoot":"","sources":["../../../src/db/schema/Table.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,MAAM,EAEN,UAAU,EAEV,qBAAqB,EACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAmDnD,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,OAAO,EAAE,EAAE;IACX,UAAU,EAAE,KAAK;IACjB,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,KAAK;IACpB,aAAa,EAAE,KAAK;IACpB,kBAAkB,EAAE,KAAK;CAC1B,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,eAAe,CAAC;AAEpD,MAAM,OAAO,KAAK;IACN,O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AAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC/B,MAAM,IAAI,KAAK,CAAC,mBAAmB,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;YACnD,CAAC;YACD,IAAI,oBAAoB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC1C,MAAM,IAAI,KAAK,CAAC,qCAAqC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;YACrE,CAAC;YAED,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;gBACnC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;oBAClC,MAAM,IAAI,KAAK,CAAC,UAAU,MAAM,CAAC,IAAI,wBAAwB,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;gBAC7E,CAAC;YACH,CAAC;YAED,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAED,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,SAAS,EAAE,IAAI,CAAC,QAAQ;YACxB,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;YAC5C,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAChD,GAAG,kBAAkB,CAAC,IAAI,CAAC;SAC5B,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TableOrRawTableOptions } from './Table.js';
|
|
2
|
+
/**
|
|
3
|
+
* @internal Not exported from `index.ts`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function encodeTableOptions(options: TableOrRawTableOptions): {
|
|
6
|
+
local_only: boolean | undefined;
|
|
7
|
+
insert_only: boolean | undefined;
|
|
8
|
+
include_old: any;
|
|
9
|
+
include_old_only_when_changed: boolean;
|
|
10
|
+
include_metadata: boolean | undefined;
|
|
11
|
+
ignore_empty_update: boolean | undefined;
|
|
12
|
+
};
|
|
@@ -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
|
|
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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AmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,kBAAkB,CAAC"}
|
package/package.json
CHANGED
|
@@ -499,12 +499,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
|
|
|
499
499
|
}
|
|
500
500
|
this._schema = schema;
|
|
501
501
|
|
|
502
|
-
|
|
503
|
-
// on some DB adapters.
|
|
504
|
-
this.database.writeTransaction((tx) => {
|
|
505
|
-
return tx.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
|
|
506
|
-
});
|
|
507
|
-
|
|
502
|
+
await this.database.execute('SELECT powersync_replace_schema(?)', [JSON.stringify(this.schema.toJSON())]);
|
|
508
503
|
await this.database.refreshSchema();
|
|
509
504
|
this.iterateListeners(async (cb) => cb.schemaChanged?.(schema));
|
|
510
505
|
}
|
|
@@ -223,6 +223,13 @@ export interface CreateDiffTriggerOptions extends BaseCreateDiffTriggerOptions {
|
|
|
223
223
|
* This table will be dropped once the trigger is removed.
|
|
224
224
|
*/
|
|
225
225
|
destination: string;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* When true, the diff trigger will not create or drop the destination table.
|
|
229
|
+
* The caller is responsible for ensuring the table exists with the correct
|
|
230
|
+
* schema before creating the trigger and for dropping it when no longer needed.
|
|
231
|
+
*/
|
|
232
|
+
manageDestinationExternally?: boolean;
|
|
226
233
|
}
|
|
227
234
|
|
|
228
235
|
/**
|
|
@@ -201,6 +201,7 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
201
201
|
columns,
|
|
202
202
|
when,
|
|
203
203
|
hooks,
|
|
204
|
+
manageDestinationExternally = false,
|
|
204
205
|
// Fall back to the provided default if not given on this level
|
|
205
206
|
useStorage = this.defaultConfig.useStorageByDefault
|
|
206
207
|
} = options;
|
|
@@ -272,7 +273,9 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
272
273
|
disposeWarningListener();
|
|
273
274
|
return this.db.writeLock(async (tx) => {
|
|
274
275
|
await this.removeTriggers(tx, triggerIds);
|
|
275
|
-
|
|
276
|
+
if (!manageDestinationExternally) {
|
|
277
|
+
await tx.execute(/* sql */ `DROP TABLE IF EXISTS ${destination};`);
|
|
278
|
+
}
|
|
276
279
|
await releaseStorageClaim?.();
|
|
277
280
|
});
|
|
278
281
|
};
|
|
@@ -280,16 +283,18 @@ export class TriggerManagerImpl implements TriggerManager {
|
|
|
280
283
|
const setup = async (tx: LockContext) => {
|
|
281
284
|
// Allow user code to execute in this lock context before the trigger is created.
|
|
282
285
|
await hooks?.beforeCreate?.(tx);
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
286
|
+
if (!manageDestinationExternally) {
|
|
287
|
+
await tx.execute(/* sql */ `
|
|
288
|
+
CREATE ${tableTriggerTypeClause} TABLE ${destination} (
|
|
289
|
+
operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
290
|
+
id TEXT,
|
|
291
|
+
operation TEXT,
|
|
292
|
+
timestamp TEXT,
|
|
293
|
+
value TEXT,
|
|
294
|
+
previous_value TEXT
|
|
295
|
+
)
|
|
296
|
+
`);
|
|
297
|
+
}
|
|
293
298
|
|
|
294
299
|
if (operations.includes(DiffTriggerOperation.INSERT)) {
|
|
295
300
|
const insertTriggerId = this.generateTriggerName(DiffTriggerOperation.INSERT, destination, id);
|
|
@@ -1,8 +1,23 @@
|
|
|
1
|
+
import { TableOrRawTableOptions } from './Table.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
|
55
|
-
* another table. Instead, this name is used by the sync client to
|
|
56
|
-
* appears in the source / backend database) are to be handled
|
|
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
|
-
|
|
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
|
+
};
|
package/src/db/schema/Schema.ts
CHANGED
|
@@ -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(
|
|
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
|
}
|
package/src/db/schema/Table.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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';
|