Open History to see the last commit
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`
27 | `sync-absent-error`
28 | `reset`
29
30export type MobileSQLiteSchemaMismatchPolicy =
31 | MobileSQLiteCoreSchemaMismatchPolicy
32 | `throw`
33
34type MobileSQLitePersistenceBaseOptions = Omit<
35 SQLiteCoreAdapterOptions,
36 `driver` | `schemaVersion` | `schemaMismatchPolicy`
37> & {
38 database: OpSQLiteDatabaseLike
39 arrayResultMode?: OpSQLiteArrayResultMode
40 coordinator?: PersistedCollectionCoordinator
41 schemaMismatchPolicy?: MobileSQLiteSchemaMismatchPolicy
42}
43
44export type MobileSQLitePersistenceOptions = MobileSQLitePersistenceBaseOptions
45
46function normalizeSchemaMismatchPolicy(
47 policy: MobileSQLiteSchemaMismatchPolicy,
48): MobileSQLiteCoreSchemaMismatchPolicy {
49 if (policy === `throw`) {
50 return `sync-absent-error`
51 }
52
53 return policy
54}
55
56function resolveSchemaMismatchPolicy(
57 explicitPolicy: MobileSQLiteSchemaMismatchPolicy | undefined,
58 mode: PersistedCollectionMode,
59): MobileSQLiteCoreSchemaMismatchPolicy {
60 if (explicitPolicy) {
61 return normalizeSchemaMismatchPolicy(explicitPolicy)
62 }
63
64 return mode === `sync-present` ? `sync-present-reset` : `sync-absent-error`
65}
66
67function createAdapterCacheKey(
68 schemaMismatchPolicy: MobileSQLiteCoreSchemaMismatchPolicy,
69 schemaVersion: number | undefined,
70): string {
71 const schemaVersionKey =
72 schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
73 return `${schemaMismatchPolicy}|${schemaVersionKey}`
74}
75
76function createInternalSQLiteDriver(
77 options: MobileSQLitePersistenceOptions,
78): SQLiteDriver {
79 return new OpSQLiteDriver({
80 database: options.database,
81 arrayResultMode: options.arrayResultMode,
82 })
83}
84
85function resolveAdapterBaseOptions(
86 options: MobileSQLitePersistenceOptions,
87): Omit<
88 SQLiteCoreAdapterOptions,
89 `driver` | `schemaVersion` | `schemaMismatchPolicy`
90> {
91 return {
92 appliedTxPruneMaxRows:
93 options.appliedTxPruneMaxRows ?? DEFAULT_APPLIED_TX_PRUNE_MAX_ROWS,
94 appliedTxPruneMaxAgeSeconds:
95 options.appliedTxPruneMaxAgeSeconds ??
96 DEFAULT_APPLIED_TX_PRUNE_MAX_AGE_SECONDS,
97 pullSinceReloadThreshold: options.pullSinceReloadThreshold,
98 }
99}
100
101export function createMobileSQLitePersistence(
102 options: MobileSQLitePersistenceOptions,
103): PersistedCollectionPersistence {
104 const { coordinator, schemaMismatchPolicy } = options
105 const driver = createInternalSQLiteDriver(options)
106 const adapterBaseOptions = resolveAdapterBaseOptions(options)
107 const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
108 const adapterCache = new Map<
109 string,
110 ReturnType<typeof createSQLiteCorePersistenceAdapter>
111 >()
112
113 const getAdapterForCollection = (
114 mode: PersistedCollectionMode,
115 schemaVersion: number | undefined,
116 ) => {
117 const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
118 schemaMismatchPolicy,
119 mode,
120 )
121 const cacheKey = createAdapterCacheKey(
122 resolvedSchemaMismatchPolicy,
123 schemaVersion,
124 )
125 const cachedAdapter = adapterCache.get(cacheKey)
126 if (cachedAdapter) {
127 return cachedAdapter
128 }
129
130 const adapter = createSQLiteCorePersistenceAdapter({
131 ...adapterBaseOptions,
132 driver,
133 schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
134 ...(schemaVersion === undefined ? {} : { schemaVersion }),
135 })
136 adapterCache.set(cacheKey, adapter)
137 return adapter
138 }
139
140 const createCollectionPersistence = (
141 mode: PersistedCollectionMode,
142 schemaVersion: number | undefined,
143 ): PersistedCollectionPersistence => ({
144 adapter: getAdapterForCollection(mode, schemaVersion),
145 coordinator: resolvedCoordinator,
146 })
147
148 const defaultPersistence = createCollectionPersistence(
149 `sync-absent`,
150 undefined,
151 )
152
153 return {
154 ...defaultPersistence,
155 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
156 createCollectionPersistence(mode, schemaVersion),
157 // Backward compatible fallback for older callers.
158 resolvePersistenceForMode: (mode) =>
159 createCollectionPersistence(mode, undefined),
160 }
161}