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