@tanstack/db-sqlite-persistence-core 0.2.23 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +258 -7
- package/dist/cjs/broadcast-coordinator.cjs +1689 -0
- package/dist/cjs/broadcast-coordinator.cjs.map +1 -0
- package/dist/cjs/broadcast-coordinator.d.cts +116 -0
- package/dist/cjs/errors.cjs +73 -0
- package/dist/cjs/errors.cjs.map +1 -1
- package/dist/cjs/errors.d.cts +44 -0
- package/dist/cjs/index.cjs +19 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/persisted.cjs +1782 -394
- package/dist/cjs/persisted.cjs.map +1 -1
- package/dist/cjs/persisted.d.cts +176 -11
- package/dist/cjs/remote-subset-owner.cjs +20 -0
- package/dist/cjs/remote-subset-owner.cjs.map +1 -0
- package/dist/cjs/remote-subset-owner.d.cts +7 -0
- package/dist/cjs/remote-subset-wire.cjs +681 -0
- package/dist/cjs/remote-subset-wire.cjs.map +1 -0
- package/dist/cjs/remote-subset-wire.d.cts +59 -0
- package/dist/cjs/sqlite-core-adapter.cjs +969 -364
- package/dist/cjs/sqlite-core-adapter.cjs.map +1 -1
- package/dist/cjs/sqlite-core-adapter.d.cts +45 -1
- package/dist/cjs/sqlite-value.cjs +31 -0
- package/dist/cjs/sqlite-value.cjs.map +1 -0
- package/dist/cjs/sqlite-value.d.cts +10 -0
- package/dist/esm/broadcast-coordinator.d.ts +116 -0
- package/dist/esm/broadcast-coordinator.js +1689 -0
- package/dist/esm/broadcast-coordinator.js.map +1 -0
- package/dist/esm/errors.d.ts +44 -0
- package/dist/esm/errors.js +74 -1
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +21 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/persisted.d.ts +176 -11
- package/dist/esm/persisted.js +1784 -396
- package/dist/esm/persisted.js.map +1 -1
- package/dist/esm/remote-subset-owner.d.ts +7 -0
- package/dist/esm/remote-subset-owner.js +20 -0
- package/dist/esm/remote-subset-owner.js.map +1 -0
- package/dist/esm/remote-subset-wire.d.ts +59 -0
- package/dist/esm/remote-subset-wire.js +681 -0
- package/dist/esm/remote-subset-wire.js.map +1 -0
- package/dist/esm/sqlite-core-adapter.d.ts +45 -1
- package/dist/esm/sqlite-core-adapter.js +955 -350
- package/dist/esm/sqlite-core-adapter.js.map +1 -1
- package/dist/esm/sqlite-value.d.ts +10 -0
- package/dist/esm/sqlite-value.js +31 -0
- package/dist/esm/sqlite-value.js.map +1 -0
- package/package.json +12 -2
- package/src/broadcast-coordinator.ts +2456 -0
- package/src/errors.ts +120 -0
- package/src/index.ts +2 -0
- package/src/persisted.ts +2617 -442
- package/src/remote-subset-owner.ts +36 -0
- package/src/remote-subset-wire.ts +946 -0
- package/src/sqlite-core-adapter.ts +1297 -410
- package/src/sqlite-value.ts +30 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LoadSubsetOptions } from '@tanstack/db';
|
|
2
|
-
import { PersistedIndexSpec, PersistedRowScanOptions, PersistedScannedRow, PersistedTx, PersistenceAdapter, ReplayableTxDelta, SQLiteDriver } from './persisted.cjs';
|
|
2
|
+
import { HydrationPersistenceAdapter, PersistedIndexSpec, PersistedKeySetEvidence, PersistedRowScanOptions, PersistedScannedRow, PersistedTx, PersistenceAdapter, ReplayableTxDelta, SQLiteDriver } from './persisted.cjs';
|
|
3
3
|
type SQLiteCoreAdapterSchemaMismatchPolicy = `sync-present-reset` | `sync-absent-error` | `reset`;
|
|
4
4
|
export type SQLiteCoreAdapterOptions = {
|
|
5
5
|
driver: SQLiteDriver;
|
|
@@ -33,16 +33,27 @@ export declare const DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS = 1000;
|
|
|
33
33
|
export declare const DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS: number;
|
|
34
34
|
export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter {
|
|
35
35
|
private readonly driver;
|
|
36
|
+
private readonly schedulingIdentitySource;
|
|
37
|
+
private scheduler;
|
|
38
|
+
private activeUnscheduledHydration;
|
|
39
|
+
private readonly hydrationAdapter;
|
|
36
40
|
private readonly schemaVersion;
|
|
37
41
|
private readonly schemaMismatchPolicy;
|
|
38
42
|
private readonly appliedTxPruneMaxRows;
|
|
39
43
|
private readonly appliedTxPruneMaxAgeSeconds;
|
|
40
44
|
private readonly pullSinceReloadThreshold;
|
|
45
|
+
private readonly replacementBatchSize;
|
|
41
46
|
private initialized;
|
|
42
47
|
private readonly collectionTableCache;
|
|
43
48
|
private readonly collectionTableLoads;
|
|
44
49
|
constructor(options: SQLiteCoreAdapterOptions);
|
|
50
|
+
runInHydrationScope<T>(task: (adapter: HydrationPersistenceAdapter) => Promise<T>): Promise<T>;
|
|
51
|
+
runInRegularScope<T>(task: (adapter: HydrationPersistenceAdapter) => Promise<T>): Promise<T>;
|
|
52
|
+
isHydrationScopeScheduled(): boolean;
|
|
53
|
+
private runRegular;
|
|
54
|
+
private resolveScheduler;
|
|
45
55
|
private runInTransaction;
|
|
56
|
+
private assertCurrentSchemaVersion;
|
|
46
57
|
loadSubset(collectionId: string, options: LoadSubsetOptions, ctx?: {
|
|
47
58
|
requiredIndexSignatures?: ReadonlyArray<string>;
|
|
48
59
|
}): Promise<Array<{
|
|
@@ -50,20 +61,51 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
50
61
|
value: Record<string, unknown>;
|
|
51
62
|
metadata?: unknown;
|
|
52
63
|
}>>;
|
|
64
|
+
private loadSubsetUnscheduled;
|
|
65
|
+
loadResumeSnapshot(collectionId: string, ctx?: {
|
|
66
|
+
requiredIndexSignatures?: ReadonlyArray<string>;
|
|
67
|
+
includeRows?: boolean;
|
|
68
|
+
}): Promise<{
|
|
69
|
+
rows: Array<{
|
|
70
|
+
key: string | number;
|
|
71
|
+
value: Record<string, unknown>;
|
|
72
|
+
metadata?: unknown;
|
|
73
|
+
}>;
|
|
74
|
+
keySet: PersistedKeySetEvidence;
|
|
75
|
+
collectionMetadata: Array<{
|
|
76
|
+
key: string;
|
|
77
|
+
value: unknown;
|
|
78
|
+
}>;
|
|
79
|
+
latestTerm: number;
|
|
80
|
+
latestSeq: number;
|
|
81
|
+
latestRowVersion: number;
|
|
82
|
+
resetEpoch: number;
|
|
83
|
+
}>;
|
|
84
|
+
private loadResumeSnapshotUnscheduled;
|
|
53
85
|
applyCommittedTx(collectionId: string, tx: PersistedTx): Promise<void>;
|
|
86
|
+
private applyCommittedTxUnscheduled;
|
|
54
87
|
loadCollectionMetadata(collectionId: string): Promise<Array<{
|
|
55
88
|
key: string;
|
|
56
89
|
value: unknown;
|
|
57
90
|
}>>;
|
|
91
|
+
private loadCollectionMetadataUnscheduled;
|
|
58
92
|
scanRows(collectionId: string, options?: PersistedRowScanOptions): Promise<Array<PersistedScannedRow>>;
|
|
93
|
+
private scanRowsUnscheduled;
|
|
59
94
|
ensureIndex(collectionId: string, signature: string, spec: PersistedIndexSpec): Promise<void>;
|
|
95
|
+
private ensureIndexUnscheduled;
|
|
60
96
|
markIndexRemoved(collectionId: string, signature: string): Promise<void>;
|
|
97
|
+
private markIndexRemovedUnscheduled;
|
|
61
98
|
getStreamPosition(collectionId: string): Promise<{
|
|
62
99
|
latestTerm: number;
|
|
63
100
|
latestSeq: number;
|
|
64
101
|
latestRowVersion: number;
|
|
65
102
|
}>;
|
|
103
|
+
private getStreamPositionUnscheduled;
|
|
104
|
+
private readLatestRowVersion;
|
|
105
|
+
private readStreamPosition;
|
|
106
|
+
private readKeySetEvidence;
|
|
66
107
|
pullSince(collectionId: string, fromRowVersion: number): Promise<SQLitePullSinceResult<string | number>>;
|
|
108
|
+
private pullSinceUnscheduled;
|
|
67
109
|
private loadSubsetInternal;
|
|
68
110
|
private applyInMemoryWhere;
|
|
69
111
|
private applyInMemoryOrderBy;
|
|
@@ -71,7 +113,9 @@ export declare class SQLiteCorePersistenceAdapter implements PersistenceAdapter
|
|
|
71
113
|
private touchRequiredIndexes;
|
|
72
114
|
private pruneAppliedTxRows;
|
|
73
115
|
private ensureCollectionReady;
|
|
116
|
+
private loadCollectionRegistration;
|
|
74
117
|
private ensureCollectionReadyInternal;
|
|
118
|
+
private ensureCollectionKeyEvidenceTriggers;
|
|
75
119
|
private handleSchemaMismatch;
|
|
76
120
|
private ensureInitialized;
|
|
77
121
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const errors = require("./errors.cjs");
|
|
4
|
+
const SQLITE_BIGINT_MIN = -9223372036854775808n;
|
|
5
|
+
const SQLITE_BIGINT_MAX = 9223372036854775807n;
|
|
6
|
+
const PERSISTED_TYPE_TAG = `__tanstack_db_persisted_type__`;
|
|
7
|
+
const PERSISTED_VALUE_TAG = `value`;
|
|
8
|
+
function assertSQLiteBigIntInRange(value) {
|
|
9
|
+
if (value < SQLITE_BIGINT_MIN || value > SQLITE_BIGINT_MAX) {
|
|
10
|
+
throw new errors.SQLiteBigIntOutOfRangeError(
|
|
11
|
+
value,
|
|
12
|
+
SQLITE_BIGINT_MIN,
|
|
13
|
+
SQLITE_BIGINT_MAX
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function serializeSQLiteBigInt(value) {
|
|
19
|
+
assertSQLiteBigIntInRange(value);
|
|
20
|
+
return {
|
|
21
|
+
[PERSISTED_TYPE_TAG]: `bigint`,
|
|
22
|
+
[PERSISTED_VALUE_TAG]: value.toString()
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.PERSISTED_TYPE_TAG = PERSISTED_TYPE_TAG;
|
|
26
|
+
exports.PERSISTED_VALUE_TAG = PERSISTED_VALUE_TAG;
|
|
27
|
+
exports.SQLITE_BIGINT_MAX = SQLITE_BIGINT_MAX;
|
|
28
|
+
exports.SQLITE_BIGINT_MIN = SQLITE_BIGINT_MIN;
|
|
29
|
+
exports.assertSQLiteBigIntInRange = assertSQLiteBigIntInRange;
|
|
30
|
+
exports.serializeSQLiteBigInt = serializeSQLiteBigInt;
|
|
31
|
+
//# sourceMappingURL=sqlite-value.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-value.cjs","sources":["../../src/sqlite-value.ts"],"sourcesContent":["import { SQLiteBigIntOutOfRangeError } from './errors'\n\nexport const SQLITE_BIGINT_MIN = -9_223_372_036_854_775_808n\nexport const SQLITE_BIGINT_MAX = 9_223_372_036_854_775_807n\nexport const PERSISTED_TYPE_TAG = `__tanstack_db_persisted_type__`\nexport const PERSISTED_VALUE_TAG = `value`\n\nexport type SerializedSQLiteBigInt = {\n [PERSISTED_TYPE_TAG]: `bigint`\n [PERSISTED_VALUE_TAG]: string\n}\n\nexport function assertSQLiteBigIntInRange(value: bigint): bigint {\n if (value < SQLITE_BIGINT_MIN || value > SQLITE_BIGINT_MAX) {\n throw new SQLiteBigIntOutOfRangeError(\n value,\n SQLITE_BIGINT_MIN,\n SQLITE_BIGINT_MAX,\n )\n }\n return value\n}\n\nexport function serializeSQLiteBigInt(value: bigint): SerializedSQLiteBigInt {\n assertSQLiteBigIntInRange(value)\n return {\n [PERSISTED_TYPE_TAG]: `bigint`,\n [PERSISTED_VALUE_TAG]: value.toString(),\n }\n}\n"],"names":["SQLiteBigIntOutOfRangeError"],"mappings":";;;AAEO,MAAM,oBAAoB,CAAC;AAC3B,MAAM,oBAAoB;AAC1B,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAO5B,SAAS,0BAA0B,OAAuB;AAC/D,MAAI,QAAQ,qBAAqB,QAAQ,mBAAmB;AAC1D,UAAM,IAAIA,OAAAA;AAAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AACA,SAAO;AACT;AAEO,SAAS,sBAAsB,OAAuC;AAC3E,4BAA0B,KAAK;AAC/B,SAAO;AAAA,IACL,CAAC,kBAAkB,GAAG;AAAA,IACtB,CAAC,mBAAmB,GAAG,MAAM,SAAA;AAAA,EAAS;AAE1C;;;;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const SQLITE_BIGINT_MIN = -9223372036854775808n;
|
|
2
|
+
export declare const SQLITE_BIGINT_MAX = 9223372036854775807n;
|
|
3
|
+
export declare const PERSISTED_TYPE_TAG = "__tanstack_db_persisted_type__";
|
|
4
|
+
export declare const PERSISTED_VALUE_TAG = "value";
|
|
5
|
+
export type SerializedSQLiteBigInt = {
|
|
6
|
+
[PERSISTED_TYPE_TAG]: `bigint`;
|
|
7
|
+
[PERSISTED_VALUE_TAG]: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function assertSQLiteBigIntInRange(value: bigint): bigint;
|
|
10
|
+
export declare function serializeSQLiteBigInt(value: bigint): SerializedSQLiteBigInt;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { LoadSubsetOptions } from '@tanstack/db';
|
|
2
|
+
import { RemoteSubsetOwner } from './remote-subset-owner.js';
|
|
3
|
+
import { ApplyCommittedTxResponse, ApplyLocalMutationsResponse, HydrationPersistenceAdapter, PersistedCollectionCoordinator, PersistedIndexSpec, PersistedMutationEnvelope, PersistedTx, PersistenceAdapter, ProtocolEnvelope, PullSinceResponse } from './persisted.js';
|
|
4
|
+
type CoordinatorAdapter = Pick<PersistenceAdapter, `loadSubset` | `applyCommittedTx` | `ensureIndex` | `runInRegularScope`> & {
|
|
5
|
+
pullSince?: (collectionId: string, fromRowVersion: number) => Promise<{
|
|
6
|
+
latestRowVersion: number;
|
|
7
|
+
requiresFullReload: true;
|
|
8
|
+
} | {
|
|
9
|
+
latestRowVersion: number;
|
|
10
|
+
requiresFullReload: false;
|
|
11
|
+
changedKeys: Array<string | number>;
|
|
12
|
+
deletedKeys: Array<string | number>;
|
|
13
|
+
}>;
|
|
14
|
+
getStreamPosition?: (collectionId: string) => Promise<{
|
|
15
|
+
latestTerm: number;
|
|
16
|
+
latestSeq: number;
|
|
17
|
+
latestRowVersion: number;
|
|
18
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
/** @internal Shared by the Browser and Electron persistence hosts. */
|
|
21
|
+
export type BroadcastCollectionCoordinatorOptions = {
|
|
22
|
+
dbName: string;
|
|
23
|
+
adapter?: CoordinatorAdapter;
|
|
24
|
+
coordinatorName: string;
|
|
25
|
+
};
|
|
26
|
+
/** @internal Shared coordination engine for BroadcastChannel/Web Locks hosts. */
|
|
27
|
+
export declare class BroadcastCollectionCoordinator implements PersistedCollectionCoordinator {
|
|
28
|
+
private readonly nodeId;
|
|
29
|
+
private readonly dbName;
|
|
30
|
+
private readonly coordinatorName;
|
|
31
|
+
private defaultAdapter;
|
|
32
|
+
private readonly collectionAdapters;
|
|
33
|
+
private readonly remoteSubsetOwners;
|
|
34
|
+
private readonly remoteSubsetIds;
|
|
35
|
+
private readonly outboundRemoteSubsetAcquisitions;
|
|
36
|
+
private readonly inboundRemoteSubsetAcquisitions;
|
|
37
|
+
private readonly releasedRemoteSubsetAcquisitionTimes;
|
|
38
|
+
private readonly channel;
|
|
39
|
+
private readonly collections;
|
|
40
|
+
private readonly pendingRPCs;
|
|
41
|
+
private readonly appliedEnvelopes;
|
|
42
|
+
private readonly inFlightEnvelopes;
|
|
43
|
+
private appliedEnvelopePruneTimer;
|
|
44
|
+
private rejectDisposed;
|
|
45
|
+
private readonly disposedPromise;
|
|
46
|
+
private disposed;
|
|
47
|
+
/** Method indirection to prevent TypeScript from narrowing `disposed` across awaits */
|
|
48
|
+
private isDisposed;
|
|
49
|
+
/** Method indirection to re-read leadership participation across awaits. */
|
|
50
|
+
private participatesInLeadership;
|
|
51
|
+
private requireAdapter;
|
|
52
|
+
constructor(options: BroadcastCollectionCoordinatorOptions);
|
|
53
|
+
/**
|
|
54
|
+
* Set or replace the persistence adapter used for leader-side RPC handling.
|
|
55
|
+
* Called by a host persistence factory to wire the internally-created adapter.
|
|
56
|
+
*/
|
|
57
|
+
setAdapter(adapter: CoordinatorAdapter): void;
|
|
58
|
+
/** Register the persistence adapter that owns one collection. */
|
|
59
|
+
setAdapterForCollection(collectionId: string, adapter: CoordinatorAdapter): void;
|
|
60
|
+
registerRemoteSubsetOwner(collectionId: string, owner: RemoteSubsetOwner): () => void;
|
|
61
|
+
getNodeId(): string;
|
|
62
|
+
subscribe(collectionId: string, onMessage: (message: ProtocolEnvelope<unknown>) => void): () => void;
|
|
63
|
+
publish(_collectionId: string, message: ProtocolEnvelope<unknown>): void;
|
|
64
|
+
isLeader(collectionId: string): boolean;
|
|
65
|
+
ensureLeadership(collectionId: string): Promise<void>;
|
|
66
|
+
requestEnsureRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void>;
|
|
67
|
+
requestReleaseRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void>;
|
|
68
|
+
private acquireRemoteSubset;
|
|
69
|
+
requestEnsurePersistedIndex(collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean): Promise<void>;
|
|
70
|
+
requestApplyLocalMutations(collectionId: string, mutations: Array<PersistedMutationEnvelope>): Promise<ApplyLocalMutationsResponse>;
|
|
71
|
+
requestApplyCommittedTx(collectionId: string, tx: PersistedTx, scopedAdapter?: HydrationPersistenceAdapter): Promise<ApplyCommittedTxResponse>;
|
|
72
|
+
pullSince(collectionId: string, fromRowVersion: number, scopedAdapter?: HydrationPersistenceAdapter): Promise<PullSinceResponse>;
|
|
73
|
+
dispose(): void;
|
|
74
|
+
private getOrCreateCollectionState;
|
|
75
|
+
private ensureCollectionState;
|
|
76
|
+
private acquireLeadership;
|
|
77
|
+
private waitForInitialRoute;
|
|
78
|
+
private waitForLeadershipRoute;
|
|
79
|
+
private notifyRouteWaiters;
|
|
80
|
+
private releaseLeadership;
|
|
81
|
+
private releaseCollectionIfUnused;
|
|
82
|
+
private postRemoteSubsetRelease;
|
|
83
|
+
private cancelRemoteSubsetReplayRetry;
|
|
84
|
+
private scheduleRemoteSubsetReplayRetry;
|
|
85
|
+
private replayRemoteSubsetAcquisitions;
|
|
86
|
+
private emitHeartbeat;
|
|
87
|
+
private onChannelMessage;
|
|
88
|
+
private sendRPC;
|
|
89
|
+
private captureMutationRoute;
|
|
90
|
+
private assertMutationRouteUnchanged;
|
|
91
|
+
private sendRPCOnce;
|
|
92
|
+
private handleRPCRequest;
|
|
93
|
+
private dispatchRPCRequest;
|
|
94
|
+
private handleEnsureRemoteSubset;
|
|
95
|
+
private handleReleaseRemoteSubset;
|
|
96
|
+
private releaseRemoteSubsetAcquisition;
|
|
97
|
+
private releaseInboundRemoteSubsetAcquisitions;
|
|
98
|
+
private rebindRemoteInboundSubsetAcquisitions;
|
|
99
|
+
private rebindRemoteInboundSubsetAcquisition;
|
|
100
|
+
private bindAwaitingRemoteSubsetAcquisition;
|
|
101
|
+
private startRemoteSubsetOwnerAcquisition;
|
|
102
|
+
private handleEnsurePersistedIndex;
|
|
103
|
+
private handleApplyLocalMutations;
|
|
104
|
+
private applyLocalMutationsOnce;
|
|
105
|
+
private handleApplyCommittedTx;
|
|
106
|
+
private runDeduplicatedEnvelope;
|
|
107
|
+
private applyCommittedTxOnce;
|
|
108
|
+
private applyDurablyAtNextStreamPosition;
|
|
109
|
+
private handlePullSince;
|
|
110
|
+
private withScheduledWriterLock;
|
|
111
|
+
private withWriterLock;
|
|
112
|
+
private pruneAppliedEnvelopes;
|
|
113
|
+
private setReleasedRemoteSubsetAcquisition;
|
|
114
|
+
private pruneReleasedRemoteSubsetAcquisitions;
|
|
115
|
+
}
|
|
116
|
+
export {};
|