Skip to content

Commit fdf7274

Browse files
committed
mini refactoring
1 parent 30dd5e6 commit fdf7274

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

cds-plugin.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
1-
const cds = require ('@sap/cds/lib')
1+
const cds = require('@sap/cds/lib')
2+
3+
const isChangeTracked = (entity) => (
4+
entity['@changelog']
5+
|| entity['@changelog.keys']
6+
// || entity.elements && Object.values(entity.elements).some(e => e['@changelog'])
7+
)
8+
29

310
// Unfold @changelog annotations in loaded model
411
cds.on('loaded', m => {
512

613
// Get definitions from Dummy entity in our models
714
const { 'sap.changelog.aspect': aspect } = m.definitions; if (!aspect) return // some other model
8-
const { '@UI.Facets': [facet], elements: {changes} } = aspect
15+
const { '@UI.Facets': [facet], elements: { changes } } = aspect
916
changes.on.pop() // remove ID -> filled in below
1017

1118
for (let name in m.definitions) {
1219
const entity = m.definitions[name]
13-
if (entity['@changelog'] || entity['@changelog.keys']) {
20+
if (isChangeTracked(entity)) {
1421

1522
// Determine entity keys
16-
const keys = [], {elements:elms} = entity
23+
const keys = [], { elements: elms } = entity
1724
for (let e in elms) if (elms[e].key) keys.push(e)
1825

1926
// Add association to ChangeView...
20-
const on = [...changes.on]; keys.forEach((k,i) => { i && on.push('||'); on.push({ref:[k]}) })
27+
const on = [...changes.on]; keys.forEach((k, i) => { i && on.push('||'); on.push({ ref: [k] }) })
2128
const assoc = { ...changes, on }
2229
const query = entity.projection || entity.query?.SELECT
2330
if (query) {
24-
(query.columns ??= ['*']).push({ as: 'changes', cast: assoc})
31+
(query.columns ??= ['*']).push({ as: 'changes', cast: assoc })
2532
} else {
2633
entity.elements.changes = assoc
2734
}
2835

2936
// Add defaults for @changelog.keys
30-
entity['@changelog.keys'] ??= keys.map(k => ({'=':k}))
37+
entity['@changelog.keys'] ??= keys.map(k => ({ '=': k }))
3138

3239
// Add UI.Facet for Change History List
3340
entity['@UI.Facets']?.push(facet)
@@ -36,4 +43,22 @@ cds.on('loaded', m => {
3643
})
3744

3845
// Add generic change tracking handlers
39-
cds.on('served', ()=> require("./lib/change-log")())
46+
cds.on('served', () => {
47+
const { _logChanges, _afterReadChangeView } = require("./lib/change-log")
48+
for (const srv of cds.services) {
49+
if (srv instanceof cds.ApplicationService) {
50+
let any = false
51+
for (const entity of Object.values(srv.entities)) {
52+
if (isChangeTracked(entity) || Object.values(entity.elements).some(e => e['@changelog'])) {
53+
cds.db.before("CREATE", entity, _logChanges)
54+
cds.db.before("UPDATE", entity, _logChanges)
55+
cds.db.before("DELETE", entity, _logChanges)
56+
any = true
57+
}
58+
}
59+
if (any && srv.entities.ChangeView) {
60+
srv.after("READ", srv.entities.ChangeView, _afterReadChangeView)
61+
}
62+
}
63+
}
64+
})

lib/change-log.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -563,23 +563,4 @@ const _logChanges = async function (req) {
563563
}
564564
}
565565

566-
module.exports = () => {
567-
for (const srv of cds.services) {
568-
if (srv instanceof cds.ApplicationService) {
569-
let hasElementEnabled = false
570-
for (const entity of Object.values(srv.entities)) {
571-
if (
572-
Object.values(entity.elements).some((ele) => ele[CHANGE_LOG_ANNOTATION])
573-
) {
574-
cds.db.before("CREATE", entity, _logChanges)
575-
cds.db.before("UPDATE", entity, _logChanges)
576-
cds.db.before("DELETE", entity, _logChanges)
577-
hasElementEnabled = true
578-
}
579-
}
580-
if (hasElementEnabled && srv.entities.ChangeView) {
581-
srv.after("READ", srv.entities.ChangeView, _afterReadChangeView)
582-
}
583-
}
584-
}
585-
}
566+
module.exports = { _logChanges, _afterReadChangeView }

0 commit comments

Comments
 (0)