Skip to content

Commit 6dfb0ca

Browse files
hakimionkaputnik
andauthored
Introduce "disableCreateTracking", "disableUpdateTracking" and "disableDeleteTracking" global configs. (#160)
Closes #154 Added tests and updated `README`. --------- Co-authored-by: Nick Josipovic <[email protected]> Co-authored-by: Nick Josipovic <[email protected]>
1 parent 7b08f78 commit 6dfb0ca

File tree

3 files changed

+113
-7
lines changed

3 files changed

+113
-7
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ For some scenarios, e.g. when doing `UNION` and the `@changelog` annotion is sti
246246
> [!IMPORTANT]
247247
> This will also supress the addition of the UI facet, since the change-view is not available as target entity anymore.
248248
249+
### Select types of changes to track
250+
251+
If you do not want to track some types of changes, you can disable them using `disableCreateTracking`, `disableUpdateTracking`
252+
and `disableDeleteTracking` configs in your project settings:
253+
```json
254+
{
255+
"cds": {
256+
"requires": {
257+
"change-tracking": {
258+
"disableCreateTracking": true,
259+
"disableUpdateTracking": false,
260+
"disableDeleteTracking": true
261+
}
262+
}
263+
}
264+
}
265+
```
266+
249267
### Preserve change logs of deleted data
250268

251269
By default, deleting a record will also automatically delete all associated change logs. This helps reduce the impact on the size of the database.

lib/change-log.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,16 @@ function isEmpty(value) {
489489
}
490490

491491
async function track_changes (req) {
492+
const config = cds.env.requires["change-tracking"];
493+
494+
if (
495+
(req.event === 'UPDATE' && config?.disableUpdateTracking) ||
496+
(req.event === 'CREATE' && config?.disableCreateTracking) ||
497+
(req.event === 'DELETE' && config?.disableDeleteTracking)
498+
) {
499+
return;
500+
}
501+
492502
let diff = await req.diff()
493503
if (!diff) return
494504

tests/integration/service-api.test.js

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("change log integration test", () => {
5454
isUsed: false,
5555
netAmount: 0
5656
};
57-
57+
5858
await INSERT.into(adminService.entities.Order).entries(ordersData);
5959
let changes = await adminService.run(SELECT.from(ChangeView));
6060

@@ -122,11 +122,11 @@ describe("change log integration test", () => {
122122
valueChangedTo: ""
123123
},
124124
]);
125-
125+
126126
delete cds.services.AdminService.entities.Order.elements.netAmount["@changelog"];
127127
delete cds.services.AdminService.entities.Order.elements.isUsed["@changelog"];
128128
});
129-
129+
130130
it("1.9 For DateTime and Timestamp, support for input via Date objects.", async () => {
131131
cds.env.requires["change-tracking"].preserveDeletes = true;
132132
cds.services.AdminService.entities.RootEntity.elements.dateTime["@changelog"] = true;
@@ -217,15 +217,15 @@ describe("change log integration test", () => {
217217
it("3.6 Composition operation of inline entity operation by QL API", async () => {
218218
await UPDATE(adminService.entities["Order.Items"])
219219
.where({
220-
up__ID: "3b23bb4b-4ac7-4a24-ac02-aa10cabd842c",
220+
up__ID: "3b23bb4b-4ac7-4a24-ac02-aa10cabd842c",
221221
ID: "2b23bb4b-4ac7-4a24-ac02-aa10cabd842c"
222222
})
223223
.with({
224224
quantity: 12
225225
});
226226

227227
const changes = await adminService.run(SELECT.from(ChangeView));
228-
228+
229229
expect(changes.length).to.equal(1);
230230
const change = changes[0];
231231
expect(change.attribute).to.equal("quantity");
@@ -254,7 +254,7 @@ describe("change log integration test", () => {
254254
expect(createBookStoresChange.objectID).to.equal("new name");
255255

256256
await UPDATE(adminService.entities.BookStores)
257-
.where({
257+
.where({
258258
ID: "9d703c23-54a8-4eff-81c1-cdce6b6587c4"
259259
})
260260
.with({
@@ -272,7 +272,7 @@ describe("change log integration test", () => {
272272
expect(updateBookStoresChange.objectID).to.equal("BookStores name changed");
273273

274274
cds.services.AdminService.entities.BookStores["@changelog"].pop();
275-
275+
276276
const level3EntityData = [
277277
{
278278
ID: "12ed5dd8-d45b-11ed-afa1-0242ac654321",
@@ -530,6 +530,84 @@ describe("change log integration test", () => {
530530
expect(changes[0].valueChangedTo).to.equal("");
531531
});
532532

533+
it(`11.1 "disableUpdateTracking" setting`, async () => {
534+
cds.env.requires["change-tracking"].disableUpdateTracking = true;
535+
await UPDATE(adminService.entities.BookStores)
536+
.where({ID: "64625905-c234-4d0d-9bc1-283ee8946770"})
537+
.with({name: 'New name'});
538+
539+
let changes = await SELECT.from(ChangeView).where({
540+
entity: "sap.capire.bookshop.BookStores",
541+
attribute: "name",
542+
modification: "update"
543+
});
544+
expect(changes.length).to.equal(0);
545+
546+
cds.env.requires["change-tracking"].disableUpdateTracking = false;
547+
await UPDATE(adminService.entities.BookStores)
548+
.where({ID: "64625905-c234-4d0d-9bc1-283ee8946770"})
549+
.with({name: 'Another name'});
550+
551+
changes = await SELECT.from(ChangeView).where({
552+
entity: "sap.capire.bookshop.BookStores",
553+
attribute: "name",
554+
modification: "update"
555+
});
556+
expect(changes.length).to.equal(1);
557+
});
558+
559+
it(`11.2 "disableCreateTracking" setting`, async () => {
560+
cds.env.requires["change-tracking"].disableCreateTracking = true;
561+
await INSERT.into(adminService.entities.BookStores).entries({
562+
ID: "9d703c23-54a8-4eff-81c1-cdce6b6587c4",
563+
name: "new name",
564+
});
565+
566+
let changes = await SELECT.from(ChangeView).where({
567+
entity: "sap.capire.bookshop.BookStores",
568+
attribute: "name",
569+
modification: "create",
570+
});
571+
expect(changes.length).to.equal(0);
572+
573+
cds.env.requires["change-tracking"].disableCreateTracking = false;
574+
await INSERT.into(adminService.entities.BookStores).entries({
575+
ID: "04e93234-a5cb-4bfb-89b3-f242ddfaa4ad",
576+
name: "another name",
577+
});
578+
579+
changes = await SELECT.from(ChangeView).where({
580+
entity: "sap.capire.bookshop.BookStores",
581+
attribute: "name",
582+
modification: "create",
583+
});
584+
expect(changes.length).to.equal(1);
585+
});
586+
587+
it(`11.3 "disableDeleteTracking" setting`, async () => {
588+
cds.env.requires["change-tracking"].disableDeleteTracking = true;
589+
await DELETE.from(adminService.entities.Level3Entity)
590+
.where({ID: "12ed5dd8-d45b-11ed-afa1-0242ac654321"});
591+
592+
let changes = await SELECT.from(ChangeView).where({
593+
entity: "sap.capire.bookshop.Level3Entity",
594+
attribute: "title",
595+
modification: "delete",
596+
});
597+
expect(changes.length).to.equal(0);
598+
599+
cds.env.requires["change-tracking"].disableDeleteTracking = false;
600+
await DELETE.from(adminService.entities.Level2Entity)
601+
.where({ID: "dd1fdd7d-da2a-4600-940b-0baf2946c4ff"});
602+
603+
changes = await SELECT.from(ChangeView).where({
604+
entity: "sap.capire.bookshop.Level2Entity",
605+
attribute: "title",
606+
modification: "delete",
607+
});
608+
expect(changes.length).to.equal(1);
609+
});
610+
533611
it("Do not change track personal data", async () => {
534612
const allCustomers = await SELECT.from(adminService.entities.Customers);
535613
await UPDATE(adminService.entities.Customers).where({ ID: allCustomers[0].ID }).with({

0 commit comments

Comments
 (0)