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

mobile-persistence.ts

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