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

mobile-persistence.ts

148 lines4.3 KB

1import {
2 SingleProcessCoordinator,
3 createSQLiteCorePersistenceAdapter,
4} from '@tanstack/db-sqlite-persistence-core'
5import { OpSQLiteDriver } from './op-sqlite-driver'
6import type {
7 PersistedCollectionCoordinator,
8 PersistedCollectionMode,
9 PersistedCollectionPersistence,
10 SQLiteCoreAdapterOptions,
11 SQLiteDriver,
12} from '@tanstack/db-sqlite-persistence-core'
13import type { OpSQLiteDatabaseLike } from './op-sqlite-driver'
14​
15export type { OpSQLiteDatabaseLike } from './op-sqlite-driver'
16​
17type MobileSQLiteCoreSchemaMismatchPolicy =
18 | `sync-present-reset`
19 | `sync-absent-error`
20 | `reset`
21​
22export type MobileSQLiteSchemaMismatchPolicy =
23 | MobileSQLiteCoreSchemaMismatchPolicy
24 | `throw`
25​
26type MobileSQLitePersistenceBaseOptions = Omit<
27 SQLiteCoreAdapterOptions,
28 `driver` | `schemaVersion` | `schemaMismatchPolicy`
29> & {
30 database: OpSQLiteDatabaseLike
31 coordinator?: PersistedCollectionCoordinator
32 schemaMismatchPolicy?: MobileSQLiteSchemaMismatchPolicy
34​
35export type MobileSQLitePersistenceOptions = MobileSQLitePersistenceBaseOptions
36​
37function normalizeSchemaMismatchPolicy(
38 policy: MobileSQLiteSchemaMismatchPolicy,
39): MobileSQLiteCoreSchemaMismatchPolicy {
40 if (policy === `throw`) {
41 return `sync-absent-error`
42 }
43​
44 return policy
46​
47function resolveSchemaMismatchPolicy(
48 explicitPolicy: MobileSQLiteSchemaMismatchPolicy | undefined,
49 mode: PersistedCollectionMode,
50): MobileSQLiteCoreSchemaMismatchPolicy {
51 if (explicitPolicy) {
52 return normalizeSchemaMismatchPolicy(explicitPolicy)
53 }
54​
55 return mode === `sync-present` ? `sync-present-reset` : `sync-absent-error`
57​
58function createAdapterCacheKey(
59 schemaMismatchPolicy: MobileSQLiteCoreSchemaMismatchPolicy,
60 schemaVersion: number | undefined,
61): string {
62 const schemaVersionKey =
63 schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
64 return `${schemaMismatchPolicy}|${schemaVersionKey}`
66​
67function createInternalSQLiteDriver(
68 options: MobileSQLitePersistenceOptions,
69): SQLiteDriver {
70 return new OpSQLiteDriver({
71 database: options.database,
72 })
74​
75function resolveAdapterBaseOptions(
76 options: MobileSQLitePersistenceOptions,
77): Omit<
78 SQLiteCoreAdapterOptions,
79 `driver` | `schemaVersion` | `schemaMismatchPolicy`
80> {
81 return {
82 appliedTxPruneMaxRows: options.appliedTxPruneMaxRows,
83 appliedTxPruneMaxAgeSeconds: options.appliedTxPruneMaxAgeSeconds,
84 pullSinceReloadThreshold: options.pullSinceReloadThreshold,
85 }
87​
88export function createMobileSQLitePersistence(
89 options: MobileSQLitePersistenceOptions,
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 // Backward compatible fallback for older callers.
145 resolvePersistenceForMode: (mode) =>
146 createCollectionPersistence(mode, undefined),
147 }