Skip to content

Commit 2380bee

Browse files
authored
Update cds-plugin.js (#147)
1 parent c79c0be commit 2380bee

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

cds-plugin.js

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const hasParent = 'change-tracking-parentEntity'
55

66
const isChangeTracked = (entity) => (
77
(entity['@changelog']
8-
|| entity.elements && Object.values(entity.elements).some(e => e['@changelog'])) && entity.query?.SET?.op !== 'union'
8+
|| entity.elements && Object.values(entity.elements).some(e => e['@changelog'])) && entity.query?.SET?.op !== 'union'
99
)
1010

1111
// Add the appropriate Side Effects attribute to the custom action
@@ -30,113 +30,113 @@ const addSideEffects = (actions, flag, element) => {
3030
}
3131
}
3232

33-
function setChangeTrackingIsRootEntity(entity, csn, val = true) {
33+
function setChangeTrackingIsRootEntity (entity, csn, val = true) {
3434
if (csn.definitions?.[entity.name]) {
35-
csn.definitions[entity.name][isRoot] = val;
35+
csn.definitions[entity.name][isRoot] = val
3636
}
3737
}
3838

39-
function checkAndSetRootEntity(parentEntity, entity, csn) {
39+
function checkAndSetRootEntity (parentEntity, entity, csn) {
4040
if (entity[isRoot] === false) {
41-
return entity;
41+
return entity
4242
}
4343
if (parentEntity) {
44-
return compositionRoot(parentEntity, csn);
44+
return compositionRoot(parentEntity, csn)
4545
} else {
46-
setChangeTrackingIsRootEntity(entity, csn);
47-
return { ...csn.definitions?.[entity.name], name: entity.name };
46+
setChangeTrackingIsRootEntity(entity, csn)
47+
return { ...csn.definitions?.[entity.name], name: entity.name }
4848
}
4949
}
5050

51-
function processEntities(m) {
51+
function processEntities (m) {
5252
for (let name in m.definitions) {
53-
compositionRoot({...m.definitions[name], name}, m)
53+
compositionRoot({ ...m.definitions[name], name }, m)
5454
}
5555
}
5656

57-
function compositionRoot(entity, csn) {
57+
function compositionRoot (entity, csn) {
5858
if (!entity || entity.kind !== 'entity') {
59-
return;
59+
return
6060
}
61-
const parentEntity = compositionParent(entity, csn);
62-
return checkAndSetRootEntity(parentEntity, entity, csn);
61+
const parentEntity = compositionParent(entity, csn)
62+
return checkAndSetRootEntity(parentEntity, entity, csn)
6363
}
6464

65-
function compositionParent(entity, csn) {
65+
function compositionParent (entity, csn) {
6666
if (!entity || entity.kind !== 'entity') {
67-
return;
67+
return
6868
}
69-
const parentAssociation = compositionParentAssociation(entity, csn);
70-
return parentAssociation ?? null;
69+
const parentAssociation = compositionParentAssociation(entity, csn)
70+
return parentAssociation ?? null
7171
}
7272

73-
function compositionParentAssociation(entity, csn) {
73+
function compositionParentAssociation (entity, csn) {
7474
if (!entity || entity.kind !== 'entity') {
75-
return;
75+
return
7676
}
77-
const elements = entity.elements ?? {};
77+
const elements = entity.elements ?? {}
7878

7979
// Add the change-tracking-isRootEntity attribute of the child entity
80-
processCompositionElements(entity, csn, elements);
80+
processCompositionElements(entity, csn, elements)
8181

82-
const hasChildFlag = entity[isRoot] !== false;
83-
const hasParentEntity = entity[hasParent];
82+
const hasChildFlag = entity[isRoot] !== false
83+
const hasParentEntity = entity[hasParent]
8484

8585
if (hasChildFlag || !hasParentEntity) {
8686
// Find parent association of the entity
87-
const parentAssociation = findParentAssociation(entity, csn, elements);
87+
const parentAssociation = findParentAssociation(entity, csn, elements)
8888
if (parentAssociation) {
89-
const parentAssociationTarget = elements[parentAssociation]?.target;
90-
if (hasChildFlag) setChangeTrackingIsRootEntity(entity, csn, false);
89+
const parentAssociationTarget = elements[parentAssociation]?.target
90+
if (hasChildFlag) setChangeTrackingIsRootEntity(entity, csn, false)
9191
return {
9292
...csn.definitions?.[parentAssociationTarget],
9393
name: parentAssociationTarget
94-
};
95-
} else return;
94+
}
95+
} else return
9696
}
97-
return { ...csn.definitions?.[entity.name], name: entity.name };
97+
return { ...csn.definitions?.[entity.name], name: entity.name }
9898
}
9999

100-
function processCompositionElements(entity, csn, elements) {
100+
function processCompositionElements (entity, csn, elements) {
101101
for (const name in elements) {
102-
const element = elements[name];
103-
const target = element?.target;
104-
const definition = csn.definitions?.[target];
102+
const element = elements[name]
103+
const target = element?.target
104+
const definition = csn.definitions?.[target]
105105
if (
106106
element.type !== 'cds.Composition' ||
107107
target === entity.name ||
108108
!definition ||
109109
definition[isRoot] === false
110110
) {
111-
continue;
111+
continue
112112
}
113-
setChangeTrackingIsRootEntity({ ...definition, name: target }, csn, false);
113+
setChangeTrackingIsRootEntity({ ...definition, name: target }, csn, false)
114114
}
115115
}
116116

117-
function findParentAssociation(entity, csn, elements) {
117+
function findParentAssociation (entity, csn, elements) {
118118
return Object.keys(elements).find((name) => {
119-
const element = elements[name];
120-
const target = element?.target;
119+
const element = elements[name]
120+
const target = element?.target
121121
if (element.type === 'cds.Association' && target !== entity.name) {
122-
const parentDefinition = csn.definitions?.[target] ?? {};
123-
const parentElements = parentDefinition?.elements ?? {};
122+
const parentDefinition = csn.definitions?.[target] ?? {}
123+
const parentElements = parentDefinition?.elements ?? {}
124124
return !!Object.keys(parentElements).find((parentEntityName) => {
125-
const parentElement = parentElements?.[parentEntityName] ?? {};
125+
const parentElement = parentElements?.[parentEntityName] ?? {}
126126
if (parentElement.type === 'cds.Composition') {
127-
const isCompositionEntity = parentElement.target === entity.name;
127+
const isCompositionEntity = parentElement.target === entity.name
128128
// add parent information in the current entity
129129
if (isCompositionEntity) {
130130
csn.definitions[entity.name][hasParent] = {
131131
associationName: name,
132132
entityName: target
133-
};
133+
}
134134
}
135-
return isCompositionEntity;
135+
return isCompositionEntity
136136
}
137-
});
137+
})
138138
}
139-
});
139+
})
140140
}
141141

142142
// Unfold @changelog annotations in loaded model
@@ -146,7 +146,7 @@ cds.on('loaded', m => {
146146
const { 'sap.changelog.aspect': aspect } = m.definitions; if (!aspect) return // some other model
147147
const { '@UI.Facets': [facet], elements: { changes } } = aspect
148148
changes.on.pop() // remove ID -> filled in below
149-
149+
150150
// Process entities to define the relation
151151
processEntities(m)
152152

@@ -162,54 +162,53 @@ cds.on('loaded', m => {
162162
keys.push({
163163
e,
164164
val: elms[e]
165-
});
165+
})
166166
} else {
167-
keys.push(e);
167+
keys.push(e)
168168
}
169169
}
170170
}
171171

