-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathloader.ts
More file actions
272 lines (248 loc) · 8.27 KB
/
loader.ts
File metadata and controls
272 lines (248 loc) · 8.27 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import {
type BaseQuery,
type CreateOpts,
type LoadOpts,
SyncOptions,
type UpdateOpts,
} from '@ceramicnetwork/common'
import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance'
import { StreamID, StreamRef } from '@ceramicnetwork/streamid'
import type { CeramicAPI } from '@composedb/types'
import DataLoader from 'dataloader'
import type { Connection } from 'graphql-relay'
import {
type GenesisMetadata,
createDeterministicKey,
getDeterministicCacheKey,
} from './deterministic.js'
import { type ConnectionQuery, queryConnection, queryOne } from './query.js'
import type { DeterministicKeysCache, DocumentCache, LoadKey } from './types.js'
export const DEFAULT_DETERMINISTIC_OPTIONS: LoadOpts = { sync: SyncOptions.NEVER_SYNC }
export type CreateOptions = CreateOpts & {
controller?: string
shouldIndex?: boolean
}
export type DeterministicLoadOptions = CreateOptions & {
onlyIndexed?: boolean
}
export type UpdateDocOptions = {
replace?: boolean
shouldIndex?: boolean
version?: string
}
export type UpdateOptions = UpdateOpts & UpdateDocOptions
export type DocumentLoaderParams = {
/**
* A Ceramic client instance
*/
ceramic: CeramicAPI
/**
* A supported cache implementation, `true` to use the default implementation or `false` to
* disable the cache (default)
*/
cache?: DocumentCache | boolean
/**
* Optional cache for deterministic streams keys
*/
deterministicKeysCache?: DeterministicKeysCache
/**
* MultiQuery request timeout in milliseconds
*/
multiqueryTimeout?: number
}
export function getKeyID(key: LoadKey): string {
return typeof key.id === 'string' ? StreamRef.from(key.id).toString() : key.id.toString()
}
/** @internal */
export function removeNullValues(content: Record<string, unknown>): Record<string, unknown> {
const copy = { ...content }
for (const [key, value] of Object.entries(copy)) {
if (value == null) {
delete copy[key]
} else if (Array.isArray(value)) {
copy[key] = value.map((item: unknown) => {
return typeof item === 'object' && !Array.isArray(item) && item != null
? removeNullValues(item as Record<string, unknown>)
: item
})
} else if (typeof value === 'object') {
copy[key] = removeNullValues(value as Record<string, unknown>)
}
}
return copy
}
/**
* The DocumentLoader class provides APIs to batch load and cache ModelInstanceDocument streams.
*
* It is exported by the {@linkcode loader} module.
*
* ```sh
* import { DocumentLoader } from '@composedb/loader'
* ```
*/
export class DocumentLoader extends DataLoader<LoadKey, ModelInstanceDocument, string> {
#ceramic: CeramicAPI
#deterministicKeys: DeterministicKeysCache
#useCache: boolean
constructor(params: DocumentLoaderParams) {
super(
async (keys: ReadonlyArray<LoadKey>) => {
if (!params.cache) {
// Disable cache but keep batching behavior - from https://github.com/graphql/dataloader#disabling-cache
this.clearAll()
}
const results = await params.ceramic.multiQuery(
keys.map(({ id, ...rest }) => ({ streamId: id, ...rest })),
params.multiqueryTimeout,
)
return keys.map((key) => {
const id = getKeyID(key)
const doc = results[id]
return doc
? (doc as unknown as ModelInstanceDocument)
: new Error(`Failed to load document: ${id}`)
})
},
{
cache: true, // Cache needs to be enabled for batching
cacheKeyFn: getKeyID,
cacheMap:
params.cache != null && typeof params.cache !== 'boolean' ? params.cache : undefined,
},
)
this.#ceramic = params.ceramic
this.#deterministicKeys = params.deterministicKeysCache ?? new Map()
this.#useCache = !!params.cache
}
/**
* Add a ModelInstanceDocument to the local cache, if enabled.
*/
cache(stream: ModelInstanceDocument): boolean {
if (!this.#useCache) {
return false
}
const key = { id: stream.id.toString() }
this.clear(key).prime(key, stream)
return true
}
/**
* Get or create the LoadKey for a deterministic stream.
* @internal
*/
_getDeterministicKey(meta: GenesisMetadata): Promise<LoadKey> {
const cacheKey = getDeterministicCacheKey(meta)
const existing = this.#deterministicKeys.get(cacheKey)
if (existing != null) {
return existing
}
const loadKeyPromise = createDeterministicKey(meta)
this.#deterministicKeys.set(cacheKey, loadKeyPromise)
return loadKeyPromise
}
/**
* Create a new ModelInstanceDocument and add it to the cache, if enabled.
*/
async create<T extends Record<string, any> = Record<string, any>>(
model: string | StreamID,
content: T,
{ controller, shouldIndex, ...options }: CreateOptions = {},
): Promise<ModelInstanceDocument<T>> {
const metadata = {
controller,
model: model instanceof StreamID ? model : StreamID.fromString(model),
shouldIndex,
}
const stream = await ModelInstanceDocument.create<T>(
this.#ceramic,
removeNullValues(content) as T,
metadata,
options,
)
this.cache(stream)
return stream
}
/**
* Load a ModelInstanceDocument from the cache (if enabled) or remotely.
*/
async load<T extends Record<string, any> = Record<string, any>>(
key: LoadKey,
): Promise<ModelInstanceDocument<T>> {
return (await super.load(key)) as ModelInstanceDocument<T>
}
/**
* Load a deterministic stream and add it to the cache.
* @internal
*/
async _loadDeterministic<T extends Record<string, any> = Record<string, any>>(
meta: GenesisMetadata,
options: DeterministicLoadOptions = {},
): Promise<ModelInstanceDocument<T> | null> {
const opts = { ...DEFAULT_DETERMINISTIC_OPTIONS, ...options }
const key = await this._getDeterministicKey(meta)
const doc = await this.load<T>({ ...key, opts })
return options.onlyIndexed === false || doc.metadata.shouldIndex !== false ? doc : null
}
/**
* Create or load a deterministic ModelInstanceDocument and cache it.
*/
async loadSingle<T extends Record<string, any> = Record<string, any>>(
controller: string,
model: string | StreamID,
options?: DeterministicLoadOptions,
): Promise<ModelInstanceDocument<T> | null> {
return await this._loadDeterministic({ controller, model }, options)
}
/**
* Create or load a deterministic ModelInstanceDocument using the SET account
* relation and cache it.
*/
async loadSet<T extends Record<string, any> = Record<string, any>>(
controller: string,
model: string | StreamID,
unique: Array<string>,
options?: DeterministicLoadOptions,
): Promise<ModelInstanceDocument<T> | null> {
return await this._loadDeterministic({ controller, model, unique }, options)
}
/**
* Update a ModelInstanceDocument after loading the stream remotely, bypassing the cache.
*/
async update<T extends Record<string, any> = Record<string, any>>(
streamID: string | StreamID,
content: T,
{ replace, shouldIndex, version, ...options }: UpdateOptions = {},
): Promise<ModelInstanceDocument<T>> {
const key = { id: streamID }
this.clear(key)
const stream = await this.load<T>(key)
if (version != null && stream.commitId.toString() !== version) {
throw new Error('Stream version mismatch')
}
const newContent = replace ? content : { ...(stream.content ?? {}), ...content }
const metadata = typeof shouldIndex === 'undefined' ? undefined : { shouldIndex }
await stream.replace(removeNullValues(newContent) as T, metadata, options)
return stream
}
/**
* Query the index for multiple ModelInstanceDocument streams and cache the results.
*/
async queryConnection(query: ConnectionQuery): Promise<Connection<ModelInstanceDocument | null>> {
const connection = await queryConnection(this.#ceramic, query)
for (const edge of connection.edges) {
if (edge.node != null) {
this.cache(edge.node)
}
}
return connection
}
/**
* Query the index for a single ModelInstanceDocument stream and cache it.
*/
async queryOne(query: BaseQuery): Promise<ModelInstanceDocument | null> {
const doc = await queryOne(this.#ceramic, query)
if (doc != null) {
this.cache(doc)
}
return doc
}
}