@fjell/cache 4.6.3 → 4.6.5

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/Cache.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AllItemTypeArrays, ComKey, Item, ItemQuery, LocKeyArray, PriKey, TypesProperties } from '@fjell/core';
1
+ import { AllItemTypeArrays, ComKey, Item, ItemQuery, LocKeyArray, PriKey } from '@fjell/core';
2
2
  import { CacheMap } from './CacheMap';
3
3
  import { ClientApi } from '@fjell/client-api';
4
4
  export interface Cache<V extends Item<S, L1, L2, L3, L4, L5>, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> {
@@ -7,11 +7,11 @@ export interface Cache<V extends Item<S, L1, L2, L3, L4, L5>, S extends string,
7
7
  action: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, action: string, body?: any) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
8
8
  allAction: (action: string, body?: any, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
9
9
  allFacet: (facet: string, params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
10
- create: (item: TypesProperties<V, S, L1, L2, L3, L4, L5>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
10
+ create: (item: Partial<Item<S, L1, L2, L3, L4, L5>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
11
11
  get: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
12
12
  retrieve: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
13
13
  remove: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
14
- update: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, item: TypesProperties<V, S, L1, L2, L3, L4, L5>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
14
+ update: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, item: Partial<Item<S, L1, L2, L3, L4, L5>>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
15
15
  facet: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, facet: string, params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
16
16
  find: (finder: string, params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
17
17
  findOne: (finder: string, params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@fjell/cache",
3
3
  "description": "Cache for Fjell",
4
- "version": "4.6.3",
4
+ "version": "4.6.5",
5
+ "keywords": [
6
+ "cache",
7
+ "fjell"
8
+ ],
5
9
  "license": "Apache-2.0",
6
10
  "type": "module",
7
11
  "main": "./dist/index.cjs.js",
@@ -15,40 +19,33 @@
15
19
  }
16
20
  },
17
21
  "dependencies": {
18
- "@fjell/client-api": "^4.4.3",
19
- "@fjell/core": "^4.4.3",
20
- "@fjell/http-api": "^4.4.1",
21
- "@fjell/logging": "^4.4.3",
22
- "d3": "^7.9.0",
23
- "dayjs": "^1.11.13",
24
- "react": "19.1.0"
22
+ "@fjell/client-api": "^4.4.5",
23
+ "@fjell/core": "^4.4.6",
24
+ "@fjell/http-api": "^4.4.4",
25
+ "@fjell/logging": "^4.4.6"
25
26
  },
26
27
  "devDependencies": {
27
- "@babel/preset-env": "^7.26.9",
28
- "@babel/preset-typescript": "^7.26.0",
29
28
  "@eslint/eslintrc": "^3.3.1",
30
- "@eslint/js": "^9.27.0",
31
- "@swc/core": "^1.11.24",
32
- "@tsconfig/recommended": "^1.0.8",
33
- "@types/babel__preset-env": "^7.10.0",
34
- "@types/d3": "^7.4.3",
35
- "@types/multer": "^1.4.12",
36
- "@types/node": "^24.0.3",
37
- "@typescript-eslint/eslint-plugin": "^8.24.1",
38
- "@typescript-eslint/parser": "^8.24.1",
39
- "@vitest/coverage-v8": "^3.1.4",
40
- "@vitest/ui": "^3.1.4",
41
- "concurrently": "^9.1.2",
42
- "eslint": "^9.21.0",
43
- "nodemon": "^3.1.9",
29
+ "@eslint/js": "^9.30.1",
30
+ "@swc/core": "^1.12.11",
31
+ "@tsconfig/recommended": "^1.0.10",
32
+ "@types/multer": "^2.0.0",
33
+ "@types/node": "^24.0.12",
34
+ "@typescript-eslint/eslint-plugin": "^8.36.0",
35
+ "@typescript-eslint/parser": "^8.36.0",
36
+ "@vitest/coverage-v8": "^3.2.4",
37
+ "@vitest/ui": "^3.2.4",
38
+ "concurrently": "^9.2.0",
39
+ "eslint": "^9.30.1",
40
+ "nodemon": "^3.1.10",
44
41
  "rimraf": "^6.0.1",
45
42
  "ts-node": "^10.9.2",
46
- "tsc-alias": "^1.8.10",
47
- "typescript": "^5.7.3",
48
- "vite": "^6.3.5",
43
+ "tsc-alias": "^1.8.16",
44
+ "typescript": "^5.8.3",
45
+ "vite": "^7.0.3",
49
46
  "vite-plugin-dts": "^4.5.4",
50
- "vite-plugin-node": "^5.0.1",
51
- "vitest": "^3.1.4"
47
+ "vite-plugin-node": "^7.0.0",
48
+ "vitest": "^3.2.4"
52
49
  },
53
50
  "repository": {
54
51
  "type": "git",
@@ -57,6 +54,7 @@
57
54
  "scripts": {
58
55
  "dev": "concurrently \"tsc --noEmit --watch\" \"vite build --watch\"",
59
56
  "build": "pnpm run lint && tsc --noEmit && vite build",
57
+ "typecheck": "tsc --noEmit",
60
58
  "lint": "eslint . --ext .ts --fix",
61
59
  "clean": "rimraf dist",
62
60
  "test": "pnpm run lint && vitest run --coverage"
@@ -1,10 +0,0 @@
1
- verbose: false
2
- model: gpt-4.1
3
- contextDirectories:
4
- - .kodrdriv/context
5
- commit:
6
- cached: true
7
- sendit: true
8
- release:
9
- from: main
10
- to: HEAD
@@ -1 +0,0 @@
1
- This is the Fjell Library for Client-side Caches
package/src/Aggregator.ts DELETED
@@ -1,331 +0,0 @@
1
- /* eslint-disable no-undefined */
2
- import {
3
- ComKey,
4
- Item,
5
- ItemQuery,
6
- LocKeyArray,
7
- PriKey,
8
- TypesProperties
9
- } from "@fjell/core";
10
- import { Cache } from "./Cache";
11
- import { CacheMap } from "./CacheMap";
12
- import LibLogger from "./logger";
13
-
14
- const logger = LibLogger.get('ItemAggregator');
15
-
16
- export interface Aggregator<
17
- V extends Item<S, L1, L2, L3, L4, L5>,
18
- S extends string,
19
- L1 extends string = never,
20
- L2 extends string = never,
21
- L3 extends string = never,
22
- L4 extends string = never,
23
- L5 extends string = never
24
- > extends Cache<V, S, L1, L2, L3, L4, L5> {
25
- populate: (item: V) => Promise<V>;
26
- populateAggregate: (key: string, item: V) => Promise<void>;
27
- populateEvent: (key: string, item: V) => Promise<void>;
28
- }
29
-
30
- export interface CacheConfig { cache: any, optional: boolean }
31
-
32
- export interface AggregateConfig { [key: string]: (CacheConfig) }
33
-
34
- export const toCacheConfig = <
35
- V extends Item<S, L1, L2, L3, L4, L5>,
36
- S extends string,
37
- L1 extends string = never,
38
- L2 extends string = never,
39
- L3 extends string = never,
40
- L4 extends string = never,
41
- L5 extends string = never
42
- >(config: CacheConfig | Cache<V, S, L1, L2, L3, L4, L5>): CacheConfig => {
43
- let cacheConfig: CacheConfig;
44
- if ((config as CacheConfig).optional === undefined) {
45
- cacheConfig = { cache: config as any, optional: false };
46
- } else {
47
- cacheConfig = config as CacheConfig;
48
- }
49
- return cacheConfig;
50
- }
51
-
52
- export const createAggregator = async <
53
- V extends Item<S, L1, L2, L3, L4, L5>,
54
- S extends string,
55
- L1 extends string = never,
56
- L2 extends string = never,
57
- L3 extends string = never,
58
- L4 extends string = never,
59
- L5 extends string = never
60
- >(
61
- cache: Cache<V, S, L1, L2, L3, L4, L5>,
62
- { aggregates = {}, events = {} }:
63
- {
64
- aggregates?: AggregateConfig,
65
- events?: AggregateConfig
66
- }
67
- ): Promise<Aggregator<V, S, L1, L2, L3, L4, L5>> => {
68
-
69
- const populate = async (item: V): Promise<V> => {
70
- logger.default('populate', { item });
71
- for (const key in aggregates) {
72
- await populateAggregate(key, item);
73
- }
74
- for (const key in events) {
75
- await populateEvent(key, item);
76
- }
77
- logger.default('populate done', { item });
78
- return item;
79
- }
80
-
81
- const populateAggregate = async (key: string, item: V) => {
82
- logger.default('populate aggregate key', { key });
83
- const cacheConfig = toCacheConfig(aggregates[key]);
84
- if (item.refs === undefined) {
85
- if (cacheConfig.optional === false) {
86
- logger.error('Item does not have refs an is not optional ' + JSON.stringify(item));
87
- throw new Error('Item does not have refs an is not optional ' + JSON.stringify(item));
88
- } else {
89
- if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
90
- delete item.events[key];
91
- }
92
- }
93
- } else if (item.refs[key] === undefined) {
94
- if (cacheConfig.optional === false) {
95
- logger.error('Item does not have mandatory ref with key, not optional ' +
96
- key + ' ' + JSON.stringify(item));
97
- throw new Error('Item does not have mandatory ref with key, not optional ' +
98
- key + ' ' + JSON.stringify(item));
99
- } else {
100
- if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
101
- delete item.events[key];
102
- }
103
- }
104
- } else {
105
-
106
- const ref = item.refs[key];
107
-
108
- logger.default('AGG Retrieving Item in Populate', { key: ref });
109
- const [, newItem] = await cacheConfig.cache.retrieve(ref);
110
- if (newItem) {
111
- if (item.aggs === undefined) {
112
- item.aggs = {};
113
- }
114
- item.aggs[key] = {
115
- key: ref,
116
- item: newItem as Item,
117
- };
118
- }
119
- }
120
- }
121
-
122
- // TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
123
- const populateEvent = async (key: string, item: V) => {
124
- logger.default('populate event key', { key });
125
- const cacheConfig = toCacheConfig(events[key]);
126
-
127
- if (item.events === undefined) {
128
- throw new Error('Item does not have events ' + JSON.stringify(item));
129
- } else if (item.events[key] === undefined) {
130
- if (cacheConfig.optional === false) {
131
- logger.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
132
- throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
133
- }
134
- } else {
135
- const event = item.events[key];
136
-
137
- if (event.by === undefined) {
138
- logger.error(
139
- 'populateEvent with an Event that does not have by', { event, ik: item.key, eventKey: key });
140
- throw new Error('populateEvent with an Event that does not have by: ' + JSON.stringify({ key, event }));
141
- }
142
-
143
- logger.default('EVENT Retrieving Item in Populate', { key: event.by });
144
- const [, newItem] = await cacheConfig.cache.retrieve(event.by);
145
- if (newItem) {
146
- event.agg = newItem as Item;
147
- }
148
- }
149
- }
150
-
151
- const all = async (
152
- query: ItemQuery = {},
153
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
154
- ):
155
- Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
156
- logger.default('all', { query, locations });
157
- const [cacheMap, items] = await cache.all(query, locations);
158
- const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
159
- return [cacheMap, populatedItems];
160
- }
161
-
162
- const one = async (
163
- query: ItemQuery = {},
164
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
165
- ):
166
- Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
167
- logger.default('one', { query, locations });
168
- const [cacheMap, item] = await cache.one(query, locations);
169
- let populatedItem = null;
170
- if (item) {
171
- populatedItem = await populate(item);
172
- }
173
- return [cacheMap, populatedItem];
174
- }
175
-
176
- const action = async (
177
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
178
- action: string,
179
- body: any = {},
180
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
181
- logger.default('action', { key, action, body });
182
- const [cacheMap, item] = await cache.action(key, action, body);
183
- const populatedItem = await populate(item);
184
- return [cacheMap, populatedItem];
185
- }
186
-
187
- const allAction = async (
188
- action: string,
189
- body: any = {},
190
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
191
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
192
- logger.default('action', { action, body, locations });
193
- const [cacheMap, items] = await cache.allAction(action, body, locations);
194
- const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
195
- return [cacheMap, populatedItems];
196
- }
197
-
198
- const allFacet = async (
199
- facet: string,
200
- params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
201
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
202
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
203
- logger.default('allFacet', { facet, params, locations });
204
- const [cacheMap, response] = await cache.allFacet(facet, params, locations);
205
- return [cacheMap, response];
206
- }
207
-
208
- const create = async (
209
- v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
210
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
211
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
212
- logger.default('create', { v, locations });
213
- const [cacheMap, item] = await cache.create(v, locations);
214
- const populatedItem = await populate(item);
215
- return [cacheMap, populatedItem];
216
- }
217
-
218
- const get = async (
219
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
220
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
221
- logger.default('get', { key });
222
- const [cacheMap, item] = await cache.get(key);
223
- let populatedItem = null;
224
- if (item) {
225
- populatedItem = await populate(item);
226
- }
227
- return [cacheMap, populatedItem];
228
- }
229
-
230
- const retrieve = async (
231
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
232
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
233
- logger.default('retrieve', { key });
234
- const [cacheMap, item] = await cache.retrieve(key);
235
- let populatedItem = null;
236
- if (item) {
237
- populatedItem = await populate(item);
238
- }
239
- return [cacheMap, populatedItem];
240
- }
241
-
242
- const remove = async (
243
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
244
- ): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
245
- logger.default('remove', { key });
246
- const cacheMap = await cache.remove(key);
247
- return cacheMap;
248
- }
249
-
250
- const update = async (
251
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
252
- v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
253
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
254
- logger.default('update', { key, v });
255
- const [cacheMap, item] = await cache.update(key, v);
256
- const populatedItem = await populate(item);
257
- return [cacheMap, populatedItem];
258
- }
259
-
260
- // Facets are a pass-thru for aggregators
261
- const facet = async (
262
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
263
- facet: string,
264
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
265
- logger.default('facet', { key, facet });
266
- const [cacheMap, response] = await cache.facet(key, facet);
267
- return [cacheMap, response];
268
- }
269
-
270
- const find = async (
271
- finder: string,
272
- finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
273
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
274
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
275
- logger.default('find', { finder, finderParams, locations });
276
- const [cacheMap, items] = await cache.find(finder, finderParams, locations);
277
- const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
278
- return [cacheMap, populatedItems];
279
- }
280
-
281
- const findOne = async (
282
- finder: string,
283
- finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
284
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
285
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
286
- logger.default('find', { finder, finderParams, locations });
287
- const [cacheMap, item] = await cache.findOne(finder, finderParams, locations);
288
- const populatedItem = await populate(item);
289
- return [cacheMap, populatedItem];
290
- }
291
-
292
- const set = async (
293
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
294
- v: Item<S, L1, L2, L3, L4, L5>
295
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
296
- logger.default('set', { key, v });
297
-
298
- // TODO: There should be some input validation here to ensure a valid item.
299
- const [cacheMap, item] = await cache.set(key, v);
300
- const populatedItem = await populate(item);
301
- return [cacheMap, populatedItem];
302
- }
303
-
304
- const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
305
- const cacheMap = await cache.reset();
306
- return cacheMap;
307
- }
308
-
309
- return {
310
- all,
311
- one,
312
- action,
313
- allAction,
314
- allFacet,
315
- create,
316
- get,
317
- retrieve,
318
- remove,
319
- update,
320
- facet,
321
- find,
322
- findOne,
323
- reset,
324
- set,
325
- pkTypes: cache.pkTypes,
326
- cacheMap: cache.cacheMap,
327
- populate,
328
- populateAggregate,
329
- populateEvent
330
- }
331
- }
package/src/Cache.ts DELETED
@@ -1,429 +0,0 @@
1
- import {
2
- AllItemTypeArrays,
3
- ComKey,
4
- isItemKeyEqual,
5
- isValidItemKey,
6
- Item,
7
- ItemQuery,
8
- LocKeyArray,
9
- PriKey,
10
- TypesProperties,
11
- validatePK
12
- } from "@fjell/core";
13
- import { CacheMap } from "./CacheMap";
14
- import LibLogger from "./logger";
15
-
16
- import { ClientApi } from "@fjell/client-api";
17
- import { NotFoundError } from "@fjell/http-api";
18
-
19
- const logger = LibLogger.get('Cache');
20
-
21
- export interface Cache<
22
- V extends Item<S, L1, L2, L3, L4, L5>,
23
- S extends string,
24
- L1 extends string = never,
25
- L2 extends string = never,
26
- L3 extends string = never,
27
- L4 extends string = never,
28
- L5 extends string = never
29
- > {
30
-
31
- all: (
32
- query?: ItemQuery,
33
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
34
- ) =>
35
- Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>,
36
-
37
- one: (
38
- query?: ItemQuery,
39
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
40
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>
41
-
42
- action: (
43
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
44
- action: string,
45
- body?: any,
46
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>
47
-
48
- allAction: (
49
- action: string,
50
- body?: any,
51
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
52
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>
53
-
54
- allFacet: (
55
- facet: string,
56
- params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
57
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
58
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
59
-
60
- create: (
61
- item: TypesProperties<V, S, L1, L2, L3, L4, L5>,
62
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
63
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
64
-
65
- get: (
66
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
67
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
68
-
69
- retrieve: (
70
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
71
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
72
-
73
- remove: (
74
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
75
- ) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
76
-
77
- update: (
78
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
79
- item: TypesProperties<V, S, L1, L2, L3, L4, L5>,
80
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
81
-
82
- facet: (
83
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
84
- facet: string,
85
- params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
86
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
87
-
88
- find: (
89
- finder: string,
90
- params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
91
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
92
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
93
-
94
- findOne: (
95
- finder: string,
96
- params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
97
- locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
98
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
99
-
100
- set: (
101
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
102
- item: Item<S, L1, L2, L3, L4, L5>
103
- ) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
104
-
105
- reset: () => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
106
-
107
- pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
108
-
109
- cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
110
- }
111
-
112
- export const createCache = async <
113
- V extends Item<S, L1, L2, L3, L4, L5>,
114
- S extends string,
115
- L1 extends string = never,
116
- L2 extends string = never,
117
- L3 extends string = never,
118
- L4 extends string = never,
119
- L5 extends string = never
120
- >(
121
- api: ClientApi<V, S, L1, L2, L3, L4, L5>,
122
- pkType: S,
123
- parentCache?: Cache<Item<L1, L2, L3, L4, L5>, L1, L2, L3, L4, L5>
124
- ): Promise<Cache<V, S, L1, L2, L3, L4, L5>> => {
125
-
126
- let pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5> = [pkType];
127
- if (parentCache) {
128
- pkTypes = pkTypes.concat(parentCache.pkTypes as any) as unknown as AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
129
- }
130
-
131
- let cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5> =
132
- new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes as AllItemTypeArrays<S, L1, L2, L3, L4, L5>);
133
-
134
- const all = async (
135
- query: ItemQuery = {},
136
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
137
- ):
138
- Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
139
- logger.default('all', { query, locations });
140
- let ret: V[] = [];
141
- try {
142
- ret = await api.all(query, locations);
143
- ret.forEach((v) => {
144
- cacheMap.set(v.key, v);
145
- });
146
- } catch (e: unknown) {
147
- if (e instanceof NotFoundError) {
148
- } else {
149
- throw e;
150
- }
151
-
152
- }
153
- return [cacheMap, validatePK(ret, pkType) as V[]];
154
- }
155
-
156
- const one = async (
157
- query: ItemQuery = {},
158
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
159
- ):
160
- Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
161
- logger.default('one', { query, locations });
162
-
163
- let retItem: V | null = null;
164
- try {
165
- retItem = await api.one(query, locations);
166
- if (retItem) {
167
- cacheMap.set(retItem.key, retItem);
168
- }
169
- } catch (e: unknown) {
170
- if (e instanceof NotFoundError) {
171
- } else {
172
- throw e;
173
- }
174
-
175
- }
176
- return [
177
- cacheMap,
178
- retItem ?
179
- validatePK(retItem, pkType) as V :
180
- null
181
- ];
182
- }
183
-
184
- const action = async (
185
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
186
- action: string,
187
- body: any = {},
188
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
189
- logger.default('action', { key, action, body });
190
-
191
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
192
- // This should be looking at the parentCaches and calculating an array of pkTypes
193
- if (!isValidItemKey(key)) {
194
- logger.error('Key for Action is not a valid ItemKey: %j', key);
195
- throw new Error('Key for Action is not a valid ItemKey');
196
- }
197
-
198
- const updated = await api.action(key, action, body);
199
- cacheMap.set(updated.key, updated);
200
- return [cacheMap, validatePK(updated, pkType) as V];
201
- }
202
-
203
- const allAction = async (
204
- action: string,
205
- body: any = {},
206
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
207
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
208
- logger.default('allAction', { action, body, locations });
209
- let ret: V[] = [];
210
- try {
211
- ret = await api.allAction(action, body, locations);
212
- ret.forEach((v) => {
213
- cacheMap.set(v.key, v);
214
- });
215
- } catch (e: unknown) {
216
- // istanbul ignore next
217
- if (e instanceof NotFoundError) {
218
- } else {
219
- throw e;
220
- }
221
-
222
- }
223
- return [cacheMap, validatePK(ret, pkType) as V[]];
224
- }
225
-
226
- const allFacet = async (
227
- facet: string,
228
- params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
229
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
230
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
231
- logger.default('allFacet', { facet, params, locations });
232
- const ret = await api.allFacet(facet, params, locations);
233
- return [cacheMap, ret];
234
- }
235
-
236
- const create = async (
237
- v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
238
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
239
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
240
- logger.default('create', { v, locations });
241
- const created = await api.create(v, locations);
242
- cacheMap.set(created.key, created);
243
- return [cacheMap, validatePK(created, pkType) as V];
244
- }
245
-
246
- const get = async (
247
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
248
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
249
- logger.default('get', { key });
250
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
251
- // This should be looking at the parentCaches and calculating an array of pkTypes
252
- if (!isValidItemKey(key)) {
253
- logger.error('Key for Get is not a valid ItemKey: %j', key);
254
- throw new Error('Key for Get is not a valid ItemKey');
255
- }
256
- let ret: V | null;
257
- try {
258
- ret = await api.get(key);
259
- if (ret) {
260
- cacheMap.set(ret.key, ret);
261
- }
262
- } catch (e: any) {
263
- logger.error("Error getting item for key", { key, message: e.message, stack: e.stack });
264
- throw e;
265
- }
266
- return [
267
- cacheMap,
268
- ret ?
269
- validatePK(ret, pkType) as V :
270
- null
271
- ];
272
- }
273
-
274
- const retrieve = async (
275
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
276
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
277
- logger.default('retrieve', { key });
278
- if (!isValidItemKey(key)) {
279
- logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
280
- throw new Error('Key for Retrieve is not a valid ItemKey');
281
- }
282
- const containsItemKey = cacheMap.includesKey(key);
283
-
284
- let retrieved: V | null;
285
- if (containsItemKey) {
286
- logger.default('Looking for Object in Cache', key);
287
- retrieved = cacheMap.get(key);
288
- } else {
289
- logger.default('Object Not Found in Cache, Retrieving from Server API', { key });
290
- [, retrieved] = await get(key);
291
- }
292
- const retValue: [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null] = [
293
- containsItemKey ? null : cacheMap,
294
- retrieved ?
295
- validatePK(retrieved, pkType) as V :
296
- null
297
- ];
298
- // logger.debug('Returning from retrieve', { retValue });
299
- return retValue;
300
- }
301
-
302
- const remove = async (
303
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
304
- ): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
305
- logger.default('remove', { key });
306
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
307
- // This should be looking at the parentCaches and calculating an array of pkTypes
308
- if (!isValidItemKey(key)) {
309
- logger.error('Key for Remove is not a valid ItemKey: %j', key);
310
- throw new Error('Key for Remove is not a valid ItemKey');
311
- }
312
- try {
313
- await api.remove(key);
314
- cacheMap.delete(key);
315
- } catch (e) {
316
- logger.error("Error deleting item", { error: e });
317
- throw e;
318
- }
319
- return cacheMap;
320
- }
321
-
322
- const update = async (
323
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
324
- v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
325
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
326
- logger.default('update', { key, v });
327
-
328
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
329
- // This should be looking at the parentCaches and calculating an array of pkTypes
330
- if (!isValidItemKey(key)) {
331
- logger.error('Key for Update is not a valid ItemKey: %j', key);
332
- throw new Error('Key for Update is not a valid ItemKey');
333
- }
334
-
335
- try {
336
- const updated = await api.update(key, v);
337
- cacheMap.set(updated.key, updated);
338
- return [cacheMap, validatePK(updated, pkType) as V];
339
- } catch (e) {
340
- logger.error("Error updating chat", { error: e });
341
- throw e;
342
- }
343
- }
344
-
345
- // Facets are a pass-thru for caches
346
- const facet = async (
347
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
348
- facet: string,
349
- params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
350
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
351
- logger.default('facet', { key, facet });
352
- const ret = await api.facet(key, facet, params);
353
- return [cacheMap, ret];
354
- }
355
-
356
- const find = async (
357
- finder: string,
358
- params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
359
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
360
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
361
- logger.default('find', { finder, params, locations });
362
- const ret: V[] = await api.find(finder, params, locations);
363
- ret.forEach((v) => {
364
- cacheMap.set(v.key, v);
365
- });
366
- return [cacheMap, validatePK(ret, pkType) as V[]];
367
- }
368
-
369
- const findOne = async (
370
- finder: string,
371
- finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
372
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
373
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
374
- logger.default('findOne', { finder, finderParams, locations });
375
- const ret = await api.findOne(finder, finderParams, locations);
376
- cacheMap.set(ret.key, ret);
377
- return [cacheMap, validatePK(ret, pkType) as V];
378
- }
379
-
380
- const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
381
- cacheMap = new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes);
382
- return [cacheMap];
383
- }
384
-
385
- const set = async (
386
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
387
- v: Item<S, L1, L2, L3, L4, L5>
388
- ): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
389
- logger.default('set', { key, v });
390
-
391
- // TODO: This is validating the key, but it doesn't have knowledge of the pkType
392
- // This should be looking at the parentCaches and calculating an array of pkTypes
393
- if (!isValidItemKey(key)) {
394
- logger.error('Key for Update is not a valid ItemKey: %j', key);
395
- throw new Error('Key for Update is not a valid ItemKey');
396
- }
397
-
398
- // TODO: This could be merged with the isValidItemKey check, later.
399
- validatePK(v, pkType);
400
-
401
- if (!isItemKeyEqual(key, v.key)) {
402
- logger.error('Key does not match item key: %j != %j', key, v.key);
403
- throw new Error('Key does not match item key');
404
- }
405
-
406
- cacheMap.set(key, v as V);
407
- return [cacheMap, validatePK(v, pkType) as V];
408
- }
409
-
410
- return {
411
- all,
412
- one,
413
- action,
414
- allAction,
415
- allFacet,
416
- create,
417
- get,
418
- retrieve,
419
- remove,
420
- update,
421
- facet,
422
- find,
423
- findOne,
424
- reset,
425
- set,
426
- pkTypes,
427
- cacheMap
428
- }
429
- }
package/src/CacheMap.ts DELETED
@@ -1,132 +0,0 @@
1
- import {
2
- AllItemTypeArrays,
3
- ComKey,
4
- Dictionary,
5
- isComKey,
6
- isQueryMatch,
7
- Item,
8
- ItemQuery,
9
- LocKeyArray,
10
- PriKey
11
- } from "@fjell/core";
12
- import LibLogger from "./logger";
13
-
14
- const logger = LibLogger.get("CacheMap");
15
-
16
- // const isObj = (x: any) => typeof x === "object" && x !== null;
17
-
18
- // const intersection = (a: object, b: object): object => {
19
- // const result: { [key: string]: any } = {}
20
-
21
- // if (([a, b]).every(isObj)) {
22
- // Object.keys(a).forEach((key) => {
23
- // // @ts-ignore
24
- // const value = a[key]
25
- // // @ts-ignore
26
- // const other = b[key]
27
-
28
- // if (isObj(value)) {
29
- // result[key] = intersection(value, other)
30
- // } else if (value === other) {
31
- // result[key] = value
32
- // }
33
- // })
34
- // }
35
-
36
- // return result
37
- // }
38
-
39
- // const removeEmptyObjects = (obj: object): object => {
40
- // const result: { [key: string]: any } = {}
41
-
42
- // Object.keys(obj).forEach((key) => {
43
- // // @ts-ignore
44
- // const value = obj[key];
45
-
46
- // if (isObj(value)) {
47
- // const nested = removeEmptyObjects(value);
48
-
49
- // if (Object.keys(nested).length > 0) {
50
- // result[key] = nested
51
- // }
52
- // } else if (value !== null) {
53
- // result[key] = value
54
- // }
55
- // });
56
-
57
- // return result;
58
- // }
59
-
60
- export class CacheMap<
61
- V extends Item<S, L1, L2, L3, L4, L5>,
62
- S extends string,
63
- L1 extends string = never,
64
- L2 extends string = never,
65
- L3 extends string = never,
66
- L4 extends string = never,
67
- L5 extends string = never
68
- > extends Dictionary<ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, V> {
69
-
70
- private types: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
71
-
72
- public constructor(
73
- types: AllItemTypeArrays<S, L1, L2, L3, L4, L5>,
74
- map?: { [key: string]: V },
75
- ) {
76
- super(map);
77
- this.types = types;
78
- }
79
-
80
- public get(
81
- key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
82
- ): V | null {
83
- return super.get(key) as V | null;
84
- }
85
-
86
- public allIn(
87
- locations: LocKeyArray<L1, L2, L3, L4, L5> | []
88
- ): V[] {
89
- if (locations.length === 0) {
90
- logger.debug('Returning all items, LocKeys is empty');
91
- return this.values();
92
- } else {
93
- const locKeys: LocKeyArray<L1, L2, L3, L4, L5> | [] = locations;
94
- logger.debug('allIn', { locKeys, keys: this.keys().length });
95
- return this.keys()
96
- .filter((key) => key && isComKey(key))
97
- .filter((key) => {
98
- const ComKey = key as ComKey<S, L1, L2, L3, L4, L5>;
99
- logger.debug('Comparing Location Keys', {
100
- locKeys,
101
- ComKey,
102
- });
103
- return JSON.stringify(locKeys) === JSON.stringify(ComKey.loc);
104
- })
105
- .map((key) => this.get(key) as V);
106
- }
107
- }
108
-
109
- // TODO: Can we do case insensitive matching?
110
- public contains(query: ItemQuery, locations: LocKeyArray<L1, L2, L3, L4, L5> | []): boolean {
111
- logger.debug('contains', { query, locations });
112
- const items = this.allIn(locations);
113
-
114
- return items.some((item) => isQueryMatch(item, query));
115
- }
116
-
117
- public queryIn(
118
- query: ItemQuery,
119
- locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
120
- ): V[] {
121
- logger.debug('queryIn', { query, locations });
122
- const items = this.allIn(locations);
123
-
124
- return items.filter((item) => isQueryMatch(item, query));
125
- }
126
-
127
- public clone(): CacheMap<V, S, L1, L2, L3, L4, L5> {
128
- const clone = new CacheMap<V, S, L1, L2, L3, L4, L5>(this.types, this.map);
129
- return clone;
130
- }
131
-
132
- };
@@ -1,66 +0,0 @@
1
- import { Item } from "@fjell/core";
2
- import { Cache } from "./Cache";
3
- import LibLogger from './logger';
4
-
5
- const logger = LibLogger.get('CacheRegistry');
6
-
7
- export class CacheRegistry {
8
-
9
- private static instance: CacheRegistry;
10
-
11
- public constructor() {
12
- logger.debug('CacheRegistry instance created');
13
- }
14
-
15
- // TODO: My use of Generics has Boxed me into a corner where I can't reference AbstractCache without the types
16
- private cacheMap: { [kt: string]: any } = {};
17
-
18
- public registerCache = async <
19
- S extends string,
20
- L1 extends string = never,
21
- L2 extends string = never,
22
- L3 extends string = never,
23
- L4 extends string = never,
24
- L5 extends string = never
25
- >(cache: Cache<Item<S, L1, L2, L3, L4, L5>, S, L1, L2, L3, L4, L5>): Promise<void> => {
26
- try {
27
- logger.debug('Attempting to register cache with pkTypes:', cache.pkTypes);
28
- const key = JSON.stringify(cache.pkTypes);
29
- if (this.cacheMap[key]) {
30
- logger.debug(`Cache with pkTypes ${key} already exists, will be overwritten`);
31
- }
32
- this.cacheMap[key] = cache;
33
- logger.debug('Cache registered successfully with key:', key);
34
- } catch (error) {
35
- logger.error('Failed to register cache:', error);
36
- throw error;
37
- }
38
- };
39
-
40
- public getCache = (kts: string[]): any => {
41
- logger.debug('Attempting to get cache for key types:', kts);
42
-
43
- const key = JSON.stringify(kts);
44
- logger.debug('Looking up cache with key:', key);
45
-
46
- const cache = this.cacheMap[key];
47
- if (!cache) {
48
- logger.warning(`No cache found for key types: ${key}`);
49
- }
50
- return cache;
51
- };
52
-
53
- public printRegisteredCaches = (): void => {
54
- logger.debug('Printing all registered caches:');
55
- const cacheCount = Object.keys(this.cacheMap).length;
56
- logger.debug(`Total number of registered caches: ${cacheCount}`);
57
-
58
- if (cacheCount === 0) {
59
- logger.debug('No caches are currently registered');
60
- }
61
-
62
- Object.entries(this.cacheMap).forEach(([keyTypes]) => {
63
- logger.debug(`Cache with key types: ${keyTypes}`);
64
- });
65
- };
66
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './Aggregator';
2
- export * from './Cache';
3
- export * from './CacheMap';
4
- export * from './CacheRegistry';
package/src/logger.ts DELETED
@@ -1,5 +0,0 @@
1
- import Logging from '@fjell/logging';
2
-
3
- const LibLogger = Logging.getLogger('@fjell/cache');
4
-
5
- export default LibLogger;