@fjell/cache 4.6.7 → 4.6.11

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.
Files changed (77) hide show
  1. package/dist/Aggregator.cjs.js +26 -20
  2. package/dist/Aggregator.d.ts +17 -1
  3. package/dist/Aggregator.es.js +26 -20
  4. package/dist/Cache.cjs.js +22 -345
  5. package/dist/Cache.d.ts +25 -20
  6. package/dist/Cache.es.js +22 -346
  7. package/dist/Instance.cjs.js +23 -0
  8. package/dist/Instance.d.ts +17 -0
  9. package/dist/Instance.es.js +18 -0
  10. package/dist/InstanceFactory.cjs.js +35 -0
  11. package/dist/InstanceFactory.d.ts +8 -0
  12. package/dist/InstanceFactory.es.js +31 -0
  13. package/dist/Operations.cjs.js +43 -0
  14. package/dist/Operations.d.ts +70 -0
  15. package/dist/Operations.es.js +39 -0
  16. package/dist/Registry.cjs.js +36 -0
  17. package/dist/Registry.d.ts +15 -0
  18. package/dist/Registry.es.js +31 -0
  19. package/dist/index.cjs +459 -408
  20. package/dist/index.cjs.js +12 -3
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.ts +4 -1
  23. package/dist/index.es.js +6 -3
  24. package/dist/ops/action.cjs.js +28 -0
  25. package/dist/ops/action.d.ts +4 -0
  26. package/dist/ops/action.es.js +24 -0
  27. package/dist/ops/all.cjs.js +33 -0
  28. package/dist/ops/all.d.ts +4 -0
  29. package/dist/ops/all.es.js +29 -0
  30. package/dist/ops/allAction.cjs.js +35 -0
  31. package/dist/ops/allAction.d.ts +4 -0
  32. package/dist/ops/allAction.es.js +31 -0
  33. package/dist/ops/allFacet.cjs.js +22 -0
  34. package/dist/ops/allFacet.d.ts +4 -0
  35. package/dist/ops/allFacet.es.js +18 -0
  36. package/dist/ops/create.cjs.js +23 -0
  37. package/dist/ops/create.d.ts +4 -0
  38. package/dist/ops/create.es.js +19 -0
  39. package/dist/ops/facet.cjs.js +21 -0
  40. package/dist/ops/facet.d.ts +4 -0
  41. package/dist/ops/facet.es.js +17 -0
  42. package/dist/ops/find.cjs.js +26 -0
  43. package/dist/ops/find.d.ts +4 -0
  44. package/dist/ops/find.es.js +22 -0
  45. package/dist/ops/findOne.cjs.js +24 -0
  46. package/dist/ops/findOne.d.ts +4 -0
  47. package/dist/ops/findOne.es.js +20 -0
  48. package/dist/ops/get.cjs.js +38 -0
  49. package/dist/ops/get.d.ts +4 -0
  50. package/dist/ops/get.es.js +34 -0
  51. package/dist/ops/one.cjs.js +33 -0
  52. package/dist/ops/one.d.ts +4 -0
  53. package/dist/ops/one.es.js +29 -0
  54. package/dist/ops/remove.cjs.js +30 -0
  55. package/dist/ops/remove.d.ts +4 -0
  56. package/dist/ops/remove.es.js +26 -0
  57. package/dist/ops/reset.cjs.js +15 -0
  58. package/dist/ops/reset.d.ts +4 -0
  59. package/dist/ops/reset.es.js +11 -0
  60. package/dist/ops/retrieve.cjs.js +37 -0
  61. package/dist/ops/retrieve.d.ts +4 -0
  62. package/dist/ops/retrieve.es.js +33 -0
  63. package/dist/ops/set.cjs.js +71 -0
  64. package/dist/ops/set.d.ts +3 -0
  65. package/dist/ops/set.es.js +67 -0
  66. package/dist/ops/update.cjs.js +34 -0
  67. package/dist/ops/update.d.ts +4 -0
  68. package/dist/ops/update.es.js +30 -0
  69. package/examples/README.md +302 -0
  70. package/examples/aggregator-example.ts +328 -0
  71. package/examples/basic-cache-example.ts +270 -0
  72. package/examples/cache-map-example.ts +265 -0
  73. package/package.json +7 -6
  74. package/vitest.config.ts +2 -2
  75. package/dist/CacheRegistry.cjs.js +0 -66
  76. package/dist/CacheRegistry.d.ts +0 -10
  77. package/dist/CacheRegistry.es.js +0 -62
package/dist/index.cjs CHANGED
@@ -5,10 +5,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const Logging = require('@fjell/logging');
6
6
  const core = require('@fjell/core');
7
7
  const httpApi = require('@fjell/http-api');
8
+ const registry = require('@fjell/registry');
8
9
 
9
- const LibLogger = Logging.getLogger('@fjell/cache');
10
+ const logger$1.default = Logging.getLogger('@fjell/cache');
10
11
 
