@fjell/cache 4.6.4 → 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/package.json +18 -21
- package/.kodrdriv/config.yaml +0 -14
- package/.kodrdriv/context/content.md +0 -1
- package/commit.sh +0 -8
- package/release.sh +0 -89
- package/src/Aggregator.ts +0 -330
- package/src/Cache.ts +0 -428
- package/src/CacheMap.ts +0 -132
- package/src/CacheRegistry.ts +0 -66
- package/src/index.ts +0 -4
- package/src/logger.ts +0 -5
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.
|
|
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,39 +19,32 @@
|
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"dependencies": {
|
|
18
|
-
"@fjell/client-api": "^4.4.
|
|
19
|
-
"@fjell/core": "^4.4.
|
|
20
|
-
"@fjell/http-api": "^4.4.
|
|
21
|
-
"@fjell/logging": "^4.4.
|
|
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.27.2",
|
|
28
|
-
"@babel/preset-typescript": "^7.27.1",
|
|
29
28
|
"@eslint/eslintrc": "^3.3.1",
|
|
30
|
-
"@eslint/js": "^9.30.
|
|
31
|
-
"@swc/core": "^1.12.
|
|
29
|
+
"@eslint/js": "^9.30.1",
|
|
30
|
+
"@swc/core": "^1.12.11",
|
|
32
31
|
"@tsconfig/recommended": "^1.0.10",
|
|
33
|
-
"@types/
|
|
34
|
-
"@types/
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"@typescript-eslint/eslint-plugin": "^8.35.0",
|
|
38
|
-
"@typescript-eslint/parser": "^8.35.0",
|
|
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",
|
|
39
36
|
"@vitest/coverage-v8": "^3.2.4",
|
|
40
37
|
"@vitest/ui": "^3.2.4",
|
|
41
38
|
"concurrently": "^9.2.0",
|
|
42
|
-
"eslint": "^9.30.
|
|
39
|
+
"eslint": "^9.30.1",
|
|
43
40
|
"nodemon": "^3.1.10",
|
|
44
41
|
"rimraf": "^6.0.1",
|
|
45
42
|
"ts-node": "^10.9.2",
|
|
46
43
|
"tsc-alias": "^1.8.16",
|
|
47
44
|
"typescript": "^5.8.3",
|
|
48
|
-
"vite": "^7.0.
|
|
45
|
+
"vite": "^7.0.3",
|
|
49
46
|
"vite-plugin-dts": "^4.5.4",
|
|
50
|
-
"vite-plugin-node": "^
|
|
47
|
+
"vite-plugin-node": "^7.0.0",
|
|
51
48
|
"vitest": "^3.2.4"
|
|
52
49
|
},
|
|
53
50
|
"repository": {
|
package/.kodrdriv/config.yaml
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
This is the Fjell Library for Client-side Caches
|
package/commit.sh
DELETED
package/release.sh
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
set -e
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
echo "Preparing for release: switching from workspace to remote dependencies."
|
|
6
|
-
if [ -f "pnpm-workspace.yaml" ]; then
|
|
7
|
-
echo "Renaming pnpm-workspace.yaml to prevent workspace-protocol resolution"
|
|
8
|
-
mv pnpm-workspace.yaml pnpm-workspace.yaml.bak
|
|
9
|
-
else
|
|
10
|
-
echo "pnpm-workspace.yaml not found, skipping rename."
|
|
11
|
-
fi
|
|
12
|
-
|
|
13
|
-
echo "Updating dependencies to latest versions from registry"
|
|
14
|
-
pnpm update --latest
|
|
15
|
-
|
|
16
|
-
echo "Staging changes for release commit"
|
|
17
|
-
git add package.json pnpm-lock.yaml
|
|
18
|
-
|
|
19
|
-
echo "Running clean, lint, build, and test..."
|
|
20
|
-
pnpm run clean && pnpm run lint && pnpm run build && pnpm run test
|
|
21
|
-
|
|
22
|
-
if git diff --staged --quiet; then
|
|
23
|
-
echo "No changes to commit, skipping commit."
|
|
24
|
-
else
|
|
25
|
-
./commit.sh
|
|
26
|
-
fi
|
|
27
|
-
|
|
28
|
-
echo "Bumping version..."
|
|
29
|
-
pnpm version patch
|
|
30
|
-
|
|
31
|
-
echo "Generating release notes..."
|
|
32
|
-
pnpm dlx @eldrforge/kodrdriv release > RELEASE_NOTES.md
|
|
33
|
-
|
|
34
|
-
echo "Pushing to origin..."
|
|
35
|
-
git push --follow-tags
|
|
36
|
-
|
|
37
|
-
echo "Creating GitHub pull request..."
|
|
38
|
-
PR_URL=$(gh pr create --fill)
|
|
39
|
-
PR_NUM=$(echo "$PR_URL" | grep -o '[0-9]*$')
|
|
40
|
-
echo "Pull request created: $PR_URL"
|
|
41
|
-
|
|
42
|
-
echo "Waiting for PR #$PR_NUM checks to complete..."
|
|
43
|
-
while true; do
|
|
44
|
-
STATUS=$(gh pr view "$PR_NUM" --json statusCheckRollup --jq '.statusCheckRollup.state' 2>/dev/null)
|
|
45
|
-
if [[ -z "$STATUS" ]]; then
|
|
46
|
-
STATUS="PENDING"
|
|
47
|
-
fi
|
|
48
|
-
echo "PR status: $STATUS"
|
|
49
|
-
if [[ "$STATUS" == "SUCCESS" ]]; then
|
|
50
|
-
echo "All checks passed!"
|
|
51
|
-
break
|
|
52
|
-
elif [[ "$STATUS" == "FAILURE" || "$STATUS" == "ERROR" ]]; then
|
|
53
|
-
echo "PR checks failed."
|
|
54
|
-
gh pr checks "$PR_NUM"
|
|
55
|
-
exit 1
|
|
56
|
-
elif [[ "$STATUS" == "PENDING" || "$STATUS" == "EXPECTED" ]]; then
|
|
57
|
-
echo "Checks are pending... waiting 10 seconds."
|
|
58
|
-
sleep 10
|
|
59
|
-
else
|
|
60
|
-
echo "Unknown PR status: $STATUS. Waiting 10 seconds."
|
|
61
|
-
sleep 10
|
|
62
|
-
fi
|
|
63
|
-
done
|
|
64
|
-
|
|
65
|
-
echo "Merging PR #$PR_NUM..."
|
|
66
|
-
gh pr merge "$PR_NUM" --squash --delete-branch
|
|
67
|
-
|
|
68
|
-
echo "Checking out main branch..."
|
|
69
|
-
git checkout main
|
|
70
|
-
git pull origin main
|
|
71
|
-
|
|
72
|
-
echo "Creating GitHub release..."
|
|
73
|
-
TAG_NAME="v$(jq -r .version package.json)"
|
|
74
|
-
gh release create "$TAG_NAME" --notes-file RELEASE_NOTES.md
|
|
75
|
-
|
|
76
|
-
echo "Creating next release branch..."
|
|
77
|
-
CURRENT_VERSION=$(jq -r .version package.json)
|
|
78
|
-
MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1)
|
|
79
|
-
MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2)
|
|
80
|
-
PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3)
|
|
81
|
-
NEXT_PATCH=$((PATCH + 1))
|
|
82
|
-
NEXT_VERSION="$MAJOR.$MINOR.$NEXT_PATCH"
|
|
83
|
-
|
|
84
|
-
echo "Next version is $NEXT_VERSION"
|
|
85
|
-
git checkout -b "release/v$NEXT_VERSION"
|
|
86
|
-
git commit -m "feat: Start release v$NEXT_VERSION"
|
|
87
|
-
git push -u origin "release/v$NEXT_VERSION"
|
|
88
|
-
|
|
89
|
-
echo "Release process completed."
|
package/src/Aggregator.ts
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-undefined */
|
|
2
|
-
import {
|
|
3
|
-
ComKey,
|
|
4
|
-
Item,
|
|
5
|
-
ItemQuery,
|
|
6
|
-
LocKeyArray,
|
|
7
|
-
PriKey
|
|
8
|
-
} from "@fjell/core";
|
|
9
|
-
import { Cache } from "./Cache";
|
|
10
|
-
import { CacheMap } from "./CacheMap";
|
|
11
|
-
import LibLogger from "./logger";
|
|
12
|
-
|
|
13
|
-
const logger = LibLogger.get('ItemAggregator');
|
|
14
|
-
|
|
15
|
-
export interface Aggregator<
|
|
16
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
17
|
-
S extends string,
|
|
18
|
-
L1 extends string = never,
|
|
19
|
-
L2 extends string = never,
|
|
20
|
-
L3 extends string = never,
|
|
21
|
-
L4 extends string = never,
|
|
22
|
-
L5 extends string = never
|
|
23
|
-
> extends Cache<V, S, L1, L2, L3, L4, L5> {
|
|
24
|
-
populate: (item: V) => Promise<V>;
|
|
25
|
-
populateAggregate: (key: string, item: V) => Promise<void>;
|
|
26
|
-
populateEvent: (key: string, item: V) => Promise<void>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface CacheConfig { cache: any, optional: boolean }
|
|
30
|
-
|
|
31
|
-
export interface AggregateConfig { [key: string]: (CacheConfig) }
|
|
32
|
-
|
|
33
|
-
export const toCacheConfig = <
|
|
34
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
35
|
-
S extends string,
|
|
36
|
-
L1 extends string = never,
|
|
37
|
-
L2 extends string = never,
|
|
38
|
-
L3 extends string = never,
|
|
39
|
-
L4 extends string = never,
|
|
40
|
-
L5 extends string = never
|
|
41
|
-
>(config: CacheConfig | Cache<V, S, L1, L2, L3, L4, L5>): CacheConfig => {
|
|
42
|
-
let cacheConfig: CacheConfig;
|
|
43
|
-
if ((config as CacheConfig).optional === undefined) {
|
|
44
|
-
cacheConfig = { cache: config as any, optional: false };
|
|
45
|
-
} else {
|
|
46
|
-
cacheConfig = config as CacheConfig;
|
|
47
|
-
}
|
|
48
|
-
return cacheConfig;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export const createAggregator = async <
|
|
52
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
53
|
-
S extends string,
|
|
54
|
-
L1 extends string = never,
|
|
55
|
-
L2 extends string = never,
|
|
56
|
-
L3 extends string = never,
|
|
57
|
-
L4 extends string = never,
|
|
58
|
-
L5 extends string = never
|
|
59
|
-
>(
|
|
60
|
-
cache: Cache<V, S, L1, L2, L3, L4, L5>,
|
|
61
|
-
{ aggregates = {}, events = {} }:
|
|
62
|
-
{
|
|
63
|
-
aggregates?: AggregateConfig,
|
|
64
|
-
events?: AggregateConfig
|
|
65
|
-
}
|
|
66
|
-
): Promise<Aggregator<V, S, L1, L2, L3, L4, L5>> => {
|
|
67
|
-
|
|
68
|
-
const populate = async (item: V): Promise<V> => {
|
|
69
|
-
logger.default('populate', { item });
|
|
70
|
-
for (const key in aggregates) {
|
|
71
|
-
await populateAggregate(key, item);
|
|
72
|
-
}
|
|
73
|
-
for (const key in events) {
|
|
74
|
-
await populateEvent(key, item);
|
|
75
|
-
}
|
|
76
|
-
logger.default('populate done', { item });
|
|
77
|
-
return item;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const populateAggregate = async (key: string, item: V) => {
|
|
81
|
-
logger.default('populate aggregate key', { key });
|
|
82
|
-
const cacheConfig = toCacheConfig(aggregates[key]);
|
|
83
|
-
if (item.refs === undefined) {
|
|
84
|
-
if (cacheConfig.optional === false) {
|
|
85
|
-
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
|
-
} else {
|
|
88
|
-
if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
|
|
89
|
-
delete item.events[key];
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
} else if (item.refs[key] === undefined) {
|
|
93
|
-
if (cacheConfig.optional === false) {
|
|
94
|
-
logger.error('Item does not have mandatory ref with key, not optional ' +
|
|
95
|
-
key + ' ' + JSON.stringify(item));
|
|
96
|
-
throw new Error('Item does not have mandatory ref with key, not optional ' +
|
|
97
|
-
key + ' ' + JSON.stringify(item));
|
|
98
|
-
} else {
|
|
99
|
-
if (item.events && Object.prototype.hasOwnProperty.call(item.events, key)) {
|
|
100
|
-
delete item.events[key];
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
|
|
105
|
-
const ref = item.refs[key];
|
|
106
|
-
|
|
107
|
-
logger.default('AGG Retrieving Item in Populate', { key: ref });
|
|
108
|
-
const [, newItem] = await cacheConfig.cache.retrieve(ref);
|
|
109
|
-
if (newItem) {
|
|
110
|
-
if (item.aggs === undefined) {
|
|
111
|
-
item.aggs = {};
|
|
112
|
-
}
|
|
113
|
-
item.aggs[key] = {
|
|
114
|
-
key: ref,
|
|
115
|
-
item: newItem as Item,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// TODO: I'm not a big fan that this just "automatically" assumes that the "by" key in event is a ref.
|
|
122
|
-
const populateEvent = async (key: string, item: V) => {
|
|
123
|
-
logger.default('populate event key', { key });
|
|
124
|
-
const cacheConfig = toCacheConfig(events[key]);
|
|
125
|
-
|
|
126
|
-
if (item.events === undefined) {
|
|
127
|
-
throw new Error('Item does not have events ' + JSON.stringify(item));
|
|
128
|
-
} else if (item.events[key] === undefined) {
|
|
129
|
-
if (cacheConfig.optional === false) {
|
|
130
|
-
logger.error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
|
|
131
|
-
throw new Error('Item does not have mandatory event with key ' + key + ' ' + JSON.stringify(item));
|
|
132
|
-
}
|
|
133
|
-
} else {
|
|
134
|
-
const event = item.events[key];
|
|
135
|
-
|
|
136
|
-
if (event.by === undefined) {
|
|
137
|
-
logger.error(
|
|
138
|
-
'populateEvent with an Event that does not have by', { event, ik: item.key, eventKey: key });
|
|
139
|
-
throw new Error('populateEvent with an Event that does not have by: ' + JSON.stringify({ key, event }));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
logger.default('EVENT Retrieving Item in Populate', { key: event.by });
|
|
143
|
-
const [, newItem] = await cacheConfig.cache.retrieve(event.by);
|
|
144
|
-
if (newItem) {
|
|
145
|
-
event.agg = newItem as Item;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const all = async (
|
|
151
|
-
query: ItemQuery = {},
|
|
152
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
153
|
-
):
|
|
154
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
155
|
-
logger.default('all', { query, locations });
|
|
156
|
-
const [cacheMap, items] = await cache.all(query, locations);
|
|
157
|
-
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
158
|
-
return [cacheMap, populatedItems];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const one = async (
|
|
162
|
-
query: ItemQuery = {},
|
|
163
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
164
|
-
):
|
|
165
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
166
|
-
logger.default('one', { query, locations });
|
|
167
|
-
const [cacheMap, item] = await cache.one(query, locations);
|
|
168
|
-
let populatedItem = null;
|
|
169
|
-
if (item) {
|
|
170
|
-
populatedItem = await populate(item);
|
|
171
|
-
}
|
|
172
|
-
return [cacheMap, populatedItem];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const action = async (
|
|
176
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
177
|
-
action: string,
|
|
178
|
-
body: any = {},
|
|
179
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
180
|
-
logger.default('action', { key, action, body });
|
|
181
|
-
const [cacheMap, item] = await cache.action(key, action, body);
|
|
182
|
-
const populatedItem = await populate(item);
|
|
183
|
-
return [cacheMap, populatedItem];
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const allAction = async (
|
|
187
|
-
action: string,
|
|
188
|
-
body: any = {},
|
|
189
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
190
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
191
|
-
logger.default('action', { action, body, locations });
|
|
192
|
-
const [cacheMap, items] = await cache.allAction(action, body, locations);
|
|
193
|
-
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
194
|
-
return [cacheMap, populatedItems];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const allFacet = async (
|
|
198
|
-
facet: string,
|
|
199
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
200
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
201
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
202
|
-
logger.default('allFacet', { facet, params, locations });
|
|
203
|
-
const [cacheMap, response] = await cache.allFacet(facet, params, locations);
|
|
204
|
-
return [cacheMap, response];
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const create = async (
|
|
208
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
209
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
210
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
211
|
-
logger.default('create', { v, locations });
|
|
212
|
-
const [cacheMap, item] = await cache.create(v, locations);
|
|
213
|
-
const populatedItem = await populate(item);
|
|
214
|
-
return [cacheMap, populatedItem];
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const get = async (
|
|
218
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
219
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
220
|
-
logger.default('get', { key });
|
|
221
|
-
const [cacheMap, item] = await cache.get(key);
|
|
222
|
-
let populatedItem = null;
|
|
223
|
-
if (item) {
|
|
224
|
-
populatedItem = await populate(item);
|
|
225
|
-
}
|
|
226
|
-
return [cacheMap, populatedItem];
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const retrieve = async (
|
|
230
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
231
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
|
|
232
|
-
logger.default('retrieve', { key });
|
|
233
|
-
const [cacheMap, item] = await cache.retrieve(key);
|
|
234
|
-
let populatedItem = null;
|
|
235
|
-
if (item) {
|
|
236
|
-
populatedItem = await populate(item);
|
|
237
|
-
}
|
|
238
|
-
return [cacheMap, populatedItem];
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const remove = async (
|
|
242
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
243
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
|
|
244
|
-
logger.default('remove', { key });
|
|
245
|
-
const cacheMap = await cache.remove(key);
|
|
246
|
-
return cacheMap;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const update = async (
|
|
250
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
251
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
252
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
253
|
-
logger.default('update', { key, v });
|
|
254
|
-
const [cacheMap, item] = await cache.update(key, v);
|
|
255
|
-
const populatedItem = await populate(item);
|
|
256
|
-
return [cacheMap, populatedItem];
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Facets are a pass-thru for aggregators
|
|
260
|
-
const facet = async (
|
|
261
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
262
|
-
facet: string,
|
|
263
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
264
|
-
logger.default('facet', { key, facet });
|
|
265
|
-
const [cacheMap, response] = await cache.facet(key, facet);
|
|
266
|
-
return [cacheMap, response];
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const find = async (
|
|
270
|
-
finder: string,
|
|
271
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
272
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
273
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
274
|
-
logger.default('find', { finder, finderParams, locations });
|
|
275
|
-
const [cacheMap, items] = await cache.find(finder, finderParams, locations);
|
|
276
|
-
const populatedItems = await Promise.all(items.map(async (item) => populate(item)));
|
|
277
|
-
return [cacheMap, populatedItems];
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const findOne = async (
|
|
281
|
-
finder: string,
|
|
282
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
283
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
284
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
285
|
-
logger.default('find', { finder, finderParams, locations });
|
|
286
|
-
const [cacheMap, item] = await cache.findOne(finder, finderParams, locations);
|
|
287
|
-
const populatedItem = await populate(item);
|
|
288
|
-
return [cacheMap, populatedItem];
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const set = async (
|
|
292
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
293
|
-
v: Item<S, L1, L2, L3, L4, L5>
|
|
294
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
295
|
-
logger.default('set', { key, v });
|
|
296
|
-
|
|
297
|
-
// TODO: There should be some input validation here to ensure a valid item.
|
|
298
|
-
const [cacheMap, item] = await cache.set(key, v);
|
|
299
|
-
const populatedItem = await populate(item);
|
|
300
|
-
return [cacheMap, populatedItem];
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
|
|
304
|
-
const cacheMap = await cache.reset();
|
|
305
|
-
return cacheMap;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
all,
|
|
310
|
-
one,
|
|
311
|
-
action,
|
|
312
|
-
allAction,
|
|
313
|
-
allFacet,
|
|
314
|
-
create,
|
|
315
|
-
get,
|
|
316
|
-
retrieve,
|
|
317
|
-
remove,
|
|
318
|
-
update,
|
|
319
|
-
facet,
|
|
320
|
-
find,
|
|
321
|
-
findOne,
|
|
322
|
-
reset,
|
|
323
|
-
set,
|
|
324
|
-
pkTypes: cache.pkTypes,
|
|
325
|
-
cacheMap: cache.cacheMap,
|
|
326
|
-
populate,
|
|
327
|
-
populateAggregate,
|
|
328
|
-
populateEvent
|
|
329
|
-
}
|
|
330
|
-
}
|
package/src/Cache.ts
DELETED
|
@@ -1,428 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AllItemTypeArrays,
|
|
3
|
-
ComKey,
|
|
4
|
-
isItemKeyEqual,
|
|
5
|
-
isValidItemKey,
|
|
6
|
-
Item,
|
|
7
|
-
ItemQuery,
|
|
8
|
-
LocKeyArray,
|
|
9
|
-
PriKey,
|
|
10
|
-
validatePK
|
|
11
|
-
} from "@fjell/core";
|
|
12
|
-
import { CacheMap } from "./CacheMap";
|
|
13
|
-
import LibLogger from "./logger";
|
|
14
|
-
|
|
15
|
-
import { ClientApi } from "@fjell/client-api";
|
|
16
|
-
import { NotFoundError } from "@fjell/http-api";
|
|
17
|
-
|
|
18
|
-
const logger = LibLogger.get('Cache');
|
|
19
|
-
|
|
20
|
-
export interface Cache<
|
|
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
|
-
> {
|
|
29
|
-
|
|
30
|
-
all: (
|
|
31
|
-
query?: ItemQuery,
|
|
32
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
33
|
-
) =>
|
|
34
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>,
|
|
35
|
-
|
|
36
|
-
one: (
|
|
37
|
-
query?: ItemQuery,
|
|
38
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
39
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>
|
|
40
|
-
|
|
41
|
-
action: (
|
|
42
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
43
|
-
action: string,
|
|
44
|
-
body?: any,
|
|
45
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>
|
|
46
|
-
|
|
47
|
-
allAction: (
|
|
48
|
-
action: string,
|
|
49
|
-
body?: any,
|
|
50
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
51
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>
|
|
52
|
-
|
|
53
|
-
allFacet: (
|
|
54
|
-
facet: string,
|
|
55
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
56
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
57
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
|
|
58
|
-
|
|
59
|
-
create: (
|
|
60
|
-
item: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
61
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
62
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
63
|
-
|
|
64
|
-
get: (
|
|
65
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
66
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]>;
|
|
67
|
-
|
|
68
|
-
retrieve: (
|
|
69
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
70
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]>;
|
|
71
|
-
|
|
72
|
-
remove: (
|
|
73
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
74
|
-
) => Promise<CacheMap<V, S, L1, L2, L3, L4, L5>>;
|
|
75
|
-
|
|
76
|
-
update: (
|
|
77
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
78
|
-
item: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
79
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
80
|
-
|
|
81
|
-
facet: (
|
|
82
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
83
|
-
facet: string,
|
|
84
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
85
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]>;
|
|
86
|
-
|
|
87
|
-
find: (
|
|
88
|
-
finder: string,
|
|
89
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
90
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
91
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]>;
|
|
92
|
-
|
|
93
|
-
findOne: (
|
|
94
|
-
finder: string,
|
|
95
|
-
params?: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>>,
|
|
96
|
-
locations?: LocKeyArray<L1, L2, L3, L4, L5> | []
|
|
97
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
98
|
-
|
|
99
|
-
set: (
|
|
100
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
101
|
-
item: Item<S, L1, L2, L3, L4, L5>
|
|
102
|
-
) => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]>;
|
|
103
|
-
|
|
104
|
-
reset: () => Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]>;
|
|
105
|
-
|
|
106
|
-
pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
107
|
-
|
|
108
|
-
cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5>;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export const createCache = async <
|
|
112
|
-
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
113
|
-
S extends string,
|
|
114
|
-
L1 extends string = never,
|
|
115
|
-
L2 extends string = never,
|
|
116
|
-
L3 extends string = never,
|
|
117
|
-
L4 extends string = never,
|
|
118
|
-
L5 extends string = never
|
|
119
|
-
>(
|
|
120
|
-
api: ClientApi<V, S, L1, L2, L3, L4, L5>,
|
|
121
|
-
pkType: S,
|
|
122
|
-
parentCache?: Cache<Item<L1, L2, L3, L4, L5>, L1, L2, L3, L4, L5>
|
|
123
|
-
): Promise<Cache<V, S, L1, L2, L3, L4, L5>> => {
|
|
124
|
-
|
|
125
|
-
let pkTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5> = [pkType];
|
|
126
|
-
if (parentCache) {
|
|
127
|
-
pkTypes = pkTypes.concat(parentCache.pkTypes as any) as unknown as AllItemTypeArrays<S, L1, L2, L3, L4, L5>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
let cacheMap: CacheMap<V, S, L1, L2, L3, L4, L5> =
|
|
131
|
-
new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes as AllItemTypeArrays<S, L1, L2, L3, L4, L5>);
|
|
132
|
-
|
|
133
|
-
const all = async (
|
|
134
|
-
query: ItemQuery = {},
|
|
135
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
136
|
-
):
|
|
137
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
138
|
-
logger.default('all', { query, locations });
|
|
139
|
-
let ret: V[] = [];
|
|
140
|
-
try {
|
|
141
|
-
ret = await api.all(query, locations);
|
|
142
|
-
ret.forEach((v) => {
|
|
143
|
-
cacheMap.set(v.key, v);
|
|
144
|
-
});
|
|
145
|
-
} catch (e: unknown) {
|
|
146
|
-
if (e instanceof NotFoundError) {
|
|
147
|
-
} else {
|
|
148
|
-
throw e;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const one = async (
|
|
156
|
-
query: ItemQuery = {},
|
|
157
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
158
|
-
):
|
|
159
|
-
Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
160
|
-
logger.default('one', { query, locations });
|
|
161
|
-
|
|
162
|
-
let retItem: V | null = null;
|
|
163
|
-
try {
|
|
164
|
-
retItem = await api.one(query, locations);
|
|
165
|
-
if (retItem) {
|
|
166
|
-
cacheMap.set(retItem.key, retItem);
|
|
167
|
-
}
|
|
168
|
-
} catch (e: unknown) {
|
|
169
|
-
if (e instanceof NotFoundError) {
|
|
170
|
-
} else {
|
|
171
|
-
throw e;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
return [
|
|
176
|
-
cacheMap,
|
|
177
|
-
retItem ?
|
|
178
|
-
validatePK(retItem, pkType) as V :
|
|
179
|
-
null
|
|
180
|
-
];
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const action = async (
|
|
184
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
185
|
-
action: string,
|
|
186
|
-
body: any = {},
|
|
187
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
188
|
-
logger.default('action', { key, action, body });
|
|
189
|
-
|
|
190
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
191
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
192
|
-
if (!isValidItemKey(key)) {
|
|
193
|
-
logger.error('Key for Action is not a valid ItemKey: %j', key);
|
|
194
|
-
throw new Error('Key for Action is not a valid ItemKey');
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const updated = await api.action(key, action, body);
|
|
198
|
-
cacheMap.set(updated.key, updated);
|
|
199
|
-
return [cacheMap, validatePK(updated, pkType) as V];
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const allAction = async (
|
|
203
|
-
action: string,
|
|
204
|
-
body: any = {},
|
|
205
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
206
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
207
|
-
logger.default('allAction', { action, body, locations });
|
|
208
|
-
let ret: V[] = [];
|
|
209
|
-
try {
|
|
210
|
-
ret = await api.allAction(action, body, locations);
|
|
211
|
-
ret.forEach((v) => {
|
|
212
|
-
cacheMap.set(v.key, v);
|
|
213
|
-
});
|
|
214
|
-
} catch (e: unknown) {
|
|
215
|
-
// istanbul ignore next
|
|
216
|
-
if (e instanceof NotFoundError) {
|
|
217
|
-
} else {
|
|
218
|
-
throw e;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const allFacet = async (
|
|
226
|
-
facet: string,
|
|
227
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
228
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
229
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
230
|
-
logger.default('allFacet', { facet, params, locations });
|
|
231
|
-
const ret = await api.allFacet(facet, params, locations);
|
|
232
|
-
return [cacheMap, ret];
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const create = async (
|
|
236
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
237
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
238
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
239
|
-
logger.default('create', { v, locations });
|
|
240
|
-
const created = await api.create(v, locations);
|
|
241
|
-
cacheMap.set(created.key, created);
|
|
242
|
-
return [cacheMap, validatePK(created, pkType) as V];
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const get = async (
|
|
246
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
247
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V | null]> => {
|
|
248
|
-
logger.default('get', { key });
|
|
249
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
250
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
251
|
-
if (!isValidItemKey(key)) {
|
|
252
|
-
logger.error('Key for Get is not a valid ItemKey: %j', key);
|
|
253
|
-
throw new Error('Key for Get is not a valid ItemKey');
|
|
254
|
-
}
|
|
255
|
-
let ret: V | null;
|
|
256
|
-
try {
|
|
257
|
-
ret = await api.get(key);
|
|
258
|
-
if (ret) {
|
|
259
|
-
cacheMap.set(ret.key, ret);
|
|
260
|
-
}
|
|
261
|
-
} catch (e: any) {
|
|
262
|
-
logger.error("Error getting item for key", { key, message: e.message, stack: e.stack });
|
|
263
|
-
throw e;
|
|
264
|
-
}
|
|
265
|
-
return [
|
|
266
|
-
cacheMap,
|
|
267
|
-
ret ?
|
|
268
|
-
validatePK(ret, pkType) as V :
|
|
269
|
-
null
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const retrieve = async (
|
|
274
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
275
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null]> => {
|
|
276
|
-
logger.default('retrieve', { key });
|
|
277
|
-
if (!isValidItemKey(key)) {
|
|
278
|
-
logger.error('Key for Retrieve is not a valid ItemKey: %j', key);
|
|
279
|
-
throw new Error('Key for Retrieve is not a valid ItemKey');
|
|
280
|
-
}
|
|
281
|
-
const containsItemKey = cacheMap.includesKey(key);
|
|
282
|
-
|
|
283
|
-
let retrieved: V | null;
|
|
284
|
-
if (containsItemKey) {
|
|
285
|
-
logger.default('Looking for Object in Cache', key);
|
|
286
|
-
retrieved = cacheMap.get(key);
|
|
287
|
-
} else {
|
|
288
|
-
logger.default('Object Not Found in Cache, Retrieving from Server API', { key });
|
|
289
|
-
[, retrieved] = await get(key);
|
|
290
|
-
}
|
|
291
|
-
const retValue: [CacheMap<V, S, L1, L2, L3, L4, L5> | null, V | null] = [
|
|
292
|
-
containsItemKey ? null : cacheMap,
|
|
293
|
-
retrieved ?
|
|
294
|
-
validatePK(retrieved, pkType) as V :
|
|
295
|
-
null
|
|
296
|
-
];
|
|
297
|
-
// logger.debug('Returning from retrieve', { retValue });
|
|
298
|
-
return retValue;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
const remove = async (
|
|
302
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
303
|
-
): Promise<CacheMap<V, S, L1, L2, L3, L4, L5>> => {
|
|
304
|
-
logger.default('remove', { key });
|
|
305
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
306
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
307
|
-
if (!isValidItemKey(key)) {
|
|
308
|
-
logger.error('Key for Remove is not a valid ItemKey: %j', key);
|
|
309
|
-
throw new Error('Key for Remove is not a valid ItemKey');
|
|
310
|
-
}
|
|
311
|
-
try {
|
|
312
|
-
await api.remove(key);
|
|
313
|
-
cacheMap.delete(key);
|
|
314
|
-
} catch (e) {
|
|
315
|
-
logger.error("Error deleting item", { error: e });
|
|
316
|
-
throw e;
|
|
317
|
-
}
|
|
318
|
-
return cacheMap;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
const update = async (
|
|
322
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
323
|
-
v: Partial<Item<S, L1, L2, L3, L4, L5>>,
|
|
324
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
325
|
-
logger.default('update', { key, v });
|
|
326
|
-
|
|
327
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
328
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
329
|
-
if (!isValidItemKey(key)) {
|
|
330
|
-
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
331
|
-
throw new Error('Key for Update is not a valid ItemKey');
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
try {
|
|
335
|
-
const updated = await api.update(key, v);
|
|
336
|
-
cacheMap.set(updated.key, updated);
|
|
337
|
-
return [cacheMap, validatePK(updated, pkType) as V];
|
|
338
|
-
} catch (e) {
|
|
339
|
-
logger.error("Error updating chat", { error: e });
|
|
340
|
-
throw e;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// Facets are a pass-thru for caches
|
|
345
|
-
const facet = async (
|
|
346
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
347
|
-
facet: string,
|
|
348
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
349
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, any]> => {
|
|
350
|
-
logger.default('facet', { key, facet });
|
|
351
|
-
const ret = await api.facet(key, facet, params);
|
|
352
|
-
return [cacheMap, ret];
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
const find = async (
|
|
356
|
-
finder: string,
|
|
357
|
-
params: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
358
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
359
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V[]]> => {
|
|
360
|
-
logger.default('find', { finder, params, locations });
|
|
361
|
-
const ret: V[] = await api.find(finder, params, locations);
|
|
362
|
-
ret.forEach((v) => {
|
|
363
|
-
cacheMap.set(v.key, v);
|
|
364
|
-
});
|
|
365
|
-
return [cacheMap, validatePK(ret, pkType) as V[]];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const findOne = async (
|
|
369
|
-
finder: string,
|
|
370
|
-
finderParams: Record<string, string | number | boolean | Date | Array<string | number | boolean | Date>> = {},
|
|
371
|
-
locations: LocKeyArray<L1, L2, L3, L4, L5> | [] = []
|
|
372
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
373
|
-
logger.default('findOne', { finder, finderParams, locations });
|
|
374
|
-
const ret = await api.findOne(finder, finderParams, locations);
|
|
375
|
-
cacheMap.set(ret.key, ret);
|
|
376
|
-
return [cacheMap, validatePK(ret, pkType) as V];
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const reset = async (): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>]> => {
|
|
380
|
-
cacheMap = new CacheMap<V, S, L1, L2, L3, L4, L5>(pkTypes);
|
|
381
|
-
return [cacheMap];
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
const set = async (
|
|
385
|
-
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>,
|
|
386
|
-
v: Item<S, L1, L2, L3, L4, L5>
|
|
387
|
-
): Promise<[CacheMap<V, S, L1, L2, L3, L4, L5>, V]> => {
|
|
388
|
-
logger.default('set', { key, v });
|
|
389
|
-
|
|
390
|
-
// TODO: This is validating the key, but it doesn't have knowledge of the pkType
|
|
391
|
-
// This should be looking at the parentCaches and calculating an array of pkTypes
|
|
392
|
-
if (!isValidItemKey(key)) {
|
|
393
|
-
logger.error('Key for Update is not a valid ItemKey: %j', key);
|
|
394
|
-
throw new Error('Key for Update is not a valid ItemKey');
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
// TODO: This could be merged with the isValidItemKey check, later.
|
|
398
|
-
validatePK(v, pkType);
|
|
399
|
-
|
|
400
|
-
if (!isItemKeyEqual(key, v.key)) {
|
|
401
|
-
logger.error('Key does not match item key: %j != %j', key, v.key);
|
|
402
|
-
throw new Error('Key does not match item key');
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
cacheMap.set(key, v as V);
|
|
406
|
-
return [cacheMap, validatePK(v, pkType) as V];
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
return {
|
|
410
|
-
all,
|
|
411
|
-
one,
|
|
412
|
-
action,
|
|
413
|
-
allAction,
|
|
414
|
-
allFacet,
|
|
415
|
-
create,
|
|
416
|
-
get,
|
|
417
|
-
retrieve,
|
|
418
|
-
remove,
|
|
419
|
-
update,
|
|
420
|
-
facet,
|
|
421
|
-
find,
|
|
422
|
-
findOne,
|
|
423
|
-
reset,
|
|
424
|
-
set,
|
|
425
|
-
pkTypes,
|
|
426
|
-
cacheMap
|
|
427
|
-
}
|
|
428
|
-
}
|
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
|
-
};
|
package/src/CacheRegistry.ts
DELETED
|
@@ -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