@tanstack/db-sqlite-persistence-core 0.1.5 → 0.1.7
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/persisted.cjs +13 -3
- package/dist/cjs/persisted.cjs.map +1 -1
- package/dist/cjs/persisted.d.cts +16 -16
- package/dist/cjs/sqlite-core-adapter.cjs +6 -10
- package/dist/cjs/sqlite-core-adapter.cjs.map +1 -1
- package/dist/cjs/sqlite-core-adapter.d.cts +7 -7
- package/dist/esm/persisted.d.ts +16 -16
- package/dist/esm/persisted.js +14 -4
- package/dist/esm/persisted.js.map +1 -1
- package/dist/esm/sqlite-core-adapter.d.ts +7 -7
- package/dist/esm/sqlite-core-adapter.js +6 -10
- package/dist/esm/sqlite-core-adapter.js.map +1 -1
- package/package.json +2 -2
- package/src/persisted.ts +71 -81
- package/src/sqlite-core-adapter.ts +46 -50
package/src/persisted.ts
CHANGED
|
@@ -199,7 +199,7 @@ export type ReplayableTxDelta<
|
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
export type PersistedScannedRow<
|
|
202
|
-
T extends object,
|
|
202
|
+
T extends object = Record<string, unknown>,
|
|
203
203
|
TKey extends string | number = string | number,
|
|
204
204
|
> = {
|
|
205
205
|
key: TKey
|
|
@@ -212,7 +212,7 @@ export type PersistedRowScanOptions = {
|
|
|
212
212
|
}
|
|
213
213
|
|
|
214
214
|
export type PersistedTx<
|
|
215
|
-
T extends object,
|
|
215
|
+
T extends object = Record<string, unknown>,
|
|
216
216
|
TKey extends string | number = string | number,
|
|
217
217
|
> = {
|
|
218
218
|
txId: string
|
|
@@ -241,26 +241,26 @@ export type PersistedTx<
|
|
|
241
241
|
collectionMetadataMutations?: Array<PersistedCollectionMetadataMutation>
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
-
export interface PersistenceAdapter
|
|
245
|
-
T extends object,
|
|
246
|
-
TKey extends string | number = string | number,
|
|
247
|
-
> {
|
|
244
|
+
export interface PersistenceAdapter {
|
|
248
245
|
loadSubset: (
|
|
249
246
|
collectionId: string,
|
|
250
247
|
options: LoadSubsetOptions,
|
|
251
248
|
ctx?: { requiredIndexSignatures?: ReadonlyArray<string> },
|
|
252
|
-
) => Promise<
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
249
|
+
) => Promise<
|
|
250
|
+
Array<{
|
|
251
|
+
key: string | number
|
|
252
|
+
value: Record<string, unknown>
|
|
253
|
+
metadata?: unknown
|
|
254
|
+
}>
|
|
255
|
+
>
|
|
256
|
+
applyCommittedTx: (collectionId: string, tx: PersistedTx) => Promise<void>
|
|
257
257
|
loadCollectionMetadata?: (
|
|
258
258
|
collectionId: string,
|
|
259
259
|
) => Promise<Array<{ key: string; value: unknown }>>
|
|
260
260
|
scanRows?: (
|
|
261
261
|
collectionId: string,
|
|
262
262
|
options?: PersistedRowScanOptions,
|
|
263
|
-
) => Promise<Array<PersistedScannedRow
|
|
263
|
+
) => Promise<Array<PersistedScannedRow>>
|
|
264
264
|
ensureIndex: (
|
|
265
265
|
collectionId: string,
|
|
266
266
|
signature: string,
|
|
@@ -317,26 +317,20 @@ export interface PersistedCollectionCoordinator {
|
|
|
317
317
|
) => Promise<PullSinceResponse>
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
-
export interface PersistedCollectionPersistence
|
|
321
|
-
|
|
322
|
-
TKey extends string | number = string | number,
|
|
323
|
-
> {
|
|
324
|
-
adapter: PersistenceAdapter<T, TKey>
|
|
320
|
+
export interface PersistedCollectionPersistence {
|
|
321
|
+
adapter: PersistenceAdapter
|
|
325
322
|
coordinator?: PersistedCollectionCoordinator
|
|
326
323
|
resolvePersistenceForCollection?: (options: {
|
|
327
324
|
collectionId: string
|
|
328
325
|
mode: PersistedCollectionMode
|
|
329
326
|
schemaVersion?: number
|
|
330
|
-
}) => PersistedCollectionPersistence
|
|
327
|
+
}) => PersistedCollectionPersistence
|
|
331
328
|
resolvePersistenceForMode?: (
|
|
332
329
|
mode: PersistedCollectionMode,
|
|
333
|
-
) => PersistedCollectionPersistence
|
|
330
|
+
) => PersistedCollectionPersistence
|
|
334
331
|
}
|
|
335
332
|
|
|
336
|
-
type PersistedResolvedPersistence
|
|
337
|
-
T extends object,
|
|
338
|
-
TKey extends string | number,
|
|
339
|
-
> = PersistedCollectionPersistence<T, TKey> & {
|
|
333
|
+
type PersistedResolvedPersistence = PersistedCollectionPersistence & {
|
|
340
334
|
coordinator: PersistedCollectionCoordinator
|
|
341
335
|
}
|
|
342
336
|
|
|
@@ -360,7 +354,7 @@ export type PersistedSyncWrappedOptions<
|
|
|
360
354
|
TUtils extends UtilsRecord = UtilsRecord,
|
|
361
355
|
> = CollectionConfig<T, TKey, TSchema, TUtils> & {
|
|
362
356
|
sync: SyncConfig<T, TKey>
|
|
363
|
-
persistence: PersistedCollectionPersistence
|
|
357
|
+
persistence: PersistedCollectionPersistence
|
|
364
358
|
schemaVersion?: number
|
|
365
359
|
}
|
|
366
360
|
|
|
@@ -370,7 +364,7 @@ export type PersistedLocalOnlyOptions<
|
|
|
370
364
|
TSchema extends StandardSchemaV1 = never,
|
|
371
365
|
TUtils extends UtilsRecord = UtilsRecord,
|
|
372
366
|
> = Omit<CollectionConfig<T, TKey, TSchema, TUtils>, `sync`> & {
|
|
373
|
-
persistence: PersistedCollectionPersistence
|
|
367
|
+
persistence: PersistedCollectionPersistence
|
|
374
368
|
schemaVersion?: number
|
|
375
369
|
}
|
|
376
370
|
|
|
@@ -380,7 +374,7 @@ type PersistedSyncOptionsResult<
|
|
|
380
374
|
TSchema extends StandardSchemaV1,
|
|
381
375
|
TUtils extends UtilsRecord,
|
|
382
376
|
> = CollectionConfig<T, TKey, TSchema, TUtils> & {
|
|
383
|
-
persistence: PersistedResolvedPersistence
|
|
377
|
+
persistence: PersistedResolvedPersistence
|
|
384
378
|
}
|
|
385
379
|
|
|
386
380
|
type PersistedLocalOnlyOptionsResult<
|
|
@@ -390,7 +384,7 @@ type PersistedLocalOnlyOptionsResult<
|
|
|
390
384
|
TUtils extends UtilsRecord,
|
|
391
385
|
> = CollectionConfig<T, TKey, TSchema, TUtils & PersistedCollectionUtils> & {
|
|
392
386
|
id: string
|
|
393
|
-
persistence: PersistedResolvedPersistence
|
|
387
|
+
persistence: PersistedResolvedPersistence
|
|
394
388
|
utils: TUtils & PersistedCollectionUtils
|
|
395
389
|
}
|
|
396
390
|
|
|
@@ -415,7 +409,7 @@ const REQUIRED_COORDINATOR_METHODS: ReadonlyArray<
|
|
|
415
409
|
|
|
416
410
|
const REQUIRED_ADAPTER_METHODS: ReadonlyArray<
|
|
417
411
|
keyof Pick<
|
|
418
|
-
PersistenceAdapter
|
|
412
|
+
PersistenceAdapter,
|
|
419
413
|
`loadSubset` | `applyCommittedTx` | `ensureIndex`
|
|
420
414
|
>
|
|
421
415
|
> = [`loadSubset`, `applyCommittedTx`, `ensureIndex`]
|
|
@@ -496,10 +490,7 @@ export function validatePersistedCollectionCoordinator(
|
|
|
496
490
|
}
|
|
497
491
|
}
|
|
498
492
|
|
|
499
|
-
function validatePersistenceAdapter
|
|
500
|
-
T extends object,
|
|
501
|
-
TKey extends string | number,
|
|
502
|
-
>(adapter: PersistenceAdapter<T, TKey>): void {
|
|
493
|
+
function validatePersistenceAdapter(adapter: PersistenceAdapter): void {
|
|
503
494
|
for (const method of REQUIRED_ADAPTER_METHODS) {
|
|
504
495
|
if (typeof adapter[method] !== `function`) {
|
|
505
496
|
throw new InvalidPersistenceAdapterError(method)
|
|
@@ -507,9 +498,9 @@ function validatePersistenceAdapter<
|
|
|
507
498
|
}
|
|
508
499
|
}
|
|
509
500
|
|
|
510
|
-
function resolvePersistence
|
|
511
|
-
persistence: PersistedCollectionPersistence
|
|
512
|
-
): PersistedResolvedPersistence
|
|
501
|
+
function resolvePersistence(
|
|
502
|
+
persistence: PersistedCollectionPersistence,
|
|
503
|
+
): PersistedResolvedPersistence {
|
|
513
504
|
validatePersistenceAdapter(persistence.adapter)
|
|
514
505
|
|
|
515
506
|
const coordinator = persistence.coordinator ?? new SingleProcessCoordinator()
|
|
@@ -521,28 +512,22 @@ function resolvePersistence<T extends object, TKey extends string | number>(
|
|
|
521
512
|
}
|
|
522
513
|
}
|
|
523
514
|
|
|
524
|
-
function resolvePersistenceForMode
|
|
525
|
-
|
|
526
|
-
TKey extends string | number,
|
|
527
|
-
>(
|
|
528
|
-
persistence: PersistedCollectionPersistence<T, TKey>,
|
|
515
|
+
function resolvePersistenceForMode(
|
|
516
|
+
persistence: PersistedCollectionPersistence,
|
|
529
517
|
mode: PersistedCollectionMode,
|
|
530
|
-
): PersistedResolvedPersistence
|
|
518
|
+
): PersistedResolvedPersistence {
|
|
531
519
|
const modeSpecificPersistence = persistence.resolvePersistenceForMode?.(mode)
|
|
532
520
|
return resolvePersistence(modeSpecificPersistence ?? persistence)
|
|
533
521
|
}
|
|
534
522
|
|
|
535
|
-
function resolvePersistenceForCollection
|
|
536
|
-
|
|
537
|
-
TKey extends string | number,
|
|
538
|
-
>(
|
|
539
|
-
persistence: PersistedCollectionPersistence<T, TKey>,
|
|
523
|
+
function resolvePersistenceForCollection(
|
|
524
|
+
persistence: PersistedCollectionPersistence,
|
|
540
525
|
options: {
|
|
541
526
|
collectionId: string
|
|
542
527
|
mode: PersistedCollectionMode
|
|
543
528
|
schemaVersion?: number
|
|
544
529
|
},
|
|
545
|
-
): PersistedResolvedPersistence
|
|
530
|
+
): PersistedResolvedPersistence {
|
|
546
531
|
const collectionSpecificPersistence =
|
|
547
532
|
persistence.resolvePersistenceForCollection?.(options)
|
|
548
533
|
if (collectionSpecificPersistence) {
|
|
@@ -838,7 +823,7 @@ class PersistedCollectionRuntime<
|
|
|
838
823
|
constructor(
|
|
839
824
|
private readonly mode: PersistedMode,
|
|
840
825
|
private readonly collectionId: string,
|
|
841
|
-
private readonly persistence: PersistedResolvedPersistence
|
|
826
|
+
private readonly persistence: PersistedResolvedPersistence,
|
|
842
827
|
private readonly syncMode: `eager` | `on-demand`,
|
|
843
828
|
private readonly dbName: string,
|
|
844
829
|
) {}
|
|
@@ -1195,7 +1180,7 @@ class PersistedCollectionRuntime<
|
|
|
1195
1180
|
): Promise<Array<{ key: TKey; value: T; metadata?: unknown }>> {
|
|
1196
1181
|
return this.persistence.adapter.loadSubset(this.collectionId, options, {
|
|
1197
1182
|
requiredIndexSignatures: this.getRequiredIndexSignatures(),
|
|
1198
|
-
})
|
|
1183
|
+
}) as Promise<Array<{ key: TKey; value: T; metadata?: unknown }>>
|
|
1199
1184
|
}
|
|
1200
1185
|
|
|
1201
1186
|
private async scanPersistedRowsUnsafe(
|
|
@@ -1205,7 +1190,10 @@ class PersistedCollectionRuntime<
|
|
|
1205
1190
|
return []
|
|
1206
1191
|
}
|
|
1207
1192
|
|
|
1208
|
-
return this.persistence.adapter.scanRows(
|
|
1193
|
+
return this.persistence.adapter.scanRows(
|
|
1194
|
+
this.collectionId,
|
|
1195
|
+
options,
|
|
1196
|
+
) as Promise<Array<PersistedScannedRow<T, TKey>>>
|
|
1209
1197
|
}
|
|
1210
1198
|
|
|
1211
1199
|
async scanPersistedRows(
|
|
@@ -1421,7 +1409,10 @@ class PersistedCollectionRuntime<
|
|
|
1421
1409
|
requiresFullReload: transaction.truncate,
|
|
1422
1410
|
changedRows: transaction.operations
|
|
1423
1411
|
.filter((operation) => operation.type === `update`)
|
|
1424
|
-
.map((operation) => ({
|
|
1412
|
+
.map((operation) => ({
|
|
1413
|
+
key: operation.key,
|
|
1414
|
+
value: operation.value as Record<string, unknown>,
|
|
1415
|
+
})),
|
|
1425
1416
|
deletedKeys: transaction.operations
|
|
1426
1417
|
.filter((operation) => operation.type === `delete`)
|
|
1427
1418
|
.map((operation) => operation.key),
|
|
@@ -1434,7 +1425,7 @@ class PersistedCollectionRuntime<
|
|
|
1434
1425
|
private createPersistedTxFromOperations(
|
|
1435
1426
|
transaction: BufferedSyncTransaction<T, TKey>,
|
|
1436
1427
|
streamPosition: { term: number; seq: number; rowVersion: number },
|
|
1437
|
-
): PersistedTx
|
|
1428
|
+
): PersistedTx {
|
|
1438
1429
|
return {
|
|
1439
1430
|
txId: crypto.randomUUID(),
|
|
1440
1431
|
term: streamPosition.term,
|
|
@@ -1444,22 +1435,26 @@ class PersistedCollectionRuntime<
|
|
|
1444
1435
|
mutations: transaction.operations.map((operation) =>
|
|
1445
1436
|
operation.type === `update`
|
|
1446
1437
|
? {
|
|
1447
|
-
type: `update
|
|
1438
|
+
type: `update` as const,
|
|
1448
1439
|
key: operation.key,
|
|
1449
|
-
value: operation.value,
|
|
1440
|
+
value: operation.value as Record<string, unknown>,
|
|
1450
1441
|
}
|
|
1451
1442
|
: {
|
|
1452
|
-
type: `delete
|
|
1443
|
+
type: `delete` as const,
|
|
1453
1444
|
key: operation.key,
|
|
1454
|
-
value: operation.value,
|
|
1445
|
+
value: operation.value as Record<string, unknown>,
|
|
1455
1446
|
},
|
|
1456
1447
|
),
|
|
1457
1448
|
rowMetadataMutations: Array.from(
|
|
1458
1449
|
transaction.rowMetadataWrites.entries(),
|
|
1459
1450
|
).map(([key, metadataWrite]) =>
|
|
1460
1451
|
metadataWrite.type === `delete`
|
|
1461
|
-
? { type: `delete
|
|
1462
|
-
: {
|
|
1452
|
+
? { type: `delete` as const, key: key }
|
|
1453
|
+
: {
|
|
1454
|
+
type: `set` as const,
|
|
1455
|
+
key: key,
|
|
1456
|
+
value: metadataWrite.value,
|
|
1457
|
+
},
|
|
1463
1458
|
),
|
|
1464
1459
|
collectionMetadataMutations: Array.from(
|
|
1465
1460
|
transaction.collectionMetadataWrites.entries(),
|
|
@@ -1474,7 +1469,7 @@ class PersistedCollectionRuntime<
|
|
|
1474
1469
|
private createPersistedTxFromMutations(
|
|
1475
1470
|
mutations: Array<PendingMutation<T>>,
|
|
1476
1471
|
streamPosition: { term: number; seq: number; rowVersion: number },
|
|
1477
|
-
): PersistedTx
|
|
1472
|
+
): PersistedTx {
|
|
1478
1473
|
return {
|
|
1479
1474
|
txId: crypto.randomUUID(),
|
|
1480
1475
|
term: streamPosition.term,
|
|
@@ -1483,24 +1478,24 @@ class PersistedCollectionRuntime<
|
|
|
1483
1478
|
mutations: mutations.map((mutation) => {
|
|
1484
1479
|
if (mutation.type === `delete`) {
|
|
1485
1480
|
return {
|
|
1486
|
-
type: `delete
|
|
1487
|
-
key: mutation.key as
|
|
1488
|
-
value: mutation.original as
|
|
1481
|
+
type: `delete` as const,
|
|
1482
|
+
key: mutation.key as string | number,
|
|
1483
|
+
value: mutation.original as Record<string, unknown>,
|
|
1489
1484
|
}
|
|
1490
1485
|
}
|
|
1491
1486
|
|
|
1492
1487
|
if (mutation.type === `insert`) {
|
|
1493
1488
|
return {
|
|
1494
|
-
type: `insert
|
|
1495
|
-
key: mutation.key as
|
|
1496
|
-
value: mutation.modified,
|
|
1489
|
+
type: `insert` as const,
|
|
1490
|
+
key: mutation.key as string | number,
|
|
1491
|
+
value: mutation.modified as Record<string, unknown>,
|
|
1497
1492
|
}
|
|
1498
1493
|
}
|
|
1499
1494
|
|
|
1500
1495
|
return {
|
|
1501
|
-
type: `update
|
|
1502
|
-
key: mutation.key as
|
|
1503
|
-
value: mutation.modified,
|
|
1496
|
+
type: `update` as const,
|
|
1497
|
+
key: mutation.key as string | number,
|
|
1498
|
+
value: mutation.modified as Record<string, unknown>,
|
|
1504
1499
|
}
|
|
1505
1500
|
}),
|
|
1506
1501
|
}
|
|
@@ -1618,12 +1613,12 @@ class PersistedCollectionRuntime<
|
|
|
1618
1613
|
changedRows: mutations
|
|
1619
1614
|
.filter((mutation) => mutation.type !== `delete`)
|
|
1620
1615
|
.map((mutation) => ({
|
|
1621
|
-
key: mutation.key as
|
|
1622
|
-
value: mutation.modified,
|
|
1616
|
+
key: mutation.key as string | number,
|
|
1617
|
+
value: mutation.modified as Record<string, unknown>,
|
|
1623
1618
|
})),
|
|
1624
1619
|
deletedKeys: mutations
|
|
1625
1620
|
.filter((mutation) => mutation.type === `delete`)
|
|
1626
|
-
.map((mutation) => mutation.key as
|
|
1621
|
+
.map((mutation) => mutation.key as string | number),
|
|
1627
1622
|
rowMetadataMutations: tx.rowMetadataMutations,
|
|
1628
1623
|
collectionMetadataMutations: tx.collectionMetadataMutations,
|
|
1629
1624
|
}),
|
|
@@ -1637,9 +1632,9 @@ class PersistedCollectionRuntime<
|
|
|
1637
1632
|
seq: number
|
|
1638
1633
|
txId: string
|
|
1639
1634
|
latestRowVersion: number
|
|
1640
|
-
changedRows: Array<{ key:
|
|
1641
|
-
deletedKeys: Array<
|
|
1642
|
-
rowMetadataMutations?: Array<PersistedRowMetadataMutation
|
|
1635
|
+
changedRows: Array<{ key: string | number; value: Record<string, unknown> }>
|
|
1636
|
+
deletedKeys: Array<string | number>
|
|
1637
|
+
rowMetadataMutations?: Array<PersistedRowMetadataMutation>
|
|
1643
1638
|
collectionMetadataMutations?: Array<PersistedCollectionMetadataMutation>
|
|
1644
1639
|
hasMetadataChanges?: boolean
|
|
1645
1640
|
requiresFullReload?: boolean
|
|
@@ -1672,14 +1667,9 @@ class PersistedCollectionRuntime<
|
|
|
1672
1667
|
txId: args.txId,
|
|
1673
1668
|
latestRowVersion: args.latestRowVersion,
|
|
1674
1669
|
requiresFullReload: false,
|
|
1675
|
-
changedRows: args.changedRows
|
|
1676
|
-
key: string | number
|
|
1677
|
-
value: Record<string, unknown>
|
|
1678
|
-
}>,
|
|
1670
|
+
changedRows: args.changedRows,
|
|
1679
1671
|
deletedKeys: args.deletedKeys,
|
|
1680
|
-
rowMetadataMutations
|
|
1681
|
-
PersistedRowMetadataMutation<string | number>
|
|
1682
|
-
>,
|
|
1672
|
+
rowMetadataMutations,
|
|
1683
1673
|
collectionMetadataMutations,
|
|
1684
1674
|
}
|
|
1685
1675
|
}
|
|
@@ -608,16 +608,20 @@ function sanitizeExpressionSqlFragment(fragment: string): string {
|
|
|
608
608
|
return fragment
|
|
609
609
|
}
|
|
610
610
|
|
|
611
|
-
type InMemoryRow<
|
|
611
|
+
type InMemoryRow<
|
|
612
|
+
TKey extends string | number = string | number,
|
|
613
|
+
T extends object = Record<string, unknown>,
|
|
614
|
+
> = {
|
|
612
615
|
key: TKey
|
|
613
616
|
value: T
|
|
614
617
|
metadata?: unknown
|
|
615
618
|
rowVersion: number
|
|
616
619
|
}
|
|
617
620
|
|
|
618
|
-
function decodeStoredSqliteRows<
|
|
619
|
-
|
|
620
|
-
|
|
621
|
+
function decodeStoredSqliteRows<
|
|
622
|
+
TKey extends string | number = string | number,
|
|
623
|
+
T extends object = Record<string, unknown>,
|
|
624
|
+
>(storedRows: ReadonlyArray<StoredSqliteRow>): Array<InMemoryRow<TKey, T>> {
|
|
621
625
|
return storedRows.map((row) => {
|
|
622
626
|
const key = decodePersistedStorageKey(row.key) as TKey
|
|
623
627
|
const value = deserializePersistedRowValue<T>(row.value)
|
|
@@ -1002,10 +1006,7 @@ function buildIndexName(collectionId: string, signature: string): string {
|
|
|
1002
1006
|
return `idx_${hashedPart}_${suffix}`
|
|
1003
1007
|
}
|
|
1004
1008
|
|
|
1005
|
-
export class SQLiteCorePersistenceAdapter
|
|
1006
|
-
T extends object,
|
|
1007
|
-
TKey extends string | number = string | number,
|
|
1008
|
-
> implements PersistenceAdapter<T, TKey> {
|
|
1009
|
+
export class SQLiteCorePersistenceAdapter implements PersistenceAdapter {
|
|
1009
1010
|
private readonly driver: SQLiteDriver
|
|
1010
1011
|
private readonly schemaVersion: number
|
|
1011
1012
|
private readonly schemaMismatchPolicy: SQLiteCoreAdapterSchemaMismatchPolicy
|
|
@@ -1085,7 +1086,13 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1085
1086
|
collectionId: string,
|
|
1086
1087
|
options: LoadSubsetOptions,
|
|
1087
1088
|
ctx?: { requiredIndexSignatures?: ReadonlyArray<string> },
|
|
1088
|
-
): Promise<
|
|
1089
|
+
): Promise<
|
|
1090
|
+
Array<{
|
|
1091
|
+
key: string | number
|
|
1092
|
+
value: Record<string, unknown>
|
|
1093
|
+
metadata?: unknown
|
|
1094
|
+
}>
|
|
1095
|
+
> {
|
|
1089
1096
|
const tableMapping = await this.ensureCollectionReady(collectionId)
|
|
1090
1097
|
await this.touchRequiredIndexes(collectionId, ctx?.requiredIndexSignatures)
|
|
1091
1098
|
|
|
@@ -1109,7 +1116,10 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1109
1116
|
this.loadSubsetInternal(tableMapping, whereFromOptions),
|
|
1110
1117
|
])
|
|
1111
1118
|
|
|
1112
|
-
const mergedRows = new Map<
|
|
1119
|
+
const mergedRows = new Map<
|
|
1120
|
+
string,
|
|
1121
|
+
InMemoryRow<string | number, Record<string, unknown>>
|
|
1122
|
+
>()
|
|
1113
1123
|
for (const row of [...whereCurrentRows, ...whereFromRows]) {
|
|
1114
1124
|
mergedRows.set(encodePersistedStorageKey(row.key), row)
|
|
1115
1125
|
}
|
|
@@ -1134,10 +1144,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1134
1144
|
}))
|
|
1135
1145
|
}
|
|
1136
1146
|
|
|
1137
|
-
async applyCommittedTx(
|
|
1138
|
-
collectionId: string,
|
|
1139
|
-
tx: PersistedTx<T, TKey>,
|
|
1140
|
-
): Promise<void> {
|
|
1147
|
+
async applyCommittedTx(collectionId: string, tx: PersistedTx): Promise<void> {
|
|
1141
1148
|
const tableMapping = await this.ensureCollectionReady(collectionId)
|
|
1142
1149
|
const collectionTableSql = quoteIdentifier(tableMapping.tableName)
|
|
1143
1150
|
const tombstoneTableSql = quoteIdentifier(tableMapping.tombstoneTableName)
|
|
@@ -1166,10 +1173,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1166
1173
|
)
|
|
1167
1174
|
const currentRowVersion = versionRows[0]?.latest_row_version ?? 0
|
|
1168
1175
|
const nextRowVersion = Math.max(currentRowVersion + 1, tx.rowVersion)
|
|
1169
|
-
const replayDelta: ReplayableTxDelta
|
|
1170
|
-
Record<string, unknown>,
|
|
1171
|
-
TKey
|
|
1172
|
-
> | null = tx.truncate
|
|
1176
|
+
const replayDelta: ReplayableTxDelta | null = tx.truncate
|
|
1173
1177
|
? null
|
|
1174
1178
|
: {
|
|
1175
1179
|
txId: tx.txId,
|
|
@@ -1178,7 +1182,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1178
1182
|
.filter((mutation) => mutation.type !== `delete`)
|
|
1179
1183
|
.map((mutation) => ({
|
|
1180
1184
|
key: mutation.key,
|
|
1181
|
-
value: mutation.value
|
|
1185
|
+
value: mutation.value,
|
|
1182
1186
|
})),
|
|
1183
1187
|
deletedKeys: tx.mutations
|
|
1184
1188
|
.filter((mutation) => mutation.type === `delete`)
|
|
@@ -1379,7 +1383,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1379
1383
|
async scanRows(
|
|
1380
1384
|
collectionId: string,
|
|
1381
1385
|
options?: PersistedRowScanOptions,
|
|
1382
|
-
): Promise<Array<PersistedScannedRow
|
|
1386
|
+
): Promise<Array<PersistedScannedRow>> {
|
|
1383
1387
|
const tableMapping = await this.ensureCollectionReady(collectionId)
|
|
1384
1388
|
const collectionTableSql = quoteIdentifier(tableMapping.tableName)
|
|
1385
1389
|
|
|
@@ -1392,7 +1396,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1392
1396
|
FROM ${collectionTableSql}`,
|
|
1393
1397
|
)
|
|
1394
1398
|
|
|
1395
|
-
return decodeStoredSqliteRows
|
|
1399
|
+
return decodeStoredSqliteRows(storedRows).map((row) => ({
|
|
1396
1400
|
key: row.key,
|
|
1397
1401
|
value: row.value,
|
|
1398
1402
|
metadata: row.metadata,
|
|
@@ -1531,7 +1535,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1531
1535
|
async pullSince(
|
|
1532
1536
|
collectionId: string,
|
|
1533
1537
|
fromRowVersion: number,
|
|
1534
|
-
): Promise<SQLitePullSinceResult<
|
|
1538
|
+
): Promise<SQLitePullSinceResult<string | number>> {
|
|
1535
1539
|
const tableMapping = await this.ensureCollectionReady(collectionId)
|
|
1536
1540
|
const collectionTableSql = quoteIdentifier(tableMapping.tableName)
|
|
1537
1541
|
const tombstoneTableSql = quoteIdentifier(tableMapping.tombstoneTableName)
|
|
@@ -1593,9 +1597,9 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1593
1597
|
}
|
|
1594
1598
|
}
|
|
1595
1599
|
|
|
1596
|
-
const decodeKey = (encodedKey: string):
|
|
1600
|
+
const decodeKey = (encodedKey: string): string | number => {
|
|
1597
1601
|
try {
|
|
1598
|
-
return decodePersistedStorageKey(encodedKey)
|
|
1602
|
+
return decodePersistedStorageKey(encodedKey)
|
|
1599
1603
|
} catch (error) {
|
|
1600
1604
|
throw new InvalidPersistedStorageKeyEncodingError(
|
|
1601
1605
|
`${encodedKey}: ${(error as Error).message}`,
|
|
@@ -1604,10 +1608,9 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1604
1608
|
}
|
|
1605
1609
|
|
|
1606
1610
|
const deltas = replayRows.map((row) => {
|
|
1607
|
-
const parsed = deserializePersistedRowValue<ReplayableTxDelta
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
> | null>(row.replay_json ?? `null`)
|
|
1611
|
+
const parsed = deserializePersistedRowValue<ReplayableTxDelta | null>(
|
|
1612
|
+
row.replay_json ?? `null`,
|
|
1613
|
+
)
|
|
1611
1614
|
if (!parsed) {
|
|
1612
1615
|
throw new InvalidPersistedCollectionConfigError(
|
|
1613
1616
|
`missing replay payload for applied_tx row`,
|
|
@@ -1645,7 +1648,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1645
1648
|
private async loadSubsetInternal(
|
|
1646
1649
|
tableMapping: CollectionTableMapping,
|
|
1647
1650
|
options: LoadSubsetOptions,
|
|
1648
|
-
): Promise<Array<InMemoryRow<
|
|
1651
|
+
): Promise<Array<InMemoryRow<string | number, Record<string, unknown>>>> {
|
|
1649
1652
|
const collectionTableSql = quoteIdentifier(tableMapping.tableName)
|
|
1650
1653
|
const whereCompiled = options.where
|
|
1651
1654
|
? compileSqlExpression(options.where)
|
|
@@ -1669,7 +1672,7 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1669
1672
|
sql,
|
|
1670
1673
|
queryParams,
|
|
1671
1674
|
)
|
|
1672
|
-
const parsedRows = decodeStoredSqliteRows
|
|
1675
|
+
const parsedRows = decodeStoredSqliteRows(storedRows)
|
|
1673
1676
|
|
|
1674
1677
|
const filteredRows = this.applyInMemoryWhere(parsedRows, options.where)
|
|
1675
1678
|
const orderedRows = this.applyInMemoryOrderBy(filteredRows, options.orderBy)
|
|
@@ -1681,25 +1684,23 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1681
1684
|
}
|
|
1682
1685
|
|
|
1683
1686
|
private applyInMemoryWhere(
|
|
1684
|
-
rows: Array<InMemoryRow<
|
|
1687
|
+
rows: Array<InMemoryRow<string | number, Record<string, unknown>>>,
|
|
1685
1688
|
where: IR.BasicExpression<boolean> | undefined,
|
|
1686
|
-
): Array<InMemoryRow<
|
|
1689
|
+
): Array<InMemoryRow<string | number, Record<string, unknown>>> {
|
|
1687
1690
|
if (!where) {
|
|
1688
1691
|
return rows
|
|
1689
1692
|
}
|
|
1690
1693
|
|
|
1691
1694
|
const evaluator = compileRowExpressionEvaluator(where)
|
|
1692
1695
|
return rows.filter((row) =>
|
|
1693
|
-
toBooleanPredicate(
|
|
1694
|
-
evaluator(row.value as Record<string, unknown>) as boolean | null,
|
|
1695
|
-
),
|
|
1696
|
+
toBooleanPredicate(evaluator(row.value) as boolean | null),
|
|
1696
1697
|
)
|
|
1697
1698
|
}
|
|
1698
1699
|
|
|
1699
1700
|
private applyInMemoryOrderBy(
|
|
1700
|
-
rows: Array<InMemoryRow<
|
|
1701
|
+
rows: Array<InMemoryRow<string | number, Record<string, unknown>>>,
|
|
1701
1702
|
orderBy: IR.OrderBy | undefined,
|
|
1702
|
-
): Array<InMemoryRow<
|
|
1703
|
+
): Array<InMemoryRow<string | number, Record<string, unknown>>> {
|
|
1703
1704
|
if (!orderBy || orderBy.length === 0) {
|
|
1704
1705
|
return rows
|
|
1705
1706
|
}
|
|
@@ -1712,12 +1713,8 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1712
1713
|
const ordered = [...rows]
|
|
1713
1714
|
ordered.sort((left, right) => {
|
|
1714
1715
|
for (const clause of compiledClauses) {
|
|
1715
|
-
const leftValue = clause.evaluator(
|
|
1716
|
-
|
|
1717
|
-
)
|
|
1718
|
-
const rightValue = clause.evaluator(
|
|
1719
|
-
right.value as Record<string, unknown>,
|
|
1720
|
-
)
|
|
1716
|
+
const leftValue = clause.evaluator(left.value)
|
|
1717
|
+
const rightValue = clause.evaluator(right.value)
|
|
1721
1718
|
|
|
1722
1719
|
const comparison = compareOrderByValues(
|
|
1723
1720
|
leftValue,
|
|
@@ -1747,10 +1744,10 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
1747
1744
|
}
|
|
1748
1745
|
|
|
1749
1746
|
private applyInMemoryPagination(
|
|
1750
|
-
rows: Array<InMemoryRow<
|
|
1747
|
+
rows: Array<InMemoryRow<string | number, Record<string, unknown>>>,
|
|
1751
1748
|
limit: number | undefined,
|
|
1752
1749
|
offset: number | undefined,
|
|
1753
|
-
): Array<InMemoryRow<
|
|
1750
|
+
): Array<InMemoryRow<string | number, Record<string, unknown>>> {
|
|
1754
1751
|
const start = offset ?? 0
|
|
1755
1752
|
if (limit === undefined) {
|
|
1756
1753
|
return rows.slice(start)
|
|
@@ -2121,9 +2118,8 @@ export class SQLiteCorePersistenceAdapter<
|
|
|
2121
2118
|
}
|
|
2122
2119
|
}
|
|
2123
2120
|
|
|
2124
|
-
export function createSQLiteCorePersistenceAdapter
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
return new SQLiteCorePersistenceAdapter<T, TKey>(options)
|
|
2121
|
+
export function createSQLiteCorePersistenceAdapter(
|
|
2122
|
+
options: SQLiteCoreAdapterOptions,
|
|
2123
|
+
): PersistenceAdapter {
|
|
2124
|
+
return new SQLiteCorePersistenceAdapter(options)
|
|
2129
2125
|
}
|