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