Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

capacitor-persistence.ts

153 lines4.6 KB

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
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
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`
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}`
67​
68function createInternalSQLiteDriver(
69 options: CapacitorSQLitePersistenceOptions,
70): SQLiteDriver {
71 return new CapacitorSQLiteDriver({
72 database: options.database,
73 })
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 cacheGenerationClaimTtlMs: options.cacheGenerationClaimTtlMs,
90 now: options.now,
91 }
93​
94export function createCapacitorSQLitePersistence(
95 options: CapacitorSQLitePersistenceOptions,
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 mode: PersistedCollectionMode,
135 schemaVersion: number | undefined,
136 ): PersistedCollectionPersistence => ({
137 adapter: getAdapterForCollection(mode, schemaVersion),
138 coordinator: resolvedCoordinator,
139 })
140​
141 const defaultPersistence = createCollectionPersistence(
142 `sync-absent`,
143 undefined,
144 )
145​
146 return {
147 ...defaultPersistence,
148 resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
149 createCollectionPersistence(mode, schemaVersion),
150 resolvePersistenceForMode: (mode) =>
151 createCollectionPersistence(mode, undefined),
152 }