@fjell/cache 4.5.1 → 4.6.0
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.cjs.js +276 -0
- package/dist/{src/Aggregator.d.ts → Aggregator.d.ts} +2 -2
- package/dist/Aggregator.es.js +271 -0
- package/dist/Cache.cjs.js +279 -0
- package/dist/{src/Cache.d.ts → Cache.d.ts} +4 -4
- package/dist/{src/Cache.js → Cache.es.js} +129 -77
- package/dist/CacheMap.cjs.js +108 -0
- package/dist/{src/CacheMap.d.ts → CacheMap.d.ts} +1 -1
- package/dist/{src/CacheMap.js → CacheMap.es.js} +42 -23
- package/dist/CacheRegistry.cjs.js +54 -0
- package/dist/{src/CacheRegistry.d.ts → CacheRegistry.d.ts} +2 -2
- package/dist/CacheRegistry.es.js +50 -0
- package/dist/index.cjs +696 -0
- package/dist/index.cjs.js +17 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.es.js +5 -0
- package/dist/logger.cjs.js +10 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.es.js +6 -0
- package/package.json +30 -18
- package/src/Aggregator.ts +9 -1
- package/src/Cache.ts +3 -3
- package/src/index.ts +4 -0
- package/vite.config.ts +91 -0
- package/dist/src/Aggregator.js +0 -182
- package/dist/src/Aggregator.js.map +0 -1
- package/dist/src/Cache.js.map +0 -1
- package/dist/src/CacheMap.js.map +0 -1
- package/dist/src/CacheRegistry.js +0 -34
- package/dist/src/CacheRegistry.js.map +0 -1
- package/dist/src/logger.d.ts +0 -2
- package/dist/src/logger.js +0 -4
- package/dist/src/logger.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const CacheMap = require('./CacheMap.cjs.js');
|
|
7
|
+
const logger$1 = require('./logger.cjs.js');
|
|
8
|
+
const httpApi = require('@fjell/http-api');
|
|
9
|
+
|
|
10
|
+
const logger = logger$1.default.get('Cache');
|
|
11
|
+
const createCache = (api, pkType, parentCache)=>{
|
|
12
|
+
let pkTypes = [
|
|
13
|
+
pkType
|
|
14
|
+
];
|
|
15
|
+
if (parentCache) {
|
|
16
|
+
pkTypes = pkTypes.concat(parentCache.pkTypes);
|
|
17
|
+
}
|
|
18
|
+
let cacheMap = new CacheMap.CacheMap(pkTypes);
|
|
19
|
+
const all = async (query = {}, locations = [])=>{
|
|
20
|
+
logger.default('all', {
|
|
21
|
+
query,
|
|
22
|
+
locations
|
|
23
|
+
});
|
|
24
|
+
let ret = [];
|
|
25
|
+
try {
|
|
26
|
+
ret = await api.all(query, {}, locations);
|
|
27
|
+
ret.forEach((v)=>{
|
|
28
|
+
cacheMap.set(v.key, v);
|
|
29
|
+
});
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof httpApi.NotFoundError) ; else {
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return [
|
|
36
|
+
cacheMap,
|
|
37
|
+
core.validatePK(ret, pkType)
|
|
38
|
+
];
|
|
39
|
+
};
|
|
40
|
+
const one = async (query = {}, locations = [])=>{
|
|
41
|
+
logger.default('one', {
|
|
42
|
+
query,
|
|
43
|
+
locations
|
|
44
|
+
});
|
|
45
|
+
let retItem = null;
|
|
46
|
+
try {
|
|
47
|
+
retItem = await api.one(query, {}, locations);
|
|
48
|
+
if (retItem) {
|
|
49
|
+
cacheMap.set(retItem.key, retItem);
|
|
50
|
+
}
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (e instanceof httpApi.NotFoundError) ; else {
|
|
53
|
+
throw e;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return [
|
|
57
|
+
cacheMap,
|
|
58
|
+
retItem ? core.validatePK(retItem, pkType) : null
|
|
59
|
+
];
|
|
60
|
+
};
|
|
61
|
+
const action = async (key, action, body = {})=>{
|
|
62
|
+
logger.default('action', {
|
|
63
|
+
key,
|
|
64
|
+
action,
|
|
65
|
+
body
|
|
66
|
+
});
|
|
67
|
+
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
68
|
+
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
69
|
+
if (!core.isValidItemKey(key)) {
|
|
70
|
+
logger.error('Key for Action is not a valid ItemKey: %j', key);
|
|
71
|
+
throw new Error('Key for Action is not a valid ItemKey');
|
|
72
|
+
}
|
|
73
|
+
const updated = await api.action(key, action, body, {});
|
|
74
|
+
cacheMap.set(updated.key, updated);
|
|
75
|
+
return [
|
|
76
|
+
cacheMap,
|
|
77
|
+
core.validatePK(updated, pkType)
|
|
78
|
+
];
|
|
79
|
+
};
|
|
80
|
+
const allAction = async (action, body = {}, locations = [])=>{
|
|
81
|
+
logger.default('allAction', {
|
|
82
|
+
action,
|
|
83
|
+
body,
|
|
84
|
+
locations
|
|
85
|
+
});
|
|
86
|
+
let ret = [];
|
|
87
|
+
try {
|
|
88
|
+
ret = await api.allAction(action, body, {}, locations);
|
|
89
|
+
ret.forEach((v)=>{
|
|
90
|
+
cacheMap.set(v.key, v);
|
|
91
|
+
});
|
|
92
|
+
} catch (e) {
|
|
93
|
+
// istanbul ignore next
|
|
94
|
+
if (e instanceof httpApi.NotFoundError) ; else {
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return [
|
|
99
|
+
cacheMap,
|
|
100
|
+
core.validatePK(ret, pkType)
|
|
101
|
+
];
|
|
102
|
+
};
|
|
103
|
+
const create = async (v, locations = [])=>{
|
|
104
|
+
logger.default('create', {
|
|
105
|
+
v,
|
|
106
|
+
locations
|
|
107
|
+
});
|
|
108
|
+
const created = await api.create(v, {}, locations);
|
|
109
|
+
cacheMap.set(created.key, created);
|
|
110
|
+
return [
|
|
111
|
+
cacheMap,
|
|
112
|
+
core.validatePK(created, pkType)
|
|
113
|
+
];
|
|
114
|
+
};
|
|
115
|
+
const get = async (key)=>{
|
|
116
|
+
logger.default('get', {
|
|
117
|
+
key
|
|
118
|
+
});
|
|
119
|
+
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
120
|
+
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
121
|
+
if (!core.isValidItemKey(key)) {
|
|
122
|
+
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
123
|
+
throw new Error('Key for Get is not a valid ItemKey');
|
|
124
|
+
}
|
|
125
|
+
let ret;
|
|
126
|
+
try {
|
|
127
|
+
ret = await api.get(key, {});
|
|
128
|
+
if (ret) {
|
|
129
|
+
cacheMap.set(ret.key, ret);
|
|
130
|
+
}
|
|
131
|
+
} catch (e) {
|
|
132
|
+
logger.error("Error getting item for key", {
|
|
133
|
+
key,
|
|
134
|
+
message: e.message,
|
|
135
|
+
stack: e.stack
|
|
136
|
+
});
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
return [
|
|
140
|
+
cacheMap,
|
|
141
|
+
ret ? core.validatePK(ret, pkType) : null
|
|
142
|
+
];
|
|
143
|
+
};
|
|
144
|
+
const retrieve = async (key)=>{
|
|
145
|
+
logger.default('retrieve', {
|
|
146
|
+
key
|
|
147
|
+
});
|
|
148
|
+
if (!core.isValidItemKey(key)) {
|
|
149
|
+
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
150
|
+
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
151
|
+
}
|
|
152
|
+
const containsItemKey = cacheMap.includesKey(key);
|
|
153
|
+
let retrieved;
|
|
154
|
+
if (containsItemKey) {
|
|
155
|
+
logger.default('Looking for Object in Cache', key);
|
|
156
|
+
retrieved = cacheMap.get(key);
|
|
157
|
+
} else {
|
|
158
|
+
logger.default('Object Not Found in Cache, Retrieving from Server API', {
|
|
159
|
+
key
|
|
160
|
+
});
|
|
161
|
+
[, retrieved] = await get(key);
|
|
162
|
+
}
|
|
163
|
+
const retValue = [
|
|
164
|
+
containsItemKey ? null : cacheMap,
|
|
165
|
+
retrieved ? core.validatePK(retrieved, pkType) : null
|
|
166
|
+
];
|
|
167
|
+
// logger.debug('Returning from retrieve', { retValue });
|
|
168
|
+
return retValue;
|
|
169
|
+
};
|
|
170
|
+
const remove = async (key)=>{
|
|
171
|
+
logger.default('remove', {
|
|
172
|
+
key
|
|
173
|
+
});
|
|
174
|
+
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
175
|
+
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
176
|
+
if (!core.isValidItemKey(key)) {
|
|
177
|
+
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
178
|
+
throw new Error('Key for Remove is not a valid ItemKey');
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
await api.remove(key, {});
|
|
182
|
+
cacheMap.delete(key);
|
|
183
|
+
} catch (e) {
|
|
184
|
+
logger.error("Error deleting item", {
|
|
185
|
+
error: e
|
|
186
|
+
});
|
|
187
|
+
throw e;
|
|
188
|
+
}
|
|
189
|
+
return cacheMap;
|
|
190
|
+
};
|
|
191
|
+
const update = async (key, v)=>{
|
|
192
|
+
logger.default('update', {
|
|
193
|
+
key,
|
|
194
|
+
v
|
|
195
|
+
});
|
|
196
|
+
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
197
|
+
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
198
|
+
if (!core.isValidItemKey(key)) {
|
|
199
|
+
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
200
|
+
throw new Error('Key for Update is not a valid ItemKey');
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const updated = await api.update(key, v, {});
|
|
204
|
+
cacheMap.set(updated.key, updated);
|
|
205
|
+
return [
|
|
206
|
+
cacheMap,
|
|
207
|
+
core.validatePK(updated, pkType)
|
|
208
|
+
];
|
|
209
|
+
} catch (e) {
|
|
210
|
+
logger.error("Error updating chat", {
|
|
211
|
+
error: e
|
|
212
|
+
});
|
|
213
|
+
throw e;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const find = async (finder, finderParams, locations = [])=>{
|
|
217
|
+
logger.default('find', {
|
|
218
|
+
finder,
|
|
219
|
+
finderParams,
|
|
220
|
+
locations
|
|
221
|
+
});
|
|
222
|
+
const ret = await api.find(finder, finderParams, {}, locations);
|
|
223
|
+
ret.forEach((v)=>{
|
|
224
|
+
cacheMap.set(v.key, v);
|
|
225
|
+
});
|
|
226
|
+
return [
|
|
227
|
+
cacheMap,
|
|
228
|
+
core.validatePK(ret, pkType)
|
|
229
|
+
];
|
|
230
|
+
};
|
|
231
|
+
const reset = async ()=>{
|
|
232
|
+
cacheMap = new CacheMap.CacheMap(pkTypes);
|
|
233
|
+
return [
|
|
234
|
+
cacheMap
|
|
235
|
+
];
|
|
236
|
+
};
|
|
237
|
+
const set = async (key, v)=>{
|
|
238
|
+
logger.default('set', {
|
|
239
|
+
key,
|
|
240
|
+
v
|
|
241
|
+
});
|
|
242
|
+
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
243
|
+
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
244
|
+
if (!core.isValidItemKey(key)) {
|
|
245
|
+
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
246
|
+
throw new Error('Key for Update is not a valid ItemKey');
|
|
247
|
+
}
|
|
248
|
+
// TODO: This could be merged with the isValidItemKey check, later.
|
|
249
|
+
core.validatePK(v, pkType);
|
|
250
|
+
if (!core.isItemKeyEqual(key, v.key)) {
|
|
251
|
+
logger.error('Key does not match item key: %j != %j', key, v.key);
|
|
252
|
+
throw new Error('Key does not match item key');
|
|
253
|
+
}
|
|
254
|
+
cacheMap.set(key, v);
|
|
255
|
+
return [
|
|
256
|
+
cacheMap,
|
|
257
|
+
core.validatePK(v, pkType)
|
|
258
|
+
];
|
|
259
|
+
};
|
|
260
|
+
return {
|
|
261
|
+
all,
|
|
262
|
+
one,
|
|
263
|
+
action,
|
|
264
|
+
allAction,
|
|
265
|
+
create,
|
|
266
|
+
get,
|
|
267
|
+
retrieve,
|
|
268
|
+
remove,
|
|
269
|
+
update,
|
|
270
|
+
find,
|
|
271
|
+
reset,
|
|
272
|
+
set,
|
|
273
|
+
pkTypes,
|
|
274
|
+
cacheMap
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
exports.createCache = createCache;
|
|
279
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2FjaGUuY2pzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AllItemTypeArrays, ComKey, Item, ItemQuery, LocKeyArray, PriKey, TypesProperties } from
|
|
2
|
-
import { CacheMap } from
|
|
3
|
-
import { ClientApi } from
|
|
1
|
+
import { AllItemTypeArrays, ComKey, Item, ItemQuery, LocKeyArray, PriKey, TypesProperties } from '@fjell/core';
|
|
2
|
+
import { CacheMap } from './CacheMap';
|
|
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> {
|
|
5
5
|
all: (query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
|
|
6
6
|
one: (query?: ItemQuery, locations?: LocKeyArray<L1, L2, L3, L4, L5> | []) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
|
|
@@ -12,7 +12,7 @@ export interface Cache<V extends Item<S, L1, L2, L3, L4, L5>, S extends string,
|
|
|
12
12
|
remove: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
|
|
13
13
|
update: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, v: TypesProperties<V, S, L1, L2, L3, L4, L5>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
14
14
|
find: (finder: string, finderParams: 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[]]>;
|
|
15
|
-
set: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, v:
|
|
15
|
+
set: (key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, v: Item<S, L1, L2, L3, L4, L5>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
16
16
|
reset: () => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
|
|
17
17
|
pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
18
18
|
cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
|
|
@@ -1,57 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CacheMap } from
|
|
3
|
-
import LibLogger from
|
|
4
|
-
import { NotFoundError } from
|
|
1
|
+
import { isValidItemKey, validatePK, isItemKeyEqual } from '@fjell/core';
|
|
2
|
+
import { CacheMap } from './CacheMap.es.js';
|
|
3
|
+
import LibLogger from './logger.es.js';
|
|
4
|
+
import { NotFoundError } from '@fjell/http-api';
|
|
5
|
+
|
|
5
6
|
const logger = LibLogger.get('Cache');
|
|
6
|
-
|
|
7
|
-
let pkTypes = [
|
|
7
|
+
const createCache = (api, pkType, parentCache)=>{
|
|
8
|
+
let pkTypes = [
|
|
9
|
+
pkType
|
|
10
|
+
];
|
|
8
11
|
if (parentCache) {
|
|
9
12
|
pkTypes = pkTypes.concat(parentCache.pkTypes);
|
|
10
13
|
}
|
|
11
14
|
let cacheMap = new CacheMap(pkTypes);
|
|
12
|
-
const all = async (query = {}, locations = [])
|
|
13
|
-
logger.default('all', {
|
|
15
|
+
const all = async (query = {}, locations = [])=>{
|
|
16
|
+
logger.default('all', {
|
|
17
|
+
query,
|
|
18
|
+
locations
|
|
19
|
+
});
|
|
14
20
|
let ret = [];
|
|
15
21
|
try {
|
|
16
22
|
ret = await api.all(query, {}, locations);
|
|
17
|
-
ret.forEach((v)
|
|
23
|
+
ret.forEach((v)=>{
|
|
18
24
|
cacheMap.set(v.key, v);
|
|
19
25
|
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (e instanceof NotFoundError) {
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (e instanceof NotFoundError) ; else {
|
|
25
28
|
throw e;
|
|
26
29
|
}
|
|
27
30
|
}
|
|
28
|
-
return [
|
|
31
|
+
return [
|
|
32
|
+
cacheMap,
|
|
33
|
+
validatePK(ret, pkType)
|
|
34
|
+
];
|
|
29
35
|
};
|
|
30
|
-
const one = async (query = {}, locations = [])
|
|
31
|
-
logger.default('one', {
|
|
36
|
+
const one = async (query = {}, locations = [])=>{
|
|
37
|
+
logger.default('one', {
|
|
38
|
+
query,
|
|
39
|
+
locations
|
|
40
|
+
});
|
|
32
41
|
let retItem = null;
|
|
33
42
|
try {
|
|
34
43
|
retItem = await api.one(query, {}, locations);
|
|
35
44
|
if (retItem) {
|
|
36
45
|
cacheMap.set(retItem.key, retItem);
|
|
37
46
|
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (e instanceof NotFoundError) {
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
47
|
+
} catch (e) {
|
|
48
|
+
if (e instanceof NotFoundError) ; else {
|
|
43
49
|
throw e;
|
|
44
50
|
}
|
|
45
51
|
}
|
|
46
52
|
return [
|
|
47
53
|
cacheMap,
|
|
48
|
-
retItem ?
|
|
49
|
-
validatePK(retItem, pkType) :
|
|
50
|
-
null
|
|
54
|
+
retItem ? validatePK(retItem, pkType) : null
|
|
51
55
|
];
|
|
52
56
|
};
|
|
53
|
-
const action = async (key, action, body = {})
|
|
54
|
-
logger.default('action', {
|
|
57
|
+
const action = async (key, action, body = {})=>{
|
|
58
|
+
logger.default('action', {
|
|
59
|
+
key,
|
|
60
|
+
action,
|
|
61
|
+
body
|
|
62
|
+
});
|
|
55
63
|
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
56
64
|
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
57
65
|
if (!isValidItemKey(key)) {
|
|
@@ -60,35 +68,50 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
60
68
|
}
|
|
61
69
|
const updated = await api.action(key, action, body, {});
|
|
62
70
|
cacheMap.set(updated.key, updated);
|
|
63
|
-
return [
|
|
71
|
+
return [
|
|
72
|
+
cacheMap,
|
|
73
|
+
validatePK(updated, pkType)
|
|
74
|
+
];
|
|
64
75
|
};
|
|
65
|
-
const allAction = async (action, body = {}, locations = [])
|
|
66
|
-
logger.default('allAction', {
|
|
76
|
+
const allAction = async (action, body = {}, locations = [])=>{
|
|
77
|
+
logger.default('allAction', {
|
|
78
|
+
action,
|
|
79
|
+
body,
|
|
80
|
+
locations
|
|
81
|
+
});
|
|
67
82
|
let ret = [];
|
|
68
83
|
try {
|
|
69
84
|
ret = await api.allAction(action, body, {}, locations);
|
|
70
|
-
ret.forEach((v)
|
|
85
|
+
ret.forEach((v)=>{
|
|
71
86
|
cacheMap.set(v.key, v);
|
|
72
87
|
});
|
|
73
|
-
}
|
|
74
|
-
catch (e) {
|
|
88
|
+
} catch (e) {
|
|
75
89
|
// istanbul ignore next
|
|
76
|
-
if (e instanceof NotFoundError) {
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
90
|
+
if (e instanceof NotFoundError) ; else {
|
|
79
91
|
throw e;
|
|
80
92
|
}
|
|
81
93
|
}
|
|
82
|
-
return [
|
|
94
|
+
return [
|
|
95
|
+
cacheMap,
|
|
96
|
+
validatePK(ret, pkType)
|
|
97
|
+
];
|
|
83
98
|
};
|
|
84
|
-
const create = async (v, locations = [])
|
|
85
|
-
logger.default('create', {
|
|
99
|
+
const create = async (v, locations = [])=>{
|
|
100
|
+
logger.default('create', {
|
|
101
|
+
v,
|
|
102
|
+
locations
|
|
103
|
+
});
|
|
86
104
|
const created = await api.create(v, {}, locations);
|
|
87
105
|
cacheMap.set(created.key, created);
|
|
88
|
-
return [
|
|
106
|
+
return [
|
|
107
|
+
cacheMap,
|
|
108
|
+
validatePK(created, pkType)
|
|
109
|
+
];
|
|
89
110
|
};
|
|
90
|
-
const get = async (key)
|
|
91
|
-
logger.default('get', {
|
|
111
|
+
const get = async (key)=>{
|
|
112
|
+
logger.default('get', {
|
|
113
|
+
key
|
|
114
|
+
});
|
|
92
115
|
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
93
116
|
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
94
117
|
if (!isValidItemKey(key)) {
|
|
@@ -101,20 +124,23 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
101
124
|
if (ret) {
|
|
102
125
|
cacheMap.set(ret.key, ret);
|
|
103
126
|
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
127
|
+
} catch (e) {
|
|
128
|
+
logger.error("Error getting item for key", {
|
|
129
|
+
key,
|
|
130
|
+
message: e.message,
|
|
131
|
+
stack: e.stack
|
|
132
|
+
});
|
|
107
133
|
throw e;
|
|
108
134
|
}
|
|
109
135
|
return [
|
|
110
136
|
cacheMap,
|
|
111
|
-
ret ?
|
|
112
|
-
validatePK(ret, pkType) :
|
|
113
|
-
null
|
|
137
|
+
ret ? validatePK(ret, pkType) : null
|
|
114
138
|
];
|
|
115
139
|
};
|
|
116
|
-
const retrieve = async (key)
|
|
117
|
-
logger.default('retrieve', {
|
|
140
|
+
const retrieve = async (key)=>{
|
|
141
|
+
logger.default('retrieve', {
|
|
142
|
+
key
|
|
143
|
+
});
|
|
118
144
|
if (!isValidItemKey(key)) {
|
|
119
145
|
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
120
146
|
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
@@ -124,22 +150,23 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
124
150
|
if (containsItemKey) {
|
|
125
151
|
logger.default('Looking for Object in Cache', key);
|
|
126
152
|
retrieved = cacheMap.get(key);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
153
|
+
} else {
|
|
154
|
+
logger.default('Object Not Found in Cache, Retrieving from Server API', {
|
|
155
|
+
key
|
|
156
|
+
});
|
|
130
157
|
[, retrieved] = await get(key);
|
|
131
158
|
}
|
|
132
159
|
const retValue = [
|
|
133
160
|
containsItemKey ? null : cacheMap,
|
|
134
|
-
retrieved ?
|
|
135
|
-
validatePK(retrieved, pkType) :
|
|
136
|
-
null
|
|
161
|
+
retrieved ? validatePK(retrieved, pkType) : null
|
|
137
162
|
];
|
|
138
163
|
// logger.debug('Returning from retrieve', { retValue });
|
|
139
164
|
return retValue;
|
|
140
165
|
};
|
|
141
|
-
const remove = async (key)
|
|
142
|
-
logger.default('remove', {
|
|
166
|
+
const remove = async (key)=>{
|
|
167
|
+
logger.default('remove', {
|
|
168
|
+
key
|
|
169
|
+
});
|
|
143
170
|
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
144
171
|
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
145
172
|
if (!isValidItemKey(key)) {
|
|
@@ -149,15 +176,19 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
149
176
|
try {
|
|
150
177
|
await api.remove(key, {});
|
|
151
178
|
cacheMap.delete(key);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
179
|
+
} catch (e) {
|
|
180
|
+
logger.error("Error deleting item", {
|
|
181
|
+
error: e
|
|
182
|
+
});
|
|
155
183
|
throw e;
|
|
156
184
|
}
|
|
157
185
|
return cacheMap;
|
|
158
186
|
};
|
|
159
|
-
const update = async (key, v)
|
|
160
|
-
logger.default('update', {
|
|
187
|
+
const update = async (key, v)=>{
|
|
188
|
+
logger.default('update', {
|
|
189
|
+
key,
|
|
190
|
+
v
|
|
191
|
+
});
|
|
161
192
|
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
162
193
|
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
163
194
|
if (!isValidItemKey(key)) {
|
|
@@ -167,27 +198,43 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
167
198
|
try {
|
|
168
199
|
const updated = await api.update(key, v, {});
|
|
169
200
|
cacheMap.set(updated.key, updated);
|
|
170
|
-
return [
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
201
|
+
return [
|
|
202
|
+
cacheMap,
|
|
203
|
+
validatePK(updated, pkType)
|
|
204
|
+
];
|
|
205
|
+
} catch (e) {
|
|
206
|
+
logger.error("Error updating chat", {
|
|
207
|
+
error: e
|
|
208
|
+
});
|
|
174
209
|
throw e;
|
|
175
210
|
}
|
|
176
211
|
};
|
|
177
|
-
const find = async (finder, finderParams, locations = [])
|
|
178
|
-
logger.default('find', {
|
|
212
|
+
const find = async (finder, finderParams, locations = [])=>{
|
|
213
|
+
logger.default('find', {
|
|
214
|
+
finder,
|
|
215
|
+
finderParams,
|
|
216
|
+
locations
|
|
217
|
+
});
|
|
179
218
|
const ret = await api.find(finder, finderParams, {}, locations);
|
|
180
|
-
ret.forEach((v)
|
|
219
|
+
ret.forEach((v)=>{
|
|
181
220
|
cacheMap.set(v.key, v);
|
|
182
221
|
});
|
|
183
|
-
return [
|
|
222
|
+
return [
|
|
223
|
+
cacheMap,
|
|
224
|
+
validatePK(ret, pkType)
|
|
225
|
+
];
|
|
184
226
|
};
|
|
185
|
-
const reset = async ()
|
|
227
|
+
const reset = async ()=>{
|
|
186
228
|
cacheMap = new CacheMap(pkTypes);
|
|
187
|
-
return [
|
|
229
|
+
return [
|
|
230
|
+
cacheMap
|
|
231
|
+
];
|
|
188
232
|
};
|
|
189
|
-
const set = async (key, v)
|
|
190
|
-
logger.default('set', {
|
|
233
|
+
const set = async (key, v)=>{
|
|
234
|
+
logger.default('set', {
|
|
235
|
+
key,
|
|
236
|
+
v
|
|
237
|
+
});
|
|
191
238
|
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
192
239
|
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
193
240
|
if (!isValidItemKey(key)) {
|
|
@@ -201,7 +248,10 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
201
248
|
throw new Error('Key does not match item key');
|
|
202
249
|
}
|
|
203
250
|
cacheMap.set(key, v);
|
|
204
|
-
return [
|
|
251
|
+
return [
|
|
252
|
+
cacheMap,
|
|
253
|
+
validatePK(v, pkType)
|
|
254
|
+
];
|
|
205
255
|
};
|
|
206
256
|
return {
|
|
207
257
|
all,
|
|
@@ -220,4 +270,6 @@ export const createCache = (api, pkType, parentCache) => {
|
|
|
220
270
|
cacheMap
|
|
221
271
|
};
|
|
222
272
|
};
|
|
223
|
-
|
|
273
|
+
|
|
274
|
+
export { createCache };
|
|
275
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2FjaGUuZXMuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|