@fjell/cache 4.6.14 → 4.6.16
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/Aggregator.d.ts +3 -3
- package/dist/Cache.d.ts +5 -5
- package/dist/CacheMap.d.ts +1 -1
- package/dist/Instance.d.ts +4 -4
- package/dist/InstanceFactory.d.ts +3 -3
- package/dist/Operations.d.ts +4 -4
- package/dist/index.js +715 -0
- package/dist/index.js.map +7 -0
- package/dist/logger.d.ts +1 -1
- package/dist/ops/action.d.ts +3 -3
- package/dist/ops/all.d.ts +3 -3
- package/dist/ops/allAction.d.ts +3 -3
- package/dist/ops/allFacet.d.ts +3 -3
- package/dist/ops/create.d.ts +3 -3
- package/dist/ops/facet.d.ts +3 -3
- package/dist/ops/find.d.ts +3 -3
- package/dist/ops/findOne.d.ts +3 -3
- package/dist/ops/get.d.ts +3 -3
- package/dist/ops/one.d.ts +3 -3
- package/dist/ops/remove.d.ts +3 -3
- package/dist/ops/reset.d.ts +3 -3
- package/dist/ops/retrieve.d.ts +3 -3
- package/dist/ops/set.d.ts +2 -2
- package/dist/ops/update.d.ts +3 -3
- package/package.json +14 -18
- package/dist/Aggregator.cjs.js +0 -322
- package/dist/Aggregator.es.js +0 -317
- package/dist/Cache.cjs.js +0 -35
- package/dist/Cache.es.js +0 -30
- package/dist/CacheMap.cjs.js +0 -170
- package/dist/CacheMap.es.js +0 -166
- package/dist/Instance.cjs.js +0 -23
- package/dist/Instance.es.js +0 -18
- package/dist/InstanceFactory.cjs.js +0 -35
- package/dist/InstanceFactory.es.js +0 -31
- package/dist/Operations.cjs.js +0 -43
- package/dist/Operations.es.js +0 -39
- package/dist/Registry.cjs.js +0 -36
- package/dist/Registry.es.js +0 -31
- package/dist/index.cjs +0 -940
- package/dist/index.cjs.js +0 -26
- package/dist/index.cjs.map +0 -1
- package/dist/index.es.js +0 -8
- package/dist/logger.cjs.js +0 -10
- package/dist/logger.es.js +0 -6
- package/dist/ops/action.cjs.js +0 -28
- package/dist/ops/action.es.js +0 -24
- package/dist/ops/all.cjs.js +0 -33
- package/dist/ops/all.es.js +0 -29
- package/dist/ops/allAction.cjs.js +0 -35
- package/dist/ops/allAction.es.js +0 -31
- package/dist/ops/allFacet.cjs.js +0 -22
- package/dist/ops/allFacet.es.js +0 -18
- package/dist/ops/create.cjs.js +0 -23
- package/dist/ops/create.es.js +0 -19
- package/dist/ops/facet.cjs.js +0 -21
- package/dist/ops/facet.es.js +0 -17
- package/dist/ops/find.cjs.js +0 -26
- package/dist/ops/find.es.js +0 -22
- package/dist/ops/findOne.cjs.js +0 -24
- package/dist/ops/findOne.es.js +0 -20
- package/dist/ops/get.cjs.js +0 -38
- package/dist/ops/get.es.js +0 -34
- package/dist/ops/one.cjs.js +0 -33
- package/dist/ops/one.es.js +0 -29
- package/dist/ops/remove.cjs.js +0 -30
- package/dist/ops/remove.es.js +0 -26
- package/dist/ops/reset.cjs.js +0 -15
- package/dist/ops/reset.es.js +0 -11
- package/dist/ops/retrieve.cjs.js +0 -37
- package/dist/ops/retrieve.es.js +0 -33
- package/dist/ops/set.cjs.js +0 -71
- package/dist/ops/set.es.js +0 -67
- package/dist/ops/update.cjs.js +0 -34
- package/dist/ops/update.es.js +0 -30
- package/docs/docs.config.ts +0 -75
- package/docs/index.html +0 -18
- package/docs/package.json +0 -34
- package/docs/public/README.md +0 -96
- package/docs/public/examples-README.md +0 -302
- package/docs/public/test.txt +0 -0
- package/docs/src/index.css +0 -3
- package/docs/src/main.tsx +0 -12
- package/docs/src/test/setup.ts +0 -1
- package/docs/tsconfig.node.json +0 -15
- package/examples/README.md +0 -302
- package/examples/aggregator-example.ts +0 -329
- package/examples/basic-cache-example.ts +0 -270
- package/examples/cache-map-example.ts +0 -265
- package/vitest.config.ts +0 -44
package/dist/index.js
ADDED
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
// src/logger.ts
|
|
2
|
+
import Logging from "@fjell/logging";
|
|
3
|
+
var LibLogger = Logging.getLogger("@fjell/cache");
|
|
4
|
+
var logger_default = LibLogger;
|
|
5
|
+
|
|
6
|
+
// src/Aggregator.ts
|
|
7
|
+
var logger = logger_default.get("ItemAggregator");
|
|
8
|
+
var toCacheConfig = (config) => {
|
|
9
|
+
let cacheConfig;
|
|
10
|
+
if (config.optional === void 0) {
|
|
11
|
+
cacheConfig = { cache: config, optional: false };
|
|
12
|
+
} else {
|
|
13
|
+
cacheConfig = config;
|
|
14
|
+
}
|
|
15
|
+
return cacheConfig;
|
|
16
|
+
};
|
|
17
|
+
var createAggregator = async (cache, { aggregates = {}, events = {} }) => {
|
|
18
|
+
const populate = async (item) => {
|
|
19
|
+
logger.default("populate", { item });
|
|
20
|
+
for (const key in aggregates) {
|
|
21
|
+
await populateAggregate(key, item);
|
|
22
|
+
}
|
|
23
|
+
for (const key in events) {
|
|
24
|
+
await populateEvent(key, item);
|
|
25
|
+
}
|
|
26
|
+
logger.default("populate done", { item });
|
|
27
|
+
return item;
|
|
28
|
+
};
|
|
29
|
+
const populateAggregate = async (key, item) => {
|
|
30
|
+
logger.default("populate aggregate key", { key });
|
|
31
|
+
const cacheConfig = toCacheConfig(aggregates[key]);
|
|
32
|
+
if (item.refs === void 0) {
|
|
33
|
+
if (cacheConfig.optional === false) {
|
|
34
|
+
logger.error("Item does not have refs an is not optional " + JSON.stringify(item));
|
|
35
|
+
throw new Error("Item does not have refs an is not optional " + JSON.stringify(item));
|
|
36
|
+
} else {
|
|
37
|
+
if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
|
|
38
|
+
delete item.events[key];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} else if (item.refs[key] === void 0) {
|
|
42
|
+
if (cacheConfig.optional === false) {
|
|
43
|
+
logger.error("Item does not have mandatory ref with key, not optional " + key + " " + JSON.stringify(item));
|
|
44
|
+
throw new Error("Item does not have mandatory ref with key, not optional " + key + " " + JSON.stringify(item));
|
|
45
|
+
} else {
|
|
46
|
+
if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
|
|
47
|
+
delete item.events[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
const ref = item.refs[key];
|
|
52
|
+
logger.default("AGG Retrieving Item in Populate", { key: ref });
|
|
53
|
+
const [, newItem] = await cacheConfig.cache.operations.retrieve(ref);
|
|
54
|
+
if (newItem) {
|
|
55
|
+
if (item.aggs === void 0) {
|
|
56
|
+
item.aggs = {};
|
|
57
|
+
}
|
|
58
|
+
item.aggs[key] = {
|
|
59
|
+
key: ref,
|
|
60
|
+
item: newItem
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const populateEvent = async (key, item) => {
|
|
66
|
+
logger.default("populate event key", { key });
|
|
67
|
+
const cacheConfig = toCacheConfig(events[key]);
|
|
68
|
+
if (item.events === void 0) {
|
|
69
|
+
throw new Error("Item does not have events " + JSON.stringify(item));
|
|
70
|
+
} else if (item.events[key] === void 0) {
|
|
71
|
+
if (cacheConfig.optional === false) {
|
|
72
|
+
logger.error("Item does not have mandatory event with key " + key + " " + JSON.stringify(item));
|
|
73
|
+
throw new Error("Item does not have mandatory event with key " + key + " " + JSON.stringify(item));
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
const event = item.events[key];
|
|
77
|
+
if (event.by === void 0) {
|
|
78
|
+
logger.error(
|
|
79
|
+
"populateEvent with an Event that does not have by",
|
|
80
|
+
{ event, ik: item.key, eventKey: key }
|
|
81
|
+
);
|
|
82
|
+
throw new Error("populateEvent with an Event that does not have by: " + JSON.stringify({ key, event }));
|
|
83
|
+
}
|
|
84
|
+
logger.default("EVENT Retrieving Item in Populate", { key: event.by });
|
|
85
|
+
const [, newItem] = await cacheConfig.cache.operations.retrieve(event.by);
|
|
86
|
+
if (newItem) {
|
|
87
|
+
event.agg = newItem;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const all2 = async (query = {}, locations = []) => {
|
|
92
|
+
logger.default("all", { query, locations });
|
|
93
|
+
const [cacheMap, items] = await cache.operations.all(query, locations);
|
|
94
|
+
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
95
|
+
return [cacheMap, populatedItems];
|
|
96
|
+
};
|
|
97
|
+
const one2 = async (query = {}, locations = []) => {
|
|
98
|
+
logger.default("one", { query, locations });
|
|
99
|
+
const [cacheMap, item] = await cache.operations.one(query, locations);
|
|
100
|
+
let populatedItem = null;
|
|
101
|
+
if (item) {
|
|
102
|
+
populatedItem = await populate(item);
|
|
103
|
+
}
|
|
104
|
+
return [cacheMap, populatedItem];
|
|
105
|
+
};
|
|
106
|
+
const action2 = async (key, action3, body = {}) => {
|
|
107
|
+
logger.default("action", { key, action: action3, body });
|
|
108
|
+
const [cacheMap, item] = await cache.operations.action(key, action3, body);
|
|
109
|
+
const populatedItem = await populate(item);
|
|
110
|
+
return [cacheMap, populatedItem];
|
|
111
|
+
};
|
|
112
|
+
const allAction2 = async (action3, body = {}, locations = []) => {
|
|
113
|
+
logger.default("action", { action: action3, body, locations });
|
|
114
|
+
const [cacheMap, items] = await cache.operations.allAction(action3, body, locations);
|
|
115
|
+
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
116
|
+
return [cacheMap, populatedItems];
|
|
117
|
+
};
|
|
118
|
+
const allFacet2 = async (facet3, params = {}, locations = []) => {
|
|
119
|
+
logger.default("allFacet", { facet: facet3, params, locations });
|
|
120
|
+
const [cacheMap, response] = await cache.operations.allFacet(facet3, params, locations);
|
|
121
|
+
return [cacheMap, response];
|
|
122
|
+
};
|
|
123
|
+
const create2 = async (v, locations = []) => {
|
|
124
|
+
logger.default("create", { v, locations });
|
|
125
|
+
const [cacheMap, item] = await cache.operations.create(v, locations);
|
|
126
|
+
const populatedItem = await populate(item);
|
|
127
|
+
return [cacheMap, populatedItem];
|
|
128
|
+
};
|
|
129
|
+
const get2 = async (key) => {
|
|
130
|
+
logger.default("get", { key });
|
|
131
|
+
const [cacheMap, item] = await cache.operations.get(key);
|
|
132
|
+
let populatedItem = null;
|
|
133
|
+
if (item) {
|
|
134
|
+
populatedItem = await populate(item);
|
|
135
|
+
}
|
|
136
|
+
return [cacheMap, populatedItem];
|
|
137
|
+
};
|
|
138
|
+
const retrieve2 = async (key) => {
|
|
139
|
+
logger.default("retrieve", { key });
|
|
140
|
+
const [cacheMap, item] = await cache.operations.retrieve(key);
|
|
141
|
+
let populatedItem = null;
|
|
142
|
+
if (item) {
|
|
143
|
+
populatedItem = await populate(item);
|
|
144
|
+
}
|
|
145
|
+
return [cacheMap, populatedItem];
|
|
146
|
+
};
|
|
147
|
+
const remove2 = async (key) => {
|
|
148
|
+
logger.default("remove", { key });
|
|
149
|
+
const cacheMap = await cache.operations.remove(key);
|
|
150
|
+
return cacheMap;
|
|
151
|
+
};
|
|
152
|
+
const update2 = async (key, v) => {
|
|
153
|
+
logger.default("update", { key, v });
|
|
154
|
+
const [cacheMap, item] = await cache.operations.update(key, v);
|
|
155
|
+
const populatedItem = await populate(item);
|
|
156
|
+
return [cacheMap, populatedItem];
|
|
157
|
+
};
|
|
158
|
+
const facet2 = async (key, facet3) => {
|
|
159
|
+
logger.default("facet", { key, facet: facet3 });
|
|
160
|
+
const [cacheMap, response] = await cache.operations.facet(key, facet3);
|
|
161
|
+
return [cacheMap, response];
|
|
162
|
+
};
|
|
163
|
+
const find2 = async (finder, finderParams = {}, locations = []) => {
|
|
164
|
+
logger.default("find", { finder, finderParams, locations });
|
|
165
|
+
const [cacheMap, items] = await cache.operations.find(finder, finderParams, locations);
|
|
166
|
+
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
167
|
+
return [cacheMap, populatedItems];
|
|
168
|
+
};
|
|
169
|
+
const findOne2 = async (finder, finderParams = {}, locations = []) => {
|
|
170
|
+
logger.default("find", { finder, finderParams, locations });
|
|
171
|
+
const [cacheMap, item] = await cache.operations.findOne(finder, finderParams, locations);
|
|
172
|
+
const populatedItem = await populate(item);
|
|
173
|
+
return [cacheMap, populatedItem];
|
|
174
|
+
};
|
|
175
|
+
const set2 = async (key, v) => {
|
|
176
|
+
logger.default("set", { key, v });
|
|
177
|
+
const [cacheMap, item] = await cache.operations.set(key, v);
|
|
178
|
+
const populatedItem = await populate(item);
|
|
179
|
+
return [cacheMap, populatedItem];
|
|
180
|
+
};
|
|
181
|
+
const reset2 = async () => {
|
|
182
|
+
const cacheMap = await cache.operations.reset();
|
|
183
|
+
return cacheMap;
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
// Cache properties
|
|
187
|
+
coordinate: cache.coordinate,
|
|
188
|
+
registry: cache.registry,
|
|
189
|
+
api: cache.api,
|
|
190
|
+
cacheMap: cache.cacheMap,
|
|
191
|
+
operations: cache.operations,
|
|
192
|
+
// Cache operations exposed directly
|
|
193
|
+
all: all2,
|
|
194
|
+
one: one2,
|
|
195
|
+
action: action2,
|
|
196
|
+
allAction: allAction2,
|
|
197
|
+
allFacet: allFacet2,
|
|
198
|
+
create: create2,
|
|
199
|
+
get: get2,
|
|
200
|
+
retrieve: retrieve2,
|
|
201
|
+
remove: remove2,
|
|
202
|
+
update: update2,
|
|
203
|
+
facet: facet2,
|
|
204
|
+
find: find2,
|
|
205
|
+
findOne: findOne2,
|
|
206
|
+
reset: reset2,
|
|
207
|
+
set: set2,
|
|
208
|
+
// Aggregator-specific operations
|
|
209
|
+
populate,
|
|
210
|
+
populateAggregate,
|
|
211
|
+
populateEvent
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// src/CacheMap.ts
|
|
216
|
+
import {
|
|
217
|
+
Dictionary,
|
|
218
|
+
isComKey,
|
|
219
|
+
isQueryMatch
|
|
220
|
+
} from "@fjell/core";
|
|
221
|
+
var logger2 = logger_default.get("CacheMap");
|
|
222
|
+
var normalizeKeyValue = (value) => {
|
|
223
|
+
return String(value);
|
|
224
|
+
};
|
|
225
|
+
var createNormalizedHashFunction = () => {
|
|
226
|
+
return (key) => {
|
|
227
|
+
if (typeof key === "object" && key !== null) {
|
|
228
|
+
const normalizedKey = JSON.parse(JSON.stringify(key));
|
|
229
|
+
if ("pk" in normalizedKey && normalizedKey.pk !== null) {
|
|
230
|
+
normalizedKey.pk = normalizeKeyValue(normalizedKey.pk);
|
|
231
|
+
}
|
|
232
|
+
if ("lk" in normalizedKey && normalizedKey.lk !== null) {
|
|
233
|
+
normalizedKey.lk = normalizeKeyValue(normalizedKey.lk);
|
|
234
|
+
}
|
|
235
|
+
if ("loc" in normalizedKey && Array.isArray(normalizedKey.loc)) {
|
|
236
|
+
normalizedKey.loc = normalizedKey.loc.map((locItem) => {
|
|
237
|
+
if (locItem && "lk" in locItem && locItem.lk !== null) {
|
|
238
|
+
return { ...locItem, lk: normalizeKeyValue(locItem.lk) };
|
|
239
|
+
}
|
|
240
|
+
return locItem;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return JSON.stringify(normalizedKey);
|
|
244
|
+
}
|
|
245
|
+
return JSON.stringify(key);
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
var isLocKeyArrayEqual = (a, b) => {
|
|
249
|
+
if (a.length !== b.length) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
for (let i = 0; i < a.length; i++) {
|
|
253
|
+
const normalizedA = normalizeLocKeyItem(a[i]);
|
|
254
|
+
const normalizedB = normalizeLocKeyItem(b[i]);
|
|
255
|
+
if (JSON.stringify(normalizedA) !== JSON.stringify(normalizedB)) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return true;
|
|
260
|
+
};
|
|
261
|
+
var normalizeLocKeyItem = (item) => {
|
|
262
|
+
if (typeof item === "object" && item !== null) {
|
|
263
|
+
const normalized = { ...item };
|
|
264
|
+
if ("lk" in normalized && normalized.lk !== null) {
|
|
265
|
+
normalized.lk = normalizeKeyValue(normalized.lk);
|
|
266
|
+
}
|
|
267
|
+
return normalized;
|
|
268
|
+
}
|
|
269
|
+
return item;
|
|
270
|
+
};
|
|
271
|
+
var CacheMap = class _CacheMap extends Dictionary {
|
|
272
|
+
types;
|
|
273
|
+
constructor(types, map) {
|
|
274
|
+
super(map, createNormalizedHashFunction());
|
|
275
|
+
this.types = types;
|
|
276
|
+
}
|
|
277
|
+
get(key) {
|
|
278
|
+
return super.get(key);
|
|
279
|
+
}
|
|
280
|
+
allIn(locations) {
|
|
281
|
+
if (locations.length === 0) {
|
|
282
|
+
logger2.debug("Returning all items, LocKeys is empty");
|
|
283
|
+
return this.values();
|
|
284
|
+
} else {
|
|
285
|
+
const locKeys = locations;
|
|
286
|
+
logger2.debug("allIn", { locKeys, keys: this.keys().length });
|
|
287
|
+
return this.keys().filter((key) => key && isComKey(key)).filter((key) => {
|
|
288
|
+
const ComKey8 = key;
|
|
289
|
+
logger2.debug("Comparing Location Keys", {
|
|
290
|
+
locKeys,
|
|
291
|
+
ComKey: ComKey8
|
|
292
|
+
});
|
|
293
|
+
return isLocKeyArrayEqual(locKeys, ComKey8.loc);
|
|
294
|
+
}).map((key) => this.get(key));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
// TODO: Can we do case insensitive matching?
|
|
298
|
+
contains(query, locations) {
|
|
299
|
+
logger2.debug("contains", { query, locations });
|
|
300
|
+
const items = this.allIn(locations);
|
|
301
|
+
return items.some((item) => isQueryMatch(item, query));
|
|
302
|
+
}
|
|
303
|
+
queryIn(query, locations = []) {
|
|
304
|
+
logger2.debug("queryIn", { query, locations });
|
|
305
|
+
const items = this.allIn(locations);
|
|
306
|
+
return items.filter((item) => isQueryMatch(item, query));
|
|
307
|
+
}
|
|
308
|
+
clone() {
|
|
309
|
+
const clone = new _CacheMap(this.types, this.map);
|
|
310
|
+
return clone;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// src/ops/all.ts
|
|
315
|
+
import {
|
|
316
|
+
validatePK
|
|
317
|
+
} from "@fjell/core";
|
|
318
|
+
import { NotFoundError } from "@fjell/http-api";
|
|
319
|
+
var logger3 = logger_default.get("all");
|
|
320
|
+
var all = async (api, cacheMap, pkType, query = {}, locations = []) => {
|
|
321
|
+
logger3.default("all", { query, locations });
|
|
322
|
+
let ret = [];
|
|
323
|
+
try {
|
|
324
|
+
ret = await api.all(query, locations);
|
|
325
|
+
ret.forEach((v) => {
|
|
326
|
+
cacheMap.set(v.key, v);
|
|
327
|
+
});
|
|
328
|
+
} catch (e) {
|
|
329
|
+
if (e instanceof NotFoundError) {
|
|
330
|
+
} else {
|
|
331
|
+
throw e;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return [cacheMap, validatePK(ret, pkType)];
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/ops/one.ts
|
|
338
|
+
import {
|
|
339
|
+
validatePK as validatePK2
|
|
340
|
+
} from "@fjell/core";
|
|
341
|
+
import { NotFoundError as NotFoundError2 } from "@fjell/http-api";
|
|
342
|
+
var logger4 = logger_default.get("one");
|
|
343
|
+
var one = async (api, cacheMap, pkType, query = {}, locations = []) => {
|
|
344
|
+
logger4.default("one", { query, locations });
|
|
345
|
+
let retItem = null;
|
|
346
|
+
try {
|
|
347
|
+
retItem = await api.one(query, locations);
|
|
348
|
+
if (retItem) {
|
|
349
|
+
cacheMap.set(retItem.key, retItem);
|
|
350
|
+
}
|
|
351
|
+
} catch (e) {
|
|
352
|
+
if (e instanceof NotFoundError2) {
|
|
353
|
+
} else {
|
|
354
|
+
throw e;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return [
|
|
358
|
+
cacheMap,
|
|
359
|
+
retItem ? validatePK2(retItem, pkType) : null
|
|
360
|
+
];
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
// src/ops/create.ts
|
|
364
|
+
import {
|
|
365
|
+
validatePK as validatePK3
|
|
366
|
+
} from "@fjell/core";
|
|
367
|
+
var logger5 = logger_default.get("create");
|
|
368
|
+
var create = async (api, cacheMap, pkType, v, locations = []) => {
|
|
369
|
+
logger5.default("create", { v, locations });
|
|
370
|
+
const created = await api.create(v, locations);
|
|
371
|
+
cacheMap.set(created.key, created);
|
|
372
|
+
return [cacheMap, validatePK3(created, pkType)];
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// src/ops/get.ts
|
|
376
|
+
import {
|
|
377
|
+
isValidItemKey,
|
|
378
|
+
validatePK as validatePK4
|
|
379
|
+
} from "@fjell/core";
|
|
380
|
+
var logger6 = logger_default.get("get");
|
|
381
|
+
var get = async (api, cacheMap, pkType, key) => {
|
|
382
|
+
logger6.default("get", { key });
|
|
383
|
+
if (!isValidItemKey(key)) {
|
|
384
|
+
logger6.error("Key for Get is not a valid ItemKey: %j", key);
|
|
385
|
+
throw new Error("Key for Get is not a valid ItemKey");
|
|
386
|
+
}
|
|
387
|
+
let ret;
|
|
388
|
+
try {
|
|
389
|
+
ret = await api.get(key);
|
|
390
|
+
if (ret) {
|
|
391
|
+
cacheMap.set(ret.key, ret);
|
|
392
|
+
}
|
|
393
|
+
} catch (e) {
|
|
394
|
+
logger6.error("Error getting item for key", { key, message: e.message, stack: e.stack });
|
|
395
|
+
throw e;
|
|
396
|
+
}
|
|
397
|
+
return [
|
|
398
|
+
cacheMap,
|
|
399
|
+
ret ? validatePK4(ret, pkType) : null
|
|
400
|
+
];
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// src/ops/retrieve.ts
|
|
404
|
+
import {
|
|
405
|
+
isValidItemKey as isValidItemKey2,
|
|
406
|
+
validatePK as validatePK5
|
|
407
|
+
} from "@fjell/core";
|
|
408
|
+
var logger7 = logger_default.get("retrieve");
|
|
409
|
+
var retrieve = async (api, cacheMap, pkType, key) => {
|
|
410
|
+
logger7.default("retrieve", { key });
|
|
411
|
+
if (!isValidItemKey2(key)) {
|
|
412
|
+
logger7.error("Key for Retrieve is not a valid ItemKey: %j", key);
|
|
413
|
+
throw new Error("Key for Retrieve is not a valid ItemKey");
|
|
414
|
+
}
|
|
415
|
+
const containsItemKey = cacheMap.includesKey(key);
|
|
416
|
+
let retrieved;
|
|
417
|
+
if (containsItemKey) {
|
|
418
|
+
logger7.default("Looking for Object in Cache", key);
|
|
419
|
+
retrieved = cacheMap.get(key);
|
|
420
|
+
} else {
|
|
421
|
+
logger7.default("Object Not Found in Cache, Retrieving from Server API", { key });
|
|
422
|
+
[, retrieved] = await get(api, cacheMap, pkType, key);
|
|
423
|
+
}
|
|
424
|
+
const retValue = [
|
|
425
|
+
containsItemKey ? null : cacheMap,
|
|
426
|
+
retrieved ? validatePK5(retrieved, pkType) : null
|
|
427
|
+
];
|
|
428
|
+
return retValue;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// src/ops/remove.ts
|
|
432
|
+
import {
|
|
433
|
+
isValidItemKey as isValidItemKey3
|
|
434
|
+
} from "@fjell/core";
|
|
435
|
+
var logger8 = logger_default.get("remove");
|
|
436
|
+
var remove = async (api, cacheMap, key) => {
|
|
437
|
+
logger8.default("remove", { key });
|
|
438
|
+
if (!isValidItemKey3(key)) {
|
|
439
|
+
logger8.error("Key for Remove is not a valid ItemKey: %j", key);
|
|
440
|
+
throw new Error("Key for Remove is not a valid ItemKey");
|
|
441
|
+
}
|
|
442
|
+
try {
|
|
443
|
+
await api.remove(key);
|
|
444
|
+
cacheMap.delete(key);
|
|
445
|
+
} catch (e) {
|
|
446
|
+
logger8.error("Error deleting item", { error: e });
|
|
447
|
+
throw e;
|
|
448
|
+
}
|
|
449
|
+
return cacheMap;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
// src/ops/update.ts
|
|
453
|
+
import {
|
|
454
|
+
isValidItemKey as isValidItemKey4,
|
|
455
|
+
validatePK as validatePK6
|
|
456
|
+
} from "@fjell/core";
|
|
457
|
+
var logger9 = logger_default.get("update");
|
|
458
|
+
var update = async (api, cacheMap, pkType, key, v) => {
|
|
459
|
+
logger9.default("update", { key, v });
|
|
460
|
+
if (!isValidItemKey4(key)) {
|
|
461
|
+
logger9.error("Key for Update is not a valid ItemKey: %j", key);
|
|
462
|
+
throw new Error("Key for Update is not a valid ItemKey");
|
|
463
|
+
}
|
|
464
|
+
try {
|
|
465
|
+
const updated = await api.update(key, v);
|
|
466
|
+
cacheMap.set(updated.key, updated);
|
|
467
|
+
return [cacheMap, validatePK6(updated, pkType)];
|
|
468
|
+
} catch (e) {
|
|
469
|
+
logger9.error("Error updating item", { error: e });
|
|
470
|
+
throw e;
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// src/ops/action.ts
|
|
475
|
+
import {
|
|
476
|
+
isValidItemKey as isValidItemKey5,
|
|
477
|
+
validatePK as validatePK7
|
|
478
|
+
} from "@fjell/core";
|
|
479
|
+
var logger10 = logger_default.get("action");
|
|
480
|
+
var action = async (api, cacheMap, pkType, key, action2, body = {}) => {
|
|
481
|
+
logger10.default("action", { key, action: action2, body });
|
|
482
|
+
if (!isValidItemKey5(key)) {
|
|
483
|
+
logger10.error("Key for Action is not a valid ItemKey: %j", key);
|
|
484
|
+
throw new Error("Key for Action is not a valid ItemKey");
|
|
485
|
+
}
|
|
486
|
+
const updated = await api.action(key, action2, body);
|
|
487
|
+
cacheMap.set(updated.key, updated);
|
|
488
|
+
return [cacheMap, validatePK7(updated, pkType)];
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// src/ops/allAction.ts
|
|
492
|
+
import {
|
|
493
|
+
validatePK as validatePK8
|
|
494
|
+
} from "@fjell/core";
|
|
495
|
+
import { NotFoundError as NotFoundError3 } from "@fjell/http-api";
|
|
496
|
+
var logger11 = logger_default.get("allAction");
|
|
497
|
+
var allAction = async (api, cacheMap, pkType, action2, body = {}, locations = []) => {
|
|
498
|
+
logger11.default("allAction", { action: action2, body, locations });
|
|
499
|
+
let ret = [];
|
|
500
|
+
try {
|
|
501
|
+
ret = await api.allAction(action2, body, locations);
|
|
502
|
+
ret.forEach((v) => {
|
|
503
|
+
cacheMap.set(v.key, v);
|
|
504
|
+
});
|
|
505
|
+
} catch (e) {
|
|
506
|
+
if (e instanceof NotFoundError3) {
|
|
507
|
+
} else {
|
|
508
|
+
throw e;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return [cacheMap, validatePK8(ret, pkType)];
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
// src/ops/facet.ts
|
|
515
|
+
var logger12 = logger_default.get("facet");
|
|
516
|
+
var facet = async (api, cacheMap, key, facet2, params = {}) => {
|
|
517
|
+
logger12.default("facet", { key, facet: facet2 });
|
|
518
|
+
const ret = await api.facet(key, facet2, params);
|
|
519
|
+
return [cacheMap, ret];
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
// src/ops/allFacet.ts
|
|
523
|
+
var logger13 = logger_default.get("allFacet");
|
|
524
|
+
var allFacet = async (api, cacheMap, facet2, params = {}, locations = []) => {
|
|
525
|
+
logger13.default("allFacet", { facet: facet2, params, locations });
|
|
526
|
+
const ret = await api.allFacet(facet2, params, locations);
|
|
527
|
+
return [cacheMap, ret];
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
// src/ops/find.ts
|
|
531
|
+
import {
|
|
532
|
+
validatePK as validatePK9
|
|
533
|
+
} from "@fjell/core";
|
|
534
|
+
var logger14 = logger_default.get("find");
|
|
535
|
+
var find = async (api, cacheMap, pkType, finder, params = {}, locations = []) => {
|
|
536
|
+
logger14.default("find", { finder, params, locations });
|
|
537
|
+
const ret = await api.find(finder, params, locations);
|
|
538
|
+
ret.forEach((v) => {
|
|
539
|
+
cacheMap.set(v.key, v);
|
|
540
|
+
});
|
|
541
|
+
return [cacheMap, validatePK9(ret, pkType)];
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/ops/findOne.ts
|
|
545
|
+
import {
|
|
546
|
+
validatePK as validatePK10
|
|
547
|
+
} from "@fjell/core";
|
|
548
|
+
var logger15 = logger_default.get("findOne");
|
|
549
|
+
var findOne = async (api, cacheMap, pkType, finder, finderParams = {}, locations = []) => {
|
|
550
|
+
logger15.default("findOne", { finder, finderParams, locations });
|
|
551
|
+
const ret = await api.findOne(finder, finderParams, locations);
|
|
552
|
+
cacheMap.set(ret.key, ret);
|
|
553
|
+
return [cacheMap, validatePK10(ret, pkType)];
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
// src/ops/set.ts
|
|
557
|
+
import {
|
|
558
|
+
isItemKeyEqual,
|
|
559
|
+
isValidItemKey as isValidItemKey6,
|
|
560
|
+
validatePK as validatePK11
|
|
561
|
+
} from "@fjell/core";
|
|
562
|
+
var logger16 = logger_default.get("set");
|
|
563
|
+
var normalizeKeyValue2 = (value) => {
|
|
564
|
+
return String(value);
|
|
565
|
+
};
|
|
566
|
+
var isItemKeyEqualNormalized = (a, b) => {
|
|
567
|
+
const normalizedA = normalizeKey(a);
|
|
568
|
+
const normalizedB = normalizeKey(b);
|
|
569
|
+
return isItemKeyEqual(normalizedA, normalizedB);
|
|
570
|
+
};
|
|
571
|
+
var normalizeKey = (key) => {
|
|
572
|
+
if (typeof key === "object" && key !== null) {
|
|
573
|
+
const normalizedKey = JSON.parse(JSON.stringify(key));
|
|
574
|
+
if ("pk" in normalizedKey && normalizedKey.pk !== null) {
|
|
575
|
+
normalizedKey.pk = normalizeKeyValue2(normalizedKey.pk);
|
|
576
|
+
}
|
|
577
|
+
if ("lk" in normalizedKey && normalizedKey.lk !== null) {
|
|
578
|
+
normalizedKey.lk = normalizeKeyValue2(normalizedKey.lk);
|
|
579
|
+
}
|
|
580
|
+
if ("loc" in normalizedKey && Array.isArray(normalizedKey.loc)) {
|
|
581
|
+
normalizedKey.loc = normalizedKey.loc.map((locItem) => {
|
|
582
|
+
if (locItem && "lk" in locItem && locItem.lk !== null) {
|
|
583
|
+
return { ...locItem, lk: normalizeKeyValue2(locItem.lk) };
|
|
584
|
+
}
|
|
585
|
+
return locItem;
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return normalizedKey;
|
|
589
|
+
}
|
|
590
|
+
return key;
|
|
591
|
+
};
|
|
592
|
+
var set = async (cacheMap, pkType, key, v) => {
|
|
593
|
+
logger16.default("set", { key, v });
|
|
594
|
+
if (!isValidItemKey6(key)) {
|
|
595
|
+
logger16.error("Key for Set is not a valid ItemKey: %j", key);
|
|
596
|
+
throw new Error("Key for Set is not a valid ItemKey");
|
|
597
|
+
}
|
|
598
|
+
validatePK11(v, pkType);
|
|
599
|
+
if (!isItemKeyEqualNormalized(key, v.key)) {
|
|
600
|
+
logger16.error("Key does not match item key: %j != %j", key, v.key);
|
|
601
|
+
throw new Error("Key does not match item key");
|
|
602
|
+
}
|
|
603
|
+
cacheMap.set(key, v);
|
|
604
|
+
return [cacheMap, validatePK11(v, pkType)];
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
// src/ops/reset.ts
|
|
608
|
+
var reset = async (coordinate) => {
|
|
609
|
+
const cacheMap = new CacheMap(coordinate.kta);
|
|
610
|
+
return [cacheMap];
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
// src/Operations.ts
|
|
614
|
+
var createOperations = (api, coordinate, cacheMap, pkType) => {
|
|
615
|
+
return {
|
|
616
|
+
all: (query, locations) => all(api, cacheMap, pkType, query, locations),
|
|
617
|
+
one: (query, locations) => one(api, cacheMap, pkType, query, locations),
|
|
618
|
+
create: (item, locations) => create(api, cacheMap, pkType, item, locations),
|
|
619
|
+
get: (key) => get(api, cacheMap, pkType, key),
|
|
620
|
+
retrieve: (key) => retrieve(api, cacheMap, pkType, key),
|
|
621
|
+
remove: (key) => remove(api, cacheMap, key),
|
|
622
|
+
update: (key, item) => update(api, cacheMap, pkType, key, item),
|
|
623
|
+
action: (key, actionName, body) => action(api, cacheMap, pkType, key, actionName, body),
|
|
624
|
+
allAction: (actionName, body, locations) => allAction(api, cacheMap, pkType, actionName, body, locations),
|
|
625
|
+
facet: (key, facetName, params) => facet(api, cacheMap, key, facetName, params),
|
|
626
|
+
allFacet: (facetName, params, locations) => allFacet(api, cacheMap, facetName, params, locations),
|
|
627
|
+
find: (finder, params, locations) => find(api, cacheMap, pkType, finder, params, locations),
|
|
628
|
+
findOne: (finder, params, locations) => findOne(api, cacheMap, pkType, finder, params, locations),
|
|
629
|
+
set: (key, item) => set(cacheMap, pkType, key, item),
|
|
630
|
+
reset: () => reset(coordinate)
|
|
631
|
+
};
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// src/Cache.ts
|
|
635
|
+
var logger17 = logger_default.get("Cache");
|
|
636
|
+
var createCache = (api, coordinate, registry) => {
|
|
637
|
+
logger17.debug("createCache", { coordinate, registry });
|
|
638
|
+
const cacheMap = new CacheMap(coordinate.kta);
|
|
639
|
+
const pkType = coordinate.kta[0];
|
|
640
|
+
const operations = createOperations(api, coordinate, cacheMap, pkType);
|
|
641
|
+
return {
|
|
642
|
+
coordinate,
|
|
643
|
+
registry,
|
|
644
|
+
api,
|
|
645
|
+
cacheMap,
|
|
646
|
+
operations
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
var isCache = (cache) => {
|
|
650
|
+
return cache !== null && typeof cache === "object" && "coordinate" in cache && "registry" in cache && "api" in cache && "cacheMap" in cache && "operations" in cache;
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/Instance.ts
|
|
654
|
+
var logger18 = logger_default.get("Instance");
|
|
655
|
+
var createInstance = (registry, coordinate, api) => {
|
|
656
|
+
logger18.debug("createInstance", { coordinate, api, registry });
|
|
657
|
+
return createCache(api, coordinate, registry);
|
|
658
|
+
};
|
|
659
|
+
var isInstance = (instance) => {
|
|
660
|
+
return instance !== null && typeof instance === "object" && "coordinate" in instance && "registry" in instance && "api" in instance && "cacheMap" in instance && "operations" in instance;
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/InstanceFactory.ts
|
|
664
|
+
var logger19 = logger_default.get("InstanceFactory");
|
|
665
|
+
var createInstanceFactory = (api) => {
|
|
666
|
+
return (coordinate, context) => {
|
|
667
|
+
logger19.debug("Creating cache instance", { coordinate, registry: context.registry, api });
|
|
668
|
+
const cacheMap = new CacheMap(coordinate.kta);
|
|
669
|
+
const pkType = coordinate.kta[0];
|
|
670
|
+
const operations = createOperations(api, coordinate, cacheMap, pkType);
|
|
671
|
+
return {
|
|
672
|
+
coordinate,
|
|
673
|
+
registry: context.registry,
|
|
674
|
+
api,
|
|
675
|
+
cacheMap,
|
|
676
|
+
operations
|
|
677
|
+
};
|
|
678
|
+
};
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
// src/Registry.ts
|
|
682
|
+
import {
|
|
683
|
+
createRegistry as createBaseRegistry
|
|
684
|
+
} from "@fjell/registry";
|
|
685
|
+
var logger20 = logger_default.get("Registry");
|
|
686
|
+
var createRegistryFactory = () => {
|
|
687
|
+
return (type, registryHub) => {
|
|
688
|
+
if (type !== "cache") {
|
|
689
|
+
throw new Error(`Cache registry factory can only create 'cache' type registries, got: ${type}`);
|
|
690
|
+
}
|
|
691
|
+
logger20.debug("Creating cache registry", { type, registryHub });
|
|
692
|
+
const baseRegistry = createBaseRegistry(type, registryHub);
|
|
693
|
+
return baseRegistry;
|
|
694
|
+
};
|
|
695
|
+
};
|
|
696
|
+
var createRegistry = (registryHub) => {
|
|
697
|
+
const baseRegistry = createBaseRegistry("cache", registryHub);
|
|
698
|
+
return {
|
|
699
|
+
...baseRegistry
|
|
700
|
+
};
|
|
701
|
+
};
|
|
702
|
+
export {
|
|
703
|
+
CacheMap,
|
|
704
|
+
createAggregator,
|
|
705
|
+
createCache,
|
|
706
|
+
createInstance,
|
|
707
|
+
createInstanceFactory,
|
|
708
|
+
createOperations,
|
|
709
|
+
createRegistry,
|
|
710
|
+
createRegistryFactory,
|
|
711
|
+
isCache,
|
|
712
|
+
isInstance,
|
|
713
|
+
toCacheConfig
|
|
714
|
+
};
|
|
715
|
+
//# sourceMappingURL=index.js.map
|