@cap-js/change-tracking 1.0.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.
@@ -0,0 +1,96 @@
1
+ // Enhanced class based on cds v5.5.5 @sap/cds/libx/_runtime/common/utils/templateProcessor
2
+
3
+ const DELIMITER = require("@sap/cds/libx/_runtime/common/utils/templateDelimiter");
4
+
5
+ const _formatRowContext = (tKey, keyNames, row) => {
6
+ const keyValuePairs = keyNames.map((key) => `${key}=${row[key]}`);
7
+ const keyValuePairsSerialized = keyValuePairs.join(",");
8
+ return `${tKey}(${keyValuePairsSerialized})`;
9
+ };
10
+
11
+ const _processElement = (processFn, row, key, elements, isRoot, pathSegments, picked = {}) => {
12
+ const element = elements[key];
13
+ const { plain } = picked;
14
+
15
+ if (plain) {
16
+ /**
17
+ * @type import('../../types/api').templateProcessorProcessFnArgs
18
+ */
19
+ const elementInfo = { row, key, element, plain, isRoot, pathSegments };
20
+ processFn(elementInfo);
21
+ }
22
+ };
23
+
24
+ const _processRow = (processFn, row, template, tKey, tValue, isRoot, pathOptions) => {
25
+ const { template: subTemplate, picked } = tValue;
26
+ const key = tKey.split(DELIMITER).pop();
27
+ const { segments: pathSegments } = pathOptions;
28
+
29
+ if (!subTemplate && pathSegments) {
30
+ pathSegments.push(key);
31
+ }
32
+
33
+ _processElement(processFn, row, key, template.target.elements, isRoot, pathSegments, picked);
34
+
35
+ // process deep
36
+ if (subTemplate) {
37
+ let subRows = row && row[key];
38
+
39
+ subRows = Array.isArray(subRows) ? subRows : [subRows];
40
+
41
+ // Build entity path
42
+ subRows.forEach((subRow) => {
43
+ if (subRow && row && row._path) {
44
+ /** Enhancement by SME: Support CAP Change Histroy
45
+ * Construct path from root entity to current entity.
46
+ */
47
+ const serviceNodeName = template.target.elements[key].target;
48
+ subRow._path = `${row._path}/${serviceNodeName}(${subRow.ID})`;
49
+ }
50
+ });
51
+
52
+ _processComplex(processFn, subRows, subTemplate, key, pathOptions);
53
+ }
54
+ };
55
+
56
+ const _processComplex = (processFn, rows, template, tKey, pathOptions) => {
57
+ if (rows.length === 0) {
58
+ return;
59
+ }
60
+
61
+ const segments = pathOptions.segments;
62
+ let keyNames;
63
+
64
+ for (const row of rows) {
65
+ if (row == null) {
66
+ continue;
67
+ }
68
+
69
+ const args = { processFn, row, template, isRoot: false, pathOptions };
70
+
71
+ if (pathOptions.includeKeyValues) {
72
+ keyNames = keyNames || (template.target.keys && Object.keys(template.target.keys)) || [];
73
+ pathOptions.rowKeysGenerator(keyNames, row, template);
74
+ const pathSegment = _formatRowContext(tKey, keyNames, { ...row, ...pathOptions.extraKeys });
75
+ args.pathOptions.segments = segments ? [...segments, pathSegment] : [pathSegment];
76
+ }
77
+
78
+ templateProcessor(args);
79
+ }
80
+ };
81
+
82
+ /**
83
+ * @param {import("../../types/api").TemplateProcessor} args
84
+ */
85
+ const templateProcessor = ({ processFn, row, template, isRoot = true, pathOptions = {} }) => {
86
+ const segments = pathOptions.segments && [...pathOptions.segments];
87
+
88
+ for (const [tKey, tValue] of template.elements) {
89
+ if (segments) {
90
+ pathOptions.segments = [...segments];
91
+ }
92
+ _processRow(processFn, row, template, tKey, tValue, isRoot, pathOptions);
93
+ }
94
+ };
95
+
96
+ module.exports = templateProcessor;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@cap-js/change-tracking",
3
+ "version": "1.0.0",
4
+ "description": "CDS plugin providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.",
5
+ "repository": "cap-js/change-tracking",
6
+ "author": "SAP SE (https://www.sap.com)",
7
+ "homepage": "https://cap.cloud.sap/",
8
+ "license": "SEE LICENSE IN LICENSE",
9
+ "main": "cds-plugin.js",
10
+ "files": [
11
+ "lib",
12
+ "_i18n",
13
+ "index.cds",
14
+ "CHANGELOG.md",
15
+ "README.md"
16
+ ],
17
+ "scripts": {
18
+ "lint": "npx eslint .",
19
+ "test": "npx jest --silent"
20
+ },
21
+ "peerDependencies": {
22
+ "@sap/cds": "^7"
23
+ },
24
+ "devDependencies": {
25
+ "@cap-js/change-tracking": "file:.",
26
+ "@cap-js/sqlite": "^1",
27
+ "axios": "^1",
28
+ "chai": "^4.3.10",
29
+ "chai-as-promised": "^7.1.1",
30
+ "chai-subset": "^1.6.0",
31
+ "eslint": "^8",
32
+ "express": "^4",
33
+ "jest": "^29"
34
+ },
35
+ "cds": {
36
+ "requires": {
37
+ "change-tracking": {
38
+ "model": "@cap-js/change-tracking"
39
+ }
40
+ }
41
+ }
42
+ }