@fjell/cache 4.4.3 → 4.5.1

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.
@@ -0,0 +1,289 @@
1
+ /* eslint-disable no-undefined */
2
+ import {
3
+ ComKey,
4
+ Item,
5
+ ItemQuery,
6
+ LocKeyArray,
7
+ PriKey,
8
+ TypesProperties
9
+ } from "@fjell/core";
10
+ import { Cache } from "./Cache";
11
+ import { CacheMap } from "./CacheMap";
12
+ import LibLogger from "./logger";
13
+
14
+ const logger = LibLogger.get('ItemAggregator');
15
+
16
+ export interface Aggregator<
17
+ V extends Item<S, L1, L2, L3, L4, L5>,
18
+ S extends string,
19
+ L1 extends string = never,
20
+ L2 extends string = never,
21
+ L3 extends string = never,
22
+ L4 extends string = never,
23
+ L5 extends string = never
24
+ > extends Cache<V, S, L1, L2, L3, L4, L5> {
25
+ populate: (item: V) => Promise<V>;
26
+ populateAggregate: (key: string, item: V) => Promise<void>;
27
+ populateEvent: (key: string, item: V) => Promise<void>;
28
+ }
29
+
30
+ export interface CacheConfig { cache: any, optional: boolean }
31
+
32
+ export interface AggregateConfig { [key: string]: (CacheConfig) }
33
+
34
+ export const toCacheConfig = <
35
+ V extends Item<S, L1, L2, L3, L4, L5>,
36
+ S extends string,
37
+ L1 extends string = never,
38
+ L2 extends string = never,
39
+ L3 extends string = never,
40
+ L4 extends string = never,
41
+ L5 extends string = never
42
+ >(config: CacheConfig | Cache<V, S, L1, L2, L3, L4, L5>): CacheConfig => {
43
+ let cacheConfig: CacheConfig;
44
+ if ((config as CacheConfig).optional === undefined) {
45
+ cacheConfig = { cache: config as any, optional: false };
46
+ } else {
47
+ cacheConfig = config as CacheConfig;
48
+ }
49
+ return cacheConfig;
50
+ }
51
+
52
+ export const createAggregator = <
53
+ V extends Item<S, L1, L2, L3, L4, L5>,
54
+ S extends string,
55
+ L1 extends string = never,
56
+ L2 extends string = never,
57
+ L3 extends string = never,
58
+ L4 extends string = never,
59
+ L5 extends string = never
60
+ >(
61
+ cache: Cache<V, S, L1, L2, L3, L4, L5>,
62
+ { aggregates = {}, events = {} }:
63
+ {
64
+ aggregates?: AggregateConfig,
65
+ events?: AggregateConfig
66
+ }
67
+ ): Aggregator<V, S, L1, L2, L3, L4, L5> => {
68
+
69
+ const populate = async (item: V): Promise<V> => {
70
+ logger.default('populate', { item });
71
+ for (const key in aggregates) {
72
+ await populateAggregate(key, item);
73
+ }
74
+ for (const key in events) {
75
+ await populateEvent(key, item);
76
+ }
77
+ logger.default('populate done', { item });
78
+ return item;
79
+ }
80
+
81
+ const populateAggregate = async (key: string, item: V) => {
82
+ logger.default('populate aggregate key', { key });
83
+ const cacheConfig = toCacheConfig(aggregates[key]);
84
+ if (item.refs === undefined) {
85
+ if (cacheConfig.optional === false) {
86
+ logger.error('Item does not have refs an is not optional ' + JSON.stringify(item));
87
+ throw new Error('Item does not have refs an is not optional ' + JSON.stringify(item));
88
+ }
89
+ } else if (item.refs[key] === undefined) {
90
+ if (cacheConfig.optional === false) {
91
+ logger.error('Item does not have mandatory ref with key, not optional ' +
92
+ key + ' ' + JSON.stringify(item));
93
+ throw new Error('Item does not have mandatory ref with key, not optional ' +
94
+ key + ' ' + JSON.stringify(item));
95
+ }
96
+ } else {
97
+
98
+ const ref = item.refs[key];
99
+
100
+ logger.default('AGG Retrieving Item in Populate', { key: ref });
101
+ const [, newItem] = await cacheConfig.cache.retrieve(ref);
102
+ if (newItem) {
103
+ if (item.aggs === undefined) {
104
+ item.aggs = {};
105
+ }
106
+ item.aggs[key] = {
107
+ key: ref,
108
+ item: newItem as Item,
109
+ };
110
+ }
111
+ }
112
+ }
113
+
114
+ // TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
115
+ const populateEvent = async (key: string, item: V) => {
116
+ logger.default('populate event key', { key });
117
+ const cacheConfig = toCacheConfig(events[key]);
118
+
119
+ if (item.events === undefined) {
120
+ throw new Error('Item does not have events ' + JSON.stringify(item));
121
+ } else if (item.events[key] === undefined) {
122
+ if (cacheConfig.optional === false) {
123
+ logger.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
124
+ throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
125
+ }
126
+ } else {
127
+ const event = item.events[key];
128
+
129
+ if (event.by === undefined) {
130
+ logger.error(
131
+ 'populateEvent with an Event that does not have by', { event, ik: item.key, eventKey: key });
132
+ throw new Error('populateEvent with an Event that does not have by: ' + JSON.stringify({ key, event }));
133
+ }
134
+
135
+ logger.default('EVENT Retrieving Item in Populate', { key: event.by });
136
+ const [, newItem] = await cacheConfig.cache.retrieve(event.by);
137
+ if (newItem) {
138
+ event.agg = newItem as Item;
139
+ }
140
+ }
141
+ }
142
+
143
+ const all = async (
144
+ query: ItemQuery = {},
145
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
146
+ ):
147
+ Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
148
+ logger.default('all', { query, locations });
149
+ const [cacheMap, items] = await cache.all(query, locations);
150
+ const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
151
+ return [cacheMap, populatedItems];
152
+ }
153
+
154
+ const one = async (
155
+ query: ItemQuery = {},
156
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
157
+ ):
158
+ Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
159
+ logger.default('one', { query, locations });
160
+ const [cacheMap, item] = await cache.one(query, locations);
161
+ let populatedItem = null;
162
+ if (item) {
163
+ populatedItem = await populate(item);
164
+ }
165
+ return [cacheMap, populatedItem];
166
+ }
167
+
168
+ const action = async (
169
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
170
+ action: string,
171
+ body: any = {},
172
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
173
+ logger.default('action', { key, action, body });
174
+ const [cacheMap, item] = await cache.action(key, action, body);
175
+ const populatedItem = await populate(item);
176
+ return [cacheMap, populatedItem];
177
+ }
178
+
179
+ const allAction = async (
180
+ action: string,
181
+ body: any = {},
182
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
183
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
184
+ logger.default('action', { action, body, locations });
185
+ const [cacheMap, items] = await cache.allAction(action, body, locations);
186
+ const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
187
+ return [cacheMap, populatedItems];
188
+ }
189
+
190
+ const create = async (
191
+ v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
192
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
193
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
194
+ logger.default('create', { v, locations });
195
+ const [cacheMap, item] = await cache.create(v, locations);
196
+ const populatedItem = await populate(item);
197
+ return [cacheMap, populatedItem];
198
+ }
199
+
200
+ const get = async (
201
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
202
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
203
+ logger.default('get', { key });
204
+ const [cacheMap, item] = await cache.get(key);
205
+ let populatedItem = null;
206
+ if (item) {
207
+ populatedItem = await populate(item);
208
+ }
209
+ return [cacheMap, populatedItem];
210
+ }
211
+
212
+ const retrieve = async (
213
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
214
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
215
+ logger.default('retrieve', { key });
216
+ const [cacheMap, item] = await cache.retrieve(key);
217
+ let populatedItem = null;
218
+ if (item) {
219
+ populatedItem = await populate(item);
220
+ }
221
+ return [cacheMap, populatedItem];
222
+ }
223
+
224
+ const remove = async (
225
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
226
+ ): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
227
+ logger.default('remove', { key });
228
+ const cacheMap = await cache.remove(key);
229
+ return cacheMap;
230
+ }
231
+
232
+ const update = async (
233
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
234
+ v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
235
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
236
+ logger.default('update', { key, v });
237
+ const [cacheMap, item] = await cache.update(key, v);
238
+ const populatedItem = await populate(item);
239
+ return [cacheMap, populatedItem];
240
+ }
241
+
242
+ const find = async (
243
+ finder: string,
244
+ finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
245
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
246
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
247
+ logger.default('find', { finder, finderParams, locations });
248
+ const [cacheMap, items] = await cache.find(finder, finderParams, locations);
249
+ const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
250
+ return [cacheMap, populatedItems];
251
+ }
252
+
253
+ const set = async (
254
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
255
+ v: V
256
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
257
+ logger.default('set', { key, v });
258
+
259
+ // TODO: There should be some input validation here to ensure a valid item.
260
+ const [cacheMap, item] = await cache.set(key, v);
261
+ const populatedItem = await populate(item);
262
+ return [cacheMap, populatedItem];
263
+ }
264
+
265
+ const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
266
+ const cacheMap = await cache.reset();
267
+ return cacheMap;
268
+ }
269
+
270
+ return {
271
+ all,
272
+ one,
273
+ action,
274
+ allAction,
275
+ create,
276
+ get,
277
+ retrieve,
278
+ remove,
279
+ update,
280
+ find,
281
+ reset,
282
+ set,
283
+ pkTypes: cache.pkTypes,
284
+ cacheMap: cache.cacheMap,
285
+ populate,
286
+ populateAggregate,
287
+ populateEvent
288
+ }
289
+ }
package/src/Cache.ts CHANGED
@@ -1,13 +1,22 @@
1
- /* eslint-disable no-undefined */
2
1
  import {
2
+ AllItemTypeArrays,
3
3
  ComKey,
4
+ isItemKeyEqual,
5
+ isValidItemKey,
4
6
  Item,
5
7
  ItemQuery,
6
8
  LocKeyArray,
7
9
  PriKey,
8
- TypesProperties
10
+ TypesProperties,
11
+ validatePK
9
12
  } from "@fjell/core";
