@tanstack/powersync-db-collection 0.1.37 → 0.1.38
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/cjs/definitions.cjs.map +1 -1
- package/dist/cjs/definitions.d.cts +34 -3
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/powersync.cjs +233 -78
- package/dist/cjs/powersync.cjs.map +1 -1
- package/dist/cjs/sqlite-compiler.cjs +219 -0
- package/dist/cjs/sqlite-compiler.cjs.map +1 -0
- package/dist/cjs/sqlite-compiler.d.cts +42 -0
- package/dist/esm/definitions.d.ts +34 -3
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/powersync.js +233 -78
- package/dist/esm/powersync.js.map +1 -1
- package/dist/esm/sqlite-compiler.d.ts +42 -0
- package/dist/esm/sqlite-compiler.js +219 -0
- package/dist/esm/sqlite-compiler.js.map +1 -0
- package/package.json +7 -6
- package/src/definitions.ts +40 -2
- package/src/index.ts +1 -0
- package/src/powersync.ts +325 -89
- package/src/sqlite-compiler.ts +354 -0
package/dist/esm/powersync.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { DiffTriggerOperation, sanitizeSQL } from "@powersync/common";
|
|
2
|
+
import { or } from "@tanstack/db";
|
|
3
|
+
import { compileSQLite } from "./sqlite-compiler.js";
|
|
2
4
|
import { PendingOperationStore } from "./PendingOperationStore.js";
|
|
3
5
|
import { PowerSyncTransactor } from "./PowerSyncTransactor.js";
|
|
4
6
|
import { DEFAULT_BATCH_SIZE } from "./definitions.js";
|
|
@@ -11,6 +13,7 @@ function powerSyncCollectionOptions(config) {
|
|
|
11
13
|
table,
|
|
12
14
|
schema: inputSchema,
|
|
13
15
|
syncBatchSize = DEFAULT_BATCH_SIZE,
|
|
16
|
+
syncMode = "eager",
|
|
14
17
|
...restConfig
|
|
15
18
|
} = config;
|
|
16
19
|
const deserializationSchema = `deserializationSchema` in config ? config.deserializationSchema : null;
|
|
@@ -44,119 +47,270 @@ function powerSyncCollectionOptions(config) {
|
|
|
44
47
|
});
|
|
45
48
|
const sync = {
|
|
46
49
|
sync: (params) => {
|
|
47
|
-
const { begin, write, commit, markReady } = params;
|
|
50
|
+
const { begin, write, collection, commit, markReady } = params;
|
|
48
51
|
const abortController = new AbortController();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const operations = await context.getAll(
|
|
59
|
-
`SELECT * FROM ${trackedTableName} ORDER BY timestamp ASC`
|
|
60
|
-
);
|
|
61
|
-
const pendingOperations = [];
|
|
62
|
-
for (const op of operations) {
|
|
63
|
-
const { id, operation, timestamp, value } = op;
|
|
64
|
-
const parsedValue = deserializeSyncRow({
|
|
65
|
-
id,
|
|
66
|
-
...JSON.parse(value)
|
|
67
|
-
});
|
|
68
|
-
const parsedPreviousValue = op.operation == DiffTriggerOperation.UPDATE ? deserializeSyncRow({
|
|
69
|
-
id,
|
|
70
|
-
...JSON.parse(op.previous_value)
|
|
71
|
-
}) : void 0;
|
|
72
|
-
write({
|
|
73
|
-
type: mapOperation(operation),
|
|
74
|
-
value: parsedValue,
|
|
75
|
-
previousValue: parsedPreviousValue
|
|
76
|
-
});
|
|
77
|
-
pendingOperations.push({
|
|
78
|
-
id,
|
|
79
|
-
operation,
|
|
80
|
-
timestamp,
|
|
81
|
-
tableName: viewName
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
await context.execute(`DELETE FROM ${trackedTableName}`);
|
|
85
|
-
commit();
|
|
86
|
-
pendingOperationStore.resolvePendingFor(pendingOperations);
|
|
87
|
-
}).catch((error) => {
|
|
88
|
-
database.logger.error(
|
|
89
|
-
`An error has been detected in the sync handler`,
|
|
90
|
-
error
|
|
91
|
-
);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
signal: abortController.signal,
|
|
97
|
-
triggerImmediate: false,
|
|
98
|
-
tables: [trackedTableName]
|
|
99
|
-
}
|
|
100
|
-
);
|
|
101
|
-
const disposeTracking = await database.triggers.createDiffTrigger({
|
|
52
|
+
let disposeTracking = null;
|
|
53
|
+
if (syncMode === `eager`) {
|
|
54
|
+
return runEagerSync();
|
|
55
|
+
} else {
|
|
56
|
+
return runOnDemandSync();
|
|
57
|
+
}
|
|
58
|
+
async function createDiffTrigger(options) {
|
|
59
|
+
const { setupContext, when, writeType, batchQuery, onReady } = options;
|
|
60
|
+
return await database.triggers.createDiffTrigger({
|
|
102
61
|
source: viewName,
|
|
103
62
|
destination: trackedTableName,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
[DiffTriggerOperation.UPDATE]: `TRUE`,
|
|
107
|
-
[DiffTriggerOperation.DELETE]: `TRUE`
|
|
108
|
-
},
|
|
63
|
+
setupContext,
|
|
64
|
+
when,
|
|
109
65
|
hooks: {
|
|
110
66
|
beforeCreate: async (context) => {
|
|
111
67
|
let currentBatchCount = syncBatchSize;
|
|
112
68
|
let cursor = 0;
|
|
113
69
|
while (currentBatchCount == syncBatchSize) {
|
|
114
70
|
begin();
|
|
115
|
-
const batchItems = await
|
|
116
|
-
|
|
117
|
-
|
|
71
|
+
const batchItems = await batchQuery(
|
|
72
|
+
context,
|
|
73
|
+
syncBatchSize,
|
|
74
|
+
cursor
|
|
118
75
|
);
|
|
119
76
|
currentBatchCount = batchItems.length;
|
|
120
77
|
cursor += currentBatchCount;
|
|
121
78
|
for (const row of batchItems) {
|
|
122
79
|
write({
|
|
123
|
-
type:
|
|
80
|
+
type: writeType(row.id),
|
|
124
81
|
value: deserializeSyncRow(row)
|
|
125
82
|
});
|
|
126
83
|
}
|
|
127
84
|
commit();
|
|
128
85
|
}
|
|
129
|
-
|
|
86
|
+
onReady();
|
|
130
87
|
database.logger.info(
|
|
131
88
|
`Sync is ready for ${viewName} into ${trackedTableName}`
|
|
132
89
|
);
|
|
133
90
|
}
|
|
134
91
|
}
|
|
135
92
|
});
|
|
93
|
+
}
|
|
94
|
+
async function flushDiffRecords() {
|
|
95
|
+
await database.writeTransaction(async (context) => {
|
|
96
|
+
await flushDiffRecordsWithContext(context);
|
|
97
|
+
}).catch((error) => {
|
|
98
|
+
database.logger.error(
|
|
99
|
+
`An error has been detected in the sync handler`,
|
|
100
|
+
error
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async function flushDiffRecordsWithContext(context) {
|
|
105
|
+
try {
|
|
106
|
+
begin();
|
|
107
|
+
const operations = await context.getAll(
|
|
108
|
+
`SELECT * FROM ${trackedTableName} ORDER BY operation_id ASC`
|
|
109
|
+
);
|
|
110
|
+
const pendingOperations = [];
|
|
111
|
+
for (const op of operations) {
|
|
112
|
+
const { id, operation, timestamp, value } = op;
|
|
113
|
+
const parsedValue = deserializeSyncRow({
|
|
114
|
+
id,
|
|
115
|
+
...JSON.parse(value)
|
|
116
|
+
});
|
|
117
|
+
const parsedPreviousValue = op.operation == DiffTriggerOperation.UPDATE ? deserializeSyncRow({
|
|
118
|
+
id,
|
|
119
|
+
...JSON.parse(op.previous_value)
|
|
120
|
+
}) : void 0;
|
|
121
|
+
write({
|
|
122
|
+
type: mapOperation(operation),
|
|
123
|
+
value: parsedValue,
|
|
124
|
+
previousValue: parsedPreviousValue
|
|
125
|
+
});
|
|
126
|
+
pendingOperations.push({
|
|
127
|
+
id,
|
|
128
|
+
operation,
|
|
129
|
+
timestamp,
|
|
130
|
+
tableName: viewName
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
await context.execute(`DELETE FROM ${trackedTableName}`);
|
|
134
|
+
commit();
|
|
135
|
+
pendingOperationStore.resolvePendingFor(pendingOperations);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
database.logger.error(
|
|
138
|
+
`An error has been detected in the sync handler`,
|
|
139
|
+
error
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function start(afterOnChangeRegistered) {
|
|
144
|
+
database.logger.info(
|
|
145
|
+
`Sync is starting for ${viewName} into ${trackedTableName}`
|
|
146
|
+
);
|
|
147
|
+
database.onChangeWithCallback(
|
|
148
|
+
{
|
|
149
|
+
onChange: async () => {
|
|
150
|
+
await flushDiffRecords();
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
signal: abortController.signal,
|
|
155
|
+
triggerImmediate: false,
|
|
156
|
+
tables: [trackedTableName]
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
await afterOnChangeRegistered?.();
|
|
136
160
|
if (abortController.signal.aborted) {
|
|
137
|
-
await disposeTracking();
|
|
161
|
+
await disposeTracking?.();
|
|
138
162
|
} else {
|
|
139
163
|
abortController.signal.addEventListener(
|
|
140
164
|
`abort`,
|
|
141
|
-
() => {
|
|
142
|
-
disposeTracking();
|
|
165
|
+
async () => {
|
|
166
|
+
await disposeTracking?.();
|
|
143
167
|
},
|
|
144
168
|
{ once: true }
|
|
145
169
|
);
|
|
146
170
|
}
|
|
147
171
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
172
|
+
function runEagerSync() {
|
|
173
|
+
let onUnload = null;
|
|
174
|
+
start(async () => {
|
|
175
|
+
onUnload = await restConfig.onLoad?.();
|
|
176
|
+
disposeTracking = await createDiffTrigger({
|
|
177
|
+
when: {
|
|
178
|
+
[DiffTriggerOperation.INSERT]: `TRUE`,
|
|
179
|
+
[DiffTriggerOperation.UPDATE]: `TRUE`,
|
|
180
|
+
[DiffTriggerOperation.DELETE]: `TRUE`
|
|
181
|
+
},
|
|
182
|
+
writeType: (_rowId) => `insert`,
|
|
183
|
+
batchQuery: (lockContext, batchSize, cursor) => lockContext.getAll(
|
|
184
|
+
sanitizeSQL`SELECT * FROM ${viewName} LIMIT ? OFFSET ?`,
|
|
185
|
+
[batchSize, cursor]
|
|
186
|
+
),
|
|
187
|
+
onReady: () => markReady()
|
|
188
|
+
});
|
|
189
|
+
}).catch(
|
|
190
|
+
(error) => database.logger.error(
|
|
191
|
+
`Could not start syncing process for ${viewName} into ${trackedTableName}`,
|
|
192
|
+
error
|
|
193
|
+
)
|
|
157
194
|
);
|
|
158
|
-
|
|
159
|
-
|
|
195
|
+
return () => {
|
|
196
|
+
database.logger.info(
|
|
197
|
+
`Sync has been stopped for ${viewName} into ${trackedTableName}`
|
|
198
|
+
);
|
|
199
|
+
abortController.abort();
|
|
200
|
+
onUnload?.();
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function runOnDemandSync() {
|
|
204
|
+
let onUnloadSubset = null;
|
|
205
|
+
start().catch(
|
|
206
|
+
(error) => database.logger.error(
|
|
207
|
+
`Could not start syncing process for ${viewName} into ${trackedTableName}`,
|
|
208
|
+
error
|
|
209
|
+
)
|
|
210
|
+
);
|
|
211
|
+
const activeWhereExpressions = [];
|
|
212
|
+
const loadSubset = async (options) => {
|
|
213
|
+
if (options) {
|
|
214
|
+
activeWhereExpressions.push(options.where);
|
|
215
|
+
onUnloadSubset = await restConfig.onLoadSubset?.(options);
|
|
216
|
+
}
|
|
217
|
+
if (activeWhereExpressions.length === 0) {
|
|
218
|
+
await database.writeLock(async (ctx) => {
|
|
219
|
+
await flushDiffRecordsWithContext(ctx);
|
|
220
|
+
await disposeTracking?.({ context: ctx });
|
|
221
|
+
});
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const combinedWhere = activeWhereExpressions.length === 1 ? activeWhereExpressions[0] : or(
|
|
225
|
+
activeWhereExpressions[0],
|
|
226
|
+
activeWhereExpressions[1],
|
|
227
|
+
...activeWhereExpressions.slice(2)
|
|
228
|
+
);
|
|
229
|
+
const compiledNewData = compileSQLite(
|
|
230
|
+
{ where: combinedWhere },
|
|
231
|
+
{ jsonColumn: "NEW.data" }
|
|
232
|
+
);
|
|
233
|
+
const compiledOldData = compileSQLite(
|
|
234
|
+
{ where: combinedWhere },
|
|
235
|
+
{ jsonColumn: "OLD.data" }
|
|
236
|
+
);
|
|
237
|
+
const compiledView = compileSQLite({ where: combinedWhere });
|
|
238
|
+
const newDataWhenClause = toInlinedWhereClause(compiledNewData);
|
|
239
|
+
const oldDataWhenClause = toInlinedWhereClause(compiledOldData);
|
|
240
|
+
const viewWhereClause = toInlinedWhereClause(compiledView);
|
|
241
|
+
await database.writeLock(async (ctx) => {
|
|
242
|
+
await flushDiffRecordsWithContext(ctx);
|
|
243
|
+
await disposeTracking?.({ context: ctx });
|
|
244
|
+
disposeTracking = await createDiffTrigger({
|
|
245
|
+
setupContext: ctx,
|
|
246
|
+
when: {
|
|
247
|
+
[DiffTriggerOperation.INSERT]: newDataWhenClause,
|
|
248
|
+
[DiffTriggerOperation.UPDATE]: `(${newDataWhenClause}) OR (${oldDataWhenClause})`,
|
|
249
|
+
[DiffTriggerOperation.DELETE]: oldDataWhenClause
|
|
250
|
+
},
|
|
251
|
+
writeType: (rowId) => collection.has(rowId) ? `update` : `insert`,
|
|
252
|
+
batchQuery: (lockContext, batchSize, cursor) => lockContext.getAll(
|
|
253
|
+
`SELECT * FROM ${viewName} WHERE ${viewWhereClause} LIMIT ? OFFSET ?`,
|
|
254
|
+
[batchSize, cursor]
|
|
255
|
+
),
|
|
256
|
+
onReady: () => {
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
const toInlinedWhereClause = (compiled) => {
|
|
262
|
+
if (!compiled.where) return "TRUE";
|
|
263
|
+
const sqlParts = compiled.where.split("?");
|
|
264
|
+
return sanitizeSQL(
|
|
265
|
+
sqlParts,
|
|
266
|
+
...compiled.params
|
|
267
|
+
);
|
|
268
|
+
};
|
|
269
|
+
const unloadSubset = async (options) => {
|
|
270
|
+
onUnloadSubset?.();
|
|
271
|
+
const idx = activeWhereExpressions.indexOf(options.where);
|
|
272
|
+
if (idx !== -1) {
|
|
273
|
+
activeWhereExpressions.splice(idx, 1);
|
|
274
|
+
}
|
|
275
|
+
const compiledDeparting = compileSQLite({ where: options.where });
|
|
276
|
+
const departingWhereSQL = toInlinedWhereClause(compiledDeparting);
|
|
277
|
+
let evictionSQL;
|
|
278
|
+
if (activeWhereExpressions.length === 0) {
|
|
279
|
+
evictionSQL = `SELECT id FROM ${viewName} WHERE ${departingWhereSQL}`;
|
|
280
|
+
} else {
|
|
281
|
+
const combinedRemaining = activeWhereExpressions.length === 1 ? activeWhereExpressions[0] : or(
|
|
282
|
+
activeWhereExpressions[0],
|
|
283
|
+
activeWhereExpressions[1],
|
|
284
|
+
...activeWhereExpressions.slice(2)
|
|
285
|
+
);
|
|
286
|
+
const compiledRemaining = compileSQLite({
|
|
287
|
+
where: combinedRemaining
|
|
288
|
+
});
|
|
289
|
+
const remainingWhereSQL = toInlinedWhereClause(compiledRemaining);
|
|
290
|
+
evictionSQL = `SELECT id FROM ${viewName} WHERE (${departingWhereSQL}) AND NOT (${remainingWhereSQL})`;
|
|
291
|
+
}
|
|
292
|
+
const rowsToEvict = await database.getAll(evictionSQL);
|
|
293
|
+
if (rowsToEvict.length > 0) {
|
|
294
|
+
begin();
|
|
295
|
+
for (const { id } of rowsToEvict) {
|
|
296
|
+
write({ type: `delete`, key: id });
|
|
297
|
+
}
|
|
298
|
+
commit();
|
|
299
|
+
}
|
|
300
|
+
await loadSubset();
|
|
301
|
+
};
|
|
302
|
+
markReady();
|
|
303
|
+
return {
|
|
304
|
+
cleanup: () => {
|
|
305
|
+
database.logger.info(
|
|
306
|
+
`Sync has been stopped for ${viewName} into ${trackedTableName}`
|
|
307
|
+
);
|
|
308
|
+
abortController.abort();
|
|
309
|
+
},
|
|
310
|
+
loadSubset: (options) => loadSubset(options),
|
|
311
|
+
unloadSubset: (options) => unloadSubset(options)
|
|
312
|
+
};
|
|
313
|
+
}
|
|
160
314
|
},
|
|
161
315
|
// Expose the getSyncMetadata function
|
|
162
316
|
getSyncMetadata: void 0
|
|
@@ -168,6 +322,7 @@ function powerSyncCollectionOptions(config) {
|
|
|
168
322
|
getKey,
|
|
169
323
|
// Syncing should start immediately since we need to monitor the changes for mutations
|
|
170
324
|
startSync: true,
|
|
325
|
+
syncMode,
|
|
171
326
|
sync,
|
|
172
327
|
onInsert: async (params) => {
|
|
173
328
|
return await transactor.applyTransaction(params.transaction);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"powersync.js","sources":["../../src/powersync.ts"],"sourcesContent":["import { DiffTriggerOperation, sanitizeSQL } from '@powersync/common'\nimport { PendingOperationStore } from './PendingOperationStore'\nimport { PowerSyncTransactor } from './PowerSyncTransactor'\nimport { DEFAULT_BATCH_SIZE } from './definitions'\nimport { asPowerSyncRecord, mapOperation } from './helpers'\nimport { convertTableToSchema } from './schema'\nimport { serializeForSQLite } from './serialization'\nimport type {\n AnyTableColumnType,\n ExtractedTable,\n ExtractedTableColumns,\n MapBaseColumnType,\n OptionalExtractedTable,\n} from './helpers'\nimport type {\n BasePowerSyncCollectionConfig,\n ConfigWithArbitraryCollectionTypes,\n ConfigWithSQLiteInputType,\n ConfigWithSQLiteTypes,\n CustomSQLiteSerializer,\n EnhancedPowerSyncCollectionConfig,\n InferPowerSyncOutputType,\n PowerSyncCollectionConfig,\n PowerSyncCollectionUtils,\n} from './definitions'\nimport type { PendingOperation } from './PendingOperationStore'\nimport type { SyncConfig } from '@tanstack/db'\nimport type { StandardSchemaV1 } from '@standard-schema/spec'\nimport type { Table, TriggerDiffRecord } from '@powersync/common'\n\n/**\n * Creates PowerSync collection options for use with a standard Collection.\n *\n * @template TTable - The SQLite-based typing\n * @template TSchema - The validation schema type (optionally supports a custom input type)\n * @param config - Configuration options for the PowerSync collection\n * @returns Collection options with utilities\n */\n\n// Overload 1: No schema is provided\n\n/**\n * Creates a PowerSync collection configuration with basic default validation.\n * Input and Output types are the SQLite column types.\n *\n * @example\n * ```typescript\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * }),\n * })\n *\n * type Document = (typeof APP_SCHEMA)[\"types\"][\"documents\"]\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 function powerSyncCollectionOptions<TTable extends Table = Table>(\n config: BasePowerSyncCollectionConfig<TTable, never> & ConfigWithSQLiteTypes,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n OptionalExtractedTable<TTable>,\n never\n>\n\n// Overload 2: Schema is provided and the TInput matches SQLite types.\n\n/**\n * Creates a PowerSync collection configuration with schema validation.\n *\n * The input types satisfy the SQLite column types.\n *\n * The output types are defined by the provided schema. This schema can enforce additional\n * validation or type transforms.\n * Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard\n * serialization implementation to serialize column values. Custom or advanced types require providing additional\n * serializer specifications. Partial column overrides can be supplied to `serializer`.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\"\n *\n * // The PowerSync SQLite schema\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * // Dates are stored as ISO date strings in SQLite\n * created_at: column.text\n * }),\n * })\n *\n * // Advanced Zod validations. The output type of this schema\n * // is constrained to the SQLite schema of APP_SCHEMA\n * const schema = z.object({\n * id: z.string(),\n * // Notice that `name` is not nullable (is required) here and it has additional validation\n * name: z.string().min(3, { message: \"Should be at least 3 characters\" }).nullable(),\n * // The input type is still the SQLite string type. While collections will output smart Date instances.\n * created_at: z.string().transform(val => new Date(val))\n * })\n *\n * const collection = createCollection(\n * powerSyncCollectionOptions({\n * database: db,\n * table: APP_SCHEMA.props.documents,\n * schema,\n * serializer: {\n * // The default is toISOString, this is just to demonstrate custom overrides\n * created_at: (outputValue) => outputValue.toISOString(),\n * },\n * })\n * )\n * ```\n */\nexport function powerSyncCollectionOptions<\n TTable extends Table,\n TSchema extends StandardSchemaV1<\n // TInput is the SQLite types. We can use the supplied schema to validate sync input\n OptionalExtractedTable<TTable>,\n AnyTableColumnType<TTable>\n >,\n>(\n config: BasePowerSyncCollectionConfig<TTable, TSchema> &\n ConfigWithSQLiteInputType<TTable, TSchema>,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n InferPowerSyncOutputType<TTable, TSchema>,\n TSchema\n> & {\n schema: TSchema\n}\n\n// Overload 3: Schema is provided with arbitrary TInput and TOutput\n/**\n * Creates a PowerSync collection configuration with schema validation.\n *\n * The input types are not linked to the internal SQLite table types. This can\n * give greater flexibility, e.g. by accepting rich types as input for `insert` or `update` operations.\n * An additional `deserializationSchema` is required in order to process incoming SQLite updates to the output type.\n *\n * The output types are defined by the provided schema. This schema can enforce additional\n * validation or type transforms.\n * Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard\n * serialization implementation to serialize column values. Custom or advanced types require providing additional\n * serializer specifications. Partial column overrides can be supplied to `serializer`.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\"\n *\n * // The PowerSync SQLite schema\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * // Booleans are represented as integers in SQLite\n * is_active: column.integer\n * }),\n * })\n *\n * // Advanced Zod validations.\n * // We accept boolean values as input for operations and expose Booleans in query results\n * const schema = z.object({\n * id: z.string(),\n * isActive: z.boolean(), // TInput and TOutput are boolean\n * })\n *\n * // The deserializationSchema converts the SQLite synced INTEGER (0/1) values to booleans.\n * const deserializationSchema = z.object({\n * id: z.string(),\n * isActive: z.number().nullable().transform((val) => val == null ? true : val > 0),\n * })\n *\n * const collection = createCollection(\n * powerSyncCollectionOptions({\n * database: db,\n * table: APP_SCHEMA.props.documents,\n * schema,\n * deserializationSchema,\n * })\n * )\n * ```\n */\nexport function powerSyncCollectionOptions<\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>(\n config: BasePowerSyncCollectionConfig<TTable, TSchema> &\n ConfigWithArbitraryCollectionTypes<TTable, TSchema>,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n InferPowerSyncOutputType<TTable, TSchema>,\n TSchema\n> & {\n utils: PowerSyncCollectionUtils<TTable>\n schema: TSchema\n}\n\n/**\n * Implementation of powerSyncCollectionOptions that handles both schema and non-schema configurations.\n */\n\nexport function powerSyncCollectionOptions<\n TTable extends Table,\n TSchema extends StandardSchemaV1<any> = never,\n>(config: PowerSyncCollectionConfig<TTable, TSchema>) {\n const {\n database,\n table,\n schema: inputSchema,\n syncBatchSize = DEFAULT_BATCH_SIZE,\n ...restConfig\n } = config\n\n const deserializationSchema =\n `deserializationSchema` in config ? config.deserializationSchema : null\n const serializer = `serializer` in config ? config.serializer : undefined\n const onDeserializationError =\n `onDeserializationError` in config\n ? config.onDeserializationError\n : undefined\n\n // The SQLite table type\n type TableType = ExtractedTable<TTable>\n\n // The collection output type\n type OutputType = InferPowerSyncOutputType<TTable, TSchema>\n\n const { viewName, trackMetadata: metadataIsTracked } = table\n\n /**\n * Deserializes data from the incoming sync stream\n */\n const deserializeSyncRow = (value: TableType): OutputType => {\n const validationSchema = deserializationSchema || schema\n const validation = validationSchema[`~standard`].validate(value)\n if (`value` in validation) {\n return validation.value\n } else if (`issues` in validation) {\n const issueMessage = `Failed to validate incoming data for ${viewName}. Issues: ${validation.issues.map((issue) => `${issue.path} - ${issue.message}`)}`\n database.logger.error(issueMessage)\n onDeserializationError!(validation)\n throw new Error(issueMessage)\n } else {\n const unknownErrorMessage = `Unknown deserialization error for ${viewName}`\n database.logger.error(unknownErrorMessage)\n onDeserializationError!({ issues: [{ message: unknownErrorMessage }] })\n throw new Error(unknownErrorMessage)\n }\n }\n\n // We can do basic runtime validations for columns if not explicit schema has been provided\n const schema = inputSchema ?? (convertTableToSchema(table) as TSchema)\n /**\n * The onInsert, onUpdate, and onDelete handlers should only return\n * after we have written the changes to TanStack DB.\n * We currently only write to TanStack DB from a diff trigger.\n * We wait for the diff trigger to observe the change,\n * and only then return from the on[X] handlers.\n * This ensures that when the transaction is reported as\n * complete to the caller, the in-memory state is already\n * consistent with the database.\n */\n const pendingOperationStore = PendingOperationStore.GLOBAL\n // Keep the tracked table unique in case of multiple tabs.\n const trackedTableName = `__${viewName}_tracking_${Math.floor(\n Math.random() * 0xffffffff,\n )\n .toString(16)\n .padStart(8, `0`)}`\n\n const transactor = new PowerSyncTransactor({\n database,\n })\n\n /**\n * \"sync\"\n * Notice that this describes the Sync between the local SQLite table\n * and the in-memory tanstack-db collection.\n */\n const sync: SyncConfig<OutputType, string> = {\n sync: (params) => {\n const { begin, write, commit, markReady } = params\n const abortController = new AbortController()\n\n // The sync function needs to be synchronous\n async function start() {\n database.logger.info(\n `Sync is starting for ${viewName} into ${trackedTableName}`,\n )\n database.onChangeWithCallback(\n {\n onChange: async () => {\n await database\n .writeTransaction(async (context) => {\n begin()\n const operations = await context.getAll<TriggerDiffRecord>(\n `SELECT * FROM ${trackedTableName} ORDER BY timestamp ASC`,\n )\n const pendingOperations: Array<PendingOperation> = []\n\n for (const op of operations) {\n const { id, operation, timestamp, value } = op\n const parsedValue = deserializeSyncRow({\n id,\n ...JSON.parse(value),\n })\n const parsedPreviousValue =\n op.operation == DiffTriggerOperation.UPDATE\n ? deserializeSyncRow({\n id,\n ...JSON.parse(op.previous_value),\n })\n : undefined\n write({\n type: mapOperation(operation),\n value: parsedValue,\n previousValue: parsedPreviousValue,\n })\n pendingOperations.push({\n id,\n operation,\n timestamp,\n tableName: viewName,\n })\n }\n\n // clear the current operations\n await context.execute(`DELETE FROM ${trackedTableName}`)\n\n commit()\n pendingOperationStore.resolvePendingFor(pendingOperations)\n })\n .catch((error) => {\n database.logger.error(\n `An error has been detected in the sync handler`,\n error,\n )\n })\n },\n },\n {\n signal: abortController.signal,\n triggerImmediate: false,\n tables: [trackedTableName],\n },\n )\n\n const disposeTracking = await database.triggers.createDiffTrigger({\n source: viewName,\n destination: trackedTableName,\n when: {\n [DiffTriggerOperation.INSERT]: `TRUE`,\n [DiffTriggerOperation.UPDATE]: `TRUE`,\n [DiffTriggerOperation.DELETE]: `TRUE`,\n },\n hooks: {\n beforeCreate: async (context) => {\n let currentBatchCount = syncBatchSize\n let cursor = 0\n while (currentBatchCount == syncBatchSize) {\n begin()\n const batchItems = await context.getAll<TableType>(\n sanitizeSQL`SELECT * FROM ${viewName} LIMIT ? OFFSET ?`,\n [syncBatchSize, cursor],\n )\n currentBatchCount = batchItems.length\n cursor += currentBatchCount\n for (const row of batchItems) {\n write({\n type: `insert`,\n value: deserializeSyncRow(row),\n })\n }\n commit()\n }\n markReady()\n database.logger.info(\n `Sync is ready for ${viewName} into ${trackedTableName}`,\n )\n },\n },\n })\n\n // If the abort controller was aborted while processing the request above\n if (abortController.signal.aborted) {\n await disposeTracking()\n } else {\n abortController.signal.addEventListener(\n `abort`,\n () => {\n disposeTracking()\n },\n { once: true },\n )\n }\n }\n\n start().catch((error) =>\n database.logger.error(\n `Could not start syncing process for ${viewName} into ${trackedTableName}`,\n error,\n ),\n )\n\n return () => {\n database.logger.info(\n `Sync has been stopped for ${viewName} into ${trackedTableName}`,\n )\n abortController.abort()\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata: undefined,\n }\n\n const getKey = (record: OutputType) => asPowerSyncRecord(record).id\n\n const outputConfig: EnhancedPowerSyncCollectionConfig<\n TTable,\n OutputType,\n TSchema\n > = {\n ...restConfig,\n schema,\n getKey,\n // Syncing should start immediately since we need to monitor the changes for mutations\n startSync: true,\n sync,\n onInsert: async (params) => {\n // The transaction here should only ever contain a single insert mutation\n return await transactor.applyTransaction(params.transaction)\n },\n onUpdate: async (params) => {\n // The transaction here should only ever contain a single update mutation\n return await transactor.applyTransaction(params.transaction)\n },\n onDelete: async (params) => {\n // The transaction here should only ever contain a single delete mutation\n return await transactor.applyTransaction(params.transaction)\n },\n utils: {\n getMeta: () => ({\n tableName: viewName,\n trackedTableName,\n metadataIsTracked,\n serializeValue: (value) =>\n serializeForSQLite(\n value,\n // This is required by the input generic\n table as Table<\n MapBaseColumnType<InferPowerSyncOutputType<TTable, TSchema>>\n >,\n // Coerce serializer to the shape that corresponds to the Table constructed from OutputType\n serializer as CustomSQLiteSerializer<\n OutputType,\n ExtractedTableColumns<Table<MapBaseColumnType<OutputType>>>\n >,\n ),\n }),\n },\n }\n return outputConfig\n}\n"],"names":[],"mappings":";;;;;;;AA0NO,SAAS,2BAGd,QAAoD;AACpD,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR,gBAAgB;AAAA,IAChB,GAAG;AAAA,EAAA,IACD;AAEJ,QAAM,wBACJ,2BAA2B,SAAS,OAAO,wBAAwB;AACrE,QAAM,aAAa,gBAAgB,SAAS,OAAO,aAAa;AAChE,QAAM,yBACJ,4BAA4B,SACxB,OAAO,yBACP;AAQN,QAAM,EAAE,UAAU,eAAe,kBAAA,IAAsB;AAKvD,QAAM,qBAAqB,CAAC,UAAiC;AAC3D,UAAM,mBAAmB,yBAAyB;AAClD,UAAM,aAAa,iBAAiB,WAAW,EAAE,SAAS,KAAK;AAC/D,QAAI,WAAW,YAAY;AACzB,aAAO,WAAW;AAAA,IACpB,WAAW,YAAY,YAAY;AACjC,YAAM,eAAe,wCAAwC,QAAQ,aAAa,WAAW,OAAO,IAAI,CAAC,UAAU,GAAG,MAAM,IAAI,MAAM,MAAM,OAAO,EAAE,CAAC;AACtJ,eAAS,OAAO,MAAM,YAAY;AAClC,6BAAwB,UAAU;AAClC,YAAM,IAAI,MAAM,YAAY;AAAA,IAC9B,OAAO;AACL,YAAM,sBAAsB,qCAAqC,QAAQ;AACzE,eAAS,OAAO,MAAM,mBAAmB;AACzC,6BAAwB,EAAE,QAAQ,CAAC,EAAE,SAAS,oB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AACL,0BAAgB,OAAO;AAAA,YACrB;AAAA,YACA,MAAM;AACJ,8BAAA;AAAA,YACF;AAAA,YACA,EAAE,MAAM,KAAA;AAAA,UAAK;AAAA,QAEjB;AAAA,MACF;AAEA,YAAA,EAAQ;AAAA,QAAM,CAAC,UACb,SAAS,OAAO;AAAA,UACd,uCAAuC,QAAQ,SAAS,gBAAgB;AAAA,UACxE;AAAA,QAAA;AAAA,MACF;AAGF,aAAO,MAAM;AACX,iBAAS,OAAO;AAAA,UACd,6BAA6B,QAAQ,SAAS,gBAAgB;AAAA,QAAA;AAEhE,wBAAgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA,iBAAiB;AAAA,EAAA;AAGnB,QAAM,SAAS,CAAC,WAAuB,kBAAkB,MAAM,EAAE;AAEjE,QAAM,eAIF;AAAA,IACF,GAAG;AAAA,IACH;AAAA,IACA;AAAA;AAAA,IAEA,WAAW;AAAA,IACX;AAAA,IACA,UAAU,OAAO,WAAW;AAE1B,aAAO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,UAAU,OAAO,WAAW;AAE1B,aAAO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,UAAU,OAAO,WAAW;AAE1B,aAAO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,OAAO;AAAA,MACL,SAAS,OAAO;AAAA,QACd,WAAW;AAAA,QACX;AAAA,QACA;AAAA,QACA,gBAAgB,CAAC,UACf;AAAA,UACE;AAAA;AAAA,UAEA;AAAA;AAAA,UAIA;AAAA,QAAA;AAAA,MAIF;AAAA,IACJ;AAAA,EACF;AAEF,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"powersync.js","sources":["../../src/powersync.ts"],"sourcesContent":["import { DiffTriggerOperation, sanitizeSQL } from '@powersync/common'\nimport { or } from '@tanstack/db'\nimport { compileSQLite } from './sqlite-compiler'\nimport { PendingOperationStore } from './PendingOperationStore'\nimport { PowerSyncTransactor } from './PowerSyncTransactor'\nimport { DEFAULT_BATCH_SIZE } from './definitions'\nimport { asPowerSyncRecord, mapOperation } from './helpers'\nimport { convertTableToSchema } from './schema'\nimport { serializeForSQLite } from './serialization'\nimport type {\n CleanupFn,\n LoadSubsetOptions,\n OperationType,\n SyncConfig,\n} from '@tanstack/db'\nimport type {\n AnyTableColumnType,\n ExtractedTable,\n ExtractedTableColumns,\n MapBaseColumnType,\n OptionalExtractedTable,\n} from './helpers'\nimport type {\n BasePowerSyncCollectionConfig,\n ConfigWithArbitraryCollectionTypes,\n ConfigWithSQLiteInputType,\n ConfigWithSQLiteTypes,\n CustomSQLiteSerializer,\n EnhancedPowerSyncCollectionConfig,\n InferPowerSyncOutputType,\n PowerSyncCollectionConfig,\n PowerSyncCollectionUtils,\n} from './definitions'\nimport type { PendingOperation } from './PendingOperationStore'\nimport type { StandardSchemaV1 } from '@standard-schema/spec'\nimport type { LockContext, Table, TriggerDiffRecord } from '@powersync/common'\n\n/**\n * Creates PowerSync collection options for use with a standard Collection.\n *\n * @template TTable - The SQLite-based typing\n * @template TSchema - The validation schema type (optionally supports a custom input type)\n * @param config - Configuration options for the PowerSync collection\n * @returns Collection options with utilities\n */\n\n// Overload 1: No schema is provided\n\n/**\n * Creates a PowerSync collection configuration with basic default validation.\n * Input and Output types are the SQLite column types.\n *\n * @example\n * ```typescript\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * }),\n * })\n *\n * type Document = (typeof APP_SCHEMA)[\"types\"][\"documents\"]\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 function powerSyncCollectionOptions<TTable extends Table = Table>(\n config: BasePowerSyncCollectionConfig<TTable, never> & ConfigWithSQLiteTypes,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n OptionalExtractedTable<TTable>,\n never\n>\n\n// Overload 2: Schema is provided and the TInput matches SQLite types.\n\n/**\n * Creates a PowerSync collection configuration with schema validation.\n *\n * The input types satisfy the SQLite column types.\n *\n * The output types are defined by the provided schema. This schema can enforce additional\n * validation or type transforms.\n * Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard\n * serialization implementation to serialize column values. Custom or advanced types require providing additional\n * serializer specifications. Partial column overrides can be supplied to `serializer`.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\"\n *\n * // The PowerSync SQLite schema\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * // Dates are stored as ISO date strings in SQLite\n * created_at: column.text\n * }),\n * })\n *\n * // Advanced Zod validations. The output type of this schema\n * // is constrained to the SQLite schema of APP_SCHEMA\n * const schema = z.object({\n * id: z.string(),\n * // Notice that `name` is not nullable (is required) here and it has additional validation\n * name: z.string().min(3, { message: \"Should be at least 3 characters\" }).nullable(),\n * // The input type is still the SQLite string type. While collections will output smart Date instances.\n * created_at: z.string().transform(val => new Date(val))\n * })\n *\n * const collection = createCollection(\n * powerSyncCollectionOptions({\n * database: db,\n * table: APP_SCHEMA.props.documents,\n * schema,\n * serializer: {\n * // The default is toISOString, this is just to demonstrate custom overrides\n * created_at: (outputValue) => outputValue.toISOString(),\n * },\n * })\n * )\n * ```\n */\nexport function powerSyncCollectionOptions<\n TTable extends Table,\n TSchema extends StandardSchemaV1<\n // TInput is the SQLite types. We can use the supplied schema to validate sync input\n OptionalExtractedTable<TTable>,\n AnyTableColumnType<TTable>\n >,\n>(\n config: BasePowerSyncCollectionConfig<TTable, TSchema> &\n ConfigWithSQLiteInputType<TTable, TSchema>,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n InferPowerSyncOutputType<TTable, TSchema>,\n TSchema\n> & {\n schema: TSchema\n}\n\n// Overload 3: Schema is provided with arbitrary TInput and TOutput\n/**\n * Creates a PowerSync collection configuration with schema validation.\n *\n * The input types are not linked to the internal SQLite table types. This can\n * give greater flexibility, e.g. by accepting rich types as input for `insert` or `update` operations.\n * An additional `deserializationSchema` is required in order to process incoming SQLite updates to the output type.\n *\n * The output types are defined by the provided schema. This schema can enforce additional\n * validation or type transforms.\n * Arbitrary output typed mutations are encoded to SQLite for persistence. We provide a basic standard\n * serialization implementation to serialize column values. Custom or advanced types require providing additional\n * serializer specifications. Partial column overrides can be supplied to `serializer`.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\"\n *\n * // The PowerSync SQLite schema\n * const APP_SCHEMA = new Schema({\n * documents: new Table({\n * name: column.text,\n * // Booleans are represented as integers in SQLite\n * is_active: column.integer\n * }),\n * })\n *\n * // Advanced Zod validations.\n * // We accept boolean values as input for operations and expose Booleans in query results\n * const schema = z.object({\n * id: z.string(),\n * isActive: z.boolean(), // TInput and TOutput are boolean\n * })\n *\n * // The deserializationSchema converts the SQLite synced INTEGER (0/1) values to booleans.\n * const deserializationSchema = z.object({\n * id: z.string(),\n * isActive: z.number().nullable().transform((val) => val == null ? true : val > 0),\n * })\n *\n * const collection = createCollection(\n * powerSyncCollectionOptions({\n * database: db,\n * table: APP_SCHEMA.props.documents,\n * schema,\n * deserializationSchema,\n * })\n * )\n * ```\n */\nexport function powerSyncCollectionOptions<\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>(\n config: BasePowerSyncCollectionConfig<TTable, TSchema> &\n ConfigWithArbitraryCollectionTypes<TTable, TSchema>,\n): EnhancedPowerSyncCollectionConfig<\n TTable,\n InferPowerSyncOutputType<TTable, TSchema>,\n TSchema\n> & {\n utils: PowerSyncCollectionUtils<TTable>\n schema: TSchema\n}\n\n/**\n * Implementation of powerSyncCollectionOptions that handles both schema and non-schema configurations.\n */\n\nexport function powerSyncCollectionOptions<\n TTable extends Table,\n TSchema extends StandardSchemaV1<any> = never,\n>(config: PowerSyncCollectionConfig<TTable, TSchema>) {\n const {\n database,\n table,\n schema: inputSchema,\n syncBatchSize = DEFAULT_BATCH_SIZE,\n syncMode = 'eager',\n ...restConfig\n } = config\n\n const deserializationSchema =\n `deserializationSchema` in config ? config.deserializationSchema : null\n const serializer = `serializer` in config ? config.serializer : undefined\n const onDeserializationError =\n `onDeserializationError` in config\n ? config.onDeserializationError\n : undefined\n\n // The SQLite table type\n type TableType = ExtractedTable<TTable>\n\n // The collection output type\n type OutputType = InferPowerSyncOutputType<TTable, TSchema>\n\n const { viewName, trackMetadata: metadataIsTracked } = table\n\n /**\n * Deserializes data from the incoming sync stream\n */\n const deserializeSyncRow = (value: TableType): OutputType => {\n const validationSchema = deserializationSchema || schema\n const validation = validationSchema[`~standard`].validate(value)\n if (`value` in validation) {\n return validation.value\n } else if (`issues` in validation) {\n const issueMessage = `Failed to validate incoming data for ${viewName}. Issues: ${validation.issues.map((issue) => `${issue.path} - ${issue.message}`)}`\n database.logger.error(issueMessage)\n onDeserializationError!(validation)\n throw new Error(issueMessage)\n } else {\n const unknownErrorMessage = `Unknown deserialization error for ${viewName}`\n database.logger.error(unknownErrorMessage)\n onDeserializationError!({ issues: [{ message: unknownErrorMessage }] })\n throw new Error(unknownErrorMessage)\n }\n }\n\n // We can do basic runtime validations for columns if not explicit schema has been provided\n const schema = inputSchema ?? (convertTableToSchema(table) as TSchema)\n /**\n * The onInsert, onUpdate, and onDelete handlers should only return\n * after we have written the changes to TanStack DB.\n * We currently only write to TanStack DB from a diff trigger.\n * We wait for the diff trigger to observe the change,\n * and only then return from the on[X] handlers.\n * This ensures that when the transaction is reported as\n * complete to the caller, the in-memory state is already\n * consistent with the database.\n */\n const pendingOperationStore = PendingOperationStore.GLOBAL\n // Keep the tracked table unique in case of multiple tabs.\n const trackedTableName = `__${viewName}_tracking_${Math.floor(\n Math.random() * 0xffffffff,\n )\n .toString(16)\n .padStart(8, `0`)}`\n\n const transactor = new PowerSyncTransactor({\n database,\n })\n\n /**\n * \"sync\"\n * Notice that this describes the Sync between the local SQLite table\n * and the in-memory tanstack-db collection.\n */\n const sync: SyncConfig<OutputType, string> = {\n sync: (params) => {\n const { begin, write, collection, commit, markReady } = params\n const abortController = new AbortController()\n\n let disposeTracking:\n | ((options?: { context?: LockContext }) => Promise<void>)\n | null = null\n\n if (syncMode === `eager`) {\n return runEagerSync()\n } else {\n return runOnDemandSync()\n }\n\n async function createDiffTrigger(options: {\n setupContext?: LockContext\n when: Record<DiffTriggerOperation, string>\n writeType: (rowId: string) => OperationType\n batchQuery: (\n lockContext: LockContext,\n batchSize: number,\n cursor: number,\n ) => Promise<Array<TableType>>\n onReady: () => void\n }) {\n const { setupContext, when, writeType, batchQuery, onReady } = options\n\n return await database.triggers.createDiffTrigger({\n source: viewName,\n destination: trackedTableName,\n setupContext,\n when,\n hooks: {\n beforeCreate: async (context) => {\n let currentBatchCount = syncBatchSize\n let cursor = 0\n while (currentBatchCount == syncBatchSize) {\n begin()\n\n const batchItems = await batchQuery(\n context,\n syncBatchSize,\n cursor,\n )\n currentBatchCount = batchItems.length\n cursor += currentBatchCount\n for (const row of batchItems) {\n write({\n type: writeType(row.id),\n value: deserializeSyncRow(row),\n })\n }\n commit()\n }\n onReady()\n database.logger.info(\n `Sync is ready for ${viewName} into ${trackedTableName}`,\n )\n },\n },\n })\n }\n\n async function flushDiffRecords(): Promise<void> {\n await database\n .writeTransaction(async (context) => {\n await flushDiffRecordsWithContext(context)\n })\n .catch((error) => {\n database.logger.error(\n `An error has been detected in the sync handler`,\n error,\n )\n })\n }\n\n // We can use this directly if we want to pair a flush with dispose+recreate diff trigger.\n async function flushDiffRecordsWithContext(\n context: LockContext,\n ): Promise<void> {\n try {\n begin()\n const operations = await context.getAll<TriggerDiffRecord>(\n `SELECT * FROM ${trackedTableName} ORDER BY operation_id ASC`,\n )\n const pendingOperations: Array<PendingOperation> = []\n\n for (const op of operations) {\n const { id, operation, timestamp, value } = op\n const parsedValue = deserializeSyncRow({\n id,\n ...JSON.parse(value),\n })\n const parsedPreviousValue =\n op.operation == DiffTriggerOperation.UPDATE\n ? deserializeSyncRow({\n id,\n ...JSON.parse(op.previous_value),\n })\n : undefined\n write({\n type: mapOperation(operation),\n value: parsedValue,\n previousValue: parsedPreviousValue,\n })\n pendingOperations.push({\n id,\n operation,\n timestamp,\n tableName: viewName,\n })\n }\n\n // clear the current operations\n await context.execute(`DELETE FROM ${trackedTableName}`)\n\n commit()\n pendingOperationStore.resolvePendingFor(pendingOperations)\n } catch (error) {\n database.logger.error(\n `An error has been detected in the sync handler`,\n error,\n )\n }\n }\n\n // The sync function needs to be synchronous.\n async function start(afterOnChangeRegistered?: () => Promise<void>) {\n database.logger.info(\n `Sync is starting for ${viewName} into ${trackedTableName}`,\n )\n database.onChangeWithCallback(\n {\n onChange: async () => {\n await flushDiffRecords()\n },\n },\n {\n signal: abortController.signal,\n triggerImmediate: false,\n tables: [trackedTableName],\n },\n )\n\n await afterOnChangeRegistered?.()\n\n // If the abort controller was aborted while processing the request above\n if (abortController.signal.aborted) {\n await disposeTracking?.()\n } else {\n abortController.signal.addEventListener(\n `abort`,\n async () => {\n await disposeTracking?.()\n },\n { once: true },\n )\n }\n }\n\n // Eager mode.\n // Registers a diff trigger for the entire table.\n function runEagerSync() {\n let onUnload: CleanupFn | void | null = null\n\n start(async () => {\n onUnload = await restConfig.onLoad?.()\n\n disposeTracking = await createDiffTrigger({\n when: {\n [DiffTriggerOperation.INSERT]: `TRUE`,\n [DiffTriggerOperation.UPDATE]: `TRUE`,\n [DiffTriggerOperation.DELETE]: `TRUE`,\n },\n writeType: (_rowId: string) => `insert`,\n batchQuery: (\n lockContext: LockContext,\n batchSize: number,\n cursor: number,\n ) =>\n lockContext.getAll<TableType>(\n sanitizeSQL`SELECT * FROM ${viewName} LIMIT ? OFFSET ?`,\n [batchSize, cursor],\n ),\n onReady: () => markReady(),\n })\n }).catch((error) =>\n database.logger.error(\n `Could not start syncing process for ${viewName} into ${trackedTableName}`,\n error,\n ),\n )\n\n return () => {\n database.logger.info(\n `Sync has been stopped for ${viewName} into ${trackedTableName}`,\n )\n abortController.abort()\n onUnload?.()\n }\n }\n\n // On-demand mode.\n // Registers a diff trigger for the active WHERE expressions.\n function runOnDemandSync() {\n let onUnloadSubset: CleanupFn | void | null = null\n\n start().catch((error) =>\n database.logger.error(\n `Could not start syncing process for ${viewName} into ${trackedTableName}`,\n error,\n ),\n )\n\n // Tracks all active WHERE expressions for on-demand sync filtering.\n // Each loadSubset call pushes its predicate; unloadSubset removes it.\n const activeWhereExpressions: Array<LoadSubsetOptions['where']> = []\n\n const loadSubset = async (\n options?: LoadSubsetOptions,\n ): Promise<void> => {\n if (options) {\n activeWhereExpressions.push(options.where)\n onUnloadSubset = await restConfig.onLoadSubset?.(options)\n }\n\n if (activeWhereExpressions.length === 0) {\n await database.writeLock(async (ctx) => {\n await flushDiffRecordsWithContext(ctx)\n await disposeTracking?.({ context: ctx })\n })\n return\n }\n\n const combinedWhere =\n activeWhereExpressions.length === 1\n ? activeWhereExpressions[0]\n : or(\n activeWhereExpressions[0],\n activeWhereExpressions[1],\n ...activeWhereExpressions.slice(2),\n )\n\n const compiledNewData = compileSQLite(\n { where: combinedWhere },\n { jsonColumn: 'NEW.data' },\n )\n\n const compiledOldData = compileSQLite(\n { where: combinedWhere },\n { jsonColumn: 'OLD.data' },\n )\n\n const compiledView = compileSQLite({ where: combinedWhere })\n\n const newDataWhenClause = toInlinedWhereClause(compiledNewData)\n const oldDataWhenClause = toInlinedWhereClause(compiledOldData)\n const viewWhereClause = toInlinedWhereClause(compiledView)\n\n await database.writeLock(async (ctx) => {\n await flushDiffRecordsWithContext(ctx)\n await disposeTracking?.({ context: ctx })\n\n disposeTracking = await createDiffTrigger({\n setupContext: ctx,\n when: {\n [DiffTriggerOperation.INSERT]: newDataWhenClause,\n [DiffTriggerOperation.UPDATE]: `(${newDataWhenClause}) OR (${oldDataWhenClause})`,\n [DiffTriggerOperation.DELETE]: oldDataWhenClause,\n },\n writeType: (rowId: string) =>\n collection.has(rowId) ? `update` : `insert`,\n batchQuery: (\n lockContext: LockContext,\n batchSize: number,\n cursor: number,\n ) =>\n lockContext.getAll<TableType>(\n `SELECT * FROM ${viewName} WHERE ${viewWhereClause} LIMIT ? OFFSET ?`,\n [batchSize, cursor],\n ),\n onReady: () => {},\n })\n })\n }\n\n const toInlinedWhereClause = (compiled: {\n where?: string\n params: Array<unknown>\n }): string => {\n if (!compiled.where) return 'TRUE'\n const sqlParts = compiled.where.split('?')\n return sanitizeSQL(\n sqlParts as unknown as TemplateStringsArray,\n ...compiled.params,\n )\n }\n\n const unloadSubset = async (options: LoadSubsetOptions) => {\n onUnloadSubset?.()\n\n const idx = activeWhereExpressions.indexOf(options.where)\n if (idx !== -1) {\n activeWhereExpressions.splice(idx, 1)\n }\n\n // Evict rows that were exclusively loaded by the departing predicate.\n // These are rows matching the departing WHERE that are no longer covered\n // by any remaining active predicate.\n const compiledDeparting = compileSQLite({ where: options.where })\n const departingWhereSQL = toInlinedWhereClause(compiledDeparting)\n\n let evictionSQL: string\n if (activeWhereExpressions.length === 0) {\n evictionSQL = `SELECT id FROM ${viewName} WHERE ${departingWhereSQL}`\n } else {\n const combinedRemaining =\n activeWhereExpressions.length === 1\n ? activeWhereExpressions[0]!\n : or(\n activeWhereExpressions[0],\n activeWhereExpressions[1],\n ...activeWhereExpressions.slice(2),\n )\n const compiledRemaining = compileSQLite({\n where: combinedRemaining,\n })\n const remainingWhereSQL = toInlinedWhereClause(compiledRemaining)\n evictionSQL = `SELECT id FROM ${viewName} WHERE (${departingWhereSQL}) AND NOT (${remainingWhereSQL})`\n }\n\n const rowsToEvict = await database.getAll<{ id: string }>(evictionSQL)\n if (rowsToEvict.length > 0) {\n begin()\n for (const { id } of rowsToEvict) {\n write({ type: `delete`, key: id })\n }\n commit()\n }\n\n // Recreate the diff trigger for the remaining active WHERE expressions.\n await loadSubset()\n }\n\n markReady()\n\n return {\n cleanup: () => {\n database.logger.info(\n `Sync has been stopped for ${viewName} into ${trackedTableName}`,\n )\n abortController.abort()\n },\n loadSubset: (options: LoadSubsetOptions) => loadSubset(options),\n unloadSubset: (options: LoadSubsetOptions) => unloadSubset(options),\n }\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata: undefined,\n }\n\n const getKey = (record: OutputType) => asPowerSyncRecord(record).id\n\n const outputConfig: EnhancedPowerSyncCollectionConfig<\n TTable,\n OutputType,\n TSchema\n > = {\n ...restConfig,\n schema,\n getKey,\n // Syncing should start immediately since we need to monitor the changes for mutations\n startSync: true,\n syncMode,\n sync,\n onInsert: async (params) => {\n // The transaction here should only ever contain a single insert mutation\n return await transactor.applyTransaction(params.transaction)\n },\n onUpdate: async (params) => {\n // The transaction here should only ever contain a single update mutation\n return await transactor.applyTransaction(params.transaction)\n },\n onDelete: async (params) => {\n // The transaction here should only ever contain a single delete mutation\n return await transactor.applyTransaction(params.transaction)\n },\n utils: {\n getMeta: () => ({\n tableName: viewName,\n trackedTableName,\n metadataIsTracked,\n serializeValue: (value) =>\n serializeForSQLite(\n value,\n // This is required by the input generic\n table as Table<\n MapBaseColumnType<InferPowerSyncOutputType<TTable, TSchema>>\n >,\n // Coerce serializer to the shape that corresponds to the Table constructed from OutputType\n serializer as CustomSQLiteSerializer<\n OutputType,\n ExtractedTableColumns<Table<MapBaseColumnType<OutputType>>>\n >,\n ),\n }),\n },\n }\n return outputConfig\n}\n"],"names":[],"mappings":";;;;;;;;;AAiOO,SAAS,2BAGd,QAAoD;AACpD,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,QAAQ;AAAA,IACR,gBAAgB;AAAA,IAChB,WAAW;AAAA,IACX,GAAG;AAAA,EAAA,IACD;AAEJ,QAAM,wBACJ,2BAA2B,SAAS,OAAO,wBAAwB;AACrE,QAAM,aAAa,gBAAgB,SAAS,OAAO,aAAa;AAChE,QAAM,yBACJ,4BAA4B,SACxB,OAAO,yBACP;AAQN,QAAM,EAAE,UAAU,eAAe,kBAAA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AO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,UAAU,OAAO,WAAW;AAE1B,aAAO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,UAAU,OAAO,WAAW;AAE1B,aAAO,MAAM,WAAW,iBAAiB,OAAO,WAAW;AAAA,IAC7D;AAAA,IACA,OAAO;AAAA,MACL,SAAS,OAAO;AAAA,QACd,WAAW;AAAA,QACX;AAAA,QACA;AAAA,QACA,gBAAgB,CAAC,UACf;AAAA,UACE;AAAA;AAAA,UAEA;AAAA;AAAA,UAIA;AAAA,QAAA;AAAA,MAIF;AAAA,IACJ;AAAA,EACF;AAEF,SAAO;AACT;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { LoadSubsetOptions } from '@tanstack/db';
|
|
2
|
+
/**
|
|
3
|
+
* Result of compiling LoadSubsetOptions to SQLite
|
|
4
|
+
*/
|
|
5
|
+
export interface SQLiteCompiledQuery {
|
|
6
|
+
/** The WHERE clause (without "WHERE" keyword), e.g., "price > ?" */
|
|
7
|
+
where?: string;
|
|
8
|
+
/** The ORDER BY clause (without "ORDER BY" keyword), e.g., "price DESC" */
|
|
9
|
+
orderBy?: string;
|
|
10
|
+
/** The LIMIT value */
|
|
11
|
+
limit?: number;
|
|
12
|
+
/** Parameter values in order, to be passed to SQLite query */
|
|
13
|
+
params: Array<unknown>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for controlling how SQL is compiled.
|
|
17
|
+
*/
|
|
18
|
+
export interface CompileSQLiteOptions {
|
|
19
|
+
/**
|
|
20
|
+
* When set, column references emit `json_extract(<jsonColumn>, '$.<columnName>')`
|
|
21
|
+
* instead of `"<columnName>"`. The `id` column is excluded since it's stored
|
|
22
|
+
* as a direct column in the tracked table.
|
|
23
|
+
*/
|
|
24
|
+
jsonColumn?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Compiles TanStack DB LoadSubsetOptions to SQLite query components.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const compiled = compileSQLite({
|
|
32
|
+
* where: { type: 'func', name: 'gt', args: [
|
|
33
|
+
* { type: 'ref', path: ['price'] },
|
|
34
|
+
* { type: 'val', value: 100 }
|
|
35
|
+
* ]},
|
|
36
|
+
* orderBy: [{ expression: { type: 'ref', path: ['price'] }, compareOptions: { direction: 'desc', nulls: 'last' } }],
|
|
37
|
+
* limit: 50
|
|
38
|
+
* })
|
|
39
|
+
* // Result: { where: '"price" > ?', orderBy: '"price" DESC', limit: 50, params: [100] }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function compileSQLite(options: LoadSubsetOptions, compileOptions?: CompileSQLiteOptions): SQLiteCompiledQuery;
|