@fjell/cache 4.6.5 → 4.6.10

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/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
10
  const LibLogger = Logging.getLogger('@fjell/cache');
10
11
 
11
- const logger$3 = LibLogger.get('ItemAggregator');
12
+ const logger$5 = LibLogger.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$5.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$5.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$5.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$5.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$5.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,7 +63,7 @@ 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$5.default('AGG Retrieving Item in Populate', {
66
67
  key: ref
67
68
  });
68
69
  const [, newItem] = await cacheConfig.cache.retrieve(ref);
@@ -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$5.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$5.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$5.error('populateEvent with an Event that does not have by', {
97
98
  event,
98
99
  ik: item.key,
99
100
  eventKey: key
@@ -103,7 +104,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
103
104
  event
104
105
  }));
105
106
  }
106
- logger$3.default('EVENT Retrieving Item in Populate', {
107
+ logger$5.default('EVENT Retrieving Item in Populate', {
107
108
  key: event.by
108
109
  });
109
110
  const [, newItem] = await cacheConfig.cache.retrieve(event.by);
@@ -113,7 +114,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
113
114
  }
114
115
  };
115
116
  const all = async (query = {}, locations = [])=>{
116
- logger$3.default('all', {
117
+ logger$5.default('all', {
117
118
  query,
118
119
  locations
119
120
  });
@@ -125,7 +126,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
125
126
  ];
126
127
  };
127
128
  const one = async (query = {}, locations = [])=>{
128
- logger$3.default('one', {
129
+ logger$5.default('one', {
129
130
  query,
130
131
  locations
131
132
  });
@@ -140,7 +141,7 @@ 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$5.default('action', {
144
145
  key,
145
146
  action,
146
147
  body
@@ -153,7 +154,7 @@ 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$5.default('action', {
157
158
  action,
158
159
  body,
159
160
  locations
@@ -166,7 +167,7 @@ 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$5.default('allFacet', {
170
171
  facet,
171
172
  params,
172
173
  locations
@@ -178,7 +179,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
178
179
  ];
179
180
  };
180
181
  const create = async (v, locations = [])=>{
181
- logger$3.default('create', {
182
+ logger$5.default('create', {
182
183
  v,
183
184
  locations
184
185
  });
@@ -190,7 +191,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
190
191
  ];
191
192
  };
192
193
  const get = async (key)=>{
193
- logger$3.default('get', {
194
+ logger$5.default('get', {
194
195
  key
195
196
  });
196
197
  const [cacheMap, item] = await cache.get(key);
@@ -204,7 +205,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
204
205
  ];
205
206
  };
206
207
  const retrieve = async (key)=>{
207
- logger$3.default('retrieve', {
208
+ logger$5.default('retrieve', {
208
209
  key
209
210
  });
210
211
  const [cacheMap, item] = await cache.retrieve(key);
@@ -218,14 +219,14 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
218
219
  ];
219
220
  };
220
221
  const remove = async (key)=>{
221
- logger$3.default('remove', {
222
+ logger$5.default('remove', {
222
223
  key
223
224
  });
224
225
  const cacheMap = await cache.remove(key);
225
226
  return cacheMap;
226
227
  };
227
228
  const update = async (key, v)=>{
228
- logger$3.default('update', {
229
+ logger$5.default('update', {
229
230
  key,
230
231
  v
231
232
  });
@@ -238,7 +239,7 @@ 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$5.default('facet', {
242
243
  key,
243
244
  facet
244
245
  });
@@ -249,7 +250,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
249
250
  ];
250
251
  };
251
252
  const find = async (finder, finderParams = {}, locations = [])=>{
252
- logger$3.default('find', {
253
+ logger$5.default('find', {
253
254
  finder,
254
255
  finderParams,
255
256
  locations
@@ -262,7 +263,7 @@ 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$5.default('find', {
266
267
  finder,
267
268
  finderParams,
268
269
  locations
@@ -275,7 +276,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
275
276
  ];
276
277
  };
277
278
  const set = async (key, v)=>{
278
- logger$3.default('set', {
279
+ logger$5.default('set', {
279
280
  key,
280
281
  v
281
282
  });
@@ -315,7 +316,7 @@ const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
315
316
  };
316
317
  };
317
318
 
318
- function _define_property$1(obj, key, value) {
319
+ function _define_property(obj, key, value) {
319
320
  if (key in obj) {
320
321
  Object.defineProperty(obj, key, {
321
322
  value: value,
@@ -328,7 +329,69 @@ function _define_property$1(obj, key, value) {
328
329
  }
329
330
  return obj;
330
331
  }
331
- const logger$2 = LibLogger.get("CacheMap");
332
+ const logger$4 = LibLogger.get("CacheMap");
333
+ // Normalize a key value to string for consistent comparison and hashing
334
+ const normalizeKeyValue$1 = (value)=>{
335
+ return String(value);
336
+ };
337
+ // Normalized hash function for Dictionary that converts pk/lk values to strings
338
+ const createNormalizedHashFunction = ()=>{
339
+ return (key)=>{
340
+ if (typeof key === 'object' && key !== null) {
341
+ // Create a normalized version of the key with string values
342
+ const normalizedKey = JSON.parse(JSON.stringify(key));
343
+ // Normalize pk values
344
+ if ('pk' in normalizedKey && normalizedKey.pk !== null) {
345
+ normalizedKey.pk = normalizeKeyValue$1(normalizedKey.pk);
346
+ }
347
+ // Normalize lk values
348
+ if ('lk' in normalizedKey && normalizedKey.lk !== null) {
349
+ normalizedKey.lk = normalizeKeyValue$1(normalizedKey.lk);
350
+ }
351
+ // Normalize loc array lk values
352
+ if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
353
+ normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
354
+ if (locItem && 'lk' in locItem && locItem.lk !== null) {
355
+ return {
356
+ ...locItem,
357
+ lk: normalizeKeyValue$1(locItem.lk)
358
+ };
359
+ }
360
+ return locItem;
361
+ });
362
+ }
363
+ return JSON.stringify(normalizedKey);
364
+ }
365
+ return JSON.stringify(key);
366
+ };
367
+ };
368
+ // Helper function to normalize and compare location key arrays
369
+ const isLocKeyArrayEqual = (a, b)=>{
370
+ if (a.length !== b.length) {
371
+ return false;
372
+ }
373
+ for(let i = 0; i < a.length; i++){
374
+ const normalizedA = normalizeLocKeyItem(a[i]);
375
+ const normalizedB = normalizeLocKeyItem(b[i]);
376
+ if (JSON.stringify(normalizedA) !== JSON.stringify(normalizedB)) {
377
+ return false;
378
+ }
379
+ }
380
+ return true;
381
+ };
382
+ // Helper function to normalize a location key item
383
+ const normalizeLocKeyItem = (item)=>{
384
+ if (typeof item === 'object' && item !== null) {
385
+ const normalized = {
386
+ ...item
387
+ };
388
+ if ('lk' in normalized && normalized.lk !== null) {
389
+ normalized.lk = normalizeKeyValue$1(normalized.lk);
390
+ }
391
+ return normalized;
392
+ }
393
+ return item;
394
+ };
332
395
  // const isObj = (x: any) => typeof x === "object" && x !== null;
333
396
  // const intersection = (a: object, b: object): object => {
334
397
  // const result: { [key: string]: any } = {}
@@ -369,27 +432,27 @@ class CacheMap extends core.Dictionary {
369
432
  }
370
433
  allIn(locations) {
371
434
  if (locations.length === 0) {
372
- logger$2.debug('Returning all items, LocKeys is empty');
435
+ logger$4.debug('Returning all items, LocKeys is empty');
373
436
  return this.values();
374
437
  } else {
375
438
  const locKeys = locations;
376
- logger$2.debug('allIn', {
439
+ logger$4.debug('allIn', {
377
440
  locKeys,
378
441
  keys: this.keys().length
379
442
  });
380
443
  return this.keys().filter((key)=>key && core.isComKey(key)).filter((key)=>{
381
444
  const ComKey = key;
382
- logger$2.debug('Comparing Location Keys', {
445
+ logger$4.debug('Comparing Location Keys', {
383
446
  locKeys,
384
447
  ComKey
385
448
  });
386
- return JSON.stringify(locKeys) === JSON.stringify(ComKey.loc);
449
+ return isLocKeyArrayEqual(locKeys, ComKey.loc);
387
450
  }).map((key)=>this.get(key));
388
451
  }
389
452
  }
390
453
  // TODO: Can we do case insensitive matching?
391
454
  contains(query, locations) {
392
- logger$2.debug('contains', {
455
+ logger$4.debug('contains', {
393
456
  query,
394
457
  locations
395
458
  });
@@ -397,7 +460,7 @@ class CacheMap extends core.Dictionary {
397
460
  return items.some((item)=>core.isQueryMatch(item, query));
398
461
  }
399
462
  queryIn(query, locations = []) {
400
- logger$2.debug('queryIn', {
463
+ logger$4.debug('queryIn', {
401
464
  query,
402
465
  locations
403
466
  });
@@ -409,12 +472,51 @@ class CacheMap extends core.Dictionary {
409
472
  return clone;
410
473
  }
411
474
  constructor(types, map){
412
- super(map), _define_property$1(this, "types", void 0);
475
+ super(map, createNormalizedHashFunction()), _define_property(this, "types", void 0);
413
476
  this.types = types;
414
477
  }
415
478
  }
416
479
 
417
- const logger$1 = LibLogger.get('Cache');
480
+ const logger$3 = LibLogger.get('Cache');
481
+ // Normalize a key value to string for consistent comparison
482
+ const normalizeKeyValue = (value)=>{
483
+ return String(value);
484
+ };
485
+ // Normalized key comparison function that handles string/number differences
486
+ const isItemKeyEqualNormalized = (a, b)=>{
487
+ // For now, just normalize the keys to strings and use the original comparison
488
+ const normalizedA = normalizeKey(a);
489
+ const normalizedB = normalizeKey(b);
490
+ return core.isItemKeyEqual(normalizedA, normalizedB);
491
+ };
492
+ // Helper function to normalize a key
493
+ const normalizeKey = (key)=>{
494
+ if (typeof key === 'object' && key !== null) {
495
+ const normalizedKey = JSON.parse(JSON.stringify(key));
496
+ // Normalize pk values
497
+ if ('pk' in normalizedKey && normalizedKey.pk !== null) {
498
+ normalizedKey.pk = normalizeKeyValue(normalizedKey.pk);
499
+ }
500
+ // Normalize lk values
501
+ if ('lk' in normalizedKey && normalizedKey.lk !== null) {
502
+ normalizedKey.lk = normalizeKeyValue(normalizedKey.lk);
503
+ }
504
+ // Normalize loc array lk values
505
+ if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
506
+ normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
507
+ if (locItem && 'lk' in locItem && locItem.lk !== null) {
508
+ return {
509
+ ...locItem,
510
+ lk: normalizeKeyValue(locItem.lk)
511
+ };
512
+ }
513
+ return locItem;
514
+ });
515
+ }
516
+ return normalizedKey;
517
+ }
518
+ return key;
519
+ };
418
520
  const createCache = async (api, pkType, parentCache)=>{
419
521
  let pkTypes = [
420
522
  pkType
@@ -424,7 +526,7 @@ const createCache = async (api, pkType, parentCache)=>{
424
526
  }
425
527
  let cacheMap = new CacheMap(pkTypes);
426
528
  const all = async (query = {}, locations = [])=>{
427
- logger$1.default('all', {
529
+ logger$3.default('all', {
428
530
  query,
429
531
  locations
430
532
  });
@@ -445,7 +547,7 @@ const createCache = async (api, pkType, parentCache)=>{
445
547
  ];
446
548
  };
447
549
  const one = async (query = {}, locations = [])=>{
448
- logger$1.default('one', {
550
+ logger$3.default('one', {
449
551
  query,
450
552
  locations
451
553
  });
@@ -466,7 +568,7 @@ const createCache = async (api, pkType, parentCache)=>{
466
568
  ];
467
569
  };
468
570
  const action = async (key, action, body = {})=>{
469
- logger$1.default('action', {
571
+ logger$3.default('action', {
470
572
  key,
471
573
  action,
472
574
  body
@@ -474,7 +576,7 @@ const createCache = async (api, pkType, parentCache)=>{
474
576
  // TODO: This is validating the key, but it doesn't have knowledge of the pkType
475
577
  // This should be looking at the parentCaches and calculating an array of pkTypes
476
578
  if (!core.isValidItemKey(key)) {
477
- logger$1.error('Key for Action is not a valid ItemKey: %j', key);
579
+ logger$3.error('Key for Action is not a valid ItemKey: %j', key);
478
580
  throw new Error('Key for Action is not a valid ItemKey');
479
581
  }
480
582
  const updated = await api.action(key, action, body);
@@ -485,7 +587,7 @@ const createCache = async (api, pkType, parentCache)=>{
485
587
  ];
486
588
  };
487
589
  const allAction = async (action, body = {}, locations = [])=>{
488
- logger$1.default('allAction', {
590
+ logger$3.default('allAction', {
489
591
  action,
490
592
  body,
491
593
  locations
@@ -508,7 +610,7 @@ const createCache = async (api, pkType, parentCache)=>{
508
610
  ];
509
611
  };
510
612
  const allFacet = async (facet, params = {}, locations = [])=>{
511
- logger$1.default('allFacet', {
613
+ logger$3.default('allFacet', {
512
614
  facet,
513
615
  params,
514
616
  locations
@@ -520,7 +622,7 @@ const createCache = async (api, pkType, parentCache)=>{
520
622
  ];
521
623
  };
522
624
  const create = async (v, locations = [])=>{
523
- logger$1.default('create', {
625
+ logger$3.default('create', {
524
626
  v,
525
627
  locations
526
628
  });
@@ -532,13 +634,13 @@ const createCache = async (api, pkType, parentCache)=>{
532
634
  ];
533
635
  };
534
636
  const get = async (key)=>{
535
- logger$1.default('get', {
637
+ logger$3.default('get', {
536
638
  key
537
639
  });
538
640
  // TODO: This is validating the key, but it doesn't have knowledge of the pkType
539
641
  // This should be looking at the parentCaches and calculating an array of pkTypes
540
642
  if (!core.isValidItemKey(key)) {
541
- logger$1.error('Key for Get is not a valid ItemKey: %j', key);
643
+ logger$3.error('Key for Get is not a valid ItemKey: %j', key);
542
644
  throw new Error('Key for Get is not a valid ItemKey');
543
645
  }
544
646
  let ret;
@@ -548,7 +650,7 @@ const createCache = async (api, pkType, parentCache)=>{
548
650
  cacheMap.set(ret.key, ret);
549
651
  }
550
652
  } catch (e) {
551
- logger$1.error("Error getting item for key", {
653
+ logger$3.error("Error getting item for key", {
552
654
  key,
553
655
  message: e.message,
554
656
  stack: e.stack
@@ -561,20 +663,20 @@ const createCache = async (api, pkType, parentCache)=>{
561
663
  ];
562
664
  };
563
665
  const retrieve = async (key)=>{
564
- logger$1.default('retrieve', {
666
+ logger$3.default('retrieve', {
565
667
  key
566
668
  });
567
669
  if (!core.isValidItemKey(key)) {
568
- logger$1.error('Key for Retrieve is not a valid ItemKey: %j', key);
670
+ logger$3.error('Key for Retrieve is not a valid ItemKey: %j', key);
569
671
  throw new Error('Key for Retrieve is not a valid ItemKey');
570
672
  }
571
673
  const containsItemKey = cacheMap.includesKey(key);
572
674
  let retrieved;
573
675
  if (containsItemKey) {
574
- logger$1.default('Looking for Object in Cache', key);
676
+ logger$3.default('Looking for Object in Cache', key);
575
677
  retrieved = cacheMap.get(key);
576
678
  } else {
577
- logger$1.default('Object Not Found in Cache, Retrieving from Server API', {
679
+ logger$3.default('Object Not Found in Cache, Retrieving from Server API', {
578
680
  key
579
681
  });
580
682
  [, retrieved] = await get(key);
@@ -587,20 +689,20 @@ const createCache = async (api, pkType, parentCache)=>{
587
689
  return retValue;
588
690
  };
589
691
  const remove = async (key)=>{
590
- logger$1.default('remove', {
692
+ logger$3.default('remove', {
591
693
  key
592
694
  });
593
695
  // TODO: This is validating the key, but it doesn't have knowledge of the pkType
594
696
  // This should be looking at the parentCaches and calculating an array of pkTypes
595
697
  if (!core.isValidItemKey(key)) {
596
- logger$1.error('Key for Remove is not a valid ItemKey: %j', key);
698
+ logger$3.error('Key for Remove is not a valid ItemKey: %j', key);
597
699
  throw new Error('Key for Remove is not a valid ItemKey');
598
700
  }
599
701
  try {
600
702
  await api.remove(key);
601
703
  cacheMap.delete(key);
602
704
  } catch (e) {
603
- logger$1.error("Error deleting item", {
705
+ logger$3.error("Error deleting item", {
604
706
  error: e
605
707
  });
606
708
  throw e;
@@ -608,14 +710,14 @@ const createCache = async (api, pkType, parentCache)=>{
608
710
  return cacheMap;
609
711
  };
610
712
  const update = async (key, v)=>{
611
- logger$1.default('update', {
713
+ logger$3.default('update', {
612
714
  key,
613
715
  v
614
716
  });
615
717
  // TODO: This is validating the key, but it doesn't have knowledge of the pkType
616
718
  // This should be looking at the parentCaches and calculating an array of pkTypes
617
719
  if (!core.isValidItemKey(key)) {
618
- logger$1.error('Key for Update is not a valid ItemKey: %j', key);
720
+ logger$3.error('Key for Update is not a valid ItemKey: %j', key);
619
721
  throw new Error('Key for Update is not a valid ItemKey');
620
722
  }
621
723
  try {
@@ -626,7 +728,7 @@ const createCache = async (api, pkType, parentCache)=>{
626
728
  core.validatePK(updated, pkType)
627
729
  ];
628
730
  } catch (e) {
629
- logger$1.error("Error updating chat", {
731
+ logger$3.error("Error updating chat", {
630
732
  error: e
631
733
  });
632
734
  throw e;
@@ -634,7 +736,7 @@ const createCache = async (api, pkType, parentCache)=>{
634
736
  };
635
737
  // Facets are a pass-thru for caches
636
738
  const facet = async (key, facet, params = {})=>{
637
- logger$1.default('facet', {
739
+ logger$3.default('facet', {
638
740
  key,
639
741
  facet
640
742
  });
@@ -645,7 +747,7 @@ const createCache = async (api, pkType, parentCache)=>{
645
747
  ];
646
748
  };
647
749
  const find = async (finder, params = {}, locations = [])=>{
648
- logger$1.default('find', {
750
+ logger$3.default('find', {
649
751
  finder,
650
752
  params,
651
753
  locations
@@ -660,7 +762,7 @@ const createCache = async (api, pkType, parentCache)=>{
660
762
  ];
661
763
  };
662
764
  const findOne = async (finder, finderParams = {}, locations = [])=>{
663
- logger$1.default('findOne', {
765
+ logger$3.default('findOne', {
664
766
  finder,
665
767
  finderParams,
666
768
  locations
@@ -679,20 +781,20 @@ const createCache = async (api, pkType, parentCache)=>{
679
781
  ];
680
782
  };
681
783
  const set = async (key, v)=>{
682
- logger$1.default('set', {
784
+ logger$3.default('set', {
683
785
  key,
684
786
  v
685
787
  });
686
788
  // TODO: This is validating the key, but it doesn't have knowledge of the pkType
687
789
  // This should be looking at the parentCaches and calculating an array of pkTypes
688
790
  if (!core.isValidItemKey(key)) {
689
- logger$1.error('Key for Update is not a valid ItemKey: %j', key);
791
+ logger$3.error('Key for Update is not a valid ItemKey: %j', key);
690
792
  throw new Error('Key for Update is not a valid ItemKey');
691
793
  }
692
794
  // TODO: This could be merged with the isValidItemKey check, later.
693
795
  core.validatePK(v, pkType);
694
- if (!core.isItemKeyEqual(key, v.key)) {
695
- logger$1.error('Key does not match item key: %j != %j', key, v.key);
796
+ if (!isItemKeyEqualNormalized(key, v.key)) {
797
+ logger$3.error('Key does not match item key: %j != %j', key, v.key);
696
798
  throw new Error('Key does not match item key');
697
799
  }
698
800
  cacheMap.set(key, v);
@@ -722,67 +824,70 @@ const createCache = async (api, pkType, parentCache)=>{
722
824
  };
723
825
  };
724
826
 
725
- function _define_property(obj, key, value) {
726
- if (key in obj) {
727
- Object.defineProperty(obj, key, {
728
- value: value,
729
- enumerable: true,
730
- configurable: true,
731
- writable: true
732
- });
733
- } else {
734
- obj[key] = value;
735
- }
736
- return obj;
737
- }
738
- const logger = LibLogger.get('CacheRegistry');
739
- class CacheRegistry {
740
- constructor(){
741
- // TODO: My use of Generics has Boxed me into a corner where I can't reference AbstractCache without the types
742
- _define_property(this, "cacheMap", {});
743
- _define_property(this, "registerCache", async (cache)=>{
744
- try {
745
- logger.debug('Attempting to register cache with pkTypes:', cache.pkTypes);
746
- const key = JSON.stringify(cache.pkTypes);
747
- if (this.cacheMap[key]) {
748
- logger.debug(`Cache with pkTypes ${key} already exists, will be overwritten`);
749
- }
750
- this.cacheMap[key] = cache;
751
- logger.debug('Cache registered successfully with key:', key);
752
- } catch (error) {
753
- logger.error('Failed to register cache:', error);
754
- throw error;
755
- }
756
- });
757
- _define_property(this, "getCache", (kts)=>{
758
- logger.debug('Attempting to get cache for key types:', kts);
759
- const key = JSON.stringify(kts);
760
- logger.debug('Looking up cache with key:', key);
761
- const cache = this.cacheMap[key];
762
- if (!cache) {
763
- logger.warning(`No cache found for key types: ${key}`);
764
- }
765
- return cache;
766
- });
767
- _define_property(this, "printRegisteredCaches", ()=>{
768
- logger.debug('Printing all registered caches:');
769
- const cacheCount = Object.keys(this.cacheMap).length;
770
- logger.debug(`Total number of registered caches: ${cacheCount}`);
771
- if (cacheCount === 0) {
772
- logger.debug('No caches are currently registered');
773
- }
774
- Object.entries(this.cacheMap).forEach(([keyTypes])=>{
775
- logger.debug(`Cache with key types: ${keyTypes}`);
776
- });
827
+ const logger$2 = LibLogger.get("Instance");
828
+ const createInstance = (registry$1, coordinate, cache)=>{
829
+ logger$2.debug("createInstance", {
830
+ coordinate,
831
+ cache,
832
+ registry: registry$1
833
+ });
834
+ const baseInstance = registry.createInstance(registry$1, coordinate);
835
+ return {
836
+ ...baseInstance,
837
+ cache
838
+ };
839
+ };
840
+ const isInstance = (instance)=>{
841
+ return instance !== null && instance !== undefined && instance.coordinate !== undefined && instance.cache !== undefined && instance.registry !== undefined;
842
+ };
843
+
844
+ const logger$1 = LibLogger.get("InstanceFactory");
845
+ /**
846
+ * Factory function for creating cache instances
847
+ */ const createInstanceFactory = (cache)=>{
848
+ return (coordinate, context)=>{
849
+ logger$1.debug("Creating cache instance", {
850
+ coordinate,
851
+ registry: context.registry,
852
+ cache
853
+ });
854
+ return createInstance(context.registry, coordinate, cache);
855
+ };
856
+ };
857
+
858
+ const logger = LibLogger.get("Registry");
859
+ /**
860
+ * Factory function for creating cache registries
861
+ */ const createRegistryFactory = ()=>{
862
+ return (type, registryHub)=>{
863
+ if (type !== 'cache') {
864
+ throw new Error(`Cache registry factory can only create 'cache' type registries, got: ${type}`);
865
+ }
866
+ logger.debug("Creating cache registry", {
867
+ type,
868
+ registryHub
777
869
  });
778
- logger.debug('CacheRegistry instance created');
779
- }
780
- }
781
- _define_property(CacheRegistry, "instance", void 0);
870
+ const baseRegistry = registry.createRegistry(type, registryHub);
871
+ // Cast to Registry for type safety
872
+ return baseRegistry;
873
+ };
874
+ };
875
+ /**
876
+ * Creates a new cache registry instance
877
+ */ const createRegistry = (registryHub)=>{
878
+ const baseRegistry = registry.createRegistry('cache', registryHub);
879
+ return {
880
+ ...baseRegistry
881
+ };
882
+ };
782
883
 
783
884
  exports.CacheMap = CacheMap;
784
- exports.CacheRegistry = CacheRegistry;
785
885
  exports.createAggregator = createAggregator;
786
886
  exports.createCache = createCache;
887
+ exports.createInstance = createInstance;
888
+ exports.createInstanceFactory = createInstanceFactory;
889
+ exports.createRegistry = createRegistry;
890
+ exports.createRegistryFactory = createRegistryFactory;
891
+ exports.isInstance = isInstance;
787
892
  exports.toCacheConfig = toCacheConfig;
788
893
  //# sourceMappingURL=index.cjs.map