10
13
  import { CacheMap } from "./CacheMap";
14
+ import LibLogger from "./logger";
15
+
16
+ import { ClientApi } from "@fjell/client-api";
17
+ import { NotFoundError } from "@fjell/http-api";
18
+
19
+ const logger = LibLogger.get('Cache');
11
20
 
12
21
  export interface Cache<
13
22
  V extends Item<S, L1, L2, L3, L4, L5>,
@@ -70,4 +79,298 @@ export interface Cache<
70
79
  locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
71
80
  ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
72
81
 
82
+ set: (
83
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
84
+ v: V
85
+ ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
86
+
87
+ reset: () => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
88
+
89
+ pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
90
+
91
+ cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
92
+ }
93
+
94
+ export const createCache = <
95
+ V extends Item<S, L1, L2, L3, L4, L5>,
96
+ S extends string,
97
+ L1 extends string = never,
98
+ L2 extends string = never,
99
+ L3 extends string = never,
100
+ L4 extends string = never,
101
+ L5 extends string = never
102
+ >(
103
+ api: ClientApi<V, S, L1, L2, L3, L4, L5>,
104
+ pkType: S,
105
+ parentCache?: Cache<Item<L1, L2, L3, L4, L5>, L1, L2, L3, L4, L5>
106
+ ): Cache<V, S, L1, L2, L3, L4, L5> => {
107
+
108
+ let pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5> = [ pkType ];
109
+ if( parentCache ) {
110
+ pkTypes = pkTypes.concat(parentCache.pkTypes as any) as unknown as AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
111
+ }
112
+
113
+ let cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5> =
114
+ new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes as AllItemTypeArrays<S, L1, L2, L3, L4, L5>);
115
+
116
+ const all = async (
117
+ query: ItemQuery = {},
118
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
119
+ ):
120
+ Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
121
+ logger.default('all', { query, locations });
122
+ let ret: V[] = [];
123
+ try {
124
+ ret = await api.all(query, {}, locations);
125
+ ret.forEach((v) => {
126
+ cacheMap.set(v.key, v);
127
+ });
128
+ } catch (e: unknown) {
129
+ if (e instanceof NotFoundError) {
130
+ } else {
131
+ throw e;
132
+ }
133
+
134
+ }
135
+ return [cacheMap, validatePK(ret, pkType) as V[]];
136
+ }
137
+
138
+ const one = async (
139
+ query: ItemQuery = {},
140
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
141
+ ):
142
+ Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
143
+ logger.default('one', { query, locations });
144
+
145
+ let retItem: V | null = null;
146
+ try {
147
+ retItem = await api.one(query, {}, locations);
148
+ if (retItem) {
149
+ cacheMap.set(retItem.key, retItem);
150
+ }
151
+ } catch (e: unknown) {
152
+ if (e instanceof NotFoundError) {
153
+ } else {
154
+ throw e;
155
+ }
156
+
157
+ }
158
+ return [
159
+ cacheMap,
160
+ retItem ?
161
+ validatePK(retItem, pkType) as V :
162
+ null
163
+ ];
164
+ }
165
+
166
+ const action = async (
167
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
168
+ action: string,
169
+ body: any = {},
170
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
171
+ logger.default('action', { key, action, body });
172
+
173
+ // TODO: This is validating the key, but it doesn't have knowledge of the pkType
174
+ // This should be looking at the parentCaches and calculating an array of pkTypes
175
+ if (!isValidItemKey(key)) {
176
+ logger.error('Key for Action is not a valid ItemKey: %j', key);
177
+ throw new Error('Key for Action is not a valid ItemKey');
178
+ }
179
+
180
+ const updated = await api.action(key, action, body, {});
181
+ cacheMap.set(updated.key, updated);
182
+ return [cacheMap, validatePK(updated, pkType) as V];
183
+ }
184
+
185
+ const allAction = async (
186
+ action: string,
187
+ body: any = {},
188
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
189
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
190
+ logger.default('allAction', { action, body, locations });
191
+ let ret: V[] = [];
192
+ try {
193
+ ret = await api.allAction(action, body, {}, locations);
194
+ ret.forEach((v) => {
195
+ cacheMap.set(v.key, v);
196
+ });
197
+ } catch (e: unknown) {
198
+ // istanbul ignore next
199
+ if (e instanceof NotFoundError) {
200
+ } else {
201
+ throw e;
202
+ }
203
+
204
+ }
205
+ return [cacheMap, validatePK(ret, pkType) as V[]];
206
+ }
207
+
208
+ const create = async (
209
+ v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
210
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
211
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
212
+ logger.default('create', { v, locations });
213
+ const created = await api.create(v, {}, locations);
214
+ cacheMap.set(created.key, created);
215
+ return [cacheMap, validatePK(created, pkType) as V];
216
+ }
217
+
218
+ const get = async (
219
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
220
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
221
+ logger.default('get', { key });
222
+ // TODO: This is validating the key, but it doesn't have knowledge of the pkType
223
+ // This should be looking at the parentCaches and calculating an array of pkTypes
224
+ if (!isValidItemKey(key)) {
225
+ logger.error('Key for Get is not a valid ItemKey: %j', key);
226
+ throw new Error('Key for Get is not a valid ItemKey');
227
+ }
228
+ let ret: V | null;
229
+ try {
230
+ ret = await api.get(key, {});
231
+ if (ret) {
232
+ cacheMap.set(ret.key, ret);
233
+ }
234
+ } catch (e: any) {
235
+ logger.error("Error getting item for key", { key, message: e.message, stack: e.stack });
236
+ throw e;
237
+ }
238
+ return [
239
+ cacheMap,
240
+ ret ?
241
+ validatePK(ret, pkType) as V :
242
+ null
243
+ ];
244
+ }
245
+
246
+ const retrieve = async (
247
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
248
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
249
+ logger.default('retrieve', { key });
250
+ if (!isValidItemKey(key)) {
251
+ logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
252
+ throw new Error('Key for Retrieve is not a valid ItemKey');
253
+ }
254
+ const containsItemKey = cacheMap.includesKey(key);
255
+
256
+ let retrieved: V | null;
257
+ if (containsItemKey) {
258
+ logger.default('Looking for Object in Cache', key);
259
+ retrieved = cacheMap.get(key);
260
+ } else {
261
+ logger.default('Object Not Found in Cache, Retrieving from Server API', { key });
262
+ [, retrieved] = await get(key);
263
+ }
264
+ const retValue: [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null] = [
265
+ containsItemKey ? null : cacheMap,
266
+ retrieved ?
267
+ validatePK(retrieved, pkType) as V:
268
+ null
269
+ ];
270
+ // logger.debug('Returning from retrieve', { retValue });
271
+ return retValue;
272
+ }
273
+
274
+ const remove = async (
275
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
276
+ ): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
277
+ logger.default('remove', { key });
278
+ // TODO: This is validating the key, but it doesn't have knowledge of the pkType
279
+ // This should be looking at the parentCaches and calculating an array of pkTypes
280
+ if (!isValidItemKey(key)) {
281
+ logger.error('Key for Remove is not a valid ItemKey: %j', key);
282
+ throw new Error('Key for Remove is not a valid ItemKey');
283
+ }
284
+ try {
285
+ await api.remove(key, {});
286
+ cacheMap.delete(key);
287
+ } catch (e) {
288
+ logger.error("Error deleting item", { error: e });
289
+ throw e;
290
+ }
291
+ return cacheMap;
292
+ }
293
+
294
+ const update = async (
295
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
296
+ v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
297
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
298
+ logger.default('update', { key, v });
299
+
300
+ // TODO: This is validating the key, but it doesn't have knowledge of the pkType
301
+ // This should be looking at the parentCaches and calculating an array of pkTypes
302
+ if (!isValidItemKey(key)) {
303
+ logger.error('Key for Update is not a valid ItemKey: %j', key);
304
+ throw new Error('Key for Update is not a valid ItemKey');
305
+ }
306
+
307
+ try {
308
+ const updated = await api.update(key, v, {});
309
+ cacheMap.set(updated.key, updated);
310
+ return [cacheMap, validatePK(updated, pkType) as V];
311
+ } catch (e) {
312
+ logger.error("Error updating chat", { error: e });
313
+ throw e;
314
+ }
315
+ }
316
+
317
+ const find = async (
318
+ finder: string,
319
+ finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
320
+ locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
321
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
322
+ logger.default('find', { finder, finderParams, locations });
323
+ const ret: V[] = await api.find(finder, finderParams, {}, locations);
324
+ ret.forEach((v) => {
325
+ cacheMap.set(v.key, v);
326
+ });
327
+ return [cacheMap, validatePK(ret, pkType) as V[]];
328
+ }
329
+
330
+ const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
331
+ cacheMap = new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes);
332
+ return [cacheMap];
333
+ }
334
+
335
+ const set = async (
336
+ key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
337
+ v: V
338
+ ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
339
+ logger.default('set', { key, v });
340
+
341
+ // TODO: This is validating the key, but it doesn't have knowledge of the pkType
342
+ // This should be looking at the parentCaches and calculating an array of pkTypes
343
+ if (!isValidItemKey(key)) {
344
+ logger.error('Key for Update is not a valid ItemKey: %j', key);
345
+ throw new Error('Key for Update is not a valid ItemKey');
346
+ }
347
+
348
+ // TODO: This could be merged with the isValidItemKey check, later.
349
+ validatePK(v, pkType);
350
+
351
+ if (!isItemKeyEqual(key, v.key)) {
352
+ logger.error('Key does not match item key: %j != %j', key, v.key);
353
+ throw new Error('Key does not match item key');
354
+ }
355
+
356
+ cacheMap.set(key, v);
357
+ return [cacheMap, validatePK(v, pkType) as V];
358
+ }
359
+
360
+ return {
361
+ all,
362
+ one,
363
+ action,
364
+ allAction,
365
+ create,
366
+ get,
367
+ retrieve,
368
+ remove,
369
+ update,
370
+ find,
371
+ reset,
372
+ set,
373
+ pkTypes,
374
+ cacheMap
375
+ }
73
376
  }
