@fjell/cache 4.6.13 → 4.6.15

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 (90) hide show
  1. package/dist/Aggregator.d.ts +3 -3
  2. package/dist/Cache.d.ts +6 -6
  3. package/dist/CacheMap.d.ts +1 -1
  4. package/dist/Instance.d.ts +5 -5
  5. package/dist/InstanceFactory.d.ts +3 -3
  6. package/dist/Operations.d.ts +4 -4
  7. package/dist/index.js +715 -0
  8. package/dist/index.js.map +7 -0
  9. package/dist/logger.d.ts +1 -1
  10. package/dist/ops/action.d.ts +3 -3
  11. package/dist/ops/all.d.ts +3 -3
  12. package/dist/ops/allAction.d.ts +3 -3
  13. package/dist/ops/allFacet.d.ts +3 -3
  14. package/dist/ops/create.d.ts +3 -3
  15. package/dist/ops/facet.d.ts +3 -3
  16. package/dist/ops/find.d.ts +3 -3
  17. package/dist/ops/findOne.d.ts +3 -3
  18. package/dist/ops/get.d.ts +3 -3
  19. package/dist/ops/one.d.ts +3 -3
  20. package/dist/ops/remove.d.ts +3 -3
  21. package/dist/ops/reset.d.ts +3 -3
  22. package/dist/ops/retrieve.d.ts +3 -3
  23. package/dist/ops/set.d.ts +2 -2
  24. package/dist/ops/update.d.ts +3 -3
  25. package/package.json +8 -12
  26. package/dist/Aggregator.cjs.js +0 -322
  27. package/dist/Aggregator.es.js +0 -317
  28. package/dist/Cache.cjs.js +0 -35
  29. package/dist/Cache.es.js +0 -30
  30. package/dist/CacheMap.cjs.js +0 -170
  31. package/dist/CacheMap.es.js +0 -166
  32. package/dist/Instance.cjs.js +0 -23
  33. package/dist/Instance.es.js +0 -18
  34. package/dist/InstanceFactory.cjs.js +0 -35
  35. package/dist/InstanceFactory.es.js +0 -31
  36. package/dist/Operations.cjs.js +0 -43
  37. package/dist/Operations.es.js +0 -39
  38. package/dist/Registry.cjs.js +0 -36
  39. package/dist/Registry.es.js +0 -31
  40. package/dist/index.cjs +0 -940
  41. package/dist/index.cjs.js +0 -26
  42. package/dist/index.cjs.map +0 -1
  43. package/dist/index.es.js +0 -8
  44. package/dist/logger.cjs.js +0 -10
  45. package/dist/logger.es.js +0 -6
  46. package/dist/ops/action.cjs.js +0 -28
  47. package/dist/ops/action.es.js +0 -24
  48. package/dist/ops/all.cjs.js +0 -33
  49. package/dist/ops/all.es.js +0 -29
  50. package/dist/ops/allAction.cjs.js +0 -35
  51. package/dist/ops/allAction.es.js +0 -31
  52. package/dist/ops/allFacet.cjs.js +0 -22
  53. package/dist/ops/allFacet.es.js +0 -18
  54. package/dist/ops/create.cjs.js +0 -23
  55. package/dist/ops/create.es.js +0 -19
  56. package/dist/ops/facet.cjs.js +0 -21
  57. package/dist/ops/facet.es.js +0 -17
  58. package/dist/ops/find.cjs.js +0 -26
  59. package/dist/ops/find.es.js +0 -22
  60. package/dist/ops/findOne.cjs.js +0 -24
  61. package/dist/ops/findOne.es.js +0 -20
  62. package/dist/ops/get.cjs.js +0 -38
  63. package/dist/ops/get.es.js +0 -34
  64. package/dist/ops/one.cjs.js +0 -33
  65. package/dist/ops/one.es.js +0 -29
  66. package/dist/ops/remove.cjs.js +0 -30
  67. package/dist/ops/remove.es.js +0 -26
  68. package/dist/ops/reset.cjs.js +0 -15
  69. package/dist/ops/reset.es.js +0 -11
  70. package/dist/ops/retrieve.cjs.js +0 -37
  71. package/dist/ops/retrieve.es.js +0 -33
  72. package/dist/ops/set.cjs.js +0 -71
  73. package/dist/ops/set.es.js +0 -67
  74. package/dist/ops/update.cjs.js +0 -34
  75. package/dist/ops/update.es.js +0 -30
  76. package/docs/docs.config.ts +0 -75
  77. package/docs/index.html +0 -18
  78. package/docs/package.json +0 -34
  79. package/docs/public/README.md +0 -96
  80. package/docs/public/examples-README.md +0 -302
  81. package/docs/public/test.txt +0 -0
  82. package/docs/src/index.css +0 -3
  83. package/docs/src/main.tsx +0 -12
  84. package/docs/src/test/setup.ts +0 -1
  85. package/docs/tsconfig.node.json +0 -15
  86. package/examples/README.md +0 -302
  87. package/examples/aggregator-example.ts +0 -329
  88. package/examples/basic-cache-example.ts +0 -270
  89. package/examples/cache-map-example.ts +0 -265
  90. package/vitest.config.ts +0 -44
