Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

expo.ts

147 lines4.3 KB

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
33​
34function normalizeSchemaMismatchPolicy(
35 policy: ExpoSQLiteSchemaMismatchPolicy,
36): ExpoSQLiteCoreSchemaMismatchPolicy {
37 if (policy === `throw`) {
38 return `sync-absent-error`
39 }
40​
41 return policy
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`
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}`
63​
64function createInternalSQLiteDriver(
65 options: ExpoSQLitePersistenceOptions,
66): SQLiteDriver {
67 return new ExpoSQLiteDriver({
68 database: options.database,
69 })
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 }
87​
88export function createExpoSQLitePersistence(
89 options: ExpoSQLitePersistenceOptions,
90): PersistedCollectionPersistence {
91 const { coordinator, schemaMismatchPolicy } = options
92 const driver = createInternalSQLiteDriver(options)
93 const adapterBaseOptions = resolveAdapterBaseOptions(options)
94 const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
95 const adapterCache = new Map<
96 string,
97 ReturnType<typeof createSQLiteCorePersistenceAdapter>
98 >()
99​
100 const getAdapterForCollection = (
101 mode: PersistedCollectionMode,
102 schemaVersion: number | undefined,
103 ) => {
104 const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
105 schemaMismatchPolicy,
106 mode,
107 )
108 const cacheKey = createAdapterCacheKey(
109 resolvedSchemaMismatchPolicy,
110 schemaVersion,
111 )
112 const cachedAdapter = adapterCache.get(cacheKey)
113 if (cachedAdapter) {
114 return cachedAdapter
115 }
116​
117 const adapter = createSQLiteCorePersistenceAdapter({
118 ...adapterBaseOptions,
119 driver,
120 schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
121 ...(schemaVersion === undefined ? {} : { schemaVersion }),
122 })
123 adapterCache.set(cacheKey, adapter)
124 return adapter
125 }
126​
127 const createCollectionPersistence = (
128 mode: PersistedCollectionMode,
129 schemaVersion: number | undefined,
130 ): PersistedCollectionPersistence => ({
131 adapter: getAdapterForCollection(mode, schemaVersion),
132 coordinator: resolvedCoordinator,
133 })
134​
135 const defaultPersistence = createCollectionPersistence(
136 `sync-absent`,
137 undefined,
138 )
139​
140 return {
141 ...defaultPersistence,
142 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
143 createCollectionPersistence(mode, schemaVersion),
144 resolvePersistenceForMode: (mode) =>
145 createCollectionPersistence(mode, undefined),
146 }