@tanstack/powersync-db-collection 0.0.0 → 0.1.1
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/LICENSE +21 -0
- package/dist/cjs/PendingOperationStore.cjs +33 -0
- package/dist/cjs/PendingOperationStore.cjs.map +1 -0
- package/dist/cjs/PendingOperationStore.d.cts +29 -0
- package/dist/cjs/PowerSyncTransactor.cjs +158 -0
- package/dist/cjs/PowerSyncTransactor.cjs.map +1 -0
- package/dist/cjs/PowerSyncTransactor.d.cts +60 -0
- package/dist/cjs/definitions.cjs +5 -0
- package/dist/cjs/definitions.cjs.map +1 -0
- package/dist/cjs/definitions.d.cts +200 -0
- package/dist/cjs/helpers.cjs +35 -0
- package/dist/cjs/helpers.cjs.map +1 -0
- package/dist/cjs/helpers.d.cts +70 -0
- package/dist/cjs/index.cjs +9 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +3 -0
- package/dist/cjs/powersync.cjs +200 -0
- package/dist/cjs/powersync.cjs.map +1 -0
- package/dist/cjs/powersync.d.cts +145 -0
- package/dist/cjs/schema.cjs +65 -0
- package/dist/cjs/schema.cjs.map +1 -0
- package/dist/cjs/schema.d.cts +21 -0
- package/dist/cjs/serialization.cjs +47 -0
- package/dist/cjs/serialization.cjs.map +1 -0
- package/dist/cjs/serialization.d.cts +34 -0
- package/dist/esm/PendingOperationStore.d.ts +29 -0
- package/dist/esm/PendingOperationStore.js +33 -0
- package/dist/esm/PendingOperationStore.js.map +1 -0
- package/dist/esm/PowerSyncTransactor.d.ts +60 -0
- package/dist/esm/PowerSyncTransactor.js +158 -0
- package/dist/esm/PowerSyncTransactor.js.map +1 -0
- package/dist/esm/definitions.d.ts +200 -0
- package/dist/esm/definitions.js +5 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/helpers.d.ts +70 -0
- package/dist/esm/helpers.js +35 -0
- package/dist/esm/helpers.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/powersync.d.ts +145 -0
- package/dist/esm/powersync.js +200 -0
- package/dist/esm/powersync.js.map +1 -0
- package/dist/esm/schema.d.ts +21 -0
- package/dist/esm/schema.js +65 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/serialization.d.ts +34 -0
- package/dist/esm/serialization.js +47 -0
- package/dist/esm/serialization.js.map +1 -0
- package/package.json +8 -9
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Table } from '@powersync/common';
|
|
2
|
+
import { CustomSQLiteSerializer } from './definitions.cjs';
|
|
3
|
+
import { ExtractedTable, ExtractedTableColumns, MapBaseColumnType } from './helpers.cjs';
|
|
4
|
+
/**
|
|
5
|
+
* Serializes an object for persistence to a SQLite table, mapping its values to appropriate SQLite types.
|
|
6
|
+
*
|
|
7
|
+
* This function takes an object representing a row, a table schema, and an optional custom serializer map.
|
|
8
|
+
* It returns a new object with values transformed to be compatible with SQLite column types.
|
|
9
|
+
*
|
|
10
|
+
* ## Generics
|
|
11
|
+
* - `TOutput`: The shape of the input object, typically matching the row data.
|
|
12
|
+
* - `TTable`: The table schema, which must match the keys of `TOutput`.
|
|
13
|
+
*
|
|
14
|
+
* ## Parameters
|
|
15
|
+
* - `value`: The object to serialize (row data).
|
|
16
|
+
* - `tableSchema`: The schema describing the SQLite table columns and types.
|
|
17
|
+
* - `customSerializer`: An optional map of custom serialization functions for specific keys.
|
|
18
|
+
*
|
|
19
|
+
* ## Behavior
|
|
20
|
+
* - For each key in `value`, finds the corresponding column in `tableSchema`.
|
|
21
|
+
* - If a custom serializer is provided for a key, it is used to transform the value.
|
|
22
|
+
* - Otherwise, values are mapped according to the column type:
|
|
23
|
+
* - `TEXT`: Strings are passed through; Dates are converted to ISO strings; other types are JSON-stringified.
|
|
24
|
+
* - `INTEGER`/`REAL`: Numbers are passed through; booleans are mapped to 1/0; other types are coerced to numbers.
|
|
25
|
+
* - Throws if a column type is unknown or a value cannot be converted.
|
|
26
|
+
*
|
|
27
|
+
* ## Returns
|
|
28
|
+
* - An object with the same keys as `value`, with values transformed for SQLite compatibility.
|
|
29
|
+
*
|
|
30
|
+
* ## Errors
|
|
31
|
+
* - Throws if a key in `value` does not exist in the schema.
|
|
32
|
+
* - Throws if a value cannot be converted to the required SQLite type.
|
|
33
|
+
*/
|
|
34
|
+
export declare function serializeForSQLite<TOutput extends Record<string, unknown>, TTable extends Table<MapBaseColumnType<TOutput>> = Table<MapBaseColumnType<TOutput>>>(value: TOutput, tableSchema: TTable, customSerializer?: Partial<CustomSQLiteSerializer<TOutput, ExtractedTableColumns<TTable>>>): ExtractedTable<TTable>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DiffTriggerOperation } from '@powersync/common';
|
|
2
|
+
export type PendingOperation = {
|
|
3
|
+
tableName: string;
|
|
4
|
+
operation: DiffTriggerOperation;
|
|
5
|
+
id: string;
|
|
6
|
+
timestamp: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Optimistic mutations have their optimistic state discarded once transactions have
|
|
10
|
+
* been applied.
|
|
11
|
+
* We need to ensure that an applied transaction has been observed by the sync diff trigger
|
|
12
|
+
* before resolving the transaction application call.
|
|
13
|
+
* This store allows registering a wait for a pending operation to have been observed.
|
|
14
|
+
*/
|
|
15
|
+
export declare class PendingOperationStore {
|
|
16
|
+
private pendingOperations;
|
|
17
|
+
/**
|
|
18
|
+
* Globally accessible PendingOperationStore
|
|
19
|
+
*/
|
|
20
|
+
static GLOBAL: PendingOperationStore;
|
|
21
|
+
/**
|
|
22
|
+
* @returns A promise which will resolve once the specified operation has been seen.
|
|
23
|
+
*/
|
|
24
|
+
waitFor(operation: PendingOperation): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Marks a set of operations as seen. This will resolve any pending promises.
|
|
27
|
+
*/
|
|
28
|
+
resolvePendingFor(operations: Array<PendingOperation>): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import pDefer from "p-defer";
|
|
2
|
+
const _PendingOperationStore = class _PendingOperationStore {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.pendingOperations = /* @__PURE__ */ new Map();
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @returns A promise which will resolve once the specified operation has been seen.
|
|
8
|
+
*/
|
|
9
|
+
waitFor(operation) {
|
|
10
|
+
const managedPromise = pDefer();
|
|
11
|
+
this.pendingOperations.set(operation, managedPromise);
|
|
12
|
+
return managedPromise.promise;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Marks a set of operations as seen. This will resolve any pending promises.
|
|
16
|
+
*/
|
|
17
|
+
resolvePendingFor(operations) {
|
|
18
|
+
for (const operation of operations) {
|
|
19
|
+
for (const [pendingOp, deferred] of this.pendingOperations.entries()) {
|
|
20
|
+
if (pendingOp.tableName == operation.tableName && pendingOp.operation == operation.operation && pendingOp.id == operation.id && pendingOp.timestamp == operation.timestamp) {
|
|
21
|
+
deferred.resolve();
|
|
22
|
+
this.pendingOperations.delete(pendingOp);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
_PendingOperationStore.GLOBAL = new _PendingOperationStore();
|
|
29
|
+
let PendingOperationStore = _PendingOperationStore;
|
|
30
|
+
export {
|
|
31
|
+
PendingOperationStore
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=PendingOperationStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PendingOperationStore.js","sources":["../../src/PendingOperationStore.ts"],"sourcesContent":["import pDefer from \"p-defer\"\nimport type { DiffTriggerOperation } from \"@powersync/common\"\nimport type { DeferredPromise } from \"p-defer\"\n\nexport type PendingOperation = {\n tableName: string\n operation: DiffTriggerOperation\n id: string\n timestamp: string\n}\n\n/**\n * Optimistic mutations have their optimistic state discarded once transactions have\n * been applied.\n * We need to ensure that an applied transaction has been observed by the sync diff trigger\n * before resolving the transaction application call.\n * This store allows registering a wait for a pending operation to have been observed.\n */\nexport class PendingOperationStore {\n private pendingOperations = new Map<PendingOperation, DeferredPromise<void>>()\n\n /**\n * Globally accessible PendingOperationStore\n */\n static GLOBAL = new PendingOperationStore()\n\n /**\n * @returns A promise which will resolve once the specified operation has been seen.\n */\n waitFor(operation: PendingOperation): Promise<void> {\n const managedPromise = pDefer<void>()\n this.pendingOperations.set(operation, managedPromise)\n return managedPromise.promise\n }\n\n /**\n * Marks a set of operations as seen. This will resolve any pending promises.\n */\n resolvePendingFor(operations: Array<PendingOperation>) {\n for (const operation of operations) {\n for (const [pendingOp, deferred] of this.pendingOperations.entries()) {\n if (\n pendingOp.tableName == operation.tableName &&\n pendingOp.operation == operation.operation &&\n pendingOp.id == operation.id &&\n pendingOp.timestamp == operation.timestamp\n ) {\n deferred.resolve()\n this.pendingOperations.delete(pendingOp)\n }\n }\n }\n }\n}\n"],"names":[],"mappings":";AAkBO,MAAM,yBAAN,MAAM,uBAAsB;AAAA,EAA5B,cAAA;AACL,SAAQ,wCAAwB,IAAA;AAAA,EAA6C;AAAA;AAAA;AAAA;AAAA,EAU7E,QAAQ,WAA4C;AAClD,UAAM,iBAAiB,OAAA;AACvB,SAAK,kBAAkB,IAAI,WAAW,cAAc;AACpD,WAAO,eAAe;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA,EAKA,kBAAkB,YAAqC;AACrD,eAAW,aAAa,YAAY;AAClC,iBAAW,CAAC,WAAW,QAAQ,KAAK,KAAK,kBAAkB,WAAW;AACpE,YACE,UAAU,aAAa,UAAU,aACjC,UAAU,aAAa,UAAU,aACjC,UAAU,MAAM,UAAU,MAC1B,UAAU,aAAa,UAAU,WACjC;AACA,mBAAS,QAAA;AACT,eAAK,kBAAkB,OAAO,SAAS;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AA7BE,uBAAO,SAAS,IAAI,uBAAA;AANf,IAAM,wBAAN;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PendingOperationStore, PendingOperation } from './PendingOperationStore.js';
|
|
2
|
+
import { AbstractPowerSyncDatabase, LockContext } from '@powersync/common';
|
|
3
|
+
import { PendingMutation, Transaction } from '@tanstack/db';
|
|
4
|
+
export type TransactorOptions = {
|
|
5
|
+
database: AbstractPowerSyncDatabase;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Applies mutations to the PowerSync database. This method is called automatically by the collection's
|
|
9
|
+
* insert, update, and delete operations. You typically don't need to call this directly unless you
|
|
10
|
+
* have special transaction requirements.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a collection
|
|
15
|
+
* const collection = createCollection(
|
|
16
|
+
* powerSyncCollectionOptions<Document>({
|
|
17
|
+
* database: db,
|
|
18
|
+
* table: APP_SCHEMA.props.documents,
|
|
19
|
+
* })
|
|
20
|
+
* )
|
|
21
|
+
*
|
|
22
|
+
* const addTx = createTransaction({
|
|
23
|
+
* autoCommit: false,
|
|
24
|
+
* mutationFn: async ({ transaction }) => {
|
|
25
|
+
* await new PowerSyncTransactor({ database: db }).applyTransaction(transaction)
|
|
26
|
+
* },
|
|
27
|
+
* })
|
|
28
|
+
*
|
|
29
|
+
* addTx.mutate(() => {
|
|
30
|
+
* for (let i = 0; i < 5; i++) {
|
|
31
|
+
* collection.insert({ id: randomUUID(), name: `tx-${i}` })
|
|
32
|
+
* }
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* await addTx.commit()
|
|
36
|
+
* await addTx.isPersisted.promise
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param transaction - The transaction containing mutations to apply
|
|
40
|
+
* @returns A promise that resolves when the mutations have been persisted to PowerSync
|
|
41
|
+
*/
|
|
42
|
+
export declare class PowerSyncTransactor {
|
|
43
|
+
database: AbstractPowerSyncDatabase;
|
|
44
|
+
pendingOperationStore: PendingOperationStore;
|
|
45
|
+
constructor(options: TransactorOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Persists a {@link Transaction} to the PowerSync SQLite database.
|
|
48
|
+
*/
|
|
49
|
+
applyTransaction(transaction: Transaction<any>): Promise<void>;
|
|
50
|
+
protected handleInsert(mutation: PendingMutation<any>, context: LockContext, waitForCompletion?: boolean): Promise<PendingOperation | null>;
|
|
51
|
+
protected handleUpdate(mutation: PendingMutation<any>, context: LockContext, waitForCompletion?: boolean): Promise<PendingOperation | null>;
|
|
52
|
+
protected handleDelete(mutation: PendingMutation<any>, context: LockContext, waitForCompletion?: boolean): Promise<PendingOperation | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Helper function which wraps a persistence operation by:
|
|
55
|
+
* - Fetching the mutation's collection's SQLite table details
|
|
56
|
+
* - Executing the mutation
|
|
57
|
+
* - Returning the last pending diff operation if required
|
|
58
|
+
*/
|
|
59
|
+
protected handleOperationWithCompletion(mutation: PendingMutation<any>, context: LockContext, waitForCompletion: boolean, handler: (tableName: string, mutation: PendingMutation<any>, serializeValue: (value: any) => Record<string, unknown>) => Promise<void>): Promise<PendingOperation | null>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { sanitizeSQL } from "@powersync/common";
|
|
2
|
+
import DebugModule from "debug";
|
|
3
|
+
import { asPowerSyncRecord, mapOperationToPowerSync } from "./helpers.js";
|
|
4
|
+
import { PendingOperationStore } from "./PendingOperationStore.js";
|
|
5
|
+
const debug = DebugModule.debug(`ts/db:powersync`);
|
|
6
|
+
class PowerSyncTransactor {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.database = options.database;
|
|
9
|
+
this.pendingOperationStore = PendingOperationStore.GLOBAL;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Persists a {@link Transaction} to the PowerSync SQLite database.
|
|
13
|
+
*/
|
|
14
|
+
async applyTransaction(transaction) {
|
|
15
|
+
const { mutations } = transaction;
|
|
16
|
+
if (mutations.length == 0) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const mutationsCollectionIds = mutations.map(
|
|
20
|
+
(mutation) => mutation.collection.id
|
|
21
|
+
);
|
|
22
|
+
const collectionIds = Array.from(new Set(mutationsCollectionIds));
|
|
23
|
+
const lastCollectionMutationIndexes = /* @__PURE__ */ new Map();
|
|
24
|
+
const allCollections = collectionIds.map((id) => mutations.find((mutation) => mutation.collection.id == id)).map((mutation) => mutation.collection);
|
|
25
|
+
for (const collectionId of collectionIds) {
|
|
26
|
+
lastCollectionMutationIndexes.set(
|
|
27
|
+
collectionId,
|
|
28
|
+
mutationsCollectionIds.lastIndexOf(collectionId)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
await Promise.all(
|
|
32
|
+
allCollections.map(async (collection) => {
|
|
33
|
+
if (collection.isReady()) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
await new Promise((resolve) => collection.onFirstReady(resolve));
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
const { whenComplete } = await this.database.writeTransaction(
|
|
40
|
+
async (tx) => {
|
|
41
|
+
const pendingOperations = [];
|
|
42
|
+
for (const [index, mutation] of mutations.entries()) {
|
|
43
|
+
const shouldWait = index == lastCollectionMutationIndexes.get(mutation.collection.id);
|
|
44
|
+
switch (mutation.type) {
|
|
45
|
+
case `insert`:
|
|
46
|
+
pendingOperations.push(
|
|
47
|
+
await this.handleInsert(mutation, tx, shouldWait)
|
|
48
|
+
);
|
|
49
|
+
break;
|
|
50
|
+
case `update`:
|
|
51
|
+
pendingOperations.push(
|
|
52
|
+
await this.handleUpdate(mutation, tx, shouldWait)
|
|
53
|
+
);
|
|
54
|
+
break;
|
|
55
|
+
case `delete`:
|
|
56
|
+
pendingOperations.push(
|
|
57
|
+
await this.handleDelete(mutation, tx, shouldWait)
|
|
58
|
+
);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
whenComplete: Promise.all(
|
|
64
|
+
pendingOperations.filter((op) => !!op).map((op) => this.pendingOperationStore.waitFor(op))
|
|
65
|
+
)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
await whenComplete;
|
|
70
|
+
}
|
|
71
|
+
async handleInsert(mutation, context, waitForCompletion = false) {
|
|
72
|
+
debug(`insert`, mutation);
|
|
73
|
+
return this.handleOperationWithCompletion(
|
|
74
|
+
mutation,
|
|
75
|
+
context,
|
|
76
|
+
waitForCompletion,
|
|
77
|
+
async (tableName, mutation2, serializeValue) => {
|
|
78
|
+
const values = serializeValue(mutation2.modified);
|
|
79
|
+
const keys = Object.keys(values).map((key) => sanitizeSQL`${key}`);
|
|
80
|
+
await context.execute(
|
|
81
|
+
`
|
|
82
|
+
INSERT into ${tableName}
|
|
83
|
+
(${keys.join(`, `)})
|
|
84
|
+
VALUES
|
|
85
|
+
(${keys.map((_) => `?`).join(`, `)})
|
|
86
|
+
`,
|
|
87
|
+
Object.values(values)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
async handleUpdate(mutation, context, waitForCompletion = false) {
|
|
93
|
+
debug(`update`, mutation);
|
|
94
|
+
return this.handleOperationWithCompletion(
|
|
95
|
+
mutation,
|
|
96
|
+
context,
|
|
97
|
+
waitForCompletion,
|
|
98
|
+
async (tableName, mutation2, serializeValue) => {
|
|
99
|
+
const values = serializeValue(mutation2.modified);
|
|
100
|
+
const keys = Object.keys(values).map((key) => sanitizeSQL`${key}`);
|
|
101
|
+
await context.execute(
|
|
102
|
+
`
|
|
103
|
+
UPDATE ${tableName}
|
|
104
|
+
SET ${keys.map((key) => `${key} = ?`).join(`, `)}
|
|
105
|
+
WHERE id = ?
|
|
106
|
+
`,
|
|
107
|
+
[...Object.values(values), asPowerSyncRecord(mutation2.modified).id]
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
async handleDelete(mutation, context, waitForCompletion = false) {
|
|
113
|
+
debug(`update`, mutation);
|
|
114
|
+
return this.handleOperationWithCompletion(
|
|
115
|
+
mutation,
|
|
116
|
+
context,
|
|
117
|
+
waitForCompletion,
|
|
118
|
+
async (tableName, mutation2) => {
|
|
119
|
+
await context.execute(
|
|
120
|
+
`
|
|
121
|
+
DELETE FROM ${tableName} WHERE id = ?
|
|
122
|
+
`,
|
|
123
|
+
[asPowerSyncRecord(mutation2.original).id]
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Helper function which wraps a persistence operation by:
|
|
130
|
+
* - Fetching the mutation's collection's SQLite table details
|
|
131
|
+
* - Executing the mutation
|
|
132
|
+
* - Returning the last pending diff operation if required
|
|
133
|
+
*/
|
|
134
|
+
async handleOperationWithCompletion(mutation, context, waitForCompletion, handler) {
|
|
135
|
+
if (typeof mutation.collection.config.utils?.getMeta != `function`) {
|
|
136
|
+
throw new Error(`Could not get tableName from mutation's collection config.
|
|
137
|
+
The provided mutation might not have originated from PowerSync.`);
|
|
138
|
+
}
|
|
139
|
+
const { tableName, trackedTableName, serializeValue } = mutation.collection.config.utils.getMeta();
|
|
140
|
+
await handler(sanitizeSQL`${tableName}`, mutation, serializeValue);
|
|
141
|
+
if (!waitForCompletion) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const diffOperation = await context.get(
|
|
145
|
+
sanitizeSQL`SELECT id, timestamp FROM ${trackedTableName} ORDER BY timestamp DESC LIMIT 1`
|
|
146
|
+
);
|
|
147
|
+
return {
|
|
148
|
+
tableName,
|
|
149
|
+
id: diffOperation.id,
|
|
150
|
+
operation: mapOperationToPowerSync(mutation.type),
|
|
151
|
+
timestamp: diffOperation.timestamp
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export {
|
|
156
|
+
PowerSyncTransactor
|
|
157
|
+
};
|
|
158
|
+
//# sourceMappingURL=PowerSyncTransactor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PowerSyncTransactor.js","sources":["../../src/PowerSyncTransactor.ts"],"sourcesContent":["import { sanitizeSQL } from \"@powersync/common\"\nimport DebugModule from \"debug\"\nimport { asPowerSyncRecord, mapOperationToPowerSync } from \"./helpers\"\nimport { PendingOperationStore } from \"./PendingOperationStore\"\nimport type { AbstractPowerSyncDatabase, LockContext } from \"@powersync/common\"\nimport type { PendingMutation, Transaction } from \"@tanstack/db\"\nimport type { EnhancedPowerSyncCollectionConfig } from \"./definitions\"\nimport type { PendingOperation } from \"./PendingOperationStore\"\n\nconst debug = DebugModule.debug(`ts/db:powersync`)\n\nexport type TransactorOptions = {\n database: AbstractPowerSyncDatabase\n}\n\n/**\n * Applies mutations to the PowerSync database. This method is called automatically by the collection's\n * insert, update, and delete operations. You typically don't need to call this directly unless you\n * have special transaction requirements.\n *\n * @example\n * ```typescript\n * // Create a collection\n * const collection = createCollection(\n * powerSyncCollectionOptions<Document>({\n * database: db,\n * table: APP_SCHEMA.props.documents,\n * })\n * )\n *\n * const addTx = createTransaction({\n * autoCommit: false,\n * mutationFn: async ({ transaction }) => {\n * await new PowerSyncTransactor({ database: db }).applyTransaction(transaction)\n * },\n * })\n *\n * addTx.mutate(() => {\n * for (let i = 0; i < 5; i++) {\n * collection.insert({ id: randomUUID(), name: `tx-${i}` })\n * }\n * })\n *\n * await addTx.commit()\n * await addTx.isPersisted.promise\n * ```\n *\n * @param transaction - The transaction containing mutations to apply\n * @returns A promise that resolves when the mutations have been persisted to PowerSync\n */\nexport class PowerSyncTransactor {\n database: AbstractPowerSyncDatabase\n pendingOperationStore: PendingOperationStore\n\n constructor(options: TransactorOptions) {\n this.database = options.database\n this.pendingOperationStore = PendingOperationStore.GLOBAL\n }\n\n /**\n * Persists a {@link Transaction} to the PowerSync SQLite database.\n */\n async applyTransaction(transaction: Transaction<any>) {\n const { mutations } = transaction\n\n if (mutations.length == 0) {\n return\n }\n /**\n * The transaction might contain operations for different collections.\n * We can do some optimizations for single-collection transactions.\n */\n const mutationsCollectionIds = mutations.map(\n (mutation) => mutation.collection.id\n )\n const collectionIds = Array.from(new Set(mutationsCollectionIds))\n const lastCollectionMutationIndexes = new Map<string, number>()\n const allCollections = collectionIds\n .map((id) => mutations.find((mutation) => mutation.collection.id == id)!)\n .map((mutation) => mutation.collection)\n for (const collectionId of collectionIds) {\n lastCollectionMutationIndexes.set(\n collectionId,\n mutationsCollectionIds.lastIndexOf(collectionId)\n )\n }\n\n // Check all the observers are ready before taking a lock\n await Promise.all(\n allCollections.map(async (collection) => {\n if (collection.isReady()) {\n return\n }\n await new Promise<void>((resolve) => collection.onFirstReady(resolve))\n })\n )\n\n // Persist to PowerSync\n const { whenComplete } = await this.database.writeTransaction(\n async (tx) => {\n const pendingOperations: Array<PendingOperation | null> = []\n\n for (const [index, mutation] of mutations.entries()) {\n /**\n * Each collection processes events independently. We need to make sure the\n * last operation for each collection has been observed.\n */\n const shouldWait =\n index == lastCollectionMutationIndexes.get(mutation.collection.id)\n switch (mutation.type) {\n case `insert`:\n pendingOperations.push(\n await this.handleInsert(mutation, tx, shouldWait)\n )\n break\n case `update`:\n pendingOperations.push(\n await this.handleUpdate(mutation, tx, shouldWait)\n )\n break\n case `delete`:\n pendingOperations.push(\n await this.handleDelete(mutation, tx, shouldWait)\n )\n break\n }\n }\n\n /**\n * Return a promise from the writeTransaction, without awaiting it.\n * This promise will resolve once the entire transaction has been\n * observed via the diff triggers.\n * We return without awaiting in order to free the write lock.\n */\n return {\n whenComplete: Promise.all(\n pendingOperations\n .filter((op) => !!op)\n .map((op) => this.pendingOperationStore.waitFor(op))\n ),\n }\n }\n )\n\n // Wait for the change to be observed via the diff trigger\n await whenComplete\n }\n\n protected async handleInsert(\n mutation: PendingMutation<any>,\n context: LockContext,\n waitForCompletion: boolean = false\n ): Promise<PendingOperation | null> {\n debug(`insert`, mutation)\n\n return this.handleOperationWithCompletion(\n mutation,\n context,\n waitForCompletion,\n async (tableName, mutation, serializeValue) => {\n const values = serializeValue(mutation.modified)\n const keys = Object.keys(values).map((key) => sanitizeSQL`${key}`)\n\n await context.execute(\n `\n INSERT into ${tableName} \n (${keys.join(`, `)}) \n VALUES \n (${keys.map((_) => `?`).join(`, `)})\n `,\n Object.values(values)\n )\n }\n )\n }\n\n protected async handleUpdate(\n mutation: PendingMutation<any>,\n context: LockContext,\n waitForCompletion: boolean = false\n ): Promise<PendingOperation | null> {\n debug(`update`, mutation)\n\n return this.handleOperationWithCompletion(\n mutation,\n context,\n waitForCompletion,\n async (tableName, mutation, serializeValue) => {\n const values = serializeValue(mutation.modified)\n const keys = Object.keys(values).map((key) => sanitizeSQL`${key}`)\n\n await context.execute(\n `\n UPDATE ${tableName} \n SET ${keys.map((key) => `${key} = ?`).join(`, `)}\n WHERE id = ?\n `,\n [...Object.values(values), asPowerSyncRecord(mutation.modified).id]\n )\n }\n )\n }\n\n protected async handleDelete(\n mutation: PendingMutation<any>,\n context: LockContext,\n waitForCompletion: boolean = false\n ): Promise<PendingOperation | null> {\n debug(`update`, mutation)\n\n return this.handleOperationWithCompletion(\n mutation,\n context,\n waitForCompletion,\n async (tableName, mutation) => {\n await context.execute(\n `\n DELETE FROM ${tableName} WHERE id = ?\n `,\n [asPowerSyncRecord(mutation.original).id]\n )\n }\n )\n }\n\n /**\n * Helper function which wraps a persistence operation by:\n * - Fetching the mutation's collection's SQLite table details\n * - Executing the mutation\n * - Returning the last pending diff operation if required\n */\n protected async handleOperationWithCompletion(\n mutation: PendingMutation<any>,\n context: LockContext,\n waitForCompletion: boolean,\n handler: (\n tableName: string,\n mutation: PendingMutation<any>,\n serializeValue: (value: any) => Record<string, unknown>\n ) => Promise<void>\n ): Promise<PendingOperation | null> {\n if (\n typeof (mutation.collection.config as any).utils?.getMeta != `function`\n ) {\n throw new Error(`Could not get tableName from mutation's collection config.\n The provided mutation might not have originated from PowerSync.`)\n }\n\n const { tableName, trackedTableName, serializeValue } = (\n mutation.collection\n .config as unknown as EnhancedPowerSyncCollectionConfig<any>\n ).utils.getMeta()\n\n await handler(sanitizeSQL`${tableName}`, mutation, serializeValue)\n\n if (!waitForCompletion) {\n return null\n }\n\n // Need to get the operation in order to wait for it\n const diffOperation = await context.get<{ id: string; timestamp: string }>(\n sanitizeSQL`SELECT id, timestamp FROM ${trackedTableName} ORDER BY timestamp DESC LIMIT 1`\n )\n return {\n tableName,\n id: diffOperation.id,\n operation: mapOperationToPowerSync(mutation.type),\n timestamp: diffOperation.timestamp,\n }\n }\n}\n"],"names":["mutation"],"mappings":"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"}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, Table } from '@powersync/common';
|
|
2
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
import { BaseCollectionConfig, CollectionConfig, InferSchemaOutput } from '@tanstack/db';
|
|
4
|
+
import { AnyTableColumnType, ExtractedTable, OptionalExtractedTable, PowerSyncRecord } from './helpers.js';
|
|
5
|
+
/**
|
|
6
|
+
* Small helper which determines the output type if:
|
|
7
|
+
* - Standard SQLite types are to be used OR
|
|
8
|
+
* - If the provided schema should be used.
|
|
9
|
+
*/
|
|
10
|
+
export type InferPowerSyncOutputType<TTable extends Table = Table, TSchema extends StandardSchemaV1<PowerSyncRecord> = never> = TSchema extends never ? ExtractedTable<TTable> : InferSchemaOutput<TSchema>;
|
|
11
|
+
/**
|
|
12
|
+
* A mapping type for custom serialization of object properties to SQLite-compatible values.
|
|
13
|
+
*
|
|
14
|
+
* This type allows you to override, for keys in the input object (`TOutput`), a function that transforms
|
|
15
|
+
* the value to the corresponding SQLite type (`TSQLite`). Keys not specified will use the default SQLite serialization.
|
|
16
|
+
*
|
|
17
|
+
* ## Generics
|
|
18
|
+
* - `TOutput`: The input object type, representing the row data to be serialized.
|
|
19
|
+
* - `TSQLite`: The target SQLite-compatible type for each property, typically inferred from the table schema.
|
|
20
|
+
*
|
|
21
|
+
* ## Usage
|
|
22
|
+
* Use this type to define a map of serialization functions for specific keys when you need custom handling
|
|
23
|
+
* (e.g., converting complex objects, formatting dates, or handling enums).
|
|
24
|
+
*
|
|
25
|
+
* Example:
|
|
26
|
+
* ```ts
|
|
27
|
+
* const serializer: CustomSQLiteSerializer<MyRowType, MySQLiteType> = {
|
|
28
|
+
* createdAt: (date) => date.toISOString(),
|
|
29
|
+
* status: (status) => status ? 1 : 0,
|
|
30
|
+
* meta: (meta) => JSON.stringify(meta),
|
|
31
|
+
* };
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* ## Behavior
|
|
35
|
+
* - Each key maps to a function that receives the value and returns the SQLite-compatible value.
|
|
36
|
+
* - Used by `serializeForSQLite` to override default serialization for specific columns.
|
|
37
|
+
*/
|
|
38
|
+
export type CustomSQLiteSerializer<TOutput extends Record<string, unknown>, TSQLite extends Record<string, unknown>> = Partial<{
|
|
39
|
+
[Key in keyof TOutput]: (value: TOutput[Key]) => Key extends keyof TSQLite ? TSQLite[Key] : never;
|
|
40
|
+
}>;
|
|
41
|
+
export type SerializerConfig<TOutput extends Record<string, unknown>, TSQLite extends Record<string, unknown>> = {
|
|
42
|
+
/**
|
|
43
|
+
* Optional partial serializer object for customizing how individual columns are serialized for SQLite.
|
|
44
|
+
*
|
|
45
|
+
* This should be a partial map of column keys to serialization functions, following the
|
|
46
|
+
* {@link CustomSQLiteSerializer} type. Each function receives the column value and returns a value
|
|
47
|
+
* compatible with SQLite storage.
|
|
48
|
+
*
|
|
49
|
+
* If not provided for a column, the default behavior is used:
|
|
50
|
+
* - `TEXT`: Strings are stored as-is; Dates are converted to ISO strings; other types are JSON-stringified.
|
|
51
|
+
* - `INTEGER`/`REAL`: Numbers are stored as-is; booleans are mapped to 1/0.
|
|
52
|
+
*
|
|
53
|
+
* Use this option to override serialization for specific columns, such as formatting dates, handling enums,
|
|
54
|
+
* or serializing complex objects.
|
|
55
|
+
*
|
|
56
|
+
* Example:
|
|
57
|
+
* ```typescript
|
|
58
|
+
* serializer: {
|
|
59
|
+
* createdAt: (date) => date.getTime(), // Store as timestamp
|
|
60
|
+
* meta: (meta) => JSON.stringify(meta), // Custom object serialization
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
serializer?: CustomSQLiteSerializer<TOutput, TSQLite>;
|
|
65
|
+
/**
|
|
66
|
+
* Application logic should ensure that incoming synced data is always valid.
|
|
67
|
+
* Failing to deserialize and apply incoming changes results in data inconsistency - which is a fatal error.
|
|
68
|
+
* Use this callback to react to deserialization errors.
|
|
69
|
+
*/
|
|
70
|
+
onDeserializationError: (error: StandardSchemaV1.FailureResult) => void;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Config for when TInput and TOutput are both the SQLite types.
|
|
74
|
+
*/
|
|
75
|
+
export type ConfigWithSQLiteTypes = {};
|
|
76
|
+
/**
|
|
77
|
+
* Config where TInput is the SQLite types while TOutput can be defined by TSchema.
|
|
78
|
+
* We can use the same schema to validate TInput and incoming SQLite changes.
|
|
79
|
+
*/
|
|
80
|
+
export type ConfigWithSQLiteInputType<TTable extends Table, TSchema extends StandardSchemaV1<OptionalExtractedTable<TTable>, AnyTableColumnType<TTable>>> = SerializerConfig<StandardSchemaV1.InferOutput<TSchema>, ExtractedTable<TTable>> & {
|
|
81
|
+
schema: TSchema;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Config where TInput and TOutput have arbitrarily typed values.
|
|
85
|
+
* The keys of the types need to equal the SQLite types.
|
|
86
|
+
* Since TInput is not the SQLite types, we require a schema in order to deserialize incoming SQLite updates. The schema should validate from SQLite to TOutput.
|
|
87
|
+
*/
|
|
88
|
+
export type ConfigWithArbitraryCollectionTypes<TTable extends Table, TSchema extends StandardSchemaV1<AnyTableColumnType<TTable>, AnyTableColumnType<TTable>>> = SerializerConfig<StandardSchemaV1.InferOutput<TSchema>, ExtractedTable<TTable>> & {
|
|
89
|
+
schema: TSchema;
|
|
90
|
+
/**
|
|
91
|
+
* Schema for deserializing and validating input data from the sync stream.
|
|
92
|
+
*
|
|
93
|
+
* This schema defines how to transform and validate data coming from SQLite types (as stored in the database)
|
|
94
|
+
* into the desired output types (`TOutput`) expected by your application or validation logic.
|
|
95
|
+
*
|
|
96
|
+
* The generic parameters allow for arbitrary input and output types, so you can specify custom conversion rules
|
|
97
|
+
* for each column. This is especially useful when your application expects richer types (e.g., Date, enums, objects)
|
|
98
|
+
* than what SQLite natively supports.
|
|
99
|
+
*
|
|
100
|
+
* Use this to ensure that incoming data from the sync stream is properly converted and validated before use.
|
|
101
|
+
*
|
|
102
|
+
* Example:
|
|
103
|
+
* ```typescript
|
|
104
|
+
* deserializationSchema: z.object({
|
|
105
|
+
* createdAt: z.preprocess((val) => new Date(val as string), z.date()),
|
|
106
|
+
* meta: z.preprocess((val) => JSON.parse(val as string), z.object({ ... })),
|
|
107
|
+
* })
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* This enables robust type safety and validation for incoming data, bridging the gap between SQLite storage
|
|
111
|
+
* and your application's expected types.
|
|
112
|
+
*/
|
|
113
|
+
deserializationSchema: StandardSchemaV1<ExtractedTable<TTable>, StandardSchemaV1.InferOutput<TSchema>>;
|
|
114
|
+
};
|
|
115
|
+
export type BasePowerSyncCollectionConfig<TTable extends Table = Table, TSchema extends StandardSchemaV1 = never> = Omit<BaseCollectionConfig<ExtractedTable<TTable>, string, TSchema>, `onInsert` | `onUpdate` | `onDelete` | `getKey`> & {
|
|
116
|
+
/** The PowerSync schema Table definition */
|
|
117
|
+
table: TTable;
|
|
118
|
+
/** The PowerSync database instance */
|
|
119
|
+
database: AbstractPowerSyncDatabase;
|
|
120
|
+
/**
|
|
121
|
+
* The maximum number of documents to read from the SQLite table
|
|
122
|
+
* in a single batch during the initial sync between PowerSync and the
|
|
123
|
+
* in-memory TanStack DB collection.
|
|
124
|
+
*
|
|
125
|
+
* @remarks
|
|
126
|
+
* - Defaults to {@link DEFAULT_BATCH_SIZE} if not specified.
|
|
127
|
+
* - Larger values reduce the number of round trips to the storage
|
|
128
|
+
* engine but increase memory usage per batch.
|
|
129
|
+
* - Smaller values may lower memory usage and allow earlier
|
|
130
|
+
* streaming of initial results, at the cost of more query calls.
|
|
131
|
+
*/
|
|
132
|
+
syncBatchSize?: number;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Configuration interface for PowerSync collection options.
|
|
136
|
+
* @template TTable - The PowerSync table schema definition
|
|
137
|
+
* @template TSchema - The validation schema type
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Configuration options for creating a PowerSync collection.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const APP_SCHEMA = new Schema({
|
|
145
|
+
* documents: new Table({
|
|
146
|
+
* name: column.text,
|
|
147
|
+
* }),
|
|
148
|
+
* })
|
|
149
|
+
*
|
|
150
|
+
* const db = new PowerSyncDatabase({
|
|
151
|
+
* database: {
|
|
152
|
+
* dbFilename: "test.sqlite",
|
|
153
|
+
* },
|
|
154
|
+
* schema: APP_SCHEMA,
|
|
155
|
+
* })
|
|
156
|
+
*
|
|
157
|
+
* const collection = createCollection(
|
|
158
|
+
* powerSyncCollectionOptions({
|
|
159
|
+
* database: db,
|
|
160
|
+
* table: APP_SCHEMA.props.documents
|
|
161
|
+
* })
|
|
162
|
+
* )
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export type PowerSyncCollectionConfig<TTable extends Table = Table, TSchema extends StandardSchemaV1<any> = never> = BasePowerSyncCollectionConfig<TTable, TSchema> & (ConfigWithSQLiteTypes | ConfigWithSQLiteInputType<TTable, TSchema> | ConfigWithArbitraryCollectionTypes<TTable, TSchema>);
|
|
166
|
+
/**
|
|
167
|
+
* Metadata for the PowerSync Collection.
|
|
168
|
+
*/
|
|
169
|
+
export type PowerSyncCollectionMeta<TTable extends Table = Table> = {
|
|
170
|
+
/**
|
|
171
|
+
* The SQLite table representing the collection.
|
|
172
|
+
*/
|
|
173
|
+
tableName: string;
|
|
174
|
+
/**
|
|
175
|
+
* The internal table used to track diffs for the collection.
|
|
176
|
+
*/
|
|
177
|
+
trackedTableName: string;
|
|
178
|
+
/**
|
|
179
|
+
* Serializes a collection value to the SQLite type
|
|
180
|
+
*/
|
|
181
|
+
serializeValue: (value: any) => ExtractedTable<TTable>;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* A CollectionConfig which includes utilities for PowerSync.
|
|
185
|
+
*/
|
|
186
|
+
export type EnhancedPowerSyncCollectionConfig<TTable extends Table, OutputType extends Record<string, unknown> = Record<string, unknown>, TSchema extends StandardSchemaV1 = never> = CollectionConfig<OutputType, string, TSchema> & {
|
|
187
|
+
id?: string;
|
|
188
|
+
utils: PowerSyncCollectionUtils<TTable>;
|
|
189
|
+
schema?: TSchema;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Collection-level utilities for PowerSync.
|
|
193
|
+
*/
|
|
194
|
+
export type PowerSyncCollectionUtils<TTable extends Table = Table> = {
|
|
195
|
+
getMeta: () => PowerSyncCollectionMeta<TTable>;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Default value for {@link PowerSyncCollectionConfig#syncBatchSize}.
|
|
199
|
+
*/
|
|
200
|
+
export declare const DEFAULT_BATCH_SIZE = 1000;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definitions.js","sources":["../../src/definitions.ts"],"sourcesContent":["import type { AbstractPowerSyncDatabase, Table } from \"@powersync/common\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport type {\n BaseCollectionConfig,\n CollectionConfig,\n InferSchemaOutput,\n} from \"@tanstack/db\"\nimport type {\n AnyTableColumnType,\n ExtractedTable,\n OptionalExtractedTable,\n PowerSyncRecord,\n} from \"./helpers\"\n\n/**\n * Small helper which determines the output type if:\n * - Standard SQLite types are to be used OR\n * - If the provided schema should be used.\n */\nexport type InferPowerSyncOutputType<\n TTable extends Table = Table,\n TSchema extends StandardSchemaV1<PowerSyncRecord> = never,\n> = TSchema extends never ? ExtractedTable<TTable> : InferSchemaOutput<TSchema>\n\n/**\n * A mapping type for custom serialization of object properties to SQLite-compatible values.\n *\n * This type allows you to override, for keys in the input object (`TOutput`), a function that transforms\n * the value to the corresponding SQLite type (`TSQLite`). Keys not specified will use the default SQLite serialization.\n *\n * ## Generics\n * - `TOutput`: The input object type, representing the row data to be serialized.\n * - `TSQLite`: The target SQLite-compatible type for each property, typically inferred from the table schema.\n *\n * ## Usage\n * Use this type to define a map of serialization functions for specific keys when you need custom handling\n * (e.g., converting complex objects, formatting dates, or handling enums).\n *\n * Example:\n * ```ts\n * const serializer: CustomSQLiteSerializer<MyRowType, MySQLiteType> = {\n * createdAt: (date) => date.toISOString(),\n * status: (status) => status ? 1 : 0,\n * meta: (meta) => JSON.stringify(meta),\n * };\n * ```\n *\n * ## Behavior\n * - Each key maps to a function that receives the value and returns the SQLite-compatible value.\n * - Used by `serializeForSQLite` to override default serialization for specific columns.\n */\nexport type CustomSQLiteSerializer<\n TOutput extends Record<string, unknown>,\n TSQLite extends Record<string, unknown>,\n> = Partial<{\n [Key in keyof TOutput]: (\n value: TOutput[Key]\n ) => Key extends keyof TSQLite ? TSQLite[Key] : never\n}>\n\nexport type SerializerConfig<\n TOutput extends Record<string, unknown>,\n TSQLite extends Record<string, unknown>,\n> = {\n /**\n * Optional partial serializer object for customizing how individual columns are serialized for SQLite.\n *\n * This should be a partial map of column keys to serialization functions, following the\n * {@link CustomSQLiteSerializer} type. Each function receives the column value and returns a value\n * compatible with SQLite storage.\n *\n * If not provided for a column, the default behavior is used:\n * - `TEXT`: Strings are stored as-is; Dates are converted to ISO strings; other types are JSON-stringified.\n * - `INTEGER`/`REAL`: Numbers are stored as-is; booleans are mapped to 1/0.\n *\n * Use this option to override serialization for specific columns, such as formatting dates, handling enums,\n * or serializing complex objects.\n *\n * Example:\n * ```typescript\n * serializer: {\n * createdAt: (date) => date.getTime(), // Store as timestamp\n * meta: (meta) => JSON.stringify(meta), // Custom object serialization\n * }\n * ```\n */\n serializer?: CustomSQLiteSerializer<TOutput, TSQLite>\n\n /**\n * Application logic should ensure that incoming synced data is always valid.\n * Failing to deserialize and apply incoming changes results in data inconsistency - which is a fatal error.\n * Use this callback to react to deserialization errors.\n */\n onDeserializationError: (error: StandardSchemaV1.FailureResult) => void\n}\n\n/**\n * Config for when TInput and TOutput are both the SQLite types.\n */\nexport type ConfigWithSQLiteTypes = {}\n\n/**\n * Config where TInput is the SQLite types while TOutput can be defined by TSchema.\n * We can use the same schema to validate TInput and incoming SQLite changes.\n */\nexport type ConfigWithSQLiteInputType<\n TTable extends Table,\n TSchema extends StandardSchemaV1<\n // TInput is the SQLite types.\n OptionalExtractedTable<TTable>,\n AnyTableColumnType<TTable>\n >,\n> = SerializerConfig<\n StandardSchemaV1.InferOutput<TSchema>,\n ExtractedTable<TTable>\n> & {\n schema: TSchema\n}\n\n/**\n * Config where TInput and TOutput have arbitrarily typed values.\n * The keys of the types need to equal the SQLite types.\n * Since TInput is not the SQLite types, we require a schema in order to deserialize incoming SQLite updates. The schema should validate from SQLite to TOutput.\n */\nexport type ConfigWithArbitraryCollectionTypes<\n TTable extends Table,\n TSchema extends StandardSchemaV1<\n // The input and output must have the same keys, the value types can be arbitrary\n AnyTableColumnType<TTable>,\n AnyTableColumnType<TTable>\n >,\n> = SerializerConfig<\n StandardSchemaV1.InferOutput<TSchema>,\n ExtractedTable<TTable>\n> & {\n schema: TSchema\n /**\n * Schema for deserializing and validating input data from the sync stream.\n *\n * This schema defines how to transform and validate data coming from SQLite types (as stored in the database)\n * into the desired output types (`TOutput`) expected by your application or validation logic.\n *\n * The generic parameters allow for arbitrary input and output types, so you can specify custom conversion rules\n * for each column. This is especially useful when your application expects richer types (e.g., Date, enums, objects)\n * than what SQLite natively supports.\n *\n * Use this to ensure that incoming data from the sync stream is properly converted and validated before use.\n *\n * Example:\n * ```typescript\n * deserializationSchema: z.object({\n * createdAt: z.preprocess((val) => new Date(val as string), z.date()),\n * meta: z.preprocess((val) => JSON.parse(val as string), z.object({ ... })),\n * })\n * ```\n *\n * This enables robust type safety and validation for incoming data, bridging the gap between SQLite storage\n * and your application's expected types.\n */\n deserializationSchema: StandardSchemaV1<\n ExtractedTable<TTable>,\n StandardSchemaV1.InferOutput<TSchema>\n >\n}\nexport type BasePowerSyncCollectionConfig<\n TTable extends Table = Table,\n TSchema extends StandardSchemaV1 = never,\n> = Omit<\n BaseCollectionConfig<ExtractedTable<TTable>, string, TSchema>,\n `onInsert` | `onUpdate` | `onDelete` | `getKey`\n> & {\n /** The PowerSync schema Table definition */\n table: TTable\n /** The PowerSync database instance */\n database: AbstractPowerSyncDatabase\n /**\n * The maximum number of documents to read from the SQLite table\n * in a single batch during the initial sync between PowerSync and the\n * in-memory TanStack DB collection.\n *\n * @remarks\n * - Defaults to {@link DEFAULT_BATCH_SIZE} if not specified.\n * - Larger values reduce the number of round trips to the storage\n * engine but increase memory usage per batch.\n * - Smaller values may lower memory usage and allow earlier\n * streaming of initial results, at the cost of more query calls.\n */\n syncBatchSize?: number\n}\n\n/**\n * Configuration interface for PowerSync collection options.\n * @template TTable - The PowerSync table schema definition\n * @template TSchema - The validation schema type\n */\n/**\n * Configuration options for creating a PowerSync collection.\n *\n * @example\n * ```typescript\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * }),\n * })\n *\n * const db = new PowerSyncDatabase({\n * database: {\n * dbFilename: \"test.sqlite\",\n * },\n * schema: APP_SCHEMA,\n * })\n *\n * const collection = createCollection(\n * powerSyncCollectionOptions({\n * database: db,\n * table: APP_SCHEMA.props.documents\n * })\n * )\n * ```\n */\nexport type PowerSyncCollectionConfig<\n TTable extends Table = Table,\n TSchema extends StandardSchemaV1<any> = never,\n> = BasePowerSyncCollectionConfig<TTable, TSchema> &\n (\n | ConfigWithSQLiteTypes\n | ConfigWithSQLiteInputType<TTable, TSchema>\n | ConfigWithArbitraryCollectionTypes<TTable, TSchema>\n )\n\n/**\n * Metadata for the PowerSync Collection.\n */\nexport type PowerSyncCollectionMeta<TTable extends Table = Table> = {\n /**\n * The SQLite table representing the collection.\n */\n tableName: string\n /**\n * The internal table used to track diffs for the collection.\n */\n trackedTableName: string\n\n /**\n * Serializes a collection value to the SQLite type\n */\n serializeValue: (value: any) => ExtractedTable<TTable>\n}\n\n/**\n * A CollectionConfig which includes utilities for PowerSync.\n */\nexport type EnhancedPowerSyncCollectionConfig<\n TTable extends Table,\n OutputType extends Record<string, unknown> = Record<string, unknown>,\n TSchema extends StandardSchemaV1 = never,\n> = CollectionConfig<OutputType, string, TSchema> & {\n id?: string\n utils: PowerSyncCollectionUtils<TTable>\n schema?: TSchema\n}\n\n/**\n * Collection-level utilities for PowerSync.\n */\nexport type PowerSyncCollectionUtils<TTable extends Table = Table> = {\n getMeta: () => PowerSyncCollectionMeta<TTable>\n}\n\n/**\n * Default value for {@link PowerSyncCollectionConfig#syncBatchSize}.\n */\nexport const DEFAULT_BATCH_SIZE = 1000\n"],"names":[],"mappings":"AAiRO,MAAM,qBAAqB;"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DiffTriggerOperation, BaseColumnType, ExtractColumnValueType, Table } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* All PowerSync table records include a UUID `id` column.
|
|
4
|
+
*/
|
|
5
|
+
export type PowerSyncRecord = {
|
|
6
|
+
id: string;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Utility type: If T includes null, also allow undefined (to support optional fields in insert/update operations).
|
|
11
|
+
* PowerSync records are typically typed as `string | null`, where insert
|
|
12
|
+
* and update operations may also allow not specifying a value at all (optional).
|
|
13
|
+
*/
|
|
14
|
+
type WithUndefinedIfNull<T> = null extends T ? T | undefined : T;
|
|
15
|
+
type OptionalIfUndefined<T> = {
|
|
16
|
+
[K in keyof T as undefined extends T[K] ? K : never]?: T[K];
|
|
17
|
+
} & {
|
|
18
|
+
[K in keyof T as undefined extends T[K] ? never : K]: T[K];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Provides the base column types for a table. This excludes the `id` column.
|
|
22
|
+
*/
|
|
23
|
+
export type ExtractedTableColumns<TTable extends Table> = {
|
|
24
|
+
[K in keyof TTable[`columnMap`]]: ExtractColumnValueType<TTable[`columnMap`][K]>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Utility type that extracts the typed structure of a table based on its column definitions.
|
|
28
|
+
* Maps each column to its corresponding TypeScript type using ExtractColumnValueType.
|
|
29
|
+
*
|
|
30
|
+
* @template TTable - The PowerSync table definition
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const table = new Table({
|
|
34
|
+
* name: column.text,
|
|
35
|
+
* age: column.integer
|
|
36
|
+
* })
|
|
37
|
+
* type TableType = ExtractedTable<typeof table>
|
|
38
|
+
* // Results in: { id: string, name: string | null, age: number | null }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export type ExtractedTable<TTable extends Table> = ExtractedTableColumns<TTable> & {
|
|
42
|
+
id: string;
|
|
43
|
+
};
|
|
44
|
+
export type OptionalExtractedTable<TTable extends Table> = OptionalIfUndefined<{
|
|
45
|
+
[K in keyof TTable[`columnMap`]]: WithUndefinedIfNull<ExtractColumnValueType<TTable[`columnMap`][K]>>;
|
|
46
|
+
}> & {
|
|
47
|
+
id: string;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Maps the schema of TTable to a type which
|
|
51
|
+
* requires the keys be equal, but the values can have any value type.
|
|
52
|
+
*/
|
|
53
|
+
export type AnyTableColumnType<TTable extends Table> = {
|
|
54
|
+
[K in keyof TTable[`columnMap`]]: any;
|
|
55
|
+
} & {
|
|
56
|
+
id: string;
|
|
57
|
+
};
|
|
58
|
+
export declare function asPowerSyncRecord(record: any): PowerSyncRecord;
|
|
59
|
+
export type MapBaseColumnType<TOutput> = {
|
|
60
|
+
[Key in keyof TOutput]: BaseColumnType<any>;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Maps {@link DiffTriggerOperation} to TanstackDB operations
|
|
64
|
+
*/
|
|
65
|
+
export declare function mapOperation(operation: DiffTriggerOperation): "insert" | "update" | "delete";
|
|
66
|
+
/**
|
|
67
|
+
* Maps TanstackDB operations to {@link DiffTriggerOperation}
|
|
68
|
+
*/
|
|
69
|
+
export declare function mapOperationToPowerSync(operation: string): DiffTriggerOperation;
|
|
70
|
+
export {};
|