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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. 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 Apache License 2.0 - see the LICENSE file for details.
799
+ This project is licensed under the MIT License - see the LICENSE file for details.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cds-caching",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "A caching plugin for SAP CAP applications supporting Redis",
5
5
  "repository": {
6
6
  "type": "git",
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['@cache.tags'],
13
- key: action['@cache.key']
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['@cache.tags'],
27
- key: entity['@cache.key']
42
+ tags: extractCacheProperties(entity, 'tags'),
43
+ key: extractCacheProperties(entity, 'key')
28
44
  });
29
45
  return data;
30
46
  })