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