package/dist/index.cjs DELETED
@@ -1,940 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const Logging = require('@fjell/logging');
6
- const core = require('@fjell/core');
7
- const httpApi = require('@fjell/http-api');
8
- const registry = require('@fjell/registry');
9
-
10
- const logger$1.default = Logging.getLogger('@fjell/cache');
11
-
12
- const logger$j = logger$1.default.get('ItemAggregator');
13
- const toCacheConfig = (config)=>{
14
- let cacheConfig;
15
- if (config.optional === undefined) {
16
- cacheConfig = {
17
- cache: config,
18
- optional: false
19
- };
20
- } else {
21
- cacheConfig = config;
22
- }
23
- return cacheConfig;
24
- };
25
- const createAggregator = async (cache, { aggregates = {}, events = {} })=>{
26
- const populate = async (item)=>{
27
- logger$j.default('populate', {
28
- item
29
- });
30
- for(const key in aggregates){
31
- await populateAggregate(key, item);
32
- }
33
- for(const key in events){
34
- await populateEvent(key, item);
35
- }
36
- logger$j.default('populate done', {
37
- item
38
- });
39
- return item;
40
- };
41
- const populateAggregate = async (key, item)=>{
42
- logger$j.default('populate aggregate key', {
43
- key
44
- });
45
- const cacheConfig = toCacheConfig(aggregates[key]);
46
- if (item.refs === undefined) {
47
- if (cacheConfig.optional === false) {
48
- logger$j.error('Item does not have refs an is not optional ' + JSON.stringify(item));
49
- throw new Error('Item does not have refs an is not optional ' + JSON.stringify(item));
50
- } else {
51
- if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
52
- delete item.events[key];
53
- }
54
- }
55
- } else if (item.refs[key] === undefined) {
56
- if (cacheConfig.optional === false) {
57
- logger$j.error('Item does not have mandatory ref with key, not optional ' + key + ' ' + JSON.stringify(item));
58
- throw new Error('Item does not have mandatory ref with key, not optional ' + key + ' ' + JSON.stringify(item));
59
- } else {
60
- if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
61
- delete item.events[key];
62
- }
63
- }
64
- } else {
65
- const ref = item.refs[key];
66
- logger$j.default('AGG Retrieving Item in Populate', {
67
- key: ref
68
- });
69
- const [, newItem] = await cacheConfig.cache.operations.retrieve(ref);
70
- if (newItem) {
71
- if (item.aggs === undefined) {
72
- item.aggs = {};
73
- }
74
- item.aggs[key] = {
75
- key: ref,
76
- item: newItem
77
- };
78
- }
79
- }
80
- };
81
- // TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
82
- const populateEvent = async (key, item)=>{
83
- logger$j.default('populate event key', {
84
- key
85
- });
86
- const cacheConfig = toCacheConfig(events[key]);
87
- if (item.events === undefined) {
88
- throw new Error('Item does not have events ' + JSON.stringify(item));
89
- } else if (item.events[key] === undefined) {
90
- if (cacheConfig.optional === false) {
91
- logger$j.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
92
- throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
93
- }
94
- } else {
95
- const event = item.events[key];
96
- if (event.by === undefined) {
97
- logger$j.error('populateEvent with an Event that does not have by', {
98
- event,
99
- ik: item.key,
100
- eventKey: key
101
- });
102
- throw new Error('populateEvent with an Event that does not have by: ' + JSON.stringify({
103
- key,
104
- event
105
- }));
106
- }
107
- logger$j.default('EVENT Retrieving Item in Populate', {
108
- key: event.by
109
- });
110
- const [, newItem] = await cacheConfig.cache.operations.retrieve(event.by);
111
- if (newItem) {
112
- event.agg = newItem;
113
- }
114
- }
115
- };
116
- const all = async (query = {}, locations = [])=>{
117
- logger$j.default('all', {
118
- query,
119
- locations
120
- });
121
- const [cacheMap, items] = await cache.operations.all(query, locations);
122
- const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
123
- return [
124
- cacheMap,
125
- populatedItems
126
- ];
127
- };
128
- const one = async (query = {}, locations = [])=>{
129
- logger$j.default('one', {
130
- query,
131
- locations
132
- });
133
- const [cacheMap, item] = await cache.operations.one(query, locations);
134
- let populatedItem = null;
135
- if (item) {
136
- populatedItem = await populate(item);
137
- }
138
- return [
139
- cacheMap,
140
- populatedItem
141
- ];
142
- };
143
- const action = async (key, action, body = {})=>{
144
- logger$j.default('action', {
145
- key,
146
- action,
147
- body
148
- });
149
- const [cacheMap, item] = await cache.operations.action(key, action, body);
150
- const populatedItem = await populate(item);
151
- return [
152
- cacheMap,
153
- populatedItem
154
- ];
155
- };
156
- const allAction = async (action, body = {}, locations = [])=>{
157
- logger$j.default('action', {
158
- action,
159
- body,
160
- locations
161
- });
162
- const [cacheMap, items] = await cache.operations.allAction(action, body, locations);
163
- const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
164
- return [
165
- cacheMap,
166
- populatedItems
167
- ];
168
- };
169
- const allFacet = async (facet, params = {}, locations = [])=>{
170
- logger$j.default('allFacet', {
171
- facet,
172
- params,
173
- locations
174
- });
175
- const [cacheMap, response] = await cache.operations.allFacet(facet, params, locations);
176
- return [
177
- cacheMap,
178
- response
179
- ];
180
- };
181
- const create = async (v, locations = [])=>{
182
- logger$j.default('create', {
183
- v,
184
- locations
185
- });
186
- const [cacheMap, item] = await cache.operations.create(v, locations);
187
- const populatedItem = await populate(item);
188
- return [
189
- cacheMap,
190
- populatedItem
191
- ];
192
- };
193
- const get = async (key)=>{
194
- logger$j.default('get', {
195
- key
196
- });
197
- const [cacheMap, item] = await cache.operations.get(key);
198
- let populatedItem = null;
199
- if (item) {
200
- populatedItem = await populate(item);
201
- }
202
- return [
203
- cacheMap,
204
- populatedItem
205
- ];
206
- };
207
- const retrieve = async (key)=>{
208
- logger$j.default('retrieve', {
209
- key
210
- });
211
- const [cacheMap, item] = await cache.operations.retrieve(key);
212
- let populatedItem = null;
213
- if (item) {
214
- populatedItem = await populate(item);
215
- }
216
- return [
217
- cacheMap,
218
- populatedItem
219
- ];
220
- };
221
- const remove = async (key)=>{
222
- logger$j.default('remove', {
223
- key
224
- });
225
- const cacheMap = await cache.operations.remove(key);
226
- return cacheMap;
227
- };
228
- const update = async (key, v)=>{
229
- logger$j.default('update', {
230
- key,
231
- v
232
- });
233
- const [cacheMap, item] = await cache.operations.update(key, v);
234
- const populatedItem = await populate(item);
235
- return [
236
- cacheMap,
237
- populatedItem
238
- ];
239
- };
240
- // Facets are a pass-thru for aggregators
241
- const facet = async (key, facet)=>{
242
- logger$j.default('facet', {
243
- key,
244
- facet
245
- });
246
- const [cacheMap, response] = await cache.operations.facet(key, facet);
247
- return [
248
- cacheMap,
249
- response
250
- ];
251
- };
252
- const find = async (finder, finderParams = {}, locations = [])=>{
253
- logger$j.default('find', {
254
- finder,
255
- finderParams,
256
- locations
257
- });
258
- const [cacheMap, items] = await cache.operations.find(finder, finderParams, locations);
259
- const populatedItems = await Promise.all(items.map(async (item)=>populate(item)));
260
- return [
261
- cacheMap,
262
- populatedItems
263
- ];
264
- };
265
- const findOne = async (finder, finderParams = {}, locations = [])=>{
266
- logger$j.default('find', {
267
- finder,
268
- finderParams,
269
- locations
270
- });
271
- const [cacheMap, item] = await cache.operations.findOne(finder, finderParams, locations);
272
- const populatedItem = await populate(item);
273
- return [
274
- cacheMap,
275
- populatedItem
276
- ];
277
- };
278
- const set = async (key, v)=>{
279
- logger$j.default('set', {
280
- key,
281
- v
282
- });
283
- // TODO: There should be some input validation here to ensure a valid item.
284
- const [cacheMap, item] = await cache.operations.set(key, v);
285
- const populatedItem = await populate(item);
286
- return [
287
- cacheMap,
288
- populatedItem
289
- ];
290
- };
291
- const reset = async ()=>{
292
- const cacheMap = await cache.operations.reset();
293
- return cacheMap;
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
303
- all,
304
- one,
305
- action,
306
- allAction,
307
- allFacet,
308
- create,
309
- get,
310
- retrieve,
311
- remove,
312
- update,
313
- facet,
314
- find,
315
- findOne,
316
- reset,
317
- set,
318
- // Aggregator-specific operations
319
- populate,
320
- populateAggregate,
321
- populateEvent
322
- };
323
- };
324
-
325
- function _define_property(obj, key, value) {
326
- if (key in obj) {
327
- Object.defineProperty(obj, key, {
328
- value: value,
329
- enumerable: true,
330
- configurable: true,
331
- writable: true
332
- });
333
- } else {
334
- obj[key] = value;
335
- }
336
- return obj;
337
- }
338
- const logger$i = logger$1.default.get("CacheMap");
339
- // Normalize a key value to string for consistent comparison and hashing
340
- const normalizeKeyValue$1 = (value)=>{
341
- return String(value);
342
- };
343
- // Normalized hash function for Dictionary that converts pk/lk values to strings
344
- const createNormalizedHashFunction = ()=>{
345
- return (key)=>{
346
- if (typeof key === 'object' && key !== null) {
347
- // Create a normalized version of the key with string values
348
- const normalizedKey = JSON.parse(JSON.stringify(key));
349
- // Normalize pk values
350
- if ('pk' in normalizedKey && normalizedKey.pk !== null) {
351
- normalizedKey.pk = normalizeKeyValue$1(normalizedKey.pk);
352
- }
353
- // Normalize lk values
354
- if ('lk' in normalizedKey && normalizedKey.lk !== null) {
355
- normalizedKey.lk = normalizeKeyValue$1(normalizedKey.lk);
356
- }
357
- // Normalize loc array lk values
358
- if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
359
- normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
360
- if (locItem && 'lk' in locItem && locItem.lk !== null) {
361
- return {
362
- ...locItem,
363
- lk: normalizeKeyValue$1(locItem.lk)
364
- };
365
- }
366
- return locItem;
367
- });
368
- }
369
- return JSON.stringify(normalizedKey);
370
- }
371
- return JSON.stringify(key);
372
- };
373
- };
374
- // Helper function to normalize and compare location key arrays
375
- const isLocKeyArrayEqual = (a, b)=>{
376
- if (a.length !== b.length) {
377
- return false;
378
- }
379
- for(let i = 0; i < a.length; i++){
380
- const normalizedA = normalizeLocKeyItem(a[i]);
381
- const normalizedB = normalizeLocKeyItem(b[i]);
382
- if (JSON.stringify(normalizedA) !== JSON.stringify(normalizedB)) {
383
- return false;
384
- }
385
- }
386
- return true;
387
- };
388
- // Helper function to normalize a location key item
389
- const normalizeLocKeyItem = (item)=>{
390
- if (typeof item === 'object' && item !== null) {
391
- const normalized = {
392
- ...item
393
- };
394
- if ('lk' in normalized && normalized.lk !== null) {
395
- normalized.lk = normalizeKeyValue$1(normalized.lk);
396
- }
397
- return normalized;
398
- }
399
- return item;
400
- };
401
- // const isObj = (x: any) => typeof x === "object" && x !== null;
402
- // const intersection = (a: object, b: object): object => {
403
- // const result: { [key: string]: any } = {}
404
- // if (([a, b]).every(isObj)) {
405
- // Object.keys(a).forEach((key) => {
406
- // // @ts-ignore
407
- // const value = a[key]
408
- // // @ts-ignore
409
- // const other = b[key]
410
- // if (isObj(value)) {
411
- // result[key] = intersection(value, other)
412
- // } else if (value === other) {
413
- // result[key] = value
414
- // }
415
- // })
416
- // }
417
- // return result
418
- // }
419
- // const removeEmptyObjects = (obj: object): object => {
420
- // const result: { [key: string]: any } = {}
421
- // Object.keys(obj).forEach((key) => {
422
- // // @ts-ignore
423
- // const value = obj[key];
424
- // if (isObj(value)) {
425
- // const nested = removeEmptyObjects(value);
426
- // if (Object.keys(nested).length > 0) {
427
- // result[key] = nested
428
- // }
429
- // } else if (value !== null) {
430
- // result[key] = value
431
- // }
432
- // });
433
- // return result;
434
- // }
435
- class CacheMap extends core.Dictionary {
436
- get(key) {
437
- return super.get(key);
438
- }
439
- allIn(locations) {
440
- if (locations.length === 0) {
441
- logger$i.debug('Returning all items, LocKeys is empty');
442
- return this.values();
443
- } else {
444
- const locKeys = locations;
445
- logger$i.debug('allIn', {
446
- locKeys,
447
- keys: this.keys().length
448
- });
449
- return this.keys().filter((key)=>key && core.isComKey(key)).filter((key)=>{
450
- const ComKey = key;
451
- logger$i.debug('Comparing Location Keys', {
452
- locKeys,
453
- ComKey
454
- });
455
- return isLocKeyArrayEqual(locKeys, ComKey.loc);
456
- }).map((key)=>this.get(key));
457
- }
458
- }
459
- // TODO: Can we do case insensitive matching?
460
- contains(query, locations) {
461
- logger$i.debug('contains', {
462
- query,
463
- locations
464
- });
465
- const items = this.allIn(locations);
466
- return items.some((item)=>core.isQueryMatch(item, query));
467
- }
468
- queryIn(query, locations = []) {
469
- logger$i.debug('queryIn', {
470
- query,
471
- locations
472
- });
473
- const items = this.allIn(locations);
474
- return items.filter((item)=>core.isQueryMatch(item, query));
475
- }
476
- clone() {
477
- const clone = new CacheMap(this.types, this.map);
478
- return clone;
479
- }
480
- constructor(types, map){
481
- super(map, createNormalizedHashFunction()), _define_property(this, "types", void 0);
482
- this.types = types;
483
- }
484
- }
485
-
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');
752
- // Normalize a key value to string for consistent comparison
753
- const normalizeKeyValue = (value)=>{
754
- return String(value);
755
- };
756
- // Normalized key comparison function that handles string/number differences
757
- const isItemKeyEqualNormalized = (a, b)=>{
758
- // For now, just normalize the keys to strings and use the original comparison
759
- const normalizedA = normalizeKey(a);
760
- const normalizedB = normalizeKey(b);
761
- return core.isItemKeyEqual(normalizedA, normalizedB);
762
- };
763
- // Helper function to normalize a key
764
- const normalizeKey = (key)=>{
765
- if (typeof key === 'object' && key !== null) {
766
- const normalizedKey = JSON.parse(JSON.stringify(key));
767
- // Normalize pk values
768
- if ('pk' in normalizedKey && normalizedKey.pk !== null) {
769
- normalizedKey.pk = normalizeKeyValue(normalizedKey.pk);
770
- }
771
- // Normalize lk values
772
- if ('lk' in normalizedKey && normalizedKey.lk !== null) {
773
- normalizedKey.lk = normalizeKeyValue(normalizedKey.lk);
774
- }
775
- // Normalize loc array lk values
776
- if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
777
- normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
778
- if (locItem && 'lk' in locItem && locItem.lk !== null) {
779
- return {
780
- ...locItem,
781
- lk: normalizeKeyValue(locItem.lk)
782
- };
783
- }
784
- return locItem;
785
- });
786
- }
787
- return normalizedKey;
788
- }
789
- return key;
790
- };
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');
799
- }
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)
838
- };
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
859
- };
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,
897
- cacheMap,
898
- operations
899
- };
900
- };
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}`);
910
- }
911
- logger.debug("Creating cache registry", {
912
- type,
913
- registryHub
914
- });
915
- const baseRegistry = registry.createRegistry(type, registryHub);
916
- // Cast to Registry for type safety
917
- return baseRegistry;
918
- };
919
- };
920
- /**
921
- * Creates a new cache registry instance
922
- */ const createRegistry = (registryHub)=>{
923
- const baseRegistry = registry.createRegistry('cache', registryHub);
924
- return {
925
- ...baseRegistry
926
- };
927
- };
928
-
929
- exports.CacheMap = CacheMap;
930
- exports.createAggregator = createAggregator;
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;
939
- exports.toCacheConfig = toCacheConfig;
940
- //# sourceMappingURL=index.cjs.map