cds-caching 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/srv/util.js +20 -4
package/README.md
CHANGED
|
@@ -796,4 +796,4 @@ Contributions are welcome! Please read our contributing guidelines and submit pu
|
|
|
796
796
|
|
|
797
797
|
### License
|
|
798
798
|
|
|
799
|
-
This project is licensed under the
|
|
799
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
package/package.json
CHANGED
package/srv/util.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
const cds = require("@sap/cds")
|
|
2
2
|
|
|
3
|
+
const extractCacheProperties = (entity, prefix) => {
|
|
4
|
+
const result = {};
|
|
5
|
+
for (const key of Object.keys(entity)) {
|
|
6
|
+
if (key.startsWith(`@cache.${prefix}.`)) {
|
|
7
|
+
const subKey = key.substring(`@cache.${prefix}.`.length);
|
|
8
|
+
result[subKey] = entity[key];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
// If there are no subproperties but the main property exists, use it directly
|
|
12
|
+
if (Object.keys(result).length === 0 && entity[`@cache.${prefix}`]) {
|
|
13
|
+
return entity[`@cache.${prefix}`];
|
|
14
|
+
}
|
|
15
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
16
|
+
};
|
|
17
|
+
|
|
3
18
|
const bindFunction = async (service, action, isBound = false) => {
|
|
4
19
|
const cache = await cds.connect.to(action['@cache.service'] || "caching");
|
|
5
20
|
cache.addCachableFunction(action.name.split('.').pop(), action, isBound);
|
|
@@ -9,8 +24,8 @@ const bindFunction = async (service, action, isBound = false) => {
|
|
|
9
24
|
const cache = await cds.connect.to(action['@cache.service'] || "caching");
|
|
10
25
|
const data = await cache.run(req, next, {
|
|
11
26
|
ttl: action['@cache.ttl'],
|
|
12
|
-
tags: action
|
|
13
|
-
key: action
|
|
27
|
+
tags: extractCacheProperties(action, 'tags'),
|
|
28
|
+
key: extractCacheProperties(action, 'key')
|
|
14
29
|
});
|
|
15
30
|
return data;
|
|
16
31
|
})
|
|
@@ -21,10 +36,11 @@ const bindEntity = async (service, entity) => {
|
|
|
21
36
|
service.prepend(function () {
|
|
22
37
|
service.on('READ', entity.name, async (req, next) => {
|
|
23
38
|
const cache = await cds.connect.to(entity['@cache.service'] || "caching");
|
|
39
|
+
|
|
24
40
|
const data = await cache.run(req, next, {
|
|
25
41
|
ttl: entity['@cache.ttl'],
|
|
26
|
-
tags: entity
|
|
27
|
-
key: entity
|
|
42
|
+
tags: extractCacheProperties(entity, 'tags'),
|
|
43
|
+
key: extractCacheProperties(entity, 'key')
|
|
28
44
|
});
|
|
29
45
|
return data;
|
|
30
46
|
})
|