11
- const logger$3 = LibLogger.get('ItemAggregator');
12
+ const logger$j = logger$1.default.get('ItemAggregator');
12
13
  const toCacheConfig = (config)=>{
13
14
  let cacheConfig;
14
15
  if (config.optional === undefined) {
@@ -23,7 +24,7 @@ const toCacheConfig = (config)=>{
23
24
  };
24
25
  const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
25
26
  const populate = async (item)=>{
26
- logger$3.default('populate', {
27
+ logger$j.default('populate', {
27
28
  item
28
29
  });
29
30
  for(const key in aggregates){
@@ -32,19 +33,19 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
32
33
  for(const key in events){
33
34
  await populateEvent(key, item);
34
35
  }
35
- logger$3.default('populate done', {
36
+ logger$j.default('populate done', {
36
37
  item
37
38
  });
38
39
  return item;
39
40
  };
40
41
  const populateAggregate = async (key, item)=>{
41
- logger$3.default('populate aggregate key', {
42
+ logger$j.default('populate aggregate key', {
42
43
  key
43
44
  });
44
45
  const cacheConfig = toCacheConfig(aggregates[key]);
45
46
  if (item.refs === undefined) {
46
47
  if (cacheConfig.optional === false) {
47
- logger$3.error('Item does not have refs an is not optional ' + JSON.stringify(item));
48
+ logger$j.error('Item does not have refs an is not optional ' + JSON.stringify(item));
48
49
  throw new Error('Item does not have refs an is not optional ' + JSON.stringify(item));
49
50
  } else {
50
51
  if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
@@ -53,7 +54,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
53
54
  }
54
55
  } else if (item.refs[key] === undefined) {
55
56
  if (cacheConfig.optional === false) {
56
- logger$3.error('Item does not have mandatory ref with key, not optional ' + key + ' ' + JSON.stringify(item));
57
+ logger$j.error('Item does not have mandatory ref with key, not optional ' + key + ' ' + JSON.stringify(item));
57
58
  throw new Error('Item does not have mandatory ref with key, not optional ' + key + ' ' + JSON.stringify(item));
58
59
  } else {
59
60
  if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
@@ -62,10 +63,10 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
62
63
  }
63
64
  } else {
64
65
  const ref = item.refs[key];
65
- logger$3.default('AGG Retrieving Item in Populate', {
66
+ logger$j.default('AGG Retrieving Item in Populate', {
66
67
  key: ref
67
68
  });
68
- const [, newItem] = await cacheConfig.cache.retrieve(ref);
69
+ const [, newItem] = await cacheConfig.cache.operations.retrieve(ref);
69
70
  if (newItem) {
70
71
  if (item.aggs === undefined) {
71
72
  item.aggs = {};
@@ -79,7 +80,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
79
80
  };
80
81
  // TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
81
82
  const populateEvent = async (key, item)=>{
82
- logger$3.default('populate event key', {
83
+ logger$j.default('populate event key', {
83
84
  key
84
85
  });
85
86
  const cacheConfig = toCacheConfig(events[key]);
@@ -87,13 +88,13 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
87
88
  throw new Error('Item does not have events ' + JSON.stringify(item));
88
89
  } else if (item.events[key] === undefined) {
89
90
  if (cacheConfig.optional === false) {
90
- logger$3.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
91
+ logger$j.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
91
92
  throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
92
93
  }
93
94
  } else {
94
95
  const event = item.events[key];
95
96
  if (event.by === undefined) {
96
- logger$3.error('populateEvent with an Event that does not have by', {
97
+ logger$j.error('populateEvent with an Event that does not have by', {
97
98
  event,
98
99
  ik: item.key,
99
100
  eventKey: key
@@ -103,21 +104,21 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
103
104
  event
104
105
  }));
105
106
  }
106
- logger$3.default('EVENT Retrieving Item in Populate', {
107
+ logger$j.default('EVENT Retrieving Item in Populate', {
107
108
  key: event.by
108
109
  });
109
- const [, newItem] = await cacheConfig.cache.retrieve(event.by);
110
+ const [, newItem] = await cacheConfig.cache.operations.retrieve(event.by);
110
111
  if (newItem) {
111
112
  event.agg = newItem;
112
113
  }
113
114
  }
114
115
  };
115
116
  const all = async (query = {}, locations = [])=>{
116
- logger$3.default('all', {
117
+ logger$j.default('all', {
117
118
  query,
118
119
  locations
119
120
  });
120
- const [cacheMap, items] = await cache.all(query, locations);
121
+ const [cacheMap, items] = await cache.operations.all(query, locations);
121
122
  const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
122
123
  return [
123
124
  cacheMap,
@@ -125,11 +126,11 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
125
126
  ];
126
127
  };
127
128
  const one = async (query = {}, locations = [])=>{
128
- logger$3.default('one', {
129
+ logger$j.default('one', {
129
130
  query,
130
131
  locations
131
132
  });
132
- const [cacheMap, item] = await cache.one(query, locations);
133
+ const [cacheMap, item] = await cache.operations.one(query, locations);
133
134
  let populatedItem = null;
134
135
  if (item) {
135
136
  populatedItem = await populate(item);
@@ -140,12 +141,12 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
140
141
  ];
141
142
  };
142
143
  const action = async (key, action, body = {})=>{
143
- logger$3.default('action', {
144
+ logger$j.default('action', {
144
145
  key,
145
146
  action,
146
147
  body
147
148
  });
148
- const [cacheMap, item] = await cache.action(key, action, body);
149
+ const [cacheMap, item] = await cache.operations.action(key, action, body);
149
150
  const populatedItem = await populate(item);
150
151
  return [
151
152
  cacheMap,
@@ -153,12 +154,12 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
153
154
  ];
154
155
  };
155
156
  const allAction = async (action, body = {}, locations = [])=>{
156
- logger$3.default('action', {
157
+ logger$j.default('action', {
157
158
  action,
158
159
  body,
159
160
  locations
160
161
  });
161
- const [cacheMap, items] = await cache.allAction(action, body, locations);
162
+ const [cacheMap, items] = await cache.operations.allAction(action, body, locations);
162
163
  const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
163
164
  return [
164
165
  cacheMap,
@@ -166,23 +167,23 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
166
167
  ];
167
168
  };
168
169
  const allFacet = async (facet, params = {}, locations = [])=>{
169
- logger$3.default('allFacet', {
170
+ logger$j.default('allFacet', {
170
171
  facet,
171
172
  params,
172
173
  locations
173
174
  });
174
- const [cacheMap, response] = await cache.allFacet(facet, params, locations);
175
+ const [cacheMap, response] = await cache.operations.allFacet(facet, params, locations);
175
176
  return [
176
177
  cacheMap,
177
178
  response
178
179
  ];
179
180
  };
180
181
  const create = async (v, locations = [])=>{
181
- logger$3.default('create', {
182
+ logger$j.default('create', {
182
183
  v,
183
184
  locations
184
185
  });
185
- const [cacheMap, item] = await cache.create(v, locations);
186
+ const [cacheMap, item] = await cache.operations.create(v, locations);
186
187
  const populatedItem = await populate(item);
187
188
  return [
188
189
  cacheMap,
@@ -190,10 +191,10 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
190
191
  ];
191
192
  };
192
193
  const get = async (key)=>{
193
- logger$3.default('get', {
194
+ logger$j.default('get', {
194
195
  key
195
196
  });
196
- const [cacheMap, item] = await cache.get(key);
197
+ const [cacheMap, item] = await cache.operations.get(key);
197
198
  let populatedItem = null;
198
199
  if (item) {
199
200
  populatedItem = await populate(item);
@@ -204,10 +205,10 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
204
205
  ];
205
206
  };
206
207
  const retrieve = async (key)=>{
207
- logger$3.default('retrieve', {
208
+ logger$j.default('retrieve', {
208
209
  key
209
210
  });
210
- const [cacheMap, item] = await cache.retrieve(key);
211
+ const [cacheMap, item] = await cache.operations.retrieve(key);
211
212
  let populatedItem = null;
212
213
  if (item) {
213
214
  populatedItem = await populate(item);
@@ -218,18 +219,18 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
218
219
  ];
219
220
  };
220
221
  const remove = async (key)=>{
221
- logger$3.default('remove', {
222
+ logger$j.default('remove', {
222
223
  key
223
224
  });
224
- const cacheMap = await cache.remove(key);
225
+ const cacheMap = await cache.operations.remove(key);
225
226
  return cacheMap;
226
227
  };
227
228
  const update = async (key, v)=>{
228
- logger$3.default('update', {
229
+ logger$j.default('update', {
229
230
  key,
230
231
  v
231
232
  });
232
- const [cacheMap, item] = await cache.update(key, v);
233
+ const [cacheMap, item] = await cache.operations.update(key, v);
233
234
  const populatedItem = await populate(item);
234
235
  return [
235
236
  cacheMap,
@@ -238,23 +239,23 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
238
239
  };
239
240
  // Facets are a pass-thru for aggregators
240
241
  const facet = async (key, facet)=>{
241
- logger$3.default('facet', {
242
+ logger$j.default('facet', {
242
243
  key,
243
244
  facet
244
245
  });
245
- const [cacheMap, response] = await cache.facet(key, facet);
246
+ const [cacheMap, response] = await cache.operations.facet(key, facet);
246
247
  return [
247
248
  cacheMap,
248
249
  response
249
250
  ];
250
251
  };
251
252
  const find = async (finder, finderParams = {}, locations = [])=>{
252
- logger$3.default('find', {
253
+ logger$j.default('find', {
253
254
  finder,
254
255
  finderParams,
255
256
  locations
256
257
  });
257
- const [cacheMap, items] = await cache.find(finder, finderParams, locations);
258
+ const [cacheMap, items] = await cache.operations.find(finder, finderParams, locations);
258
259
  const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
259
260
  return [
260
261
  cacheMap,
@@ -262,12 +263,12 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
262
263
  ];
263
264
  };
264
265
  const findOne = async (finder, finderParams = {}, locations = [])=>{
265
- logger$3.default('find', {
266
+ logger$j.default('find', {
266
267
  finder,
267
268
  finderParams,
268
269
  locations
269
270
  });
270
- const [cacheMap, item] = await cache.findOne(finder, finderParams, locations);
271
+ const [cacheMap, item] = await cache.operations.findOne(finder, finderParams, locations);
271
272
  const populatedItem = await populate(item);
272
273
  return [
273
274
  cacheMap,
@@ -275,12 +276,12 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
275
276
  ];
276
277
  };
277
278
  const set = async (key, v)=>{
278
- logger$3.default('set', {
279
+ logger$j.default('set', {
279
280
  key,
280
281
  v
281
282
  });
282
283
  // TODO: There should be some input validation here to ensure a valid item.
283
- const [cacheMap, item] = await cache.set(key, v);
284
+ const [cacheMap, item] = await cache.operations.set(key, v);
284
285
  const populatedItem = await populate(item);
285
286
  return [
286
287
  cacheMap,
@@ -288,10 +289,17 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
288
289
  ];
289
290
  };
290
291
  const reset = async ()=>{
291
- const cacheMap = await cache.reset();
292
+ const cacheMap = await cache.operations.reset();
292
293
  return cacheMap;
293
294
  };
294
295
  return {
296
+ // Cache properties
297
+ coordinate: cache.coordinate,
298
+ registry: cache.registry,
299
+ api: cache.api,
300
+ cacheMap: cache.cacheMap,
301
+ operations: cache.operations,
302
+ // Cache operations exposed directly
295
303
  all,
296
304
  one,
297
305
  action,
@@ -307,15 +315,14 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
307
315
  findOne,
308
316
  reset,
309
317
  set,
310
- pkTypes: cache.pkTypes,
311
- cacheMap: cache.cacheMap,
318
+ // Aggregator-specific operations
312
319
  populate,
313
320
  populateAggregate,
314
321
  populateEvent
315
322
  };
316
323
  };
317
324
 
318
- function _define_property$1(obj, key, value) {
325
+ function _define_property(obj, key, value) {
319
326
  if (key in obj) {
320
327
  Object.defineProperty(obj, key, {
321
328
  value: value,
@@ -328,7 +335,7 @@ function _define_property$1(obj, key, value) {
328
335
  }
329
336
  return obj;
330
337
  }
331
- const logger$2 = LibLogger.get("CacheMap");
338
+ const logger$i = logger$1.default.get("CacheMap");
332
339
  // Normalize a key value to string for consistent comparison and hashing
333
340
  const normalizeKeyValue$1 = (value)=>{
334
341
  return String(value);
@@ -431,17 +438,17 @@ class CacheMap extends core.Dictionary {
431
438
  }
432
439
  allIn(locations) {
433
440
  if (locations.length === 0) {
434
- logger$2.debug('Returning all items, LocKeys is empty');
441
+ logger$i.debug('Returning all items, LocKeys is empty');
435
442
  return this.values();
436
443
  } else {
437
444
  const locKeys = locations;
438
- logger$2.debug('allIn', {
445
+ logger$i.debug('allIn', {
439
446
  locKeys,
440
447
  keys: this.keys().length
441
448
  });
442
449
  return this.keys().filter((key)=>key && core.isComKey(key)).filter((key)=>{
443
450
  const ComKey = key;
444
- logger$2.debug('Comparing Location Keys', {
451
+ logger$i.debug('Comparing Location Keys', {
445
452
  locKeys,
446
453
  ComKey
447
454
  });
@@ -451,7 +458,7 @@ class CacheMap extends core.Dictionary {
451
458
  }
452
459
  // TODO: Can we do case insensitive matching?
453
460
  contains(query, locations) {
454
- logger$2.debug('contains', {
461
+ logger$i.debug('contains', {
455
462
  query,
456
463
  locations
457
464
  });
@@ -459,7 +466,7 @@ class CacheMap extends core.Dictionary {
459
466
  return items.some((item)=>core.isQueryMatch(item, query));
460
467
  }
461
468
  queryIn(query, locations = []) {
462
- logger$2.debug('queryIn', {
469
+ logger$i.debug('queryIn', {
463
470
  query,
464
471
  locations
465
472
  });
@@ -471,12 +478,277 @@ class CacheMap extends core.Dictionary {
471
478
  return clone;
472
479
  }
473
480
  constructor(types, map){
474
- super(map, createNormalizedHashFunction()), _define_property$1(this, "types", void 0);
481
+ super(map, createNormalizedHashFunction()), _define_property(this, "types", void 0);
475
482
  this.types = types;
476
483
  }
477
484
  }
478
485
 
479
- const logger$1 = LibLogger.get('Cache');
486
+ const logger$h = logger$1.default.get('all');
487
+ const all = async (api, cacheMap, pkType, query = {}, locations = [])=>{
488
+ logger$h.default('all', {
489
+ query,
490
+ locations
491
+ });
492
+ let ret = [];
493
+ try {
494
+ ret = await api.all(query, locations);
495
+ ret.forEach((v)=>{
496
+ cacheMap.set(v.key, v);
497
+ });
498
+ } catch (e) {
499
+ if (e instanceof httpApi.NotFoundError) ; else {
500
+ throw e;
501
+ }
502
+ }
503
+ return [
504
+ cacheMap,
505
+ core.validatePK(ret, pkType)
506
+ ];
507
+ };
508
+
509
+ const logger$g = logger$1.default.get('one');
510
+ const one = async (api, cacheMap, pkType, query = {}, locations = [])=>{
511
+ logger$g.default('one', {
512
+ query,
513
+ locations
514
+ });
515
+ let retItem = null;
516
+ try {
517
+ retItem = await api.one(query, locations);
518
+ if (retItem) {
519
+ cacheMap.set(retItem.key, retItem);
520
+ }
521
+ } catch (e) {
522
+ if (e instanceof httpApi.NotFoundError) ; else {
523
+ throw e;
524
+ }
525
+ }
526
+ return [
527
+ cacheMap,
528
+ retItem ? core.validatePK(retItem, pkType) : null
529
+ ];
530
+ };
531
+
532
+ const logger$f = logger$1.default.get('create');
533
+ const create = async (api, cacheMap, pkType, v, locations = [])=>{
534
+ logger$f.default('create', {
535
+ v,
536
+ locations
537
+ });
538
+ const created = await api.create(v, locations);
539
+ cacheMap.set(created.key, created);
540
+ return [
541
+ cacheMap,
542
+ core.validatePK(created, pkType)
543
+ ];
544
+ };
545
+
546
+ const logger$e = logger$1.default.get('get');
547
+ const get.get = async (api, cacheMap, pkType, key)=>{
548
+ logger$e.default('get', {
549
+ key
550
+ });
551
+ if (!core.isValidItemKey(key)) {
552
+ logger$e.error('Key for Get is not a valid ItemKey: %j', key);
553
+ throw new Error('Key for Get is not a valid ItemKey');
554
+ }
555
+ let ret;
556
+ try {
557
+ ret = await api.get(key);
558
+ if (ret) {
559
+ cacheMap.set(ret.key, ret);
560
+ }
561
+ } catch (e) {
562
+ logger$e.error("Error getting item for key", {
563
+ key,
564
+ message: e.message,
565
+ stack: e.stack
566
+ });
567
+ throw e;
568
+ }
569
+ return [
570
+ cacheMap,
571
+ ret ? core.validatePK(ret, pkType) : null
572
+ ];
573
+ };
574
+
575
+ const logger$d = logger$1.default.get('retrieve');
576
+ const retrieve = async (api, cacheMap, pkType, key)=>{
577
+ logger$d.default('retrieve', {
578
+ key
579
+ });
580
+ if (!core.isValidItemKey(key)) {
581
+ logger$d.error('Key for Retrieve is not a valid ItemKey: %j', key);
582
+ throw new Error('Key for Retrieve is not a valid ItemKey');
583
+ }
584
+ const containsItemKey = cacheMap.includesKey(key);
585
+ let retrieved;
586
+ if (containsItemKey) {
587
+ logger$d.default('Looking for Object in Cache', key);
588
+ retrieved = cacheMap.get(key);
589
+ } else {
590
+ logger$d.default('Object Not Found in Cache, Retrieving from Server API', {
591
+ key
592
+ });
593
+ [, retrieved] = await get.get(api, cacheMap, pkType, key);
594
+ }
595
+ const retValue = [
596
+ containsItemKey ? null : cacheMap,
597
+ retrieved ? core.validatePK(retrieved, pkType) : null
598
+ ];
599
+ return retValue;
600
+ };
601
+
602
+ const logger$c = logger$1.default.get('remove');
603
+ const remove = async (api, cacheMap, key)=>{
604
+ logger$c.default('remove', {
605
+ key
606
+ });
607
+ if (!core.isValidItemKey(key)) {
608
+ logger$c.error('Key for Remove is not a valid ItemKey: %j', key);
609
+ throw new Error('Key for Remove is not a valid ItemKey');
610
+ }
611
+ try {
612
+ await api.remove(key);
613
+ cacheMap.delete(key);
614
+ } catch (e) {
615
+ logger$c.error("Error deleting item", {
616
+ error: e
617
+ });
618
+ throw e;
619
+ }
620
+ return cacheMap;
621
+ };
622
+
623
+ const logger$b = logger$1.default.get('update');
624
+ const update = async (api, cacheMap, pkType, key, v)=>{
625
+ logger$b.default('update', {
626
+ key,
627
+ v
628
+ });
629
+ if (!core.isValidItemKey(key)) {
630
+ logger$b.error('Key for Update is not a valid ItemKey: %j', key);
631
+ throw new Error('Key for Update is not a valid ItemKey');
632
+ }
633
+ try {
634
+ const updated = await api.update(key, v);
635
+ cacheMap.set(updated.key, updated);
636
+ return [
637
+ cacheMap,
638
+ core.validatePK(updated, pkType)
639
+ ];
640
+ } catch (e) {
641
+ logger$b.error("Error updating item", {
642
+ error: e
643
+ });
644
+ throw e;
645
+ }
646
+ };
647
+
648
+ const logger$a = logger$1.default.get('action');
649
+ const action = async (api, cacheMap, pkType, key, action, body = {})=>{
650
+ logger$a.default('action', {
651
+ key,
652
+ action,
653
+ body
654
+ });
655
+ if (!core.isValidItemKey(key)) {
656
+ logger$a.error('Key for Action is not a valid ItemKey: %j', key);
657
+ throw new Error('Key for Action is not a valid ItemKey');
658
+ }
659
+ const updated = await api.action(key, action, body);
660
+ cacheMap.set(updated.key, updated);
661
+ return [
662
+ cacheMap,
663
+ core.validatePK(updated, pkType)
664
+ ];
665
+ };
666
+
667
+ const logger$9 = logger$1.default.get('allAction');
668
+ const allAction = async (api, cacheMap, pkType, action, body = {}, locations = [])=>{
669
+ logger$9.default('allAction', {
670
+ action,
671
+ body,
672
+ locations
673
+ });
674
+ let ret = [];
675
+ try {
676
+ ret = await api.allAction(action, body, locations);
677
+ ret.forEach((v)=>{
678
+ cacheMap.set(v.key, v);
679
+ });
680
+ } catch (e) {
681
+ // istanbul ignore next
682
+ if (e instanceof httpApi.NotFoundError) ; else {
683
+ throw e;
684
+ }
685
+ }
686
+ return [
687
+ cacheMap,
688
+ core.validatePK(ret, pkType)
689
+ ];
690
+ };
691
+
692
+ const logger$8 = logger$1.default.get('facet');
693
+ const facet = async (api, cacheMap, key, facet, params = {})=>{
694
+ logger$8.default('facet', {
695
+ key,
696
+ facet
697
+ });
698
+ const ret = await api.facet(key, facet, params);
699
+ return [
700
+ cacheMap,
701
+ ret
702
+ ];
703
+ };
704
+
705
+ const logger$7 = logger$1.default.get('allFacet');
706
+ const allFacet = async (api, cacheMap, facet, params = {}, locations = [])=>{
707
+ logger$7.default('allFacet', {
708
+ facet,
709
+ params,
710
+ locations
711
+ });
712
+ const ret = await api.allFacet(facet, params, locations);
713
+ return [
714
+ cacheMap,
715
+ ret
716
+ ];
717
+ };
718
+
719
+ const logger$6 = logger$1.default.get('find');
720
+ const find = async (api, cacheMap, pkType, finder, params = {}, locations = [])=>{
721
+ logger$6.default('find', {
722
+ finder,
723
+ params,
724
+ locations
725
+ });
726
+ const ret = await api.find(finder, params, locations);
727
+ ret.forEach((v)=>{
728
+ cacheMap.set(v.key, v);
729
+ });
730
+ return [
731
+ cacheMap,
732
+ core.validatePK(ret, pkType)
733
+ ];
734
+ };
735
+
736
+ const logger$5 = logger$1.default.get('findOne');
737
+ const findOne = async (api, cacheMap, pkType, finder, finderParams = {}, locations = [])=>{
738
+ logger$5.default('findOne', {
739
+ finder,
740
+ finderParams,
741
+ locations
742
+ });
743
+ const ret = await api.findOne(finder, finderParams, locations);
744
+ cacheMap.set(ret.key, ret);
745
+ return [
746
+ cacheMap,
747
+ core.validatePK(ret, pkType)
748
+ ];
749
+ };
750
+
751
+ const logger$4 = logger$1.default.get('set');
480
752
  // Normalize a key value to string for consistent comparison
481
753
  const normalizeKeyValue = (value)=>{
482
754
  return String(value);
@@ -516,374 +788,153 @@ const normalizeKey = (key)=>{
516
788
  }
517
789
  return key;
518
790
  };
519
- const createCache = async (api, pkType, parentCache)=>{
520
- let pkTypes = [
521
- pkType
522
- ];
523
- if (parentCache) {
524
- pkTypes = pkTypes.concat(parentCache.pkTypes);
791
+ const set = async (cacheMap, pkType, key, v)=>{
792
+ logger$4.default('set', {
793
+ key,
794
+ v
795
+ });
796
+ if (!core.isValidItemKey(key)) {
797
+ logger$4.error('Key for Set is not a valid ItemKey: %j', key);
798
+ throw new Error('Key for Set is not a valid ItemKey');
525
799
  }
526
- let cacheMap = new CacheMap(pkTypes);
527
- const all = async (query = {}, locations = [])=>{
528
- logger$1.default('all', {
529
- query,
530
- locations
531
- });
532
- let ret = [];
533
- try {
534
- ret = await api.all(query, locations);
535
- ret.forEach((v)=>{
536
- cacheMap.set(v.key, v);
537
- });
538
- } catch (e) {
539
- if (e instanceof httpApi.NotFoundError) ; else {
540
- throw e;
541
- }
542
- }
543
- return [
544
- cacheMap,
545
- core.validatePK(ret, pkType)
546
- ];
547
- };
548
- const one = async (query = {}, locations = [])=>{
549
- logger$1.default('one', {
550
- query,
551
- locations
552
- });
553
- let retItem = null;
554
- try {
555
- retItem = await api.one(query, locations);
556
- if (retItem) {
557
- cacheMap.set(retItem.key, retItem);
558
- }
559
- } catch (e) {
560
- if (e instanceof httpApi.NotFoundError) ; else {
561
- throw e;
562
- }
563
- }
564
- return [
565
- cacheMap,
566
- retItem ? core.validatePK(retItem, pkType) : null
567
- ];
568
- };
569
- const action = async (key, action, body = {})=>{
570
- logger$1.default('action', {
571
- key,
572
- action,
573
- body
574
- });
575
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
576
- // This should be looking at the parentCaches and calculating an array of pkTypes
577
- if (!core.isValidItemKey(key)) {
578
- logger$1.error('Key for Action is not a valid ItemKey: %j', key);
579
- throw new Error('Key for Action is not a valid ItemKey');
580
- }
581
- const updated = await api.action(key, action, body);
582
- cacheMap.set(updated.key, updated);
583
- return [
584
- cacheMap,
585
- core.validatePK(updated, pkType)
586
- ];
587
- };
588
- const allAction = async (action, body = {}, locations = [])=>{
589
- logger$1.default('allAction', {
590
- action,
591
- body,
592
- locations
593
- });
594
- let ret = [];
595
- try {
596
- ret = await api.allAction(action, body, locations);
597
- ret.forEach((v)=>{
598
- cacheMap.set(v.key, v);
599
- });
600
- } catch (e) {
601
- // istanbul ignore next
602
- if (e instanceof httpApi.NotFoundError) ; else {
603
- throw e;
604
- }
605
- }
606
- return [
607
- cacheMap,
608
- core.validatePK(ret, pkType)
609
- ];
610
- };
611
- const allFacet = async (facet, params = {}, locations = [])=>{
612
- logger$1.default('allFacet', {
613
- facet,
614
- params,
615
- locations
616
- });
617
- const ret = await api.allFacet(facet, params, locations);
618
- return [
619
- cacheMap,
620
- ret
621
- ];
800
+ // Validate the item's primary key
801
+ core.validatePK(v, pkType);
802
+ if (!isItemKeyEqualNormalized(key, v.key)) {
803
+ logger$4.error('Key does not match item key: %j != %j', key, v.key);
804
+ throw new Error('Key does not match item key');
805
+ }
806
+ cacheMap.set(key, v);
807
+ return [
808
+ cacheMap,
809
+ core.validatePK(v, pkType)
810
+ ];
811
+ };
812
+
813
+ const reset = async (coordinate)=>{
814
+ const cacheMap = new CacheMap(coordinate.kta);
815
+ return [
816
+ cacheMap
817
+ ];
818
+ };
819
+
820
+ // Import all operation functions
821
+ const createOperations = (api, coordinate, cacheMap, pkType)=>{
822
+ return {
823
+ all: (query, locations)=>all(api, cacheMap, pkType, query, locations),
824
+ one: (query, locations)=>one(api, cacheMap, pkType, query, locations),
825
+ create: (item, locations)=>create(api, cacheMap, pkType, item, locations),
826
+ get: (key)=>get.get(api, cacheMap, pkType, key),
827
+ retrieve: (key)=>retrieve(api, cacheMap, pkType, key),
828
+ remove: (key)=>remove(api, cacheMap, key),
829
+ update: (key, item)=>update(api, cacheMap, pkType, key, item),
830
+ action: (key, actionName, body)=>action(api, cacheMap, pkType, key, actionName, body),
831
+ allAction: (actionName, body, locations)=>allAction(api, cacheMap, pkType, actionName, body, locations),
832
+ facet: (key, facetName, params)=>facet(api, cacheMap, key, facetName, params),
833
+ allFacet: (facetName, params, locations)=>allFacet(api, cacheMap, facetName, params, locations),
834
+ find: (finder, params, locations)=>find(api, cacheMap, pkType, finder, params, locations),
835
+ findOne: (finder, params, locations)=>findOne(api, cacheMap, pkType, finder, params, locations),
836
+ set: (key, item)=>set(cacheMap, pkType, key, item),
837
+ reset: ()=>reset(coordinate)
622
838
  };
623
- const create = async (v, locations = [])=>{
624
- logger$1.default('create', {
625
- v,
626
- locations
627
- });
628
- const created = await api.create(v, locations);
629
- cacheMap.set(created.key, created);
630
- return [
631
- cacheMap,
632
- core.validatePK(created, pkType)
633
- ];
839
+ };
840
+
841
+ const logger$3 = logger$1.default.get('Cache');
842
+ const createCache = async (api, coordinate, registry)=>{
843
+ logger$3.debug('createCache', {
844
+ coordinate,
845
+ registry
846
+ });
847
+ // Create the cache map using the coordinate's key type array
848
+ const cacheMap = new CacheMap(coordinate.kta);
849
+ // Get the primary key type from the coordinate
850
+ const pkType = coordinate.kta[0];
851
+ // Create operations
852
+ const operations = createOperations(api, coordinate, cacheMap, pkType);
853
+ return {
854
+ coordinate,
855
+ registry,
856
+ api,
857
+ cacheMap,
858
+ operations
634
859
  };
635
- const get = async (key)=>{
636
- logger$1.default('get', {
637
- key
638
- });
639
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
640
- // This should be looking at the parentCaches and calculating an array of pkTypes
641
- if (!core.isValidItemKey(key)) {
642
- logger$1.error('Key for Get is not a valid ItemKey: %j', key);
643
- throw new Error('Key for Get is not a valid ItemKey');
644
- }
645
- let ret;
646
- try {
647
- ret = await api.get(key);
648
- if (ret) {
649
- cacheMap.set(ret.key, ret);
650
- }
651
- } catch (e) {
652
- logger$1.error("Error getting item for key", {
653
- key,
654
- message: e.message,
655
- stack: e.stack
656
- });
657
- throw e;
658
- }
659
- return [
860
+ };
861
+ const isCache = (cache)=>{
862
+ return cache !== null && typeof cache === 'object' && 'coordinate' in cache && 'registry' in cache && 'api' in cache && 'cacheMap' in cache && 'operations' in cache;
863
+ };
864
+
865
+ const logger$2 = logger$1.default.get("Instance");
866
+ const createInstance = async (registry, coordinate, api)=>{
867
+ logger$2.debug("createInstance", {
868
+ coordinate,
869
+ api,
870
+ registry
871
+ });
872
+ return await createCache(api, coordinate, registry);
873
+ };
874
+ const isInstance = (instance)=>{
875
+ return instance !== null && typeof instance === 'object' && 'coordinate' in instance && 'registry' in instance && 'api' in instance && 'cacheMap' in instance && 'operations' in instance;
876
+ };
877
+
878
+ const logger$1 = logger$1.default.get("InstanceFactory");
879
+ /**
880
+ * Factory function for creating cache instances
881
+ */ const createInstanceFactory = (api)=>{
882
+ return (coordinate, context)=>{
883
+ logger$1.debug("Creating cache instance", {
884
+ coordinate,
885
+ registry: context.registry,
886
+ api
887
+ });
888
+ // Since InstanceFactory must be synchronous but our createInstance is async,
889
+ // we need to create a special cache instance synchronously and defer the async initialization
890
+ const cacheMap = new CacheMap(coordinate.kta);
891
+ const pkType = coordinate.kta[0];
892
+ const operations = createOperations(api, coordinate, cacheMap, pkType);
893
+ return {
894
+ coordinate,
895
+ registry: context.registry,
896
+ api,
660
897
  cacheMap,
661
- ret ? core.validatePK(ret, pkType) : null
662
- ];
663
- };
664
- const retrieve = async (key)=>{
665
- logger$1.default('retrieve', {
666
- key
667
- });
668
- if (!core.isValidItemKey(key)) {
669
- logger$1.error('Key for Retrieve is not a valid ItemKey: %j', key);
670
- throw new Error('Key for Retrieve is not a valid ItemKey');
671
- }
672
- const containsItemKey = cacheMap.includesKey(key);
673
- let retrieved;
674
- if (containsItemKey) {
675
- logger$1.default('Looking for Object in Cache', key);
676
- retrieved = cacheMap.get(key);
677
- } else {
678
- logger$1.default('Object Not Found in Cache, Retrieving from Server API', {
679
- key
680
- });
681
- [, retrieved] = await get(key);
682
- }
683
- const retValue = [
684
- containsItemKey ? null : cacheMap,
685
- retrieved ? core.validatePK(retrieved, pkType) : null
686
- ];
687
- // logger.debug('Returning from retrieve', { retValue });
688
- return retValue;
689
- };
690
- const remove = async (key)=>{
691
- logger$1.default('remove', {
692
- key
693
- });
694
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
695
- // This should be looking at the parentCaches and calculating an array of pkTypes
696
- if (!core.isValidItemKey(key)) {
697
- logger$1.error('Key for Remove is not a valid ItemKey: %j', key);
698
- throw new Error('Key for Remove is not a valid ItemKey');
699
- }
700
- try {
701
- await api.remove(key);
702
- cacheMap.delete(key);
703
- } catch (e) {
704
- logger$1.error("Error deleting item", {
705
- error: e
706
- });
707
- throw e;
708
- }
709
- return cacheMap;
898
+ operations
899
+ };
710
900
  };
711
- const update = async (key, v)=>{
712
- logger$1.default('update', {
713
- key,
714
- v
715
- });
716
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
717
- // This should be looking at the parentCaches and calculating an array of pkTypes
718
- if (!core.isValidItemKey(key)) {
719
- logger$1.error('Key for Update is not a valid ItemKey: %j', key);
720
- throw new Error('Key for Update is not a valid ItemKey');
721
- }
722
- try {
723
- const updated = await api.update(key, v);
724
- cacheMap.set(updated.key, updated);
725
- return [
726
- cacheMap,
727
- core.validatePK(updated, pkType)
728
- ];
729
- } catch (e) {
730
- logger$1.error("Error updating chat", {
731
- error: e
732
- });
733
- throw e;
901
+ };
902
+
903
+ const logger = logger$1.default.get("Registry");
904
+ /**
905
+ * Factory function for creating cache registries
906
+ */ const createRegistryFactory = ()=>{
907
+ return (type, registryHub)=>{
908
+ if (type !== 'cache') {
909
+ throw new Error(`Cache registry factory can only create 'cache' type registries, got: ${type}`);
734
910
  }
735
- };
736
- // Facets are a pass-thru for caches
737
- const facet = async (key, facet, params = {})=>{
738
- logger$1.default('facet', {
739
- key,
740
- facet
911
+ logger.debug("Creating cache registry", {
912
+ type,
913
+ registryHub
741
914
  });
742
- const ret = await api.facet(key, facet, params);
743
- return [
744
- cacheMap,
745
- ret
746
- ];
747
- };
748
- const find = async (finder, params = {}, locations = [])=>{
749
- logger$1.default('find', {
750
- finder,
751
- params,
752
- locations
753
- });
754
- const ret = await api.find(finder, params, locations);
755
- ret.forEach((v)=>{
756
- cacheMap.set(v.key, v);
757
- });
758
- return [
759
- cacheMap,
760
- core.validatePK(ret, pkType)
761
- ];
762
- };
763
- const findOne = async (finder, finderParams = {}, locations = [])=>{
764
- logger$1.default('findOne', {
765
- finder,
766
- finderParams,
767
- locations
768
- });
769
- const ret = await api.findOne(finder, finderParams, locations);
770
- cacheMap.set(ret.key, ret);
771
- return [
772
- cacheMap,
773
- core.validatePK(ret, pkType)
774
- ];
775
- };
776
- const reset = async ()=>{
777
- cacheMap = new CacheMap(pkTypes);
778
- return [
779
- cacheMap
780
- ];
781
- };
782
- const set = async (key, v)=>{
783
- logger$1.default('set', {
784
- key,
785
- v
786
- });
787
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
788
- // This should be looking at the parentCaches and calculating an array of pkTypes
789
- if (!core.isValidItemKey(key)) {
790
- logger$1.error('Key for Update is not a valid ItemKey: %j', key);
791
- throw new Error('Key for Update is not a valid ItemKey');
792
- }
793
- // TODO: This could be merged with the isValidItemKey check, later.
794
- core.validatePK(v, pkType);
795
- if (!isItemKeyEqualNormalized(key, v.key)) {
796
- logger$1.error('Key does not match item key: %j != %j', key, v.key);
797
- throw new Error('Key does not match item key');
798
- }
799
- cacheMap.set(key, v);
800
- return [
801
- cacheMap,
802
- core.validatePK(v, pkType)
803
- ];
915
+ const baseRegistry = registry.createRegistry(type, registryHub);
916
+ // Cast to Registry for type safety
917
+ return baseRegistry;
804
918
  };
919
+ };
920
+ /**
921
+ * Creates a new cache registry instance
922
+ */ const createRegistry = (registryHub)=>{
923
+ const baseRegistry = registry.createRegistry('cache', registryHub);
805
924
  return {
806
- all,
807
- one,
808
- action,
809
- allAction,
810
- allFacet,
811
- create,
812
- get,
813
- retrieve,
814
- remove,
815
- update,
816
- facet,
817
- find,
818
- findOne,
819
- reset,
820
- set,
821
- pkTypes,
822
- cacheMap
925
+ ...baseRegistry
823
926
  };
824
927
  };
825
928
 
826
- function _define_property(obj, key, value) {
827
- if (key in obj) {
828
- Object.defineProperty(obj, key, {
829
- value: value,
830
- enumerable: true,
831
- configurable: true,
832
- writable: true
833
- });
834
- } else {
835
- obj[key] = value;
836
- }
837
- return obj;
838
- }
839
- const logger = LibLogger.get('CacheRegistry');
840
- class CacheRegistry {
841
- constructor(){
842
- // TODO: My use of Generics has Boxed me into a corner where I can't reference AbstractCache without the types
843
- _define_property(this, "cacheMap", {});
844
- _define_property(this, "registerCache", async (cache)=>{
845
- try {
846
- logger.debug('Attempting to register cache with pkTypes:', cache.pkTypes);
847
- const key = JSON.stringify(cache.pkTypes);
848
- if (this.cacheMap[key]) {
849
- logger.debug(`Cache with pkTypes ${key} already exists, will be overwritten`);
850
- }
851
- this.cacheMap[key] = cache;
852
- logger.debug('Cache registered successfully with key:', key);
853
- } catch (error) {
854
- logger.error('Failed to register cache:', error);
855
- throw error;
856
- }
857
- });
858
- _define_property(this, "getCache", (kts)=>{
859
- logger.debug('Attempting to get cache for key types:', kts);
860
- const key = JSON.stringify(kts);
861
- logger.debug('Looking up cache with key:', key);
862
- const cache = this.cacheMap[key];
863
- if (!cache) {
864
- logger.warning(`No cache found for key types: ${key}`);
865
- }
866
- return cache;
867
- });
868
- _define_property(this, "printRegisteredCaches", ()=>{
869
- logger.debug('Printing all registered caches:');
870
- const cacheCount = Object.keys(this.cacheMap).length;
871
- logger.debug(`Total number of registered caches: ${cacheCount}`);
872
- if (cacheCount === 0) {
873
- logger.debug('No caches are currently registered');
874
- }
875
- Object.entries(this.cacheMap).forEach(([keyTypes])=>{
876
- logger.debug(`Cache with key types: ${keyTypes}`);
877
- });
878
- });
879
- logger.debug('CacheRegistry instance created');
880
- }
881
- }
882
- _define_property(CacheRegistry, "instance", void 0);
883
-
884
929
  exports.CacheMap = CacheMap;
885
- exports.CacheRegistry = CacheRegistry;
886
930
  exports.createAggregator = createAggregator;
887
931
  exports.createCache = createCache;
932
+ exports.createInstance = createInstance;
933
+ exports.createInstanceFactory = createInstanceFactory;
934
+ exports.createOperations = createOperations;
935
+ exports.createRegistry = createRegistry;
936
+ exports.createRegistryFactory = createRegistryFactory;
937
+ exports.isCache = isCache;
938
+ exports.isInstance = isInstance;
888
939
  exports.toCacheConfig = toCacheConfig;
889
940
  //# sourceMappingURL=index.cjs.map