@tanstack/db-sqlite-persistence-core 0.4.8 → 0.4.9

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/esm/index.js CHANGED
@@ -1,12 +1,13 @@
1
- import { SQLITE_DRIVER_SHARED_LOGICAL_SCHEDULING_KEY, SingleProcessCoordinator, createPersistedTableName, decodePersistedStorageKey, encodePersistedStorageKey, forwardSQLiteDriverSharedLogicalScheduling, persistedCollectionOptions, validatePersistedCollectionCoordinator } from "./persisted.js";
1
+ import { SQLITE_DRIVER_SHARED_LOGICAL_SCHEDULING_KEY, SingleProcessCoordinator, createPersistedTableName, decodePersistedStorageKey, eagerStorageTarget, encodePersistedStorageKey, forwardSQLiteDriverSharedLogicalScheduling, managedStorageTarget, persistedCollectionOptions, resolvePersistedStorageTarget, validatePersistedCollectionCoordinator } from "./persisted.js";
2
2
  import { DuplicateRemoteSubsetOwnerError, IndeterminateCommitError, InvalidPersistedCollectionConfigError, InvalidPersistedCollectionCoordinatorError, InvalidPersistedStorageKeyEncodingError, InvalidPersistedStorageKeyError, InvalidPersistenceAdapterError, InvalidSyncConfigError, PersistedCollectionCoreError, PersistedCollectionDurabilityError, PersistenceUnavailableError, RetryableRemoteSubsetAcquisitionError, SQLiteBigIntOutOfRangeError, toPersistedCollectionDurabilityError } from "./errors.js";
3
- import { DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS, DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS, SQLiteCorePersistenceAdapter, createSQLiteCorePersistenceAdapter } from "./sqlite-core-adapter.js";
3
+ import { DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS, DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS, DEFAULT_CACHE_GENERATION_CLAIM_TTL_MS, SQLiteCorePersistenceAdapter, createSQLiteCorePersistenceAdapter } from "./sqlite-core-adapter.js";
4
4
  import { PERSISTED_TYPE_TAG, PERSISTED_VALUE_TAG, SQLITE_BIGINT_MAX, SQLITE_BIGINT_MIN, assertSQLiteBigIntInRange, reviveSQLiteTemporal, serializeSQLiteBigInt, serializeSQLiteTemporal, sqliteTemporalIdentity, sqliteTemporalKind } from "./sqlite-value.js";
5
5
  import { RemoteSubsetWireValueError, toProcessLocalLoadSubsetOptions, toTransportedLoadSubsetOptions, validateRemoteSubsetOptions } from "./remote-subset-wire.js";
6
6
  import { safeRandomUUID } from "@tanstack/db";
