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