@fjell/cache 4.6.10 → 4.6.11
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 +26 -20
- package/dist/Aggregator.d.ts +17 -1
- package/dist/Aggregator.es.js +26 -20
- package/dist/Cache.cjs.js +22 -345
- package/dist/Cache.d.ts +25 -20
- package/dist/Cache.es.js +22 -346
- package/dist/Instance.cjs.js +7 -11
- package/dist/Instance.d.ts +5 -8
- package/dist/Instance.es.js +6 -10
- package/dist/InstanceFactory.cjs.js +17 -5
- package/dist/InstanceFactory.d.ts +3 -3
- package/dist/InstanceFactory.es.js +17 -5
- package/dist/Operations.cjs.js +43 -0
- package/dist/Operations.d.ts +70 -0
- package/dist/Operations.es.js +39 -0
- package/dist/index.cjs +416 -369
- package/dist/index.cjs.js +4 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +3 -2
- package/dist/ops/action.cjs.js +28 -0
- package/dist/ops/action.d.ts +4 -0
- package/dist/ops/action.es.js +24 -0
- package/dist/ops/all.cjs.js +33 -0
- package/dist/ops/all.d.ts +4 -0
- package/dist/ops/all.es.js +29 -0
- package/dist/ops/allAction.cjs.js +35 -0
- package/dist/ops/allAction.d.ts +4 -0
- package/dist/ops/allAction.es.js +31 -0
- package/dist/ops/allFacet.cjs.js +22 -0
- package/dist/ops/allFacet.d.ts +4 -0
- package/dist/ops/allFacet.es.js +18 -0
- package/dist/ops/create.cjs.js +23 -0
- package/dist/ops/create.d.ts +4 -0
- package/dist/ops/create.es.js +19 -0
- package/dist/ops/facet.cjs.js +21 -0
- package/dist/ops/facet.d.ts +4 -0
- package/dist/ops/facet.es.js +17 -0
- package/dist/ops/find.cjs.js +26 -0
- package/dist/ops/find.d.ts +4 -0
- package/dist/ops/find.es.js +22 -0
- package/dist/ops/findOne.cjs.js +24 -0
- package/dist/ops/findOne.d.ts +4 -0
- package/dist/ops/findOne.es.js +20 -0
- package/dist/ops/get.cjs.js +38 -0
- package/dist/ops/get.d.ts +4 -0
- package/dist/ops/get.es.js +34 -0
- package/dist/ops/one.cjs.js +33 -0
- package/dist/ops/one.d.ts +4 -0
- package/dist/ops/one.es.js +29 -0
- package/dist/ops/remove.cjs.js +30 -0
- package/dist/ops/remove.d.ts +4 -0
- package/dist/ops/remove.es.js +26 -0
- package/dist/ops/reset.cjs.js +15 -0
- package/dist/ops/reset.d.ts +4 -0
- package/dist/ops/reset.es.js +11 -0
- package/dist/ops/retrieve.cjs.js +37 -0
- package/dist/ops/retrieve.d.ts +4 -0
- package/dist/ops/retrieve.es.js +33 -0
- package/dist/ops/set.cjs.js +71 -0
- package/dist/ops/set.d.ts +3 -0
- package/dist/ops/set.es.js +67 -0
- package/dist/ops/update.cjs.js +34 -0
- package/dist/ops/update.d.ts +4 -0
- package/dist/ops/update.es.js +30 -0
- package/examples/README.md +34 -39
- package/examples/aggregator-example.ts +8 -14
- package/examples/basic-cache-example.ts +18 -21
- package/package.json +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { validatePK } from '@fjell/core';
|
|
2
|
+
import { NotFoundError } from '@fjell/http-api';
|
|
3
|
+
import LibLogger from '../logger.es.js';
|
|
4
|
+
|
|
5
|
+
const logger = LibLogger.get('one');
|
|
6
|
+
const one = async (api, cacheMap, pkType, query = {}, locations = [])=>{
|
|
7
|
+
logger.default('one', {
|
|
8
|
+
query,
|
|
9
|
+
locations
|
|
10
|
+
});
|
|
11
|
+
let retItem = null;
|
|
12
|
+
try {
|
|
13
|
+
retItem = await api.one(query, locations);
|
|
14
|
+
if (retItem) {
|
|
15
|
+
cacheMap.set(retItem.key, retItem);
|
|
16
|
+
}
|
|
17
|
+
} catch (e) {
|
|
18
|
+
if (e instanceof NotFoundError) ; else {
|
|
19
|
+
throw e;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return [
|
|
23
|
+
cacheMap,
|
|
24
|
+
retItem ? validatePK(retItem, pkType) : null
|
|
25
|
+
];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export { one };
|
|
29
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib25lLmVzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs.js');
|
|
7
|
+
|
|
8
|
+
const logger = logger$1.default.get('remove');
|
|
9
|
+
const remove = async (api, cacheMap, key)=>{
|
|
10
|
+
logger.default('remove', {
|
|
11
|
+
key
|
|
12
|
+
});
|
|
13
|
+
if (!core.isValidItemKey(key)) {
|
|
14
|
+
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
15
|
+
throw new Error('Key for Remove is not a valid ItemKey');
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
await api.remove(key);
|
|
19
|
+
cacheMap.delete(key);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
logger.error("Error deleting item", {
|
|
22
|
+
error: e
|
|
23
|
+
});
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
return cacheMap;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
exports.remove = remove;
|
|
30
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLmNqcy5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ComKey, Item, PriKey } from '@fjell/core';
|
|
2
|
+
import { ClientApi } from '@fjell/client-api';
|
|
3
|
+
import { CacheMap } from '../CacheMap';
|
|
4
|
+
export declare const remove: <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>(api: ClientApi<V, S, L1, L2, L3, L4, L5>, cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>, key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isValidItemKey } from '@fjell/core';
|
|
2
|
+
import LibLogger from '../logger.es.js';
|
|
3
|
+
|
|
4
|
+
const logger = LibLogger.get('remove');
|
|
5
|
+
const remove = async (api, cacheMap, key)=>{
|
|
6
|
+
logger.default('remove', {
|
|
7
|
+
key
|
|
8
|
+
});
|
|
9
|
+
if (!isValidItemKey(key)) {
|
|
10
|
+
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
11
|
+
throw new Error('Key for Remove is not a valid ItemKey');
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
await api.remove(key);
|
|
15
|
+
cacheMap.delete(key);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
logger.error("Error deleting item", {
|
|
18
|
+
error: e
|
|
19
|
+
});
|
|
20
|
+
throw e;
|
|
21
|
+
}
|
|
22
|
+
return cacheMap;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { remove };
|
|
26
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVtb3ZlLmVzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const CacheMap = require('../CacheMap.cjs.js');
|
|
6
|
+
|
|
7
|
+
const reset = async (coordinate)=>{
|
|
8
|
+
const cacheMap = new CacheMap.CacheMap(coordinate.kta);
|
|
9
|
+
return [
|
|
10
|
+
cacheMap
|
|
11
|
+
];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.reset = reset;
|
|
15
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVzZXQuY2pzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Item } from '@fjell/core';
|
|
2
|
+
import { CacheMap } from '../CacheMap';
|
|
3
|
+
import { Coordinate } from '@fjell/registry';
|
|
4
|
+
export declare const reset: <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>(coordinate: Coordinate<S, L1, L2, L3, L4, L5>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CacheMap } from '../CacheMap.es.js';
|
|
2
|
+
|
|
3
|
+
const reset = async (coordinate)=>{
|
|
4
|
+
const cacheMap = new CacheMap(coordinate.kta);
|
|
5
|
+
return [
|
|
6
|
+
cacheMap
|
|
7
|
+
];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { reset };
|
|
11
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVzZXQuZXMuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs.js');
|
|
7
|
+
const get = require('./get.cjs.js');
|
|
8
|
+
|
|
9
|
+
const logger = logger$1.default.get('retrieve');
|
|
10
|
+
const retrieve = async (api, cacheMap, pkType, key)=>{
|
|
11
|
+
logger.default('retrieve', {
|
|
12
|
+
key
|
|
13
|
+
});
|
|
14
|
+
if (!core.isValidItemKey(key)) {
|
|
15
|
+
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
16
|
+
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
17
|
+
}
|
|
18
|
+
const containsItemKey = cacheMap.includesKey(key);
|
|
19
|
+
let retrieved;
|
|
20
|
+
if (containsItemKey) {
|
|
21
|
+
logger.default('Looking for Object in Cache', key);
|
|
22
|
+
retrieved = cacheMap.get(key);
|
|
23
|
+
} else {
|
|
24
|
+
logger.default('Object Not Found in Cache, Retrieving from Server API', {
|
|
25
|
+
key
|
|
26
|
+
});
|
|
27
|
+
[, retrieved] = await get.get(api, cacheMap, pkType, key);
|
|
28
|
+
}
|
|
29
|
+
const retValue = [
|
|
30
|
+
containsItemKey ? null : cacheMap,
|
|
31
|
+
retrieved ? core.validatePK(retrieved, pkType) : null
|
|
32
|
+
];
|
|
33
|
+
return retValue;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.retrieve = retrieve;
|
|
37
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmV0cmlldmUuY2pzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ComKey, Item, PriKey } from '@fjell/core';
|
|
2
|
+
import { CacheMap } from '../CacheMap';
|
|
3
|
+
import { ClientApi } from '@fjell/client-api';
|
|
4
|
+
export declare const retrieve: <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>(api: ClientApi<V, S, L1, L2, L3, L4, L5>, cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>, pkType: S, key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isValidItemKey, validatePK } from '@fjell/core';
|
|
2
|
+
import LibLogger from '../logger.es.js';
|
|
3
|
+
import { get } from './get.es.js';
|
|
4
|
+
|
|
5
|
+
const logger = LibLogger.get('retrieve');
|
|
6
|
+
const retrieve = async (api, cacheMap, pkType, key)=>{
|
|
7
|
+
logger.default('retrieve', {
|
|
8
|
+
key
|
|
9
|
+
});
|
|
10
|
+
if (!isValidItemKey(key)) {
|
|
11
|
+
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
12
|
+
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
13
|
+
}
|
|
14
|
+
const containsItemKey = cacheMap.includesKey(key);
|
|
15
|
+
let retrieved;
|
|
16
|
+
if (containsItemKey) {
|
|
17
|
+
logger.default('Looking for Object in Cache', key);
|
|
18
|
+
retrieved = cacheMap.get(key);
|
|
19
|
+
} else {
|
|
20
|
+
logger.default('Object Not Found in Cache, Retrieving from Server API', {
|
|
21
|
+
key
|
|
22
|
+
});
|
|
23
|
+
[, retrieved] = await get(api, cacheMap, pkType, key);
|
|
24
|
+
}
|
|
25
|
+
const retValue = [
|
|
26
|
+
containsItemKey ? null : cacheMap,
|
|
27
|
+
retrieved ? validatePK(retrieved, pkType) : null
|
|
28
|
+
];
|
|
29
|
+
return retValue;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { retrieve };
|
|
33
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmV0cmlldmUuZXMuanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs.js');
|
|
7
|
+
|
|
8
|
+
const logger = logger$1.default.get('set');
|
|
9
|
+
// Normalize a key value to string for consistent comparison
|
|
10
|
+
const normalizeKeyValue = (value)=>{
|
|
11
|
+
return String(value);
|
|
12
|
+
};
|
|
13
|
+
// Normalized key comparison function that handles string/number differences
|
|
14
|
+
const isItemKeyEqualNormalized = (a, b)=>{
|
|
15
|
+
// For now, just normalize the keys to strings and use the original comparison
|
|
16
|
+
const normalizedA = normalizeKey(a);
|
|
17
|
+
const normalizedB = normalizeKey(b);
|
|
18
|
+
return core.isItemKeyEqual(normalizedA, normalizedB);
|
|
19
|
+
};
|
|
20
|
+
// Helper function to normalize a key
|
|
21
|
+
const normalizeKey = (key)=>{
|
|
22
|
+
if (typeof key === 'object' && key !== null) {
|
|
23
|
+
const normalizedKey = JSON.parse(JSON.stringify(key));
|
|
24
|
+
// Normalize pk values
|
|
25
|
+
if ('pk' in normalizedKey && normalizedKey.pk !== null) {
|
|
26
|
+
normalizedKey.pk = normalizeKeyValue(normalizedKey.pk);
|
|
27
|
+
}
|
|
28
|
+
// Normalize lk values
|
|
29
|
+
if ('lk' in normalizedKey && normalizedKey.lk !== null) {
|
|
30
|
+
normalizedKey.lk = normalizeKeyValue(normalizedKey.lk);
|
|
31
|
+
}
|
|
32
|
+
// Normalize loc array lk values
|
|
33
|
+
if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
|
|
34
|
+
normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
|
|
35
|
+
if (locItem && 'lk' in locItem && locItem.lk !== null) {
|
|
36
|
+
return {
|
|
37
|
+
...locItem,
|
|
38
|
+
lk: normalizeKeyValue(locItem.lk)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return locItem;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return normalizedKey;
|
|
45
|
+
}
|
|
46
|
+
return key;
|
|
47
|
+
};
|
|
48
|
+
const set = async (cacheMap, pkType, key, v)=>{
|
|
49
|
+
logger.default('set', {
|
|
50
|
+
key,
|
|
51
|
+
v
|
|
52
|
+
});
|
|
53
|
+
if (!core.isValidItemKey(key)) {
|
|
54
|
+
logger.error('Key for Set is not a valid ItemKey: %j', key);
|
|
55
|
+
throw new Error('Key for Set is not a valid ItemKey');
|
|
56
|
+
}
|
|
57
|
+
// Validate the item's primary key
|
|
58
|
+
core.validatePK(v, pkType);
|
|
59
|
+
if (!isItemKeyEqualNormalized(key, v.key)) {
|
|
60
|
+
logger.error('Key does not match item key: %j != %j', key, v.key);
|
|
61
|
+
throw new Error('Key does not match item key');
|
|
62
|
+
}
|
|
63
|
+
cacheMap.set(key, v);
|
|
64
|
+
return [
|
|
65
|
+
cacheMap,
|
|
66
|
+
core.validatePK(v, pkType)
|
|
67
|
+
];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
exports.set = set;
|
|
71
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2V0LmNqcy5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ComKey, Item, PriKey } from '@fjell/core';
|
|
2
|
+
import { CacheMap } from '../CacheMap';
|
|
3
|
+
export declare const set: <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>(cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>, pkType: S, 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]>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isValidItemKey, validatePK, isItemKeyEqual } from '@fjell/core';
|
|
2
|
+
import LibLogger from '../logger.es.js';
|
|
3
|
+
|
|
4
|
+
const logger = LibLogger.get('set');
|
|
5
|
+
// Normalize a key value to string for consistent comparison
|
|
6
|
+
const normalizeKeyValue = (value)=>{
|
|
7
|
+
return String(value);
|
|
8
|
+
};
|
|
9
|
+
// Normalized key comparison function that handles string/number differences
|
|
10
|
+
const isItemKeyEqualNormalized = (a, b)=>{
|
|
11
|
+
// For now, just normalize the keys to strings and use the original comparison
|
|
12
|
+
const normalizedA = normalizeKey(a);
|
|
13
|
+
const normalizedB = normalizeKey(b);
|
|
14
|
+
return isItemKeyEqual(normalizedA, normalizedB);
|
|
15
|
+
};
|
|
16
|
+
// Helper function to normalize a key
|
|
17
|
+
const normalizeKey = (key)=>{
|
|
18
|
+
if (typeof key === 'object' && key !== null) {
|
|
19
|
+
const normalizedKey = JSON.parse(JSON.stringify(key));
|
|
20
|
+
// Normalize pk values
|
|
21
|
+
if ('pk' in normalizedKey && normalizedKey.pk !== null) {
|
|
22
|
+
normalizedKey.pk = normalizeKeyValue(normalizedKey.pk);
|
|
23
|
+
}
|
|
24
|
+
// Normalize lk values
|
|
25
|
+
if ('lk' in normalizedKey && normalizedKey.lk !== null) {
|
|
26
|
+
normalizedKey.lk = normalizeKeyValue(normalizedKey.lk);
|
|
27
|
+
}
|
|
28
|
+
// Normalize loc array lk values
|
|
29
|
+
if ('loc' in normalizedKey && Array.isArray(normalizedKey.loc)) {
|
|
30
|
+
normalizedKey.loc = normalizedKey.loc.map((locItem)=>{
|
|
31
|
+
if (locItem && 'lk' in locItem && locItem.lk !== null) {
|
|
32
|
+
return {
|
|
33
|
+
...locItem,
|
|
34
|
+
lk: normalizeKeyValue(locItem.lk)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return locItem;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return normalizedKey;
|
|
41
|
+
}
|
|
42
|
+
return key;
|
|
43
|
+
};
|
|
44
|
+
const set = async (cacheMap, pkType, key, v)=>{
|
|
45
|
+
logger.default('set', {
|
|
46
|
+
key,
|
|
47
|
+
v
|
|
48
|
+
});
|
|
49
|
+
if (!isValidItemKey(key)) {
|
|
50
|
+
logger.error('Key for Set is not a valid ItemKey: %j', key);
|
|
51
|
+
throw new Error('Key for Set is not a valid ItemKey');
|
|
52
|
+
}
|
|
53
|
+
// Validate the item's primary key
|
|
54
|
+
validatePK(v, pkType);
|
|
55
|
+
if (!isItemKeyEqualNormalized(key, v.key)) {
|
|
56
|
+
logger.error('Key does not match item key: %j != %j', key, v.key);
|
|
57
|
+
throw new Error('Key does not match item key');
|
|
58
|
+
}
|
|
59
|
+
cacheMap.set(key, v);
|
|
60
|
+
return [
|
|
61
|
+
cacheMap,
|
|
62
|
+
validatePK(v, pkType)
|
|
63
|
+
];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export { set };
|
|
67
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2V0LmVzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OzsifQ==
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const core = require('@fjell/core');
|
|
6
|
+
const logger$1 = require('../logger.cjs.js');
|
|
7
|
+
|
|
8
|
+
const logger = logger$1.default.get('update');
|
|
9
|
+
const update = async (api, cacheMap, pkType, key, v)=>{
|
|
10
|
+
logger.default('update', {
|
|
11
|
+
key,
|
|
12
|
+
v
|
|
13
|
+
});
|
|
14
|
+
if (!core.isValidItemKey(key)) {
|
|
15
|
+
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
16
|
+
throw new Error('Key for Update is not a valid ItemKey');
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const updated = await api.update(key, v);
|
|
20
|
+
cacheMap.set(updated.key, updated);
|
|
21
|
+
return [
|
|
22
|
+
cacheMap,
|
|
23
|
+
core.validatePK(updated, pkType)
|
|
24
|
+
];
|
|
25
|
+
} catch (e) {
|
|
26
|
+
logger.error("Error updating item", {
|
|
27
|
+
error: e
|
|
28
|
+
});
|
|
29
|
+
throw e;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.update = update;
|
|
34
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBkYXRlLmNqcy5qcyIsInNvdXJjZXMiOltdLCJzb3VyY2VzQ29udGVudCI6W10sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7In0=
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ComKey, Item, PriKey } from '@fjell/core';
|
|
2
|
+
import { ClientApi } from '@fjell/client-api';
|
|
3
|
+
import { CacheMap } from '../CacheMap';
|
|
4
|
+
export declare const update: <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>(api: ClientApi<V, S, L1, L2, L3, L4, L5>, cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>, pkType: S, key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, v: Partial<Item<S, L1, L2, L3, L4, L5>>) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isValidItemKey, validatePK } from '@fjell/core';
|
|
2
|
+
import LibLogger from '../logger.es.js';
|
|
3
|
+
|
|
4
|
+
const logger = LibLogger.get('update');
|
|
5
|
+
const update = async (api, cacheMap, pkType, key, v)=>{
|
|
6
|
+
logger.default('update', {
|
|
7
|
+
key,
|
|
8
|
+
v
|
|
9
|
+
});
|
|
10
|
+
if (!isValidItemKey(key)) {
|
|
11
|
+
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
12
|
+
throw new Error('Key for Update is not a valid ItemKey');
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const updated = await api.update(key, v);
|
|
16
|
+
cacheMap.set(updated.key, updated);
|
|
17
|
+
return [
|
|
18
|
+
cacheMap,
|
|
19
|
+
validatePK(updated, pkType)
|
|
20
|
+
];
|
|
21
|
+
} catch (e) {
|
|
22
|
+
logger.error("Error updating item", {
|
|
23
|
+
error: e
|
|
24
|
+
});
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { update };
|
|
30
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXBkYXRlLmVzLmpzIiwic291cmNlcyI6W10sInNvdXJjZXNDb250ZW50IjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OyJ9
|
package/examples/README.md
CHANGED
|
@@ -6,11 +6,11 @@ This directory contains examples demonstrating how to use fjell-cache for cachin
|
|
|
6
6
|
|
|
7
7
|
### 1. `basic-cache-example.ts` ⭐ **Start Here!**
|
|
8
8
|
**Perfect for beginners!** Demonstrates the fundamental way to use fjell-cache for data caching:
|
|
9
|
-
- **Basic cache operations** - Create caches
|
|
9
|
+
- **Basic cache operations** - Create caches with coordinates and registries, use operations API
|
|
10
10
|
- **Simple data models** - User and Task entities with mock storage
|
|
11
|
-
- **
|
|
11
|
+
- **Cache-as-Instance architecture** - Caches extend Instance from fjell-registry
|
|
12
12
|
- **Cache hits vs misses** - Understand cache behavior and performance benefits
|
|
13
|
-
- **Cache management** - Updates, deletions, and data consistency
|
|
13
|
+
- **Cache management** - Updates, deletions, and data consistency through operations
|
|
14
14
|
|
|
15
15
|
Great for understanding the fundamentals of fjell-cache data management.
|
|
16
16
|
|
|
@@ -39,7 +39,8 @@ Perfect for understanding the underlying cache mechanisms and advanced use cases
|
|
|
39
39
|
### Basic Caching Operations (basic-cache-example.ts)
|
|
40
40
|
```typescript
|
|
41
41
|
// Import fjell-cache functionality
|
|
42
|
-
import { createCache
|
|
42
|
+
import { createCache } from '@fjell/cache';
|
|
43
|
+
import { createCoordinate, createRegistry } from '@fjell/registry';
|
|
43
44
|
import { ClientApi } from '@fjell/client-api';
|
|
44
45
|
|
|
45
46
|
// Create a registry for cache management
|
|
@@ -47,17 +48,15 @@ const registry = createRegistry();
|
|
|
47
48
|
|
|
48
49
|
// Create a cache instance with API integration
|
|
49
50
|
const userApi = createUserApi(); // Your API implementation
|
|
50
|
-
const userCache = await createCache(userApi, 'user');
|
|
51
|
-
|
|
52
|
-
// Create cache model instance
|
|
53
|
-
const userInstance = createInstance(registry, createCoordinate('user'), userCache);
|
|
51
|
+
const userCache = await createCache(userApi, createCoordinate('user'), registry);
|
|
54
52
|
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
const [,
|
|
58
|
-
const [,
|
|
53
|
+
// Cache is now an instance - no need for separate createInstance call
|
|
54
|
+
// Perform cache operations through the operations API
|
|
55
|
+
const [cacheMap, allUsers] = await userCache.operations.all();
|
|
56
|
+
const [, cachedUser] = await userCache.operations.get(userKey);
|
|
57
|
+
const [, retrievedUser] = await userCache.operations.retrieve(userKey); // Cache hit!
|
|
59
58
|
|
|
60
|
-
await
|
|
59
|
+
await userCache.operations.set(userKey, updatedUser);
|
|
61
60
|
```
|
|
62
61
|
|
|
63
62
|
### Advanced Aggregation (aggregator-example.ts)
|
|
@@ -78,8 +77,8 @@ if (populatedOrder.aggs?.customer?.item) {
|
|
|
78
77
|
console.log(`Order for: ${customer.name} (${customer.email})`);
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
//
|
|
82
|
-
const
|
|
80
|
+
// Aggregator is now itself an instance - access operations directly
|
|
81
|
+
const [, orders] = await orderAggregator.all();
|
|
83
82
|
```
|
|
84
83
|
|
|
85
84
|
### Direct Cache Management (cache-map-example.ts)
|
|
@@ -262,18 +261,16 @@ const populatedTicket = await ticketAggregator.populate(ticket);
|
|
|
262
261
|
|
|
263
262
|
### Performance Optimization
|
|
264
263
|
```typescript
|
|
265
|
-
// Cache with
|
|
266
|
-
const cache = await createCache(api, 'product',
|
|
267
|
-
batchSize: 100, // Batch operations
|
|
268
|
-
prefetch: true, // Prefetch related items
|
|
269
|
-
compression: true, // Compress cached data
|
|
270
|
-
ttl: 3600000, // 1 hour cache lifetime
|
|
271
|
-
maxSize: 10000 // Maximum cached items
|
|
272
|
-
});
|
|
264
|
+
// Cache with coordinate and registry
|
|
265
|
+
const cache = await createCache(api, createCoordinate('product'), registry);
|
|
273
266
|
|
|
274
267
|
// Bulk operations for efficiency
|
|
275
|
-
const [cacheMap, allProducts] = await cache.all();
|
|
268
|
+
const [cacheMap, allProducts] = await cache.operations.all();
|
|
276
269
|
const productMap = new Map(allProducts.map(p => [p.id, p]));
|
|
270
|
+
|
|
271
|
+
// Access cache properties for optimization
|
|
272
|
+
console.log(`Cache coordinate: ${cache.coordinate.kta.join(', ')}`);
|
|
273
|
+
console.log(`Cached items: ${cache.cacheMap.size()}`);
|
|
277
274
|
```
|
|
278
275
|
|
|
279
276
|
### Storage Integration
|
|
@@ -287,21 +284,19 @@ Fjell-cache works with any storage backend through the ClientApi interface:
|
|
|
287
284
|
|
|
288
285
|
### Error Handling and Resilience
|
|
289
286
|
```typescript
|
|
290
|
-
// Cache with error handling
|
|
291
|
-
const resilientCache = await createCache(api, 'user',
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
});
|
|
287
|
+
// Cache with proper error handling through the API layer
|
|
288
|
+
const resilientCache = await createCache(api, createCoordinate('user'), registry);
|
|
289
|
+
|
|
290
|
+
// Error handling in operations
|
|
291
|
+
try {
|
|
292
|
+
const [, user] = await resilientCache.operations.get(userKey);
|
|
293
|
+
return user;
|
|
294
|
+
} catch (error) {
|
|
295
|
+
// Handle cache errors gracefully
|
|
296
|
+
console.error('Cache operation failed:', error);
|
|
297
|
+
// Fallback to direct API call
|
|
298
|
+
return await api.get(userKey);
|
|
299
|
+
}
|
|
305
300
|
```
|
|
306
301
|
|
|
307
302
|
This provides the foundation for building scalable, maintainable applications with intelligent caching using fjell-cache.
|
|
@@ -181,10 +181,10 @@ export const runAggregatorExample = async (): Promise<void> => {
|
|
|
181
181
|
const productApi = createMockApi(mockProducts) as ClientApi<Product, 'product'>;
|
|
182
182
|
const ticketApi = createMockApi(mockTickets) as ClientApi<SupportTicket, 'ticket'>;
|
|
183
183
|
|
|
184
|
-
const customerCache = await createCache(customerApi, 'customer');
|
|
185
|
-
const orderCache = await createCache(orderApi, 'order');
|
|
186
|
-
const productCache = await createCache(productApi, 'product');
|
|
187
|
-
const ticketCache = await createCache(ticketApi, 'ticket');
|
|
184
|
+
const customerCache = await createCache(customerApi, createCoordinate('customer'), registry);
|
|
185
|
+
const orderCache = await createCache(orderApi, createCoordinate('order'), registry);
|
|
186
|
+
const productCache = await createCache(productApi, createCoordinate('product'), registry);
|
|
187
|
+
const ticketCache = await createCache(ticketApi, createCoordinate('ticket'), registry);
|
|
188
188
|
|
|
189
189
|
console.log('✅ Created individual caches for each entity type');
|
|
190
190
|
|
|
@@ -209,19 +209,13 @@ export const runAggregatorExample = async (): Promise<void> => {
|
|
|
209
209
|
events: {}
|
|
210
210
|
});
|
|
211
211
|
|
|
212
|
-
console.log('✅ Created aggregated caches with relationship mappings');
|
|
213
|
-
|
|
214
|
-
// Create instances for easier management
|
|
215
|
-
const orderInstance = createInstance(registry, createCoordinate('order'), orderAggregator);
|
|
216
|
-
const ticketInstance = createInstance(registry, createCoordinate('ticket'), ticketAggregator);
|
|
217
|
-
|
|
218
|
-
console.log('✅ Created aggregated cache instances\n');
|
|
212
|
+
console.log('✅ Created aggregated caches with relationship mappings (aggregators are now instances)\n');
|
|
219
213
|
|
|
220
214
|
// Step 4: Basic aggregation - Fetch orders with customer data
|
|
221
215
|
console.log('Step 4: Order aggregation with customer data');
|
|
222
216
|
console.log('--------------------------------------------');
|
|
223
217
|
|
|
224
|
-
const [, orders] = await
|
|
218
|
+
const [, orders] = await orderAggregator.all();
|
|
225
219
|
console.log(`📋 Fetched ${orders.length} orders`);
|
|
226
220
|
|
|
227
221
|
for (const order of orders) {
|
|
@@ -243,7 +237,7 @@ export const runAggregatorExample = async (): Promise<void> => {
|
|
|
243
237
|
console.log('\n\nStep 5: Support ticket aggregation with multiple references');
|
|
244
238
|
console.log('----------------------------------------------------------');
|
|
245
239
|
|
|
246
|
-
const [, tickets] = await
|
|
240
|
+
const [, tickets] = await ticketAggregator.all();
|
|
247
241
|
console.log(`🎫 Fetched ${tickets.length} support tickets`);
|
|
248
242
|
|
|
249
243
|
for (const ticket of tickets) {
|
|
@@ -275,7 +269,7 @@ export const runAggregatorExample = async (): Promise<void> => {
|
|
|
275
269
|
console.log('\n\nStep 6: Individual item retrieval with aggregation');
|
|
276
270
|
console.log('-------------------------------------------------');
|
|
277
271
|
|
|
278
|
-
const [, specificOrder] = await
|
|
272
|
+
const [, specificOrder] = await orderAggregator.get(order1.key);
|
|
279
273
|
if (specificOrder) {
|
|
280
274
|
console.log(`🔍 Retrieved specific order: ${specificOrder.id}`);
|
|
281
275
|
|