172172
// If no key attribute is defined for the entity, the logic to add association to ChangeView should be skipped.
173-
if(keys.length === 0) {
174-
continue;
173+
if (keys.length === 0) {
174+
continue
175175
}
176176

177177
// Add association to ChangeView...
178-
const on = [...changes.on];
178+
const on = [...changes.on]
179179
keys.forEach((k, i) => {
180-
i && on.push("||");
180+
i && on.push("||")
181181
on.push({
182182
ref: k?.val?.type === "cds.Association" ? [k.e, k.val.keys?.[0]?.ref?.[0]] : [k]
183-
});
184-
});
183+
})
184+
})
185185
const assoc = { ...changes, on }
186186
const query = entity.projection || entity.query?.SELECT
187-
if(!entity['@changelog.disable_assoc'])
188-
{
189-
if (query) {
190-
(query.columns ??= ['*']).push({ as: 'changes', cast: assoc })
191-
} else {
192-
entity.elements.changes = assoc
193-
}
187+
if (!entity['@changelog.disable_assoc']) {
188+
if (query) {
189+
(query.columns ??= ['*']).push({ as: 'changes', cast: assoc })
190+
} else {
191+
entity.elements.changes = assoc
192+
}
194193

195-
// Add UI.Facet for Change History List
196-
if(!entity['@changelog.disable_facet'])
197-
entity['@UI.Facets']?.push(facet)
194+
// Add UI.Facet for Change History List
195+
if (!entity['@changelog.disable_facet'])
196+
entity['@UI.Facets']?.push(facet)
198197
}
199198

200199
if (entity.actions) {
201-
const hasParentInfo = entity[hasParent];
202-
const entityName = hasParentInfo?.entityName;
203-
const parentEntity = entityName ? m.definitions[entityName] : null;
200+
const hasParentInfo = entity[hasParent]
201+
const entityName = hasParentInfo?.entityName
202+
const parentEntity = entityName ? m.definitions[entityName] : null
204203

205-
const isParentRootAndHasFacets = parentEntity?.[isRoot] && parentEntity?.['@UI.Facets'];
204+
const isParentRootAndHasFacets = parentEntity?.[isRoot] && parentEntity?.['@UI.Facets']
206205

207206
if (entity[isRoot] && entity['@UI.Facets']) {
208207
// Add side effects for root entity
209-
addSideEffects(entity.actions, true);
208+
addSideEffects(entity.actions, true)
210209
} else if (isParentRootAndHasFacets) {
211210
// Add side effects for child entity
212-
addSideEffects(entity.actions, false, hasParentInfo?.associationName);
211+
addSideEffects(entity.actions, false, hasParentInfo?.associationName)
213212
}
214213
}
215214
}

0 commit comments

Comments
 (0)