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