7
7
  export {
8
8
  DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS,
9
9
  DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS,
10
+ DEFAULT_CACHE_GENERATION_CLAIM_TTL_MS,
10
11
  DuplicateRemoteSubsetOwnerError,
11
12
  IndeterminateCommitError,
12
13
  InvalidPersistedCollectionConfigError,
@@ -32,9 +33,12 @@ export {
32
33
  createPersistedTableName,
33
34
  createSQLiteCorePersistenceAdapter,
34
35
  decodePersistedStorageKey,
36
+ eagerStorageTarget,
35
37
  encodePersistedStorageKey,
36
38
  forwardSQLiteDriverSharedLogicalScheduling,
39
+ managedStorageTarget,
37
40
  persistedCollectionOptions,
41
+ resolvePersistedStorageTarget,
38
42
  reviveSQLiteTemporal,
39
43
  safeRandomUUID,
40
44
  serializeSQLiteBigInt,
@@ -259,6 +259,27 @@ export type PersistedScannedRow<T extends object = Record<string, unknown>, TKey
259
259
  };
260
260
  export type PersistedRowScanOptions = SyncPersistenceScanOptions;
261
261
  export type PersistedKeySetEvidence = SyncPersistenceKeySetEvidence;
262
+ export type PersistedCacheGenerationClaim = {
263
+ storageCollectionId: string;
264
+ claimId: string;
265
+ /** Present when the adapter expires dormant claims. */
266
+ expiresAtMs?: number;
267
+ };
268
+ /** An eager Collection name or a claim-bound persisted cache generation. */
269
+ export type PersistedStorageTarget = {
270
+ kind: `eager`;
271
+ collectionId: string;
272
+ } | {
273
+ kind: `managed`;
274
+ storageCollectionId: string;
275
+ claimId: string;
276
+ };
277
+ export declare function eagerStorageTarget(collectionId: string): PersistedStorageTarget;
278
+ export declare function managedStorageTarget(storageCollectionId: string, claimId: string): PersistedStorageTarget;
279
+ export declare function resolvePersistedStorageTarget(target: PersistedStorageTarget, legacyClaimId?: string): {
280
+ collectionId: string;
281
+ claimId?: string;
282
+ };
262
283
  export type PersistencePullSinceResult = {
263
284
  latestRowVersion: number;
264
285
  requiresFullReload: true;
@@ -274,6 +295,8 @@ export type PersistedTx<T extends object = Record<string, unknown>, TKey extends
274
295
  term: number;
275
296
  seq: number;
276
297
  rowVersion: number;
298
+ /** Claim authorizing this transaction's persisted cache generation. */
299
+ cacheGenerationClaimId?: string;
277
300
  truncate?: boolean;
278
301
  mutations: Array<{
279
302
  type: `insert`;
@@ -308,16 +331,18 @@ export type PersistedTx<T extends object = Record<string, unknown>, TKey extends
308
331
  */
309
332
  export declare const SQLITE_DRIVER_SHARED_LOGICAL_SCHEDULING_KEY: unique symbol;
310
333
  export interface PersistenceAdapter {
311
- loadSubset: (collectionId: string, options: LoadSubsetOptions, ctx?: {
334
+ loadSubset: (target: PersistedStorageTarget, options: LoadSubsetOptions, ctx?: {
312
335
  requiredIndexSignatures?: ReadonlyArray<string>;
336
+ cacheGenerationClaimId?: string;
313
337
  }) => Promise<Array<{
314
338
  key: string | number;
315
339
  value: Record<string, unknown>;
316
340
  metadata?: unknown;
317
341
  }>>;
318
- loadResumeSnapshot: (collectionId: string, ctx?: {
342
+ loadResumeSnapshot: (target: PersistedStorageTarget, ctx?: {
319
343
  requiredIndexSignatures?: ReadonlyArray<string>;
320
344
  includeRows?: boolean;
345
+ cacheGenerationClaimId?: string;
321
346
  }) => Promise<{
322
347
  rows: Array<{
323
348
  key: string | number;
@@ -334,23 +359,43 @@ export interface PersistenceAdapter {
334
359
  latestRowVersion: number;
335
360
  resetEpoch: number;
336
361
  }>;
337
- applyCommittedTx: (collectionId: string, tx: PersistedTx) => Promise<void>;
362
+ applyCommittedTx: (target: PersistedStorageTarget, tx: PersistedTx) => Promise<void>;
363
+ claimCacheGeneration?: (collectionId: string) => Promise<PersistedCacheGenerationClaim>;
364
+ rotateCacheGeneration?: (collectionId: string, claimId: string, resetMetadata?: {
365
+ key: string;
366
+ value: unknown;
367
+ }, expectedStorageCollectionId?: string) => Promise<PersistedCacheGenerationClaim>;
368
+ releaseCacheGenerationClaim?: (claimId: string) => Promise<void>;
369
+ renewCacheGenerationClaim?: (storageCollectionId: string, claimId: string) => Promise<number | undefined>;
370
+ getCacheGenerationNow?: () => number;
338
371
  /** Atomically certify or apply one source transaction after a lost response. */
339
- reconcileCommittedTx?: (collectionId: string, tx: PersistedTx, anchor: CommittedTxAnchor) => Promise<ReconciledCommittedTx>;
340
- loadCollectionMetadata?: (collectionId: string) => Promise<Array<{
372
+ reconcileCommittedTx?: (target: PersistedStorageTarget, tx: PersistedTx, anchor: CommittedTxAnchor) => Promise<ReconciledCommittedTx>;
373
+ loadCollectionMetadata?: (target: PersistedStorageTarget, ctx?: {
374
+ cacheGenerationClaimId?: string;
375
+ }) => Promise<Array<{
341
376
  key: string;
342
377
  value: unknown;
343
378
  }>>;
344
- scanRows?: (collectionId: string, options?: PersistedRowScanOptions) => Promise<Array<PersistedScannedRow>>;
345
- ensureIndex: (collectionId: string, signature: string, spec: PersistedIndexSpec) => Promise<void>;
346
- markIndexRemoved?: (collectionId: string, signature: string) => Promise<void>;
347
- getStreamPosition?: (collectionId: string) => Promise<{
379
+ scanRows?: (target: PersistedStorageTarget, options?: PersistedRowScanOptions, ctx?: {
380
+ cacheGenerationClaimId?: string;
381
+ }) => Promise<Array<PersistedScannedRow>>;
382
+ ensureIndex: (target: PersistedStorageTarget, signature: string, spec: PersistedIndexSpec, ctx?: {
383
+ cacheGenerationClaimId?: string;
384
+ }) => Promise<void>;
385
+ markIndexRemoved?: (target: PersistedStorageTarget, signature: string, ctx?: {
386
+ cacheGenerationClaimId?: string;
387
+ }) => Promise<void>;
388
+ getStreamPosition?: (target: PersistedStorageTarget, ctx?: {
389
+ cacheGenerationClaimId?: string;
390
+ }) => Promise<{
348
391
  latestTerm: number;
349
392
  latestSeq: number;
350
393
  latestRowVersion: number;
351
394
  }>;
352
395
  /** Reserve a new durable election term before announcing Browser leadership. */
353
- reserveLeadershipTerm?: (collectionId: string, observedTerm: number) => Promise<{
396
+ reserveLeadershipTerm?: (target: PersistedStorageTarget, observedTerm: number, ctx?: {
397
+ cacheGenerationClaimId?: string;
398
+ }) => Promise<{
354
399
  latestTerm: number;
355
400
  latestSeq: number;
356
401
  latestRowVersion: number;
@@ -367,7 +412,9 @@ export interface PersistenceAdapter {
367
412
  isHydrationScopeScheduled?: () => boolean;
368
413
  }
369
414
  export type HydrationPersistenceAdapter = PersistenceAdapter & {
370
- pullSince?: (collectionId: string, fromRowVersion: number) => Promise<PersistencePullSinceResult>;
415
+ pullSince?: (target: PersistedStorageTarget, fromRowVersion: number, ctx?: {
416
+ cacheGenerationClaimId?: string;
417
+ }) => Promise<PersistencePullSinceResult>;
371
418
  };
372
419
  export type { RemoteSubsetOwner } from './remote-subset-owner.js';
373
420
  export interface SQLiteDriver {
@@ -387,7 +434,7 @@ export interface SQLiteDriver {
387
434
  export declare function forwardSQLiteDriverSharedLogicalScheduling<TDriver extends SQLiteDriver>(source: SQLiteDriver, wrapper: TDriver): TDriver;
388
435
  export interface PersistedCollectionCoordinator {
389
436
  getNodeId: () => string;
390
- setAdapterForCollection?: (collectionId: string, adapter: PersistenceAdapter) => void;
437
+ setAdapterForCollection?: (collectionId: string, adapter: PersistenceAdapter, cacheGenerationClaimId?: string) => void;
391
438
  subscribe: (collectionId: string, onMessage: (message: ProtocolEnvelope<unknown>) => void) => () => void;
392
439
  publish: (collectionId: string, message: ProtocolEnvelope<unknown>) => void;
393
440
  isLeader: (collectionId: string) => boolean;
@@ -400,8 +447,8 @@ export interface PersistedCollectionCoordinator {
400
447
  * and is never serialized to a follower. `localEnsureCompleted` lets the
401
448
  * built-in leader avoid repeating successful local work.
402
449
  */
403
- requestEnsurePersistedIndex: (collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean) => Promise<void>;
404
- requestApplyLocalMutations?: (collectionId: string, mutations: Array<PersistedMutationEnvelope>) => Promise<ApplyLocalMutationsResponse>;
450
+ requestEnsurePersistedIndex: (collectionId: string, signature: string, spec: PersistedIndexSpec, scopedAdapter?: HydrationPersistenceAdapter, localEnsureCompleted?: boolean, cacheGenerationClaimId?: string) => Promise<void>;
451
+ requestApplyLocalMutations?: (collectionId: string, mutations: Array<PersistedMutationEnvelope>, cacheGenerationClaimId?: string) => Promise<ApplyLocalMutationsResponse>;
405
452
  /**
406
453
  * Applies a committed transaction through the supplied adapter when present.
407
454
  * Wrappers must forward this leader-local loan: scheduling it again can wait
@@ -412,7 +459,7 @@ export interface PersistedCollectionCoordinator {
412
459
  /** Available only when the elected adapter can certify the durable outcome. */
413
460
  reconcileCommittedTx?: (collectionId: string, tx: PersistedTx, anchor: CommittedTxAnchor, scopedAdapter?: HydrationPersistenceAdapter) => Promise<ReconcileCommittedTxResponse>;
414
461
  /** The scoped adapter is leader-local and is never serialized to a follower. */
415
- pullSince?: (collectionId: string, fromRowVersion: number, scopedAdapter?: HydrationPersistenceAdapter) => Promise<PullSinceResponse>;
462
+ pullSince?: (collectionId: string, fromRowVersion: number, scopedAdapter?: HydrationPersistenceAdapter, cacheGenerationClaimId?: string) => Promise<PullSinceResponse>;
416
463
  }
417
464
  export interface PersistedCollectionPersistence {
418
465
  adapter: PersistenceAdapter;