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