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