Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

mobile-persistence.ts

160 lines4.7 KB

1import {
2 DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS,
3 DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS,
4 SingleProcessCoordinator,
5 createSQLiteCorePersistenceAdapter,
6} from '@tanstack/db-sqlite-persistence-core'
7import { OpSQLiteDriver } from './op-sqlite-driver'
8import type {
9 PersistedCollectionCoordinator,
10 PersistedCollectionMode,
11 PersistedCollectionPersistence,
12 SQLiteCoreAdapterOptions,
13 SQLiteDriver,
14} from '@tanstack/db-sqlite-persistence-core'
15import type {
16 OpSQLiteArrayResultMode,
17 OpSQLiteDatabaseLike,
18} from './op-sqlite-driver'
19​
20export type {
21 OpSQLiteArrayResultMode,
22 OpSQLiteDatabaseLike,
23} from './op-sqlite-driver'
24​
25type MobileSQLiteCoreSchemaMismatchPolicy =
26 `sync-present-reset` | `sync-absent-error` | `reset`
27​
28export type MobileSQLiteSchemaMismatchPolicy =
29 MobileSQLiteCoreSchemaMismatchPolicy | `throw`
30​
31type MobileSQLitePersistenceBaseOptions = Omit<
32 SQLiteCoreAdapterOptions,
33 `driver` | `schemaVersion` | `schemaMismatchPolicy`
34> & {
35 database: OpSQLiteDatabaseLike
36 arrayResultMode?: OpSQLiteArrayResultMode
37 coordinator?: PersistedCollectionCoordinator
38 schemaMismatchPolicy?: MobileSQLiteSchemaMismatchPolicy
40​
41export type MobileSQLitePersistenceOptions = MobileSQLitePersistenceBaseOptions
42​
43function normalizeSchemaMismatchPolicy(
44 policy: MobileSQLiteSchemaMismatchPolicy,
45): MobileSQLiteCoreSchemaMismatchPolicy {
46 if (policy === `throw`) {
47 return `sync-absent-error`
48 }
49​
50 return policy
52​
53function resolveSchemaMismatchPolicy(
54 explicitPolicy: MobileSQLiteSchemaMismatchPolicy | undefined,
55 mode: PersistedCollectionMode,
56): MobileSQLiteCoreSchemaMismatchPolicy {
57 if (explicitPolicy) {
58 return normalizeSchemaMismatchPolicy(explicitPolicy)
59 }
60​
61 return mode === `sync-present` ? `sync-present-reset` : `sync-absent-error`
63​
64function createAdapterCacheKey(
65 schemaMismatchPolicy: MobileSQLiteCoreSchemaMismatchPolicy,
66 schemaVersion: number | undefined,
67): string {
68 const schemaVersionKey =
69 schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
70 return `${schemaMismatchPolicy}|${schemaVersionKey}`
72​
73function createInternalSQLiteDriver(
74 options: MobileSQLitePersistenceOptions,
75): SQLiteDriver {
76 return new OpSQLiteDriver({
77 database: options.database,
78 arrayResultMode: options.arrayResultMode,
79 })
81​
82function resolveAdapterBaseOptions(
83 options: MobileSQLitePersistenceOptions,
84): Omit<
85 SQLiteCoreAdapterOptions,
86 `driver` | `schemaVersion` | `schemaMismatchPolicy`
87> {
88 return {
89 appliedTxPruneMaxRows:
90 options.appliedTxPruneMaxRows ?? DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS,
91 appliedTxPruneMaxAgeSeconds:
92 options.appliedTxPruneMaxAgeSeconds ??
93 DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS,
94 pullSinceReloadThreshold: options.pullSinceReloadThreshold,
95 cacheGenerationClaimTtlMs: options.cacheGenerationClaimTtlMs,
96 now: options.now,
97 }
99​
100export function createMobileSQLitePersistence(
101 options: MobileSQLitePersistenceOptions,
102): PersistedCollectionPersistence {
103 const { coordinator, schemaMismatchPolicy } = options
104 const driver = createInternalSQLiteDriver(options)
105 const adapterBaseOptions = resolveAdapterBaseOptions(options)
106 const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
107 const adapterCache = new Map<
108 string,
109 ReturnType<typeof createSQLiteCorePersistenceAdapter>
110 >()
111​
112 const getAdapterForCollection = (
113 mode: PersistedCollectionMode,
114 schemaVersion: number | undefined,
115 ) => {
116 const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
117 schemaMismatchPolicy,
118 mode,
119 )
120 const cacheKey = createAdapterCacheKey(
121 resolvedSchemaMismatchPolicy,
122 schemaVersion,
123 )
124 const cachedAdapter = adapterCache.get(cacheKey)
125 if (cachedAdapter) {
126 return cachedAdapter
127 }
128​
129 const adapter = createSQLiteCorePersistenceAdapter({
130 ...adapterBaseOptions,
131 driver,
132 schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
133 ...(schemaVersion === undefined ? {} : { schemaVersion }),
134 })
135 adapterCache.set(cacheKey, adapter)
136 return adapter
137 }
138​
139 const createCollectionPersistence = (
140 mode: PersistedCollectionMode,
141 schemaVersion: number | undefined,
142 ): PersistedCollectionPersistence => ({
143 adapter: getAdapterForCollection(mode, schemaVersion),
144 coordinator: resolvedCoordinator,
145 })
146​
147 const defaultPersistence = createCollectionPersistence(
148 `sync-absent`,
149 undefined,
150 )
151​
152 return {
153 ...defaultPersistence,
154 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
155 createCollectionPersistence(mode, schemaVersion),
156 // Backward compatible fallback for older callers.
157 resolvePersistenceForMode: (mode) =>
158 createCollectionPersistence(mode, undefined),
159 }