@fjell/cache 4.6.4 → 4.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Cache.cjs.js +41 -2
- package/dist/Cache.es.js +41 -2
- package/dist/CacheMap.cjs.js +65 -3
- package/dist/CacheMap.es.js +65 -3
- package/dist/index.cjs +104 -3
- package/dist/index.cjs.map +1 -1
- package/package.json +18 -21
- package/.kodrdriv/config.yaml +0 -14
- package/.kodrdriv/context/content.md +0 -1
- package/commit.sh +0 -8
- package/release.sh +0 -89
- package/src/Aggregator.ts +0 -330
- package/src/Cache.ts +0 -428
- package/src/CacheMap.ts +0 -132
- package/src/CacheRegistry.ts +0 -66
- package/src/index.ts +0 -4
- package/src/logger.ts +0 -5
package/src/Cache.ts
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AllItemTypeArrays,
|
|
3
|
-
ComKey,
|
|
4
|
-
isItemKeyEqual,
|
|
5
|
-
isValidItemKey,
|
|
6
|
-
Item,
|
|
7
|
-
ItemQuery,
|
|
8
|
-
LocKeyArray,
|
|
9
|
-
PriKey,
|
|
10
|
-
validatePK
|
|
11
|
-
} from "@fjell/core";
|
|
12
|
-
import { CacheMap } from "./CacheMap";
|
|
13
|
-
import LibLogger from "./logger";
|
|
14
|
-
|
|
15
|
-
import { ClientApi } from "@fjell/client-api";
|
|
16
|
-
import { NotFoundError } from "@fjell/http-api";
|
|
17
|
-
|
|
18
|
-
const logger = LibLogger.get('Cache');
|
|
19
|
-
|
|
20
|
-
export interface Cache<
|
|
21
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
22
|
-
S extends string,
|
|
23
|
-
L1 extends string = never,
|
|
24
|
-
L2 extends string = never,
|
|
25
|
-
L3 extends string = never,
|
|
26
|
-
L4 extends string = never,
|
|
27
|
-
L5 extends string = never
|
|
28
|
-
> {
|
|
29
|
-
|
|
30
|
-
all: (
|
|
31
|
-
query?: ItemQuery,
|
|
32
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
33
|
-
) =>
|
|
34
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>,
|
|
35
|
-
|
|
36
|
-
one: (
|
|
37
|
-
query?: ItemQuery,
|
|
38
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
39
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>
|
|
40
|
-
|
|
41
|
-
action: (
|
|
42
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
43
|
-
action: string,
|
|
44
|
-
body?: any,
|
|
45
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>
|
|
46
|
-
|
|
47
|
-
allAction: (
|
|
48
|
-
action: string,
|
|
49
|
-
body?: any,
|
|
50
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
51
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>
|
|
52
|
-
|
|
53
|
-
allFacet: (
|
|
54
|
-
facet: string,
|
|
55
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
56
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
57
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
|
|
58
|
-
|
|
59
|
-
create: (
|
|
60
|
-
item: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
61
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
62
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
63
|
-
|
|
64
|
-
get: (
|
|
65
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
66
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
|
|
67
|
-
|
|
68
|
-
retrieve: (
|
|
69
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
70
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
|
|
71
|
-
|
|
72
|
-
remove: (
|
|
73
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
74
|
-
) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
|
|
75
|
-
|
|
76
|
-
update: (
|
|
77
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
78
|
-
item: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
79
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
80
|
-
|
|
81
|
-
facet: (
|
|
82
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
83
|
-
facet: string,
|
|
84
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
85
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
|
|
86
|
-
|
|
87
|
-
find: (
|
|
88
|
-
finder: string,
|
|
89
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
90
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
91
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
|
|
92
|
-
|
|
93
|
-
findOne: (
|
|
94
|
-
finder: string,
|
|
95
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
96
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
97
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
98
|
-
|
|
99
|
-
set: (
|
|
100
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
101
|
-
item: Item<S, L1, L2, L3, L4, L5>
|
|
102
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
103
|
-
|
|
104
|
-
reset: () => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
|
|
105
|
-
|
|
106
|
-
pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
107
|
-
|
|
108
|
-
cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export const createCache = async <
|
|
112
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
113
|
-
S extends string,
|
|
114
|
-
L1 extends string = never,
|
|
115
|
-
L2 extends string = never,
|
|
116
|
-
L3 extends string = never,
|
|
117
|
-
L4 extends string = never,
|
|
118
|
-
L5 extends string = never
|
|
119
|
-
>(
|
|
120
|
-
api: ClientApi<V, S, L1, L2, L3, L4, L5>,
|
|
121
|
-
pkType: S,
|
|
122
|
-
parentCache?: Cache<Item<L1, L2, L3, L4, L5>, L1, L2, L3, L4, L5>
|
|
123
|
-
): Promise<Cache<V, S, L1, L2, L3, L4, L5>> => {
|
|
124
|
-
|
|
125
|
-
let pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5> = [pkType];
|
|
126
|
-
if (parentCache) {
|
|
127
|
-
pkTypes = pkTypes.concat(parentCache.pkTypes as any) as unknown as AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
let cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5> =
|
|
131
|
-
new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes as AllItemTypeArrays<S, L1, L2, L3, L4, L5>);
|
|
132
|
-
|
|
133
|
-
const all = async (
|
|
134
|
-
query: ItemQuery = {},
|
|
135
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
136
|
-
):
|
|
137
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
138
|
-
logger.default('all', { query, locations });
|
|
139
|
-
let ret: V[] = [];
|
|
140
|
-
try {
|
|
141
|
-
ret = await api.all(query, locations);
|
|
142
|
-
ret.forEach((v) => {
|
|
143
|
-
cacheMap.set(v.key, v);
|
|
144
|
-
});
|
|
145
|
-
} catch (e: unknown) {
|
|
146
|
-
if (e instanceof NotFoundError) {
|
|
147
|
-
} else {
|
|
148
|
-
throw e;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const one = async (
|
|
156
|
-
query: ItemQuery = {},
|
|
157
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
158
|
-
):
|
|
159
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
160
|
-
logger.default('one', { query, locations });
|
|
161
|
-
|
|
162
|
-
let retItem: V | null = null;
|
|
163
|
-
try {
|
|
164
|
-
retItem = await api.one(query, locations);
|
|
165
|
-
if (retItem) {
|
|
166
|
-
cacheMap.set(retItem.key, retItem);
|
|
167
|
-
}
|
|
168
|
-
} catch (e: unknown) {
|
|
169
|
-
if (e instanceof NotFoundError) {
|
|
170
|
-
} else {
|
|
171
|
-
throw e;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
return [
|
|
176
|
-
cacheMap,
|
|
177
|
-
retItem ?
|
|
178
|
-
validatePK(retItem, pkType) as V :
|
|
179
|
-
null
|
|
180
|
-
];
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const action = async (
|
|
184
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
185
|
-
action: string,
|
|
186
|
-
body: any = {},
|
|
187
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
188
|
-
logger.default('action', { key, action, body });
|
|
189
|
-
|
|
190
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
191
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
192
|
-
if (!isValidItemKey(key)) {
|
|
193
|
-
logger.error('Key for Action is not a valid ItemKey: %j', key);
|
|
194
|
-
throw new Error('Key for Action is not a valid ItemKey');
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const updated = await api.action(key, action, body);
|
|
198
|
-
cacheMap.set(updated.key, updated);
|
|
199
|
-
return [cacheMap, validatePK(updated, pkType) as V];
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const allAction = async (
|
|
203
|
-
action: string,
|
|
204
|
-
body: any = {},
|
|
205
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
206
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
207
|
-
logger.default('allAction', { action, body, locations });
|
|
208
|
-
let ret: V[] = [];
|
|
209
|
-
try {
|
|
210
|
-
ret = await api.allAction(action, body, locations);
|
|
211
|
-
ret.forEach((v) => {
|
|
212
|
-
cacheMap.set(v.key, v);
|
|
213
|
-
});
|
|
214
|
-
} catch (e: unknown) {
|
|
215
|
-
// istanbul ignore next
|
|
216
|
-
if (e instanceof NotFoundError) {
|
|
217
|
-
} else {
|
|
218
|
-
throw e;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const allFacet = async (
|
|
226
|
-
facet: string,
|
|
227
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
228
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
229
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
230
|
-
logger.default('allFacet', { facet, params, locations });
|
|
231
|
-
const ret = await api.allFacet(facet, params, locations);
|
|
232
|
-
return [cacheMap, ret];
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const create = async (
|
|
236
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
237
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
238
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
239
|
-
logger.default('create', { v, locations });
|
|
240
|
-
const created = await api.create(v, locations);
|
|
241
|
-
cacheMap.set(created.key, created);
|
|
242
|
-
return [cacheMap, validatePK(created, pkType) as V];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const get = async (
|
|
246
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
247
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
248
|
-
logger.default('get', { key });
|
|
249
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
250
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
251
|
-
if (!isValidItemKey(key)) {
|
|
252
|
-
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
253
|
-
throw new Error('Key for Get is not a valid ItemKey');
|
|
254
|
-
}
|
|
255
|
-
let ret: V | null;
|
|
256
|
-
try {
|
|
257
|
-
ret = await api.get(key);
|
|
258
|
-
if (ret) {
|
|
259
|
-
cacheMap.set(ret.key, ret);
|
|
260
|
-
}
|
|
261
|
-
} catch (e: any) {
|
|
262
|
-
logger.error("Error getting item for key", { key, message: e.message, stack: e.stack });
|
|
263
|
-
throw e;
|
|
264
|
-
}
|
|
265
|
-
return [
|
|
266
|
-
cacheMap,
|
|
267
|
-
ret ?
|
|
268
|
-
validatePK(ret, pkType) as V :
|
|
269
|
-
null
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const retrieve = async (
|
|
274
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
275
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
|
|
276
|
-
logger.default('retrieve', { key });
|
|
277
|
-
if (!isValidItemKey(key)) {
|
|
278
|
-
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
279
|
-
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
280
|
-
}
|
|
281
|
-
const containsItemKey = cacheMap.includesKey(key);
|
|
282
|
-
|
|
283
|
-
let retrieved: V | null;
|
|
284
|
-
if (containsItemKey) {
|
|
285
|
-
logger.default('Looking for Object in Cache', key);
|
|
286
|
-
retrieved = cacheMap.get(key);
|
|
287
|
-
} else {
|
|
288
|
-
logger.default('Object Not Found in Cache, Retrieving from Server API', { key });
|
|
289
|
-
[, retrieved] = await get(key);
|
|
290
|
-
}
|
|
291
|
-
const retValue: [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null] = [
|
|
292
|
-
containsItemKey ? null : cacheMap,
|
|
293
|
-
retrieved ?
|
|
294
|
-
validatePK(retrieved, pkType) as V :
|
|
295
|
-
null
|
|
296
|
-
];
|
|
297
|
-
// logger.debug('Returning from retrieve', { retValue });
|
|
298
|
-
return retValue;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const remove = async (
|
|
302
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
303
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
|
|
304
|
-
logger.default('remove', { key });
|
|
305
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
306
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
307
|
-
if (!isValidItemKey(key)) {
|
|
308
|
-
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
309
|
-
throw new Error('Key for Remove is not a valid ItemKey');
|
|
310
|
-
}
|
|
311
|
-
try {
|
|
312
|
-
await api.remove(key);
|
|
313
|
-
cacheMap.delete(key);
|
|
314
|
-
} catch (e) {
|
|
315
|
-
logger.error("Error deleting item", { error: e });
|
|
316
|
-
throw e;
|
|
317
|
-
}
|
|
318
|
-
return cacheMap;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const update = async (
|
|
322
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
323
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
324
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
325
|
-
logger.default('update', { key, v });
|
|
326
|
-
|
|
327
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
328
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
329
|
-
if (!isValidItemKey(key)) {
|
|
330
|
-
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
331
|
-
throw new Error('Key for Update is not a valid ItemKey');
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
try {
|
|
335
|
-
const updated = await api.update(key, v);
|
|
336
|
-
cacheMap.set(updated.key, updated);
|
|
337
|
-
return [cacheMap, validatePK(updated, pkType) as V];
|
|
338
|
-
} catch (e) {
|
|
339
|
-
logger.error("Error updating chat", { error: e });
|
|
340
|
-
throw e;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Facets are a pass-thru for caches
|
|
345
|
-
const facet = async (
|
|
346
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
347
|
-
facet: string,
|
|
348
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
349
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
350
|
-
logger.default('facet', { key, facet });
|
|
351
|
-
const ret = await api.facet(key, facet, params);
|
|
352
|
-
return [cacheMap, ret];
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
const find = async (
|
|
356
|
-
finder: string,
|
|
357
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
358
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
359
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
360
|
-
logger.default('find', { finder, params, locations });
|
|
361
|
-
const ret: V[] = await api.find(finder, params, locations);
|
|
362
|
-
ret.forEach((v) => {
|
|
363
|
-
cacheMap.set(v.key, v);
|
|
364
|
-
});
|
|
365
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const findOne = async (
|
|
369
|
-
finder: string,
|
|
370
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
371
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
372
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
373
|
-
logger.default('findOne', { finder, finderParams, locations });
|
|
374
|
-
const ret = await api.findOne(finder, finderParams, locations);
|
|
375
|
-
cacheMap.set(ret.key, ret);
|
|
376
|
-
return [cacheMap, validatePK(ret, pkType) as V];
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
|
|
380
|
-
cacheMap = new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes);
|
|
381
|
-
return [cacheMap];
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
const set = async (
|
|
385
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
386
|
-
v: Item<S, L1, L2, L3, L4, L5>
|
|
387
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
388
|
-
logger.default('set', { key, v });
|
|
389
|
-
|
|
390
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
391
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
392
|
-
if (!isValidItemKey(key)) {
|
|
393
|
-
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
394
|
-
throw new Error('Key for Update is not a valid ItemKey');
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// TODO: This could be merged with the isValidItemKey check, later.
|
|
398
|
-
validatePK(v, pkType);
|
|
399
|
-
|
|
400
|
-
if (!isItemKeyEqual(key, v.key)) {
|
|
401
|
-
logger.error('Key does not match item key: %j != %j', key, v.key);
|
|
402
|
-
throw new Error('Key does not match item key');
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
cacheMap.set(key, v as V);
|
|
406
|
-
return [cacheMap, validatePK(v, pkType) as V];
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
return {
|
|
410
|
-
all,
|
|
411
|
-
one,
|
|
412
|
-
action,
|
|
413
|
-
allAction,
|
|
414
|
-
allFacet,
|
|
415
|
-
create,
|
|
416
|
-
get,
|
|
417
|
-
retrieve,
|
|
418
|
-
remove,
|
|
419
|
-
update,
|
|
420
|
-
facet,
|
|
421
|
-
find,
|
|
422
|
-
findOne,
|
|
423
|
-
reset,
|
|
424
|
-
set,
|
|
425
|
-
pkTypes,
|
|
426
|
-
cacheMap
|
|
427
|
-
}
|
|
428
|
-
}
|
package/src/CacheMap.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AllItemTypeArrays,
|
|
3
|
-
ComKey,
|
|
4
|
-
Dictionary,
|
|
5
|
-
isComKey,
|
|
6
|
-
isQueryMatch,
|
|
7
|
-
Item,
|
|
8
|
-
ItemQuery,
|
|
9
|
-
LocKeyArray,
|
|
10
|
-
PriKey
|
|
11
|
-
} from "@fjell/core";
|
|
12
|
-
import LibLogger from "./logger";
|
|
13
|
-
|
|
14
|
-
const logger = LibLogger.get("CacheMap");
|
|
15
|
-
|
|
16
|
-
// const isObj = (x: any) => typeof x === "object" && x !== null;
|
|
17
|
-
|
|
18
|
-
// const intersection = (a: object, b: object): object => {
|
|
19
|
-
// const result: { [key: string]: any } = {}
|
|
20
|
-
|
|
21
|
-
// if (([a, b]).every(isObj)) {
|
|
22
|
-
// Object.keys(a).forEach((key) => {
|
|
23
|
-
// // @ts-ignore
|
|
24
|
-
// const value = a[key]
|
|
25
|
-
// // @ts-ignore
|
|
26
|
-
// const other = b[key]
|
|
27
|
-
|
|
28
|
-
// if (isObj(value)) {
|
|
29
|
-
// result[key] = intersection(value, other)
|
|
30
|
-
// } else if (value === other) {
|
|
31
|
-
// result[key] = value
|
|
32
|
-
// }
|
|
33
|
-
// })
|
|
34
|
-
// }
|
|
35
|
-
|
|
36
|
-
// return result
|
|
37
|
-
// }
|
|
38
|
-
|
|
39
|
-
// const removeEmptyObjects = (obj: object): object => {
|
|
40
|
-
// const result: { [key: string]: any } = {}
|
|
41
|
-
|
|
42
|
-
// Object.keys(obj).forEach((key) => {
|
|
43
|
-
// // @ts-ignore
|
|
44
|
-
// const value = obj[key];
|
|
45
|
-
|
|
46
|
-
// if (isObj(value)) {
|
|
47
|
-
// const nested = removeEmptyObjects(value);
|
|
48
|
-
|
|
49
|
-
// if (Object.keys(nested).length > 0) {
|
|
50
|
-
// result[key] = nested
|
|
51
|
-
// }
|
|
52
|
-
// } else if (value !== null) {
|
|
53
|
-
// result[key] = value
|
|
54
|
-
// }
|
|
55
|
-
// });
|
|
56
|
-
|
|
57
|
-
// return result;
|
|
58
|
-
// }
|
|
59
|
-
|
|
60
|
-
export class CacheMap<
|
|
61
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
62
|
-
S extends string,
|
|
63
|
-
L1 extends string = never,
|
|
64
|
-
L2 extends string = never,
|
|
65
|
-
L3 extends string = never,
|
|
66
|
-
L4 extends string = never,
|
|
67
|
-
L5 extends string = never
|
|
68
|
-
> extends Dictionary<ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, V> {
|
|
69
|
-
|
|
70
|
-
private types: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
71
|
-
|
|
72
|
-
public constructor(
|
|
73
|
-
types: AllItemTypeArrays<S, L1, L2, L3, L4, L5>,
|
|
74
|
-
map?: { [key: string]: V },
|
|
75
|
-
) {
|
|
76
|
-
super(map);
|
|
77
|
-
this.types = types;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public get(
|
|
81
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
82
|
-
): V | null {
|
|
83
|
-
return super.get(key) as V | null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public allIn(
|
|
87
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
88
|
-
): V[] {
|
|
89
|
-
if (locations.length === 0) {
|
|
90
|
-
logger.debug('Returning all items, LocKeys is empty');
|
|
91
|
-
return this.values();
|
|
92
|
-
} else {
|
|
93
|
-
const locKeys: LocKeyArray<L1, L2, L3, L4, L5> | [] = locations;
|
|
94
|
-
logger.debug('allIn', { locKeys, keys: this.keys().length });
|
|
95
|
-
return this.keys()
|
|
96
|
-
.filter((key) => key && isComKey(key))
|
|
97
|
-
.filter((key) => {
|
|
98
|
-
const ComKey = key as ComKey<S, L1, L2, L3, L4, L5>;
|
|
99
|
-
logger.debug('Comparing Location Keys', {
|
|
100
|
-
locKeys,
|
|
101
|
-
ComKey,
|
|
102
|
-
});
|
|
103
|
-
return JSON.stringify(locKeys) === JSON.stringify(ComKey.loc);
|
|
104
|
-
})
|
|
105
|
-
.map((key) => this.get(key) as V);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// TODO: Can we do case insensitive matching?
|
|
110
|
-
public contains(query: ItemQuery, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): boolean {
|
|
111
|
-
logger.debug('contains', { query, locations });
|
|
112
|
-
const items = this.allIn(locations);
|
|
113
|
-
|
|
114
|
-
return items.some((item) => isQueryMatch(item, query));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public queryIn(
|
|
118
|
-
query: ItemQuery,
|
|
119
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
120
|
-
): V[] {
|
|
121
|
-
logger.debug('queryIn', { query, locations });
|
|
122
|
-
const items = this.allIn(locations);
|
|
123
|
-
|
|
124
|
-
return items.filter((item) => isQueryMatch(item, query));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public clone(): CacheMap<V, S, L1, L2, L3, L4, L5> {
|
|
128
|
-
const clone = new CacheMap<V, S, L1, L2, L3, L4, L5>(this.types, this.map);
|
|
129
|
-
return clone;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
};
|
package/src/CacheRegistry.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Item } from "@fjell/core";
|
|
2
|
-
import { Cache } from "./Cache";
|
|
3
|
-
import LibLogger from './logger';
|
|
4
|
-
|
|
5
|
-
const logger = LibLogger.get('CacheRegistry');
|
|
6
|
-
|
|
7
|
-
export class CacheRegistry {
|
|
8
|
-
|
|
9
|
-
private static instance: CacheRegistry;
|
|
10
|
-
|
|
11
|
-
public constructor() {
|
|
12
|
-
logger.debug('CacheRegistry instance created');
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// TODO: My use of Generics has Boxed me into a corner where I can't reference AbstractCache without the types
|
|
16
|
-
private cacheMap: { [kt: string]: any } = {};
|
|
17
|
-
|
|
18
|
-
public registerCache = async <
|
|
19
|
-
S extends string,
|
|
20
|
-
L1 extends string = never,
|
|
21
|
-
L2 extends string = never,
|
|
22
|
-
L3 extends string = never,
|
|
23
|
-
L4 extends string = never,
|
|
24
|
-
L5 extends string = never
|
|
25
|
-
>(cache: Cache<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>): Promise<void> => {
|
|
26
|
-
try {
|
|
27
|
-
logger.debug('Attempting to register cache with pkTypes:', cache.pkTypes);
|
|
28
|
-
const key = JSON.stringify(cache.pkTypes);
|
|
29
|
-
if (this.cacheMap[key]) {
|
|
30
|
-
logger.debug(`Cache with pkTypes ${key} already exists, will be overwritten`);
|
|
31
|
-
}
|
|
32
|
-
this.cacheMap[key] = cache;
|
|
33
|
-
logger.debug('Cache registered successfully with key:', key);
|
|
34
|
-
} catch (error) {
|
|
35
|
-
logger.error('Failed to register cache:', error);
|
|
36
|
-
throw error;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
public getCache = (kts: string[]): any => {
|
|
41
|
-
logger.debug('Attempting to get cache for key types:', kts);
|
|
42
|
-
|
|
43
|
-
const key = JSON.stringify(kts);
|
|
44
|
-
logger.debug('Looking up cache with key:', key);
|
|
45
|
-
|
|
46
|
-
const cache = this.cacheMap[key];
|
|
47
|
-
if (!cache) {
|
|
48
|
-
logger.warning(`No cache found for key types: ${key}`);
|
|
49
|
-
}
|
|
50
|
-
return cache;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
public printRegisteredCaches = (): void => {
|
|
54
|
-
logger.debug('Printing all registered caches:');
|
|
55
|
-
const cacheCount = Object.keys(this.cacheMap).length;
|
|
56
|
-
logger.debug(`Total number of registered caches: ${cacheCount}`);
|
|
57
|
-
|
|
58
|
-
if (cacheCount === 0) {
|
|
59
|
-
logger.debug('No caches are currently registered');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
Object.entries(this.cacheMap).forEach(([keyTypes]) => {
|
|
63
|
-
logger.debug(`Cache with key types: ${keyTypes}`);
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
}
|
package/src/index.ts
DELETED