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` | `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
39}
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
51}
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`
62}
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}`
71}
72
73function createInternalSQLiteDriver(
74 options: MobileSQLitePersistenceOptions,
75): SQLiteDriver {
76 return new OpSQLiteDriver({
77 database: options.database,
78 arrayResultMode: options.arrayResultMode,
79 })
80}
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 }
96}
97
98export function createMobileSQLitePersistence(
99 options: MobileSQLitePersistenceOptions,
100): PersistedCollectionPersistence {
101 const { coordinator, schemaMismatchPolicy } = options
102 const driver = createInternalSQLiteDriver(options)
103 const adapterBaseOptions = resolveAdapterBaseOptions(options)
104 const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
105 const adapterCache = new Map<
106 string,
107 ReturnType<typeof createSQLiteCorePersistenceAdapter>
108 >()
109
110 const getAdapterForCollection = (
111 mode: PersistedCollectionMode,
112 schemaVersion: number | undefined,
113 ) => {
114 const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
115 schemaMismatchPolicy,
116 mode,
117 )
118 const cacheKey = createAdapterCacheKey(
119 resolvedSchemaMismatchPolicy,
120 schemaVersion,
121 )
122 const cachedAdapter = adapterCache.get(cacheKey)
123 if (cachedAdapter) {
124 return cachedAdapter
125 }
126
127 const adapter = createSQLiteCorePersistenceAdapter({
128 ...adapterBaseOptions,
129 driver,
130 schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
131 ...(schemaVersion === undefined ? {} : { schemaVersion }),
132 })
133 adapterCache.set(cacheKey, adapter)
134 return adapter
135 }
136
137 const createCollectionPersistence = (
138 mode: PersistedCollectionMode,
139 schemaVersion: number | undefined,
140 ): PersistedCollectionPersistence => ({
141 adapter: getAdapterForCollection(mode, schemaVersion),
142 coordinator: resolvedCoordinator,
143 })
144
145 const defaultPersistence = createCollectionPersistence(
146 `sync-absent`,
147 undefined,
148 )
149
150 return {
151 ...defaultPersistence,
152 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
153 createCollectionPersistence(mode, schemaVersion),
154 // Backward compatible fallback for older callers.
155 resolvePersistenceForMode: (mode) =>
156 createCollectionPersistence(mode, undefined),
157 }
158}