@@ -1,7 +1,5 @@
1
1
  import { Item } from "@fjell/core";
2
- import { PItemCache } from "./PItemCache";
3
- import { CItemCache } from "./CItemCache";
4
-
2
+ import { Cache } from "./Cache";
5
3
  import LibLogger from './logger';
6
4
 
7
5
  const logger = LibLogger.get('CacheRegistry');
@@ -33,9 +31,8 @@ export class CacheRegistry {
33
31
  L3 extends string = never,
34
32
  L4 extends string = never,
35
33
  L5 extends string = never
36
- >(cache: PItemCache<Item<S>, S> |
37
- CItemCache<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>): void => {
38
- this.cacheMap[JSON.stringify(cache.getKeyTypes())] = cache;
34
+ >(cache: Cache<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>): void => {
35
+ this.cacheMap[JSON.stringify(cache.pkTypes)] = cache;
39
36
  };
40
37
 
41
38
  public isConfigured = (): boolean => {
@@ -1,35 +0,0 @@
1
- import { ComKey, Item, ItemQuery, LocKeyArray, PriKey, TypesProperties } from "@fjell/core";
2
- import { AItemCache } from "./AItemCache";
3
- import { Cache } from "./Cache";
4
- import { CacheMap } from "./CacheMap";
5
- export interface CacheConfig {
6
- cache: any;
7
- optional: boolean;
8
- }
9
- export interface AggregateConfig {
10
- [key: string]: (CacheConfig);
11
- }
12
- export declare const toCacheConfig: <V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(config: CacheConfig | Cache<V, S, L1, L2, L3, L4, L5>) => CacheConfig;
13
- export declare class AItemAggregator<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> implements Cache<V, S, L1, L2, L3, L4, L5> {
14
- private cache;
15
- private logger;
16
- private aggregates;
17
- private events;
18
- constructor(cache: AItemCache<V, S, L1, L2, L3, L4, L5>, { aggregates, events }: {
19
- aggregates?: AggregateConfig;
20
- events?: AggregateConfig;
21
- });
22
- private populate;
23
- private populateAggregate;
24
- private populateEvent;
25
- all(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
26
- one(query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
27
- action(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, action: string, body?: any): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
28
- allAction(action: string, body?: any, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
29
- create(v: TypesProperties<V, S, L1, L2, L3, L4, L5>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
30
- get(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
31
- retrieve(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
32
- remove(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
33
- update(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, v: TypesProperties<V, S, L1, L2, L3, L4, L5>): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
34
- find(finder: string, finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
35
- }