-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathdo-persistence.ts
More file actions
152 lines (135 loc) · 4.46 KB
/
do-persistence.ts
File metadata and controls
152 lines (135 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
SingleProcessCoordinator,
createSQLiteCorePersistenceAdapter,
} from '@tanstack/db-sqlite-persistence-core'
import { CloudflareDOSQLiteDriver } from './do-driver'
import type {
PersistedCollectionCoordinator,
PersistedCollectionMode,
PersistedCollectionPersistence,
SQLiteCoreAdapterOptions,
SQLiteDriver,
} from '@tanstack/db-sqlite-persistence-core'
import type { DurableObjectStorageLike } from './do-driver'
export type { DurableObjectStorageLike } from './do-driver'
type CloudflareDOCoreSchemaMismatchPolicy =
| `sync-present-reset`
| `sync-absent-error`
| `reset`
export type CloudflareDOSchemaMismatchPolicy =
| CloudflareDOCoreSchemaMismatchPolicy
| `throw`
type CloudflareDOSQLitePersistenceBaseOptions = Omit<
SQLiteCoreAdapterOptions,
`driver` | `schemaVersion` | `schemaMismatchPolicy`
> & {
storage: DurableObjectStorageLike
coordinator?: PersistedCollectionCoordinator
schemaMismatchPolicy?: CloudflareDOSchemaMismatchPolicy
}
export type CloudflareDOSQLitePersistenceOptions =
CloudflareDOSQLitePersistenceBaseOptions
function normalizeSchemaMismatchPolicy(
policy: CloudflareDOSchemaMismatchPolicy,
): CloudflareDOCoreSchemaMismatchPolicy {
if (policy === `throw`) {
return `sync-absent-error`
}
return policy
}
function resolveSchemaMismatchPolicy(
explicitPolicy: CloudflareDOSchemaMismatchPolicy | undefined,
mode: PersistedCollectionMode,
): CloudflareDOCoreSchemaMismatchPolicy {
if (explicitPolicy) {
return normalizeSchemaMismatchPolicy(explicitPolicy)
}
return mode === `sync-present` ? `sync-present-reset` : `sync-absent-error`
}
function createAdapterCacheKey(
schemaMismatchPolicy: CloudflareDOCoreSchemaMismatchPolicy,
schemaVersion: number | undefined,
): string {
const schemaVersionKey =
schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
return `${schemaMismatchPolicy}|${schemaVersionKey}`
}
function resolveSQLiteDriver(
options: CloudflareDOSQLitePersistenceOptions,
): SQLiteDriver {
return new CloudflareDOSQLiteDriver({
storage: options.storage,
})
}
function resolveAdapterBaseOptions(
options: CloudflareDOSQLitePersistenceOptions,
): Omit<
SQLiteCoreAdapterOptions,
`driver` | `schemaVersion` | `schemaMismatchPolicy`
> {
return {
appliedTxPruneMaxRows: options.appliedTxPruneMaxRows,
appliedTxPruneMaxAgeSeconds: options.appliedTxPruneMaxAgeSeconds,
pullSinceReloadThreshold: options.pullSinceReloadThreshold,
}
}
/**
* Creates a shared Durable Object SQLite persistence instance that can be reused
* by many collections in a single Durable Object storage.
*/
export function createCloudflareDOSQLitePersistence(
options: CloudflareDOSQLitePersistenceOptions,
): PersistedCollectionPersistence {
const { coordinator, schemaMismatchPolicy } = options
const driver = resolveSQLiteDriver(options)
const adapterBaseOptions = resolveAdapterBaseOptions(options)
const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
const adapterCache = new Map<
string,
ReturnType<typeof createSQLiteCorePersistenceAdapter>
>()
const getAdapterForCollection = (
mode: PersistedCollectionMode,
schemaVersion: number | undefined,
) => {
const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
schemaMismatchPolicy,
mode,
)
const cacheKey = createAdapterCacheKey(
resolvedSchemaMismatchPolicy,
schemaVersion,
)
const cachedAdapter = adapterCache.get(cacheKey)
if (cachedAdapter) {
return cachedAdapter
}
const adapter = createSQLiteCorePersistenceAdapter({
...adapterBaseOptions,
driver,
schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
...(schemaVersion === undefined ? {} : { schemaVersion }),
})
adapterCache.set(cacheKey, adapter)
return adapter
}
const createCollectionPersistence = (
mode: PersistedCollectionMode,
schemaVersion: number | undefined,
): PersistedCollectionPersistence => ({
adapter: getAdapterForCollection(mode, schemaVersion),
coordinator: resolvedCoordinator,
})
const defaultPersistence = createCollectionPersistence(
`sync-absent`,
undefined,
)
return {
...defaultPersistence,
resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
createCollectionPersistence(mode, schemaVersion),
// Backward compatible fallback for older callers.
resolvePersistenceForMode: (mode) =>
createCollectionPersistence(mode, undefined),
}
}