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