@cap-js/change-tracking 1.0.0 → 1.0.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/CHANGELOG.md +8 -2
- package/README.md +53 -23
- package/lib/localization.js +9 -16
- package/package.json +1 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,8 +4,14 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
6
6
|
|
|
7
|
-
## Version 1.0.
|
|
7
|
+
## Version 1.0.1 - 26.10.23
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Labels are looked up from the service entity (not the db entity only).
|
|
12
|
+
|
|
13
|
+
## Version 1.0.0 - 18.10.23
|
|
8
14
|
|
|
9
15
|
### Added
|
|
10
16
|
|
|
11
|
-
- Initial release
|
|
17
|
+
- Initial release
|
package/README.md
CHANGED
|
@@ -1,23 +1,57 @@
|
|
|
1
|
+
# Change Tracking Plugin for SAP Cloud Application Programming Model (CAP)
|
|
2
|
+
|
|
3
|
+
[](https://api.reuse.software/info/github.com/cap-js/change-tracking)
|
|
4
|
+
|
|
5
|
+
The `@cap-js/change-tracking` package is a [CDS plugin](https://cap.cloud.sap/docs/node.js/cds-plugins#cds-plugin-packages) providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities as simple as that:
|
|
6
|
+
|
|
7
|
+
1. [Install the plugin: `npm add @cap-js/change-tracking`](#setup)
|
|
8
|
+
2. [Add `@changelog` annotations to your CDS models](#annotations)
|
|
9
|
+
3. [Et voilà:](#change-history-view)
|
|
10
|
+
|
|
11
|
+
<img width="1300" alt="change-history-custom" src="_assets/changes-custom.png">
|
|
12
|
+
|
|
1
13
|
|
|
2
|
-
The `@cap-js/change-tracking` package is a [CDS plugin](https://cap.cloud.sap/docs/node.js/cds-plugins#cds-plugin-packages) providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.
|
|
3
14
|
|
|
4
15
|
### Table of Contents
|
|
5
16
|
|
|
17
|
+
- [Preliminaries](#preliminaries)
|
|
6
18
|
- [Setup](#setup)
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- [Change History View](#change-history-view)
|
|
14
|
-
- [Customizations](#customizations)
|
|
19
|
+
- [Annotations](#annotations)
|
|
20
|
+
- [Human-readable Types and Fields](#human-readable-types-and-fields)
|
|
21
|
+
- [Human-readable IDs](#human-readable-ids)
|
|
22
|
+
- [Human-readable Values](#human-readable-values)
|
|
23
|
+
- [Test-drive locally](#test-drive-locally)
|
|
24
|
+
- [Change History View](#change-history-view)
|
|
15
25
|
- [Contributing](#contributing)
|
|
16
|
-
|
|
26
|
+
- [Code of Conduct](#code-of-conduct)
|
|
17
27
|
- [Licensing](#licensing)
|
|
18
28
|
|
|
19
29
|
|
|
20
30
|
|
|
31
|
+
## Preliminaries
|
|
32
|
+
|
|
33
|
+
In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base to add change tracking to. Clone the repository and apply the step-by-step instructions:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
git clone https://github.com/cap-js/incidents-app
|
|
37
|
+
cd incidents-app
|
|
38
|
+
npm i
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Alternatively**, you can clone the incidents app including the prepared enhancements for change-tracking:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
git clone https://github.com/cap-js/calesi --recursive
|
|
45
|
+
cd calesi
|
|
46
|
+
npm i
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
cds w samples/change-tracking
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
21
55
|
## Setup
|
|
22
56
|
|
|
23
57
|
To enable change tracking, simply add this self-configuring plugin package to your project:
|
|
@@ -28,11 +62,7 @@ npm add @cap-js/change-tracking
|
|
|
28
62
|
|
|
29
63
|
|
|
30
64
|
|
|
31
|
-
##
|
|
32
|
-
|
|
33
|
-
In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base to add change tracking to.
|
|
34
|
-
|
|
35
|
-
### Add `@changelog` Annotations
|
|
65
|
+
## Annotations
|
|
36
66
|
|
|
37
67
|
All we need to do is to identify what should be change-tracked by annotating respective entities and elements in our model with the `@changelog` annotation. Following the [best practice of separation of concerns](https://cap.cloud.sap/docs/guides/domain-modeling#separation-of-concerns), we do so in a separate file _srv/change-tracking.cds_:
|
|
38
68
|
|
|
@@ -55,9 +85,9 @@ The minimal annotation we require for change tracking is `@changelog` on element
|
|
|
55
85
|
Additional identifiers or labels can be added to obtain more *human-readable* change records as described below.
|
|
56
86
|
|
|
57
87
|
|
|
58
|
-
|
|
88
|
+
### Human-readable Types and Fields
|
|
59
89
|
|
|
60
|
-
By default the implementation looks up *Object Type* names or *Field* namesfrom respective `@title` or `@Common.Label` annotations, and applies i18n lookups. If no such annotations are given, the technical names of the respective CDS definitions are displayed.
|
|
90
|
+
By default the implementation looks up *Object Type* names or *Field* namesfrom respective `@title` or `@Common.Label` annotations, and applies i18n lookups. If no such annotations are given, the technical names of the respective CDS definitions are displayed.
|
|
61
91
|
|
|
62
92
|
For example, without the `@title` annotation, changes to conversation entries would show up with the technical entity name:
|
|
63
93
|
|
|
@@ -74,7 +104,7 @@ We get a human-readable display for *Object Type*:
|
|
|
74
104
|
<img width="1300" alt="change-history-type-hr" src="_assets/changes-type-hr-wbox.png">
|
|
75
105
|
|
|
76
106
|
|
|
77
|
-
|
|
107
|
+
### Human-readable IDs
|
|
78
108
|
|
|
79
109
|
The changelog annotations for *Object ID* are defined at entity level.
|
|
80
110
|
|
|
@@ -97,7 +127,7 @@ annotate ProcessorService.Conversations with @changelog: [author, timestamp] {
|
|
|
97
127
|
Expanding the changelog annotation by additional identifiers `[author, timestamp]`, we can now better identify the `message` change events by their respective author and timestamp.
|
|
98
128
|
|
|
99
129
|
|
|
100
|
-
|
|
130
|
+
### Human-readable Values
|
|
101
131
|
|
|
102
132
|
The changelog annotations for *New Value* and *Old Value* are defined at element level.
|
|
103
133
|
|
|
@@ -120,7 +150,7 @@ Hence, here it is essential to add a unique identifier to obtain human-readable
|
|
|
120
150
|
<img width="1300" alt="change-history-value-hr" src="_assets/changes-value-hr-wbox.png">
|
|
121
151
|
|
|
122
152
|
|
|
123
|
-
|
|
153
|
+
## Test-drive locally
|
|
124
154
|
|
|
125
155
|
With the steps above, we have successfully set up change tracking for our reference application. Let's see that in action.
|
|
126
156
|
|
|
@@ -130,7 +160,7 @@ With the steps above, we have successfully set up change tracking for our refere
|
|
|
130
160
|
```
|
|
131
161
|
2. **Make a change** on your change-tracked elements. This change will automatically be persisted in the database table (`sap.changelog.ChangeLog`) and made available in a pre-defined view, namely the [Change History view](#change-history-view) for your convenience.
|
|
132
162
|
|
|
133
|
-
|
|
163
|
+
## Change History View
|
|
134
164
|
|
|
135
165
|
<img width="1300" alt="change-history" src="_assets/changes.png">
|
|
136
166
|
|
|
@@ -154,7 +184,7 @@ annotate sap.changelog.ChangeView with @(
|
|
|
154
184
|
);
|
|
155
185
|
```
|
|
156
186
|
|
|
157
|
-
In the UI, the *Change History* table now contains
|
|
187
|
+
In the UI, the *Change History* table now contains 5 equally-spaced columns with the desired properties:
|
|
158
188
|
|
|
159
189
|
<img width="1300" alt="change-history-custom" src="_assets/changes-custom.png">
|
|
160
190
|
|
|
@@ -166,7 +196,7 @@ For more information and examples on adding Fiori Annotations, see [Adding SAP F
|
|
|
166
196
|
This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/change-tracking/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
|
|
167
197
|
|
|
168
198
|
|
|
169
|
-
|
|
199
|
+
## Code of Conduct
|
|
170
200
|
|
|
171
201
|
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all times.
|
|
172
202
|
|
package/lib/localization.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
const cds = require("@sap/cds/lib");
|
|
2
2
|
const LOG = cds.log("change-log");
|
|
3
3
|
const { getNameFromPathVal, getDBEntity } = require("./entity-helper");
|
|
4
|
-
const OBJECT_TYPE_I18N_LABEL_KEY = "@Common.Label"
|
|
5
|
-
const OBJECT_TYPE_I18N_TITLE_KEY = "@title"
|
|
6
4
|
|
|
7
5
|
const MODIF_I18N_MAP = {
|
|
8
6
|
create: "{i18n>ChangeLog.modification.create}",
|
|
@@ -43,7 +41,7 @@ const _localizeDefaultObjectID = function (change, locale) {
|
|
|
43
41
|
const parentEntityName = getNameFromPathVal(parentNodePathVal);
|
|
44
42
|
const dbEntity = getDBEntity(parentEntityName);
|
|
45
43
|
try {
|
|
46
|
-
const labelI18nKey = dbEntity[
|
|
44
|
+
const labelI18nKey = dbEntity['@Common.Label'] || dbEntity['@title'];
|
|
47
45
|
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
|
|
48
46
|
change.parentObjectID = labelI18nValue ? labelI18nValue : dbEntity.name;
|
|
49
47
|
} catch (e) {
|
|
@@ -56,7 +54,7 @@ const _localizeDefaultObjectID = function (change, locale) {
|
|
|
56
54
|
const _localizeEntityType = function (change, locale) {
|
|
57
55
|
if (change.entity) {
|
|
58
56
|
try {
|
|
59
|
-
const labelI18nKey = _getLabelI18nKeyOnEntity(change.
|
|
57
|
+
const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
|
|
60
58
|
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
|
|
61
59
|
|
|
62
60
|
change.entity = labelI18nValue ? labelI18nValue : change.entity;
|
|
@@ -83,12 +81,11 @@ const _localizeAttribute = function (change, locale) {
|
|
|
83
81
|
try {
|
|
84
82
|
const serviceEntity = cds.model.definitions[change.serviceEntity];
|
|
85
83
|
let labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity, change.attribute);
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
|
|
84
|
+
if (!labelI18nKey) {
|
|
85
|
+
const element = serviceEntity.elements[change.attribute];
|
|
86
|
+
if (element.isAssociation) labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
|
|
89
87
|
}
|
|
90
88
|
const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
|
|
91
|
-
|
|
92
89
|
change.attribute = labelI18nValue ? labelI18nValue : change.attribute;
|
|
93
90
|
} catch (e) {
|
|
94
91
|
LOG.error("Failed to localize change attribute", e);
|
|
@@ -98,14 +95,10 @@ const _localizeAttribute = function (change, locale) {
|
|
|
98
95
|
};
|
|
99
96
|
|
|
100
97
|
const _getLabelI18nKeyOnEntity = function (entityName, /** optinal */ attribute) {
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
return element[OBJECT_TYPE_I18N_LABEL_KEY];
|
|
106
|
-
}
|
|
107
|
-
const entityLabel = entity[OBJECT_TYPE_I18N_LABEL_KEY] ? entity[OBJECT_TYPE_I18N_LABEL_KEY] : entity[OBJECT_TYPE_I18N_TITLE_KEY];
|
|
108
|
-
return entityLabel;
|
|
98
|
+
let def = cds.model.definitions[entityName];
|
|
99
|
+
if (attribute) def = def?.elements[attribute]
|
|
100
|
+
if (!def) return "";
|
|
101
|
+
return def['@Common.Label'] || def['@title'];
|
|
109
102
|
};
|
|
110
103
|
|
|
111
104
|
const localizeLogFields = function (data, locale) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js/change-tracking",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "CDS plugin providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.",
|
|
5
5
|
"repository": "cap-js/change-tracking",
|
|
6
6
|
"author": "SAP SE (https://www.sap.com)",
|
|
7
|
-
"homepage": "https://cap.cloud.sap/",
|
|
8
7
|
"license": "SEE LICENSE IN LICENSE",
|
|
9
8
|
"main": "cds-plugin.js",
|
|
10
9
|
"files": [
|