cds-caching 0.3.3 → 1.1.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/srv/util.js DELETED
@@ -1,101 +0,0 @@
1
- const cds = require("@sap/cds")
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
-
18
- const bindFunction = async (service, action, isBound = false) => {
19
- const cache = await cds.connect.to(action['@cache.service'] || "caching");
20
- cache.addCachableFunction(action.name.split('.').pop(), action, isBound);
21
-
22
- service.prepend(function () {
23
- service.on(action.name.split('.').pop(), async (req, next) => {
24
- const cache = await cds.connect.to(action['@cache.service'] || "caching");
25
- const data = await cache.run(req, next, {
26
- ttl: action['@cache.ttl'],
27
- tags: action['@cache.tags'],
28
- key: extractCacheProperties(action, 'key')
29
- });
30
- return data;
31
- })
32
- })
33
- }
34
-
35
- const bindEntity = async (service, entity) => {
36
- service.prepend(function () {
37
- service.on('READ', entity.name, async (req, next) => {
38
- const cache = await cds.connect.to(entity['@cache.service'] || "caching");
39
-
40
- console.log(entity);
41
-
42
- const data = await cache.run(req, next, {
43
- ttl: entity['@cache.ttl'],
44
- tags: entity['@cache.tags'],
45
- key: extractCacheProperties(entity, 'key')
46
- });
47
- return data;
48
- })
49
- })
50
- }
51
-
52
- const scanCachingAnnotations = async (srvs) => {
53
- LOG = cds.log('cds-caching')
54
-
55
- // Grep all app services
56
- const services = [];
57
- for (const [name, config] of Object.entries(srvs)) {
58
- const service = await cds.connect.to(name);
59
-
60
- if (service.definition?.kind === 'service') {
61
- services.push(service);
62
- }
63
- }
64
-
65
- // Grep all external services
66
- for (const [name, config] of Object.entries(cds.env.requires)) {
67
- if (config.kind === 'odata-v2' || config.kind === 'odata' || config.kind === 'rest') {
68
- const service = await cds.connect.to(name);
69
- services.push(service);
70
- }
71
- }
72
-
73
- for (const service of services) {
74
-
75
- // functions
76
- for (const [name, action] of Object.entries(service.actions)) {
77
- if (Object.keys(action).some(key => key.startsWith('@cache')) && action.kind === 'function') {
78
- LOG._debug && LOG.debug(`Caching enabled for function ${action.name}`);
79
- await bindFunction(service, action);
80
- }
81
- }
82
-
83
- // entities
84
- for (const entity of service.entities) {
85
- if (Object.keys(entity).some(key => key.startsWith('@cache'))) {
86
- await bindEntity(service, entity);
87
- LOG._debug && LOG.debug(`Caching enabled for entity ${entity.name}`);
88
- }
89
-
90
- // bound functions
91
- for (const [name, action] of Object.entries(entity.actions || {})) {
92
- if (Object.keys(action).some(key => key.startsWith('@cache')) && action.kind === 'function') {
93
- bindFunction(service, action, true);
94
- LOG._debug && LOG.debug(`Caching enabled for bound function ${action.name}`);
95
- }
96
- }
97
- }
98
- }
99
- }
100
-
101
- module.exports = { scanCachingAnnotations }