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