@fjell/cache 4.4.3 → 4.5.1
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/src/Aggregator.d.ts +19 -0
- package/dist/src/Aggregator.js +182 -0
- package/dist/src/Aggregator.js.map +1 -0
- package/dist/src/Cache.d.ts +7 -1
- package/dist/src/Cache.js +222 -1
- package/dist/src/Cache.js.map +1 -1
- package/dist/src/CacheRegistry.d.ts +2 -3
- package/dist/src/CacheRegistry.js +1 -1
- package/dist/src/CacheRegistry.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -10
- package/src/Aggregator.ts +289 -0
- package/src/Cache.ts +305 -2
- package/src/CacheRegistry.ts +3 -6
- package/dist/src/AItemAggregator.d.ts +0 -35
- package/dist/src/AItemAggregator.js +0 -163
- package/dist/src/AItemAggregator.js.map +0 -1
- package/dist/src/AItemCache.d.ts +0 -21
- package/dist/src/AItemCache.js +0 -189
- package/dist/src/AItemCache.js.map +0 -1
- package/dist/src/CItemCache.d.ts +0 -17
- package/dist/src/CItemCache.js +0 -58
- package/dist/src/CItemCache.js.map +0 -1
- package/dist/src/PItemCache.d.ts +0 -17
- package/dist/src/PItemCache.js +0 -50
- package/dist/src/PItemCache.js.map +0 -1
- package/dist/src/index.d.ts +0 -7
- package/dist/src/index.js +0 -7
- package/dist/src/index.js.map +0 -1
- package/src/AItemAggregator.ts +0 -251
- package/src/AItemCache.ts +0 -262
- package/src/CItemCache.ts +0 -117
- package/src/PItemCache.ts +0 -99
- package/src/index.ts +0 -10
package/src/AItemAggregator.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-undefined, max-params */
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
ComKey,
|
|
5
|
-
Item,
|
|
6
|
-
ItemQuery,
|
|
7
|
-
LocKeyArray,
|
|
8
|
-
PriKey,
|
|
9
|
-
TypesProperties
|
|
10
|
-
} from "@fjell/core";
|
|
11
|
-
import { AItemCache } from "./AItemCache";
|
|
12
|
-
import { Cache } from "./Cache";
|
|
13
|
-
import { CacheMap } from "./CacheMap";
|
|
14
|
-
import LibLogger from "./logger";
|
|
15
|
-
|
|
16
|
-
export interface CacheConfig { cache: any, optional: boolean }
|
|
17
|
-
|
|
18
|
-
export interface AggregateConfig { [key: string]: (CacheConfig) }
|
|
19
|
-
|
|
20
|
-
export const toCacheConfig = <
|
|
21
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
22
|
-
S extends string,
|
|
23
|
-
L1 extends string = never,
|
|
24
|
-
L2 extends string = never,
|
|
25
|
-
L3 extends string = never,
|
|
26
|
-
L4 extends string = never,
|
|
27
|
-
L5 extends string = never
|
|
28
|
-
>(config: CacheConfig | Cache<V, S, L1, L2, L3, L4, L5>): CacheConfig => {
|
|
29
|
-
let cacheConfig: CacheConfig;
|
|
30
|
-
if ((config as CacheConfig).optional === undefined) {
|
|
31
|
-
cacheConfig = { cache: config as any, optional: false };
|
|
32
|
-
} else {
|
|
33
|
-
cacheConfig = config as CacheConfig;
|
|
34
|
-
}
|
|
35
|
-
return cacheConfig;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class AItemAggregator<
|
|
39
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
40
|
-
S extends string,
|
|
41
|
-
L1 extends string = never,
|
|
42
|
-
L2 extends string = never,
|
|
43
|
-
L3 extends string = never,
|
|
44
|
-
L4 extends string = never,
|
|
45
|
-
L5 extends string = never
|
|
46
|
-
> implements Cache<V, S, L1, L2, L3, L4, L5> {
|
|
47
|
-
|
|
48
|
-
private cache: AItemCache<V, S, L1, L2, L3, L4, L5>;
|
|
49
|
-
private logger;
|
|
50
|
-
private aggregates: AggregateConfig = {};
|
|
51
|
-
private events: AggregateConfig = {};
|
|
52
|
-
|
|
53
|
-
public constructor(
|
|
54
|
-
cache: AItemCache<V, S, L1, L2, L3, L4, L5>,
|
|
55
|
-
{ aggregates = {}, events = {} }:
|
|
56
|
-
{
|
|
57
|
-
aggregates?: AggregateConfig,
|
|
58
|
-
events?: AggregateConfig
|
|
59
|
-
},
|
|
60
|
-
) {
|
|
61
|
-
this.cache = cache;
|
|
62
|
-
this.aggregates = aggregates;
|
|
63
|
-
this.events = events;
|
|
64
|
-
// istanbul ignore next
|
|
65
|
-
this.logger = LibLogger.get("AItemAggregator", ...aggregates ? Object.keys(aggregates) : []);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
private async populate(item: V): Promise<V> {
|
|
69
|
-
this.logger.default('populate', { item });
|
|
70
|
-
for (const key in this.aggregates) {
|
|
71
|
-
await this.populateAggregate(key, item);
|
|
72
|
-
}
|
|
73
|
-
for (const key in this.events) {
|
|
74
|
-
await this.populateEvent(key, item);
|
|
75
|
-
}
|
|
76
|
-
this.logger.default('populate done', { item });
|
|
77
|
-
return item;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private async populateAggregate(key: string, item: V) {
|
|
81
|
-
this.logger.default('populate aggregate key', { key });
|
|
82
|
-
const cacheConfig = toCacheConfig(this.aggregates[key]);
|
|
83
|
-
if (item.refs === undefined) {
|
|
84
|
-
if (cacheConfig.optional === false) {
|
|
85
|
-
this.logger.error('Item does not have refs an is not optional ' + JSON.stringify(item));
|
|
86
|
-
throw new Error('Item does not have refs an is not optional ' + JSON.stringify(item));
|
|
87
|
-
}
|
|
88
|
-
} else if (item.refs[key] === undefined) {
|
|
89
|
-
if (cacheConfig.optional === false) {
|
|
90
|
-
this.logger.error('Item does not have mandatory ref with key, not optional ' +
|
|
91
|
-
key + ' ' + JSON.stringify(item));
|
|
92
|
-
throw new Error('Item does not have mandatory ref with key, not optional ' +
|
|
93
|
-
key + ' ' + JSON.stringify(item));
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
|
|
97
|
-
const ref = item.refs[key];
|
|
98
|
-
|
|
99
|
-
this.logger.default('AGG Retrieving Item in Populate', { key: ref });
|
|
100
|
-
const [, newItem] = await cacheConfig.cache.retrieve(ref);
|
|
101
|
-
if (newItem) {
|
|
102
|
-
if (item.aggs === undefined) {
|
|
103
|
-
item.aggs = {};
|
|
104
|
-
}
|
|
105
|
-
item.aggs[key] = {
|
|
106
|
-
key: ref,
|
|
107
|
-
item: newItem as Item,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
|
|
114
|
-
private async populateEvent(key: string, item: V) {
|
|
115
|
-
this.logger.default('populate event key', { key });
|
|
116
|
-
const cacheConfig = toCacheConfig(this.events[key]);
|
|
117
|
-
|
|
118
|
-
if (item.events === undefined) {
|
|
119
|
-
throw new Error('Item does not have events ' + JSON.stringify(item));
|
|
120
|
-
} else if (item.events[key] === undefined) {
|
|
121
|
-
if (cacheConfig.optional === false) {
|
|
122
|
-
this.logger.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
|
|
123
|
-
throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
const event = item.events[key];
|
|
127
|
-
|
|
128
|
-
if (event.by === undefined) {
|
|
129
|
-
this.logger.error(
|
|
130
|
-
'populateEvent with an Event that does not have by', { event, ik: item.key, eventKey: key });
|
|
131
|
-
throw new Error('populateEvent with an Event that does not have by: ' + JSON.stringify({ key, event }));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
this.logger.default('EVENT Retrieving Item in Populate', { key: event.by });
|
|
135
|
-
const [, newItem] = await cacheConfig.cache.retrieve(event.by);
|
|
136
|
-
if (newItem) {
|
|
137
|
-
event.agg = newItem as Item;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
public async all(
|
|
143
|
-
query: ItemQuery = {},
|
|
144
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
145
|
-
):
|
|
146
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
147
|
-
this.logger.default('all', { query, locations });
|
|
148
|
-
const [cacheMap, items] = await this.cache.all(query, locations);
|
|
149
|
-
const populatedItems = await Promise.all(items.map(async (item) => this.populate(item)));
|
|
150
|
-
return [cacheMap, populatedItems];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public async one(
|
|
154
|
-
query: ItemQuery = {},
|
|
155
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
156
|
-
):
|
|
157
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
158
|
-
this.logger.default('one', { query, locations });
|
|
159
|
-
const [cacheMap, item] = await this.cache.one(query, locations);
|
|
160
|
-
let populatedItem = null;
|
|
161
|
-
if (item) {
|
|
162
|
-
populatedItem = await this.populate(item);
|
|
163
|
-
}
|
|
164
|
-
return [cacheMap, populatedItem];
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public async action(
|
|
168
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
169
|
-
action: string,
|
|
170
|
-
body: any = {},
|
|
171
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
172
|
-
this.logger.default('action', { key, action, body });
|
|
173
|
-
const [cacheMap, item] = await this.cache.action(key, action, body);
|
|
174
|
-
const populatedItem = await this.populate(item);
|
|
175
|
-
return [cacheMap, populatedItem];
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public async allAction(
|
|
179
|
-
action: string,
|
|
180
|
-
body: any = {},
|
|
181
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
182
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
183
|
-
this.logger.default('action', { action, body, locations });
|
|
184
|
-
const [cacheMap, items] = await this.cache.allAction(action, body, locations);
|
|
185
|
-
const populatedItems = await Promise.all(items.map(async (item) => this.populate(item)));
|
|
186
|
-
return [cacheMap, populatedItems];
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
public async create(
|
|
190
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
191
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
192
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
193
|
-
this.logger.default('create', { v, locations });
|
|
194
|
-
const [cacheMap, item] = await this.cache.create(v, locations);
|
|
195
|
-
const populatedItem = await this.populate(item);
|
|
196
|
-
return [cacheMap, populatedItem];
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
public async get(
|
|
200
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
201
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
202
|
-
this.logger.default('get', { key });
|
|
203
|
-
const [cacheMap, item] = await this.cache.get(key);
|
|
204
|
-
let populatedItem = null;
|
|
205
|
-
if (item) {
|
|
206
|
-
populatedItem = await this.populate(item);
|
|
207
|
-
}
|
|
208
|
-
return [cacheMap, populatedItem];
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
public async retrieve(
|
|
212
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
213
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> {
|
|
214
|
-
this.logger.default('retrieve', { key });
|
|
215
|
-
const [cacheMap, item] = await this.cache.retrieve(key);
|
|
216
|
-
let populatedItem = null;
|
|
217
|
-
if (item) {
|
|
218
|
-
populatedItem = await this.populate(item);
|
|
219
|
-
}
|
|
220
|
-
return [cacheMap, populatedItem];
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
public async remove(
|
|
224
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
225
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> {
|
|
226
|
-
this.logger.default('remove', { key });
|
|
227
|
-
const cacheMap = await this.cache.remove(key);
|
|
228
|
-
return cacheMap;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
public async update(
|
|
232
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
233
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
234
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
235
|
-
this.logger.default('update', { key, v });
|
|
236
|
-
const [cacheMap, item] = await this.cache.update(key, v);
|
|
237
|
-
const populatedItem = await this.populate(item);
|
|
238
|
-
return [cacheMap, populatedItem];
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
public async find(
|
|
242
|
-
finder: string,
|
|
243
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
244
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
245
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
246
|
-
this.logger.default('find', { finder, finderParams, locations });
|
|
247
|
-
const [cacheMap, items] = await this.cache.find(finder, finderParams, locations);
|
|
248
|
-
const populatedItems = await Promise.all(items.map(async (item) => this.populate(item)));
|
|
249
|
-
return [cacheMap, populatedItems];
|
|
250
|
-
}
|
|
251
|
-
}
|
package/src/AItemCache.ts
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-undefined, max-params */
|
|
2
|
-
|
|
3
|
-
import { ClientApi } from "@fjell/client-api";
|
|
4
|
-
import {
|
|
5
|
-
AItemService,
|
|
6
|
-
ComKey,
|
|
7
|
-
isValidItemKey,
|
|
8
|
-
Item,
|
|
9
|
-
ItemQuery,
|
|
10
|
-
LocKeyArray,
|
|
11
|
-
PriKey,
|
|
12
|
-
TypesProperties,
|
|
13
|
-
validatePK
|
|
14
|
-
} from "@fjell/core";
|
|
15
|
-
import { NotFoundError } from "@fjell/http-api";
|
|
16
|
-
import { Cache } from "./Cache";
|
|
17
|
-
import { CacheMap } from "./CacheMap";
|
|
18
|
-
import LibLogger from "./logger";
|
|
19
|
-
|
|
20
|
-
const logger = LibLogger.get('AItemCache');
|
|
21
|
-
|
|
22
|
-
export class AItemCache<
|
|
23
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
24
|
-
S extends string,
|
|
25
|
-
L1 extends string = never,
|
|
26
|
-
L2 extends string = never,
|
|
27
|
-
L3 extends string = never,
|
|
28
|
-
L4 extends string = never,
|
|
29
|
-
L5 extends string = never
|
|
30
|
-
> extends AItemService<S, L1, L2, L3, L4, L5> implements Cache<V, S, L1, L2, L3, L4, L5> {
|
|
31
|
-
|
|
32
|
-
protected cacheName: string;
|
|
33
|
-
protected api: ClientApi<V, S, L1, L2, L3, L4, L5>;
|
|
34
|
-
|
|
35
|
-
public cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
|
|
36
|
-
|
|
37
|
-
public constructor(
|
|
38
|
-
cacheName: string,
|
|
39
|
-
api: ClientApi<V, S, L1, L2, L3, L4, L5>,
|
|
40
|
-
pkType: S,
|
|
41
|
-
parentCache?: AItemCache<Item<L1, L2, L3, L4, L5>, L1, L2, L3, L4, L5>
|
|
42
|
-
) {
|
|
43
|
-
super(pkType, parentCache);
|
|
44
|
-
this.cacheName = cacheName;
|
|
45
|
-
this.api = api;
|
|
46
|
-
// TODO: I wonder if this is even going to work - can you access an instance of a class in a constructor?
|
|
47
|
-
this.cacheMap =
|
|
48
|
-
new CacheMap<V, S, L1, L2, L3, L4, L5>(this.getKeyTypes());
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public async all(
|
|
52
|
-
query: ItemQuery = {},
|
|
53
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
54
|
-
):
|
|
55
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
56
|
-
logger.default('all', { query, locations });
|
|
57
|
-
let ret: V[] = [];
|
|
58
|
-
try {
|
|
59
|
-
ret = await this.api.all(query, {}, locations);
|
|
60
|
-
ret.forEach((v) => {
|
|
61
|
-
this.cacheMap.set(v.key, v);
|
|
62
|
-
});
|
|
63
|
-
} catch (e: unknown) {
|
|
64
|
-
if (e instanceof NotFoundError) {
|
|
65
|
-
} else {
|
|
66
|
-
throw e;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
return [this.cacheMap, validatePK(ret, this.getPkType()) as V[]];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public async one(
|
|
74
|
-
query: ItemQuery = {},
|
|
75
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
76
|
-
):
|
|
77
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
78
|
-
logger.default('one', { query, locations });
|
|
79
|
-
|
|
80
|
-
let retItem: V | null = null;
|
|
81
|
-
try {
|
|
82
|
-
retItem = await this.api.one(query, {}, locations);
|
|
83
|
-
if (retItem) {
|
|
84
|
-
this.cacheMap.set(retItem.key, retItem);
|
|
85
|
-
}
|
|
86
|
-
} catch (e: unknown) {
|
|
87
|
-
if (e instanceof NotFoundError) {
|
|
88
|
-
} else {
|
|
89
|
-
throw e;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
return [
|
|
94
|
-
this.cacheMap,
|
|
95
|
-
retItem ?
|
|
96
|
-
validatePK(retItem, this.getPkType()) as V :
|
|
97
|
-
null
|
|
98
|
-
];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public async action(
|
|
102
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
103
|
-
action: string,
|
|
104
|
-
body: any = {},
|
|
105
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
106
|
-
logger.default('action', { key, action, body });
|
|
107
|
-
|
|
108
|
-
if (!isValidItemKey(key)) {
|
|
109
|
-
logger.error('Key for Action is not a valid ItemKey: %j', key);
|
|
110
|
-
throw new Error('Key for Action is not a valid ItemKey');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const updated = await this.api.action(key, action, body, {});
|
|
114
|
-
this.cacheMap.set(updated.key, updated);
|
|
115
|
-
return [this.cacheMap, validatePK(updated, this.getPkType()) as V];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public async allAction(
|
|
119
|
-
action: string,
|
|
120
|
-
body: any = {},
|
|
121
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
122
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
123
|
-
logger.default('allAction', { action, body, locations });
|
|
124
|
-
let ret: V[] = [];
|
|
125
|
-
try {
|
|
126
|
-
ret = await this.api.allAction(action, body, {}, locations);
|
|
127
|
-
ret.forEach((v) => {
|
|
128
|
-
this.cacheMap.set(v.key, v);
|
|
129
|
-
});
|
|
130
|
-
} catch (e: unknown) {
|
|
131
|
-
// istanbul ignore next
|
|
132
|
-
if (e instanceof NotFoundError) {
|
|
133
|
-
} else {
|
|
134
|
-
throw e;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
return [this.cacheMap, validatePK(ret, this.getPkType()) as V[]];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
public async create(
|
|
142
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
143
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
144
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
145
|
-
logger.default('create', { v, locations });
|
|
146
|
-
const created = await this.api.create(v, {}, locations);
|
|
147
|
-
this.cacheMap.set(created.key, created);
|
|
148
|
-
return [this.cacheMap, validatePK(created, this.getPkType()) as V];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
public async get(
|
|
152
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
153
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
154
|
-
logger.default('get', { key });
|
|
155
|
-
if (!isValidItemKey(key)) {
|
|
156
|
-
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
157
|
-
throw new Error('Key for Get is not a valid ItemKey');
|
|
158
|
-
}
|
|
159
|
-
let ret: V | null;
|
|
160
|
-
try {
|
|
161
|
-
ret = await this.api.get(key, {});
|
|
162
|
-
if (ret) {
|
|
163
|
-
this.cacheMap.set(ret.key, ret);
|
|
164
|
-
}
|
|
165
|
-
} catch (e: any) {
|
|
166
|
-
logger.error("Error getting item for key", { key, message: e.message, stack: e.stack });
|
|
167
|
-
throw e;
|
|
168
|
-
}
|
|
169
|
-
return [
|
|
170
|
-
this.cacheMap,
|
|
171
|
-
ret ?
|
|
172
|
-
validatePK(ret, this.getPkType()) as V :
|
|
173
|
-
null
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
public async retrieve(
|
|
178
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
179
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> {
|
|
180
|
-
logger.default('retrieve', { key });
|
|
181
|
-
if (!isValidItemKey(key)) {
|
|
182
|
-
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
183
|
-
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
184
|
-
}
|
|
185
|
-
const containsItemKey = this.cacheMap.includesKey(key);
|
|
186
|
-
|
|
187
|
-
let retrieved: V | null;
|
|
188
|
-
if (containsItemKey) {
|
|
189
|
-
logger.default('Looking for Object in Cache', key);
|
|
190
|
-
retrieved = this.cacheMap.get(key);
|
|
191
|
-
} else {
|
|
192
|
-
logger.default('Object Not Found in Cache, Retrieving from Server API', { key });
|
|
193
|
-
[, retrieved] = await this.get(key);
|
|
194
|
-
}
|
|
195
|
-
const retValue: [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null] = [
|
|
196
|
-
containsItemKey ? null : this.cacheMap,
|
|
197
|
-
retrieved ?
|
|
198
|
-
validatePK(retrieved, this.getPkType()) as V:
|
|
199
|
-
null
|
|
200
|
-
];
|
|
201
|
-
// logger.debug('Returning from retrieve', { retValue });
|
|
202
|
-
return retValue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
public async remove(
|
|
206
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
207
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> {
|
|
208
|
-
logger.default('remove', { key });
|
|
209
|
-
if (!isValidItemKey(key)) {
|
|
210
|
-
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
211
|
-
throw new Error('Key for Remove is not a valid ItemKey');
|
|
212
|
-
}
|
|
213
|
-
try {
|
|
214
|
-
await this.api.remove(key, {});
|
|
215
|
-
this.cacheMap.delete(key);
|
|
216
|
-
} catch (e) {
|
|
217
|
-
logger.error("Error deleting item", { error: e });
|
|
218
|
-
throw e;
|
|
219
|
-
}
|
|
220
|
-
return this.cacheMap;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
public async update(
|
|
224
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
225
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
226
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
227
|
-
logger.default('update', { key, v });
|
|
228
|
-
|
|
229
|
-
if (!isValidItemKey(key)) {
|
|
230
|
-
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
231
|
-
throw new Error('Key for Update is not a valid ItemKey');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
try {
|
|
235
|
-
const updated = await this.api.update(key, v, {});
|
|
236
|
-
// }
|
|
237
|
-
this.cacheMap.set(updated.key, updated);
|
|
238
|
-
return [this.cacheMap, validatePK(updated, this.getPkType()) as V];
|
|
239
|
-
} catch (e) {
|
|
240
|
-
logger.error("Error updating chat", { error: e });
|
|
241
|
-
throw e;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
public async find(
|
|
246
|
-
finder: string,
|
|
247
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
248
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
249
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
250
|
-
logger.default('find', { finder, finderParams, locations });
|
|
251
|
-
const ret: V[] = await this.api.find(finder, finderParams, {}, locations);
|
|
252
|
-
ret.forEach((v) => {
|
|
253
|
-
this.cacheMap.set(v.key, v);
|
|
254
|
-
});
|
|
255
|
-
return [this.cacheMap, validatePK(ret, this.getPkType()) as V[]];
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
public loadCache = async (cache: CacheMap<V, S, L1, L2, L3, L4, L5>) => {
|
|
259
|
-
this.cacheMap = cache;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
}
|
package/src/CItemCache.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-undefined */
|
|
2
|
-
import { ClientApi } from "@fjell/client-api";
|
|
3
|
-
import { ComKey, Item, ItemQuery, LocKeyArray, PriKey, TypesProperties } from "@fjell/core";
|
|
4
|
-
import { AItemCache } from "./AItemCache";
|
|
5
|
-
import { CacheMap } from "./CacheMap";
|
|
6
|
-
import LibLogger from "./logger";
|
|
7
|
-
|
|
8
|
-
const logger = LibLogger.get('CItemCache');
|
|
9
|
-
|
|
10
|
-
export class CItemCache<
|
|
11
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
12
|
-
S extends string,
|
|
13
|
-
L1 extends string,
|
|
14
|
-
L2 extends string = never,
|
|
15
|
-
L3 extends string = never,
|
|
16
|
-
L4 extends string = never,
|
|
17
|
-
L5 extends string = never
|
|
18
|
-
> extends AItemCache<V, S, L1, L2, L3, L4, L5> {
|
|
19
|
-
|
|
20
|
-
public constructor(
|
|
21
|
-
cacheName: string,
|
|
22
|
-
api: ClientApi<V, S, L1, L2, L3, L4, L5>,
|
|
23
|
-
pkType: S,
|
|
24
|
-
parentCache: AItemCache<Item<L1, L2, L3, L4, L5, never>, L1, L2, L3, L4, L5>
|
|
25
|
-
) {
|
|
26
|
-
super(cacheName, api, pkType, parentCache);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// TODO: There's something annoying about these parameters. Location isn't option in a CItem, but query is.
|
|
30
|
-
public async all(
|
|
31
|
-
// istanbul ignore next
|
|
32
|
-
query: ItemQuery = {},
|
|
33
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5>
|
|
34
|
-
):
|
|
35
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
36
|
-
logger.default('all', { query, locations });
|
|
37
|
-
return await super.all(query, locations) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V[]];
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// TODO: There's something annoying about these parameters. Location isn't option in a CItem, but query is.
|
|
41
|
-
public async one(
|
|
42
|
-
// istanbul ignore next
|
|
43
|
-
query: ItemQuery = {},
|
|
44
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5>
|
|
45
|
-
):
|
|
46
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
47
|
-
logger.default('one', { query, locations });
|
|
48
|
-
return await super.one(query, locations) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V | null];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public async action(
|
|
52
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
53
|
-
action: string,
|
|
54
|
-
body: any = {}
|
|
55
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
56
|
-
logger.default('action', { key, action, body });
|
|
57
|
-
return await super.action(key, action, body) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// TODO: There's something annoying about these parameters. Location isn't option in a CItem, but query is.
|
|
61
|
-
public async allAction(
|
|
62
|
-
action: string,
|
|
63
|
-
// istanbul ignore next
|
|
64
|
-
body: any = {},
|
|
65
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5>
|
|
66
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
67
|
-
logger.default('action', { action, body, locations });
|
|
68
|
-
return await super.allAction(action, body, locations) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V[]];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public async create(
|
|
72
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>,
|
|
73
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5>
|
|
74
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
75
|
-
logger.default('create', { v });
|
|
76
|
-
return await super.create(v, locations) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public async get(
|
|
80
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
81
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> {
|
|
82
|
-
logger.default('get', { key });
|
|
83
|
-
return await super.get(key) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V | null];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public async retrieve(
|
|
87
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
88
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> {
|
|
89
|
-
logger.default('retrieve', { key });
|
|
90
|
-
return await super.retrieve(key) as [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null];
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async remove(
|
|
94
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>
|
|
95
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> {
|
|
96
|
-
logger.default('remove', { key });
|
|
97
|
-
return await super.remove(key) as CacheMap<V, S, L1, L2, L3, L4, L5>;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public async update(
|
|
101
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
102
|
-
v: TypesProperties<V, S, L1, L2, L3, L4, L5>
|
|
103
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> {
|
|
104
|
-
logger.default('update', { key, v });
|
|
105
|
-
return await super.update(key, v) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public async find(
|
|
109
|
-
finder: string,
|
|
110
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
111
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
112
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> {
|
|
113
|
-
logger.default('find', { finder, finderParams, locations });
|
|
114
|
-
return await super.find(finder, finderParams, locations) as [CacheMap<V, S, L1, L2, L3, L4, L5>, V[]];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
}
|