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` | `sync-absent-error` | `reset`
22
23export type CapacitorSQLiteSchemaMismatchPolicy =
24 CapacitorSQLiteCoreSchemaMismatchPolicy | `throw`
25
26type CapacitorSQLitePersistenceBaseOptions = Omit<
27 SQLiteCoreAdapterOptions,
28 `driver` | `schemaVersion` | `schemaMismatchPolicy`
29> & {
30 database: SQLiteDBConnection
31 coordinator?: PersistedCollectionCoordinator
32 schemaMismatchPolicy?: CapacitorSQLiteSchemaMismatchPolicy
33}
34
35export type CapacitorSQLitePersistenceOptions =
36 CapacitorSQLitePersistenceBaseOptions
37
38function normalizeSchemaMismatchPolicy(
39 policy: CapacitorSQLiteSchemaMismatchPolicy,
40): CapacitorSQLiteCoreSchemaMismatchPolicy {
41 if (policy === `throw`) {
42 return `sync-absent-error`
43 }
44
45 return policy
46}
47
48function resolveSchemaMismatchPolicy(
49 explicitPolicy: CapacitorSQLiteSchemaMismatchPolicy | undefined,
50 mode: PersistedCollectionMode,
51): CapacitorSQLiteCoreSchemaMismatchPolicy {
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: CapacitorSQLiteCoreSchemaMismatchPolicy,
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: CapacitorSQLitePersistenceOptions,
70): SQLiteDriver {
71 return new CapacitorSQLiteDriver({
72 database: options.database,
73 })
74}
75
76function resolveAdapterBaseOptions(
77 options: CapacitorSQLitePersistenceOptions,
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
92export function createCapacitorSQLitePersistence(
93 options: CapacitorSQLitePersistenceOptions,
94): PersistedCollectionPersistence {
95 const { coordinator, schemaMismatchPolicy } = options
96 const driver = createInternalSQLiteDriver(options)
97 const adapterBaseOptions = resolveAdapterBaseOptions(options)
98 const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
99 const adapterCache = new Map<
100 string,
101 ReturnType<typeof createSQLiteCorePersistenceAdapter>
102 >()
103
104 const getAdapterForCollection = (
105 mode: PersistedCollectionMode,
106 schemaVersion: number | undefined,
107 ) => {
108 const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
109 schemaMismatchPolicy,
110 mode,
111 )
112 const cacheKey = createAdapterCacheKey(
113 resolvedSchemaMismatchPolicy,
114 schemaVersion,
115 )
116 const cachedAdapter = adapterCache.get(cacheKey)
117 if (cachedAdapter) {
118 return cachedAdapter
119 }
120
121 const adapter = createSQLiteCorePersistenceAdapter({
122 ...adapterBaseOptions,
123 driver,
124 schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
125 ...(schemaVersion === undefined ? {} : { schemaVersion }),
126 })
127 adapterCache.set(cacheKey, adapter)
128 return adapter
129 }
130
131 const createCollectionPersistence = (
132 mode: PersistedCollectionMode,
133 schemaVersion: number | undefined,
134 ): PersistedCollectionPersistence => ({
135 adapter: getAdapterForCollection(mode, schemaVersion),
136 coordinator: resolvedCoordinator,
137 })
138
139 const defaultPersistence = createCollectionPersistence(
140 `sync-absent`,
141 undefined,
142 )
143
144 return {
145 ...defaultPersistence,
146 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
147 createCollectionPersistence(mode, schemaVersion),
148 resolvePersistenceForMode: (mode) =>
149 createCollectionPersistence(mode, undefined),
150 }
151}