@sap/cds-compiler 6.4.6 → 6.5.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +34 -1156
  2. package/README.md +1 -10
  3. package/doc/IncompatibleChanges_v5.md +436 -0
  4. package/doc/IncompatibleChanges_v6.md +659 -0
  5. package/doc/Versioning.md +3 -7
  6. package/lib/api/main.js +1 -0
  7. package/lib/api/options.js +5 -0
  8. package/lib/api/validate.js +3 -0
  9. package/lib/base/message-registry.js +23 -0
  10. package/lib/base/messages.js +1 -1
  11. package/lib/base/model.js +3 -2
  12. package/lib/checks/actionsFunctions.js +6 -3
  13. package/lib/checks/existsInForbiddenPlaces.js +32 -0
  14. package/lib/checks/validator.js +2 -0
  15. package/lib/compiler/assert-consistency.js +3 -5
  16. package/lib/compiler/checks.js +4 -8
  17. package/lib/compiler/define.js +244 -459
  18. package/lib/compiler/extend.js +297 -11
  19. package/lib/compiler/finalize-parse-cdl.js +2 -10
  20. package/lib/compiler/generate.js +29 -1
  21. package/lib/compiler/populate.js +21 -63
  22. package/lib/compiler/propagator.js +1 -2
  23. package/lib/compiler/resolve.js +2 -12
  24. package/lib/compiler/shared.js +18 -5
  25. package/lib/compiler/tweak-assocs.js +13 -9
  26. package/lib/compiler/utils.js +97 -0
  27. package/lib/compiler/xpr-rewrite.js +2 -1
  28. package/lib/edm/annotations/edmJson.js +9 -6
  29. package/lib/edm/annotations/genericTranslation.js +8 -4
  30. package/lib/edm/csn2edm.js +3 -4
  31. package/lib/edm/edmInboundChecks.js +1 -2
  32. package/lib/edm/edmPreprocessor.js +3 -3
  33. package/lib/gen/CdlGrammar.checksum +1 -1
  34. package/lib/gen/CdlParser.js +1 -1
  35. package/lib/gen/Dictionary.json +16 -1
  36. package/lib/json/from-csn.js +4 -6
  37. package/lib/json/to-csn.js +3 -3
  38. package/lib/model/csnRefs.js +13 -4
  39. package/lib/model/enrichCsn.js +4 -2
  40. package/lib/optionProcessor.js +8 -4
  41. package/lib/render/utils/sql.js +3 -2
  42. package/lib/transform/db/applyTransformations.js +1 -1
  43. package/lib/transform/db/assertUnique.js +3 -3
  44. package/lib/transform/db/assocsToQueries/normalizeFrom.js +33 -0
  45. package/lib/transform/db/assocsToQueries/transformExists.js +14 -3
  46. package/lib/transform/db/assocsToQueries/utils.js +1 -1
  47. package/lib/transform/db/backlinks.js +4 -4
  48. package/lib/transform/db/cdsPersistence.js +4 -4
  49. package/lib/transform/db/constraints.js +4 -4
  50. package/lib/transform/db/expansion.js +5 -5
  51. package/lib/transform/db/flattening.js +4 -5
  52. package/lib/transform/db/rewriteCalculatedElements.js +3 -3
  53. package/lib/transform/db/temporal.js +11 -11
  54. package/lib/transform/draft/db.js +2 -0
  55. package/lib/transform/draft/odata.js +5 -7
  56. package/lib/transform/effective/flattening.js +1 -2
  57. package/lib/transform/forOdata.js +3 -3
  58. package/lib/transform/forRelationalDB.js +1 -1
  59. package/lib/transform/odata/createForeignKeys.js +1 -2
  60. package/lib/transform/odata/flattening.js +1 -2
  61. package/lib/transform/odata/toFinalBaseType.js +52 -55
  62. package/lib/transform/transformUtils.js +3 -4
  63. package/package.json +1 -1
  64. package/doc/CHANGELOG_BETA.md +0 -464
  65. package/doc/CHANGELOG_DEPRECATED.md +0 -235
@@ -122,7 +122,7 @@ function processAssertUnique( csn, options, messageFunctions ) {
122
122
  * Check strictly that annotation value is an array
123
123
  * and that the individual array entries are references
124
124
  *
125
- * @param {any} val Annotation value
125
+ * @param {object} val Annotation value
126
126
  * @param {string} propName
127
127
  * @returns {Array} Array of paths
128
128
  */
@@ -149,8 +149,8 @@ function processAssertUnique( csn, options, messageFunctions ) {
149
149
  /**
150
150
  * Convert a ref object to a path string
151
151
  *
152
- * @param {any} v
153
- * @returns {string|string[]|any}
152
+ * @param {CSN.Ref | object} v
153
+ * @returns {string|string[]|object}
154
154
  */
155
155
  function unref( v ) {
156
156
  if (Array.isArray(v))
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Normalize a queries `from` clause by moving the infix filters on the last step of a `SELECT.from.ref` path.
5
+ *
6
+ * For the given query/projection, if the terminal ref element is an object
7
+ * containing a `where` (syntactic sugar), its condition is moved into `SELECT.where`.
8
+ * - If `SELECT.where` already exists, both are combined as: { xpr: [ oldWhere ] } AND { xpr: [ leafWhere ] }.
9
+ * - Otherwise the leaf `where` becomes the `SELECT.where`.
10
+ * The ref leaf object is then replaced by its plain `id`, removing the inline filter.
11
+ *
12
+ * Mutates the provided query in place.
13
+ *
14
+ * @param {CSN.Query} query - The query to normalize.
15
+ */
16
+ function normalizeFromLeaf( query ) {
17
+ const where = query.SELECT?.from.ref?.at(-1).where;
18
+ if (!where || where.length === 0)
19
+ return;
20
+ if (query.SELECT.where)
21
+ query.SELECT.where = [ { xpr: [ ...query.SELECT.where ] }, 'AND', { xpr: [ ...where ] } ];
22
+ else
23
+ query.SELECT.where = where;
24
+
25
+ // preserve `args`
26
+ if (query.SELECT?.from.ref?.at(-1).args)
27
+ delete query.SELECT.from.ref[query.SELECT.from.ref.length - 1].where;
28
+ else
29
+ query.SELECT.from.ref[query.SELECT.from.ref.length - 1] = query.SELECT.from.ref.at(-1).id;
30
+ }
31
+
32
+
33
+ module.exports = normalizeFromLeaf;
@@ -1,9 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const { forAllQueries, forEachDefinition, walkCsnPath } = require('../../../model/csnUtils');
3
+ const {
4
+ forAllQueries, forEachDefinition, walkCsnPath, transformExpression,
5
+ } = require('../../../model/csnUtils');
4
6
  const { setProp } = require('../../../base/model');
5
7
  const { getHelpers } = require('./utils');
6
8
 
9
+ const normalizeFromLeaf = require('./normalizeFrom');
10
+
7
11
  /**
8
12
  * Turn a `exists assoc[filter = 100]` into a `exists (select 1 as dummy from assoc.target where <assoc on condition> and assoc.target.filter = 100)`.
9
13
  *
@@ -62,6 +66,9 @@ function handleExists( csn, options, messageFunctions, csnUtils ) {
62
66
  forEachDefinition(csn, (artifact, artifactName) => {
63
67
  if (artifact.query || artifact.projection) {
64
68
  forAllQueries(artifact.query || { SELECT: artifact.projection }, function handleExistsQuery(query, path) {
69
+ // Normalize infix filters on the last step of a `SELECT.from.ref` path.
70
+ // By doing this before the exists transformation, we enable `SELECT * FROM Author:books[exists genre]`
71
+ normalizeFromLeaf(query);
65
72
  if (!generatedExists.has(query)) {
66
73
  const toProcess = []; // Collect all expressions we need to process here
67
74
  if (query.SELECT?.where?.length > 1)
@@ -77,8 +84,12 @@ function handleExists( csn, options, messageFunctions, csnUtils ) {
77
84
  toProcess.push([ path.slice(0, -1), path.concat([ 'from', 'on' ]) ]);
78
85
 
79
86
  for (const [ , exprPath ] of toProcess) {
80
- const expr = nestExists(exprPath);
81
- walkCsnPath(csn, exprPath.slice(0, -1))[exprPath[exprPath.length - 1]] = expr;
87
+ const token = walkCsnPath(csn, exprPath);
88
+ transformExpression(token, null, {
89
+ ref: (container, prop, ref, pathToRef) => {
90
+ nestExists(pathToRef.slice(0, -1));
91
+ },
92
+ }, exprPath);
82
93
  }
83
94
 
84
95
  while (toProcess.length > 0) {
@@ -255,7 +255,7 @@ function getHelpers( csn, inspectRef, error ) {
255
255
  /**
256
256
  * Run Object.assign on all of the passed in parameters and delete a .as and .key at the end
257
257
  *
258
- * @param {...any} args
258
+ * @param {...object} args
259
259
  * @returns {object} The merged args without an .as and .key property
260
260
  */
261
261
  function assignAndDeleteAsAndKey( ...args ) {
@@ -24,7 +24,7 @@ function getBacklinkTransformer( csnUtils, messageFunctions, options, pathDelimi
24
24
  /**
25
25
  * @param {CSN.Artifact} artifact
26
26
  * @param {string} artifactName
27
- * @param {any} dummy unused Parameter
27
+ * @param {object} dummy unused Parameter
28
28
  * @param {CSN.Path} path
29
29
  */
30
30
  function transformSelfInBacklinks( artifact, artifactName, dummy, path ) {
@@ -164,7 +164,7 @@ function getBacklinkTransformer( csnUtils, messageFunctions, options, pathDelimi
164
164
  * Return the condition to replace the comparison `<assocOp> = $self` in the ON-condition
165
165
  * of element <elem> of artifact 'art'. If there is anything to complain, use location <loc>
166
166
  *
167
- * @param {any} assocOp
167
+ * @param {CSN.Ref} assocOp
168
168
  * @param {CSN.Element} assoc
169
169
  * @param {string} assocName
170
170
  * @param {CSN.Element} elem
@@ -224,7 +224,7 @@ function getBacklinkTransformer( csnUtils, messageFunctions, options, pathDelimi
224
224
  * keys in this artifact.
225
225
  * For example, `ON elem.ass = $self` becomes `ON elem.ass_key1 = key1 AND elem.ass_key2 = key2`
226
226
  * (assuming that `ass` has the foreign keys `key1` and `key2`)
227
- * @param {any} assocOp
227
+ * @param {CSN.Ref} assocOp
228
228
  * @param {CSN.Element} assoc
229
229
  * @param {string} originalAssocName
230
230
  * @param {string} elemName
@@ -276,7 +276,7 @@ function getBacklinkTransformer( csnUtils, messageFunctions, options, pathDelimi
276
276
  * For example, `ON elem.ass = $self` becomes `ON a = elem.x AND b = elem.y`
277
277
  * (assuming that `ass` has the ON-condition `ON ass.a = x AND ass.b = y`)
278
278
  *
279
- * @param {any} assocOp
279
+ * @param {CSN.Ref} assocOp
280
280
  * @param {CSN.Element} assoc
281
281
  * @param {string} originalAssocName
282
282
  * @param {string} elemName
@@ -9,7 +9,7 @@ const {
9
9
  const { recurseElements } = require('../transformUtils');
10
10
 
11
11
  /**
12
- * Return a callback function for forEachDefinition that marks artifacts that are abstract or @cds.persistence.exists/skip
12
+ * Return a callback function for forEachDefinition that marks artifacts that are abstract or `@cds.persistence.exists/skip`
13
13
  * with $ignore.
14
14
  *
15
15
  * @returns {(artifact: CSN.Artifact, artifactName: string) => void} Callback function for forEachDefinition
@@ -31,7 +31,7 @@ function getAnnoProcessor() {
31
31
 
32
32
  /**
33
33
  * Return a callback function for forEachDefinition that marks associations with $ignore
34
- * if their target does not reach the database, i.e. marked with @cds.persistence.skip or is abstract
34
+ * if their target does not reach the database, i.e. marked with `@cds.persistence.skip` or is abstract
35
35
  *
36
36
  * @param {CSN.Model} csn
37
37
  * @param {CSN.Options} options
@@ -48,7 +48,7 @@ function getAssocToSkippedIgnorer( csn, options, messageFunctions, csnUtils ) {
48
48
 
49
49
  return ignoreAssociationToSkippedTarget;
50
50
  /**
51
- * Associations that target a @cds.persistence.skip artifact must be removed
51
+ * Associations that target a `@cds.persistence.skip` artifact must be removed
52
52
  * from the persistence model
53
53
  *
54
54
  * @param {CSN.Artifact} artifact
@@ -94,7 +94,7 @@ function getAssocToSkippedIgnorer( csn, options, messageFunctions, csnUtils ) {
94
94
  }
95
95
 
96
96
  /**
97
- * Return a callback function for forEachDefinition that handles artifacts marked with @cds.persistence.table.
97
+ * Return a callback function for forEachDefinition that handles artifacts marked with `@cds.persistence.table`.
98
98
  * If a .query artifact has this annotation, the .query will be deleted and it will be treated like a table.
99
99
  *
100
100
  * @param {CSN.Model} csn
@@ -239,7 +239,7 @@ function createReferentialConstraints( csn, options ) {
239
239
  * The following decision table reflects the current implementation:
240
240
  *
241
241
  * +-----------------+--------------------+-------------------+----------+
242
- * | Global Switch: | Global Check Type: | @assert.integrity | Generate |
242
+ * | Global Switch: | Global Check Type: | `@assert.integrity` | Generate |
243
243
  * |"assertIntegrity"| "assertIntegrityType"| | Constraint|
244
244
  * +-----------------+--------------------+-------------------+----------+
245
245
  * | on | RT | false | no |
@@ -345,7 +345,7 @@ function createReferentialConstraints( csn, options ) {
345
345
 
346
346
  /**
347
347
  * if global checks are 'individual' we evaluate every association,
348
- * we create db constraints if it is annotated with @assert.integrity: 'DB' (or true)
348
+ * we create db constraints if it is annotated with `@assert.integrity`: 'DB' (or true)
349
349
  *
350
350
  * @returns {boolean}
351
351
  */
@@ -388,7 +388,7 @@ function createReferentialConstraints( csn, options ) {
388
388
  /**
389
389
  * if global checks are on and global integrity check type is 'DB'
390
390
  * we create db constraints in any case except if annotated
391
- * with @assert.integrity: 'RT' (or false, but that is rejected earlier)
391
+ * with `@assert.integrity: 'RT'` (or false, but that is rejected earlier)
392
392
  *
393
393
  * @returns {boolean}
394
394
  */
@@ -397,7 +397,7 @@ function createReferentialConstraints( csn, options ) {
397
397
  }
398
398
 
399
399
  /**
400
- * Convenience to check if value of element's @assert.integrity annotation
400
+ * Convenience to check if value of element's `@assert.integrity` annotation
401
401
  * is the same as a given value. `@assert.integrity`-value checks do not use the "truthy"-semantics,
402
402
  * since string values _and_ booleans are allowed, but are treated differently.
403
403
  *
@@ -124,7 +124,7 @@ function expandStructureReferences( csn, options, pathDelimiter, messageFunction
124
124
  }
125
125
 
126
126
  /**
127
- * Turn .expand/.inline into normal refs. @cds.persistence.skip .expand with to-many (and all transitive views).
127
+ * Turn .expand/.inline into normal refs. `@cds.persistence.skip` .expand with to-many (and all transitive views).
128
128
  * For such skipped things, error for usage of assoc pointing to them and ignore publishing of assoc pointing to them.
129
129
  */
130
130
  function rewriteExpandInline() {
@@ -269,7 +269,7 @@ function expandStructureReferences( csn, options, pathDelimiter, messageFunction
269
269
 
270
270
  /**
271
271
  * Mark the given artifact and all (transitively) dependent artifacts as `toDummify`.
272
- * This means that they will be replaced with simple dummy views in @dummify
272
+ * This means that they will be replaced with simple dummy views in `@dummify`
273
273
  *
274
274
  * @param {CSN.Artifact} artifact
275
275
  * @param {string} name
@@ -544,7 +544,7 @@ function expandStructureReferences( csn, options, pathDelimiter, messageFunction
544
544
  }
545
545
 
546
546
  /**
547
- * Create a simple dummy view marked with @cds.persistence.skip
547
+ * Create a simple dummy view marked with `@cds.persistence.skip`
548
548
  *
549
549
  * @param {string} source
550
550
  * @returns {CSN.Artifact}
@@ -646,14 +646,14 @@ function expandStructureReferences( csn, options, pathDelimiter, messageFunction
646
646
  * leafs `a_b` and `a_c`, if `art` has elements `b` and `c`.
647
647
  * @param {string[]} colTypeRef
648
648
  * Expanded type for the column. Basically the path to the to-be-expanded `art`.
649
- * @param {(currentRef: any[], currentAlias: string[]) => object} leafCallback
649
+ * @param {(currentRef: CSN.Ref, currentAlias: string[]) => object} leafCallback
650
650
  * Callback when leaf nodes are reached. currentRef is the type reference for the expanded
651
651
  * column. currentAlias is the columns calculated alias.
652
652
  * @returns {object[]}
653
653
  */
654
654
  function _expandStructCol( art, colName, colTypeRef, leafCallback ) {
655
655
  const expanded = [];
656
- /** @type {Array<[CSN.Element, any[], string[]]>} */
656
+ /** @type {Array<[CSN.Element, CSN.Column, string[]]>} */
657
657
  const stack = [ [ art, colTypeRef, [ colName ] ] ];
658
658
  while (stack.length > 0) {
659
659
  const [ current, currentRef, currentAlias ] = stack.pop();
@@ -468,9 +468,9 @@ function handleManagedAssociationsAndCreateForeignKeys( csn, options, messageFun
468
468
  *
469
469
  * If a structure contains an assoc, this will also be resolved and vice versa
470
470
  *
471
- * @param {*} assoc
472
- * @param {*} assocName
473
- * @param {*} path
471
+ * @param {CSN.Association} assoc
472
+ * @param {string} assocName
473
+ * @param {CSN.Path} path
474
474
  */
475
475
  function flattenFKs( assoc, assocName, path ) {
476
476
  if (!assoc.keys)
@@ -697,7 +697,6 @@ function handleManagedAssociationsAndCreateForeignKeys( csn, options, messageFun
697
697
  * @returns {Array[]} First element of every sub-array is the foreign key name, second is the foreign key definition
698
698
  */
699
699
  function createForeignKeys( csnUtils, path, element, prefix, csn, options, pathDelimiter, lvl = 0, originalKey = { }) {
700
- const special$self = !csn?.definitions?.$self && '$self';
701
700
  const isInspectRefResult = !Array.isArray(path);
702
701
 
703
702
  let fks = [];
@@ -707,7 +706,7 @@ function createForeignKeys( csnUtils, path, element, prefix, csn, options, pathD
707
706
  let finalElement = element;
708
707
  let finalTypeName; // TODO: Find a way to not rely on $path?
709
708
  // TODO: effectiveType's return value is 'path' for the next inspectRef
710
- if (element.type && !isBuiltinType(element.type) && element.type !== special$self) {
709
+ if (element.type && !isBuiltinType(element.type)) {
711
710
  const tmpElt = csnUtils.effectiveType(element);
712
711
  // effective type resolves to structs and enums only but not scalars
713
712
  if (Object.keys(tmpElt).length) {
@@ -141,9 +141,9 @@ function rewriteCalculatedElementsInViews( csn, options, csnUtils, pathDelimiter
141
141
  /**
142
142
  * @param {object} parent
143
143
  * @param {string} prop
144
- * @param ref
145
- * @param p
146
- * @param root
144
+ * @param {CSN.Ref} ref
145
+ * @param {CSN.Path} p
146
+ * @param {CSN.Element} root
147
147
  */
148
148
  function transformRef(parent, prop, ref, p, root) {
149
149
  const {
@@ -11,8 +11,8 @@ const validToString = '@cds.valid.to';
11
11
  const validFromString = '@cds.valid.from';
12
12
  /**
13
13
  * Get the forEachDefinition callback function that adds a where condition to views that
14
- * - are annotated with @cds.valid.from and @cds.valid.to,
15
- * - have only one @cds.valid.from and @cds.valid.to,
14
+ * - are annotated with `@cds.valid.from` and `@cds.valid.to`,
15
+ * - have only one `@cds.valid.from` and `@cds.valid.to`,
16
16
  * - and both annotations come from the same entity
17
17
  *
18
18
  * If the view has one of the annotations but the other conditions are not met, an error will be raised.
@@ -30,8 +30,8 @@ function getViewDecorator( csn, messageFunctions, csnUtils, options ) {
30
30
  return addTemporalWhereConditionToView;
31
31
  /**
32
32
  * Add a where condition to views that
33
- * - are annotated with @cds.valid.from and @cds.valid.to,
34
- * - have only one @cds.valid.from and @cds.valid.to,
33
+ * - are annotated with `@cds.valid.from` and `@cds.valid.to`,
34
+ * - have only one `@cds.valid.from` and `@cds.valid.to`,
35
35
  * - and both annotations come from the same entity
36
36
  *
37
37
  * If the view has one of the annotations but the other conditions are not met, an error will be raised.
@@ -97,10 +97,10 @@ function getViewDecorator( csn, messageFunctions, csnUtils, options ) {
97
97
  }
98
98
 
99
99
  /**
100
- * Get all elements tagged with @cds.valid.from/to from the union of all entities of the from-clause.
100
+ * Get all elements tagged with `@cds.valid.from/to` from the union of all entities of the from-clause.
101
101
  *
102
- * @param {any} combined union of all entities of the from-clause
103
- * @returns {Array[]} Array where first field is array of elements with @cds.valid.from, second field is array of elements with @cds.valid.to.
102
+ * @param {CSN.QueryFrom} combined union of all entities of the from-clause
103
+ * @returns {Array[]} Array where first field is array of elements with `@cds.valid.from`, second field is array of elements with `@cds.valid.to`.
104
104
  */
105
105
  function getFromToElements( combined ) {
106
106
  const from = [];
@@ -121,7 +121,7 @@ function getViewDecorator( csn, messageFunctions, csnUtils, options ) {
121
121
  }
122
122
 
123
123
  /**
124
- * Check if the given SELECT has a falsy @cds.valid.from and a falsy @cds.valid.to
124
+ * Check if the given SELECT has a falsy `@cds.valid.from` and a falsy `@cds.valid.to`
125
125
  *
126
126
  * @param {CSN.QuerySelect} SELECT
127
127
  * @param {CSN.Elements} elements
@@ -150,13 +150,13 @@ function getViewDecorator( csn, messageFunctions, csnUtils, options ) {
150
150
  }
151
151
 
152
152
  /**
153
- * Get the forEachDefinition callback function that collects all usages of @cds.valid.from/to/key and checks that
153
+ * Get the forEachDefinition callback function that collects all usages of `@cds.valid.from/to/key` and checks that
154
154
  * - the assignment is on a valid element
155
155
  * - the annotation is only assigned once
156
156
  * - key is only used in conjunction with from and to
157
157
  *
158
- * Furthermore, @cds.valid.from and @cds.valid.key is processed - @cds.valid.from is marked as key or marked as unique if @cds.valid.key is used.
159
- * If @cds.valid.key is used, the real key-elements have their key-property removed (set non-enumerable as $key) and instead the @cds.valid.key-marked elements have it added.
158
+ * Furthermore, `@cds.valid.from` and `@cds.valid.key` is processed - `@cds.valid.from` is marked as key or marked as unique if `@cds.valid.key` is used.
159
+ * If `@cds.valid.key` is used, the real key-elements have their key-property removed (set non-enumerable as $key) and instead the `@cds.valid.key`-marked elements have it added.
160
160
  *
161
161
  * @param {CSN.Model} csn
162
162
  * @param {CSN.Options} options
@@ -188,6 +188,8 @@ function generateDrafts( csn, options, pathDelimiter, messageFunctions ) {
188
188
  // Copy all elements
189
189
  for (const elemName in artifact.elements) {
190
190
  const origElem = artifact.elements[elemName];
191
+ // elements with $calc are rendered as usual
192
+
191
193
  if (origElem.value?.stored) {
192
194
  calcOnWriteElements.push(elemName);
193
195
  }
@@ -27,11 +27,12 @@ const { makeMessageFunction } = require('../../base/messages');
27
27
  * @param {CSN.Options} options
28
28
  * @param {string[]|undefined} services Will be calculated JIT if not provided
29
29
  * @param {object} [messageFunctions]
30
+ * @param {function} [isExternalServiceMember]
30
31
  * @returns {CSN.Model} Returns the transformed input model
31
32
  * @todo should be done by the compiler - Check associations for valid foreign keys
32
33
  * @todo check if needed at all: Remove '$projection' from paths in the element's ON-condition
33
34
  */
34
- function generateDrafts( csn, options, services, messageFunctions ) {
35
+ function generateDrafts( csn, options, services, messageFunctions, isExternalServiceMember ) {
35
36
  // TEMP(2024-02-26): Temporary! Umbrella uses this file directly in cds/lib/compile/for/drafts.js#L1
36
37
  messageFunctions ??= makeMessageFunction(csn, options, 'odata-drafts');
37
38
 
@@ -53,14 +54,11 @@ function generateDrafts( csn, options, services, messageFunctions ) {
53
54
  services = getServiceNames(csn);
54
55
 
55
56
  const visitedArtifacts = Object.create(null);
56
- // @ts-ignore
57
- const externalServices = services.filter(serviceName => csn.definitions[serviceName]['@cds.external']);
58
- // @ts-ignore
59
- const isExternalServiceMember = (_art, name) => externalServices.includes(getServiceName(name));
57
+
60
58
  const filterDict = Object.create(null);
61
59
 
62
60
  // validate the 'DRAFT.DraftAdministrativeData_DraftMessage' type if already present in the model
63
- if (options.draftMessages) {
61
+ if (options.draftMessages && options.odataVersion === 'v4') {
64
62
  const draftAdminDataMessagesType = csn.definitions['DRAFT.DraftAdministrativeData_DraftMessage'];
65
63
  if (draftAdminDataMessagesType && !isValidDraftAdminDataMessagesType(draftAdminDataMessagesType)) {
66
64
  error(null, [ 'definitions', 'DRAFT.DraftAdministrativeData_DraftMessage' ], { name: 'DRAFT.DraftAdministrativeData_DraftMessage' },
@@ -181,7 +179,7 @@ function generateDrafts( csn, options, services, messageFunctions ) {
181
179
  // ... on SiblingEntity.IsActiveEntity != IsActiveEntity ...
182
180
  siblingEntity.SiblingEntity.on = createAssociationPathComparison('SiblingEntity', 'IsActiveEntity', '!=', 'IsActiveEntity');
183
181
 
184
- if (options.draftMessages) {
182
+ if (options.draftMessages && options.odataVersion === 'v4') {
185
183
  const draftMessages = { DraftMessages: { '@Core.Computed': true, virtual: true, items: { type: 'DRAFT.DraftAdministrativeData_DraftMessage' } } };
186
184
  addElement(draftMessages, artifact, artifactName);
187
185
 
@@ -62,13 +62,12 @@ function flattenRefs(csn, options, csnUtils, messageFunctions) {
62
62
 
63
63
  // explicit binding parameter of bound action
64
64
  if (def.actions) {
65
- const special$self = !csn?.definitions?.$self && '$self';
66
65
  Object.entries(def.actions).forEach(([ an, a ]) => {
67
66
  if (a.params) {
68
67
  const params = Object.entries(a.params);
69
68
  const firstParam = params[0][1];
70
69
  const type = firstParam?.items?.type || firstParam?.type;
71
- if (type === special$self) {
70
+ if (type === '$self') {
72
71
  const bindingParamName = params[0][0];
73
72
  const markBindingParam = {
74
73
  ref: (parent, prop, xpr) => {
@@ -117,9 +117,9 @@ function transform4odataWithCsn(inputModel, options, messageFunctions) {
117
117
  // use the array when there is a need to identify if an artifact is in a service or not
118
118
  const services = getServiceNames(csn);
119
119
  // @ts-ignore
120
- const externalServices = services.filter(serviceName => csn.definitions[serviceName]['@cds.external']);
120
+ const externalServices = services.filter(serviceName => csn.definitions[serviceName]['@cds.external'] && csn.definitions[serviceName]['@cds.external'] !== 2);
121
121
  // @ts-ignore
122
- const isExternalServiceMember = (art, name) => !!(externalServices.includes(getServiceName(name)) || (art && art['@cds.external']));
122
+ const isExternalServiceMember = (art, name) => !!(externalServices.includes(getServiceName(name)) || (art && art['@cds.external'] && art['@cds.external'] !== 2));
123
123
 
124
124
  if (options.csnFlavor === 'universal' && isBetaEnabled(options, 'enableUniversalCsn'))
125
125
  enrichUniversalCsn(csn, options);
@@ -127,7 +127,7 @@ function transform4odataWithCsn(inputModel, options, messageFunctions) {
127
127
  // - Generate artificial draft fields on a structured CSN if requested, flattening and struct
128
128
  // expansion do their magic including foreign key generation and annotation propagation.
129
129
  // Tenantenizer has to decorate the DraftAdministrativeData, so draft decoration must be done before.
130
- generateDrafts(csn, options, services, messageFunctions);
130
+ generateDrafts(csn, options, services, messageFunctions, isExternalServiceMember);
131
131
 
132
132
  if (options.tenantDiscriminator)
133
133
  addTenantFields(csn, options);
@@ -49,7 +49,7 @@ function forEachDefinition(csn, cb) {
49
49
  * such as flattening, wildcard expansion, etc.
50
50
  *
51
51
  * @param {CSN.Model} csn
52
- * @param {CSN.SqlOptions} options
52
+ * @param {SqlOptions} options
53
53
  * @param {object} messageFunctions Message functions such as `error()`, `info()`, …
54
54
  */
55
55
  function transformForRelationalDBWithCsn(csn, options, messageFunctions) {
@@ -76,7 +76,6 @@ function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iter
76
76
  }
77
77
 
78
78
  function createForeignKeysForElement(path, element, prefix, csn, options, pathDelimiter, lvl = 0, originalKey = {} ) {
79
- const special$self = !csn?.definitions?.$self && '$self';
80
79
  const isInspectRefResult = !Array.isArray(path);
81
80
 
82
81
  let fks = [];
@@ -87,7 +86,7 @@ function createForeignKeyElements(csn, options, messageFunctions, csnUtils, iter
87
86
  let finalTypeName;
88
87
 
89
88
  // resolve derived type
90
- if (element.type && !isBuiltinType(element.type) && element.type !== special$self) {
89
+ if (element.type && !isBuiltinType(element.type)) {
91
90
  const tmpElt = csnUtils.effectiveType(element);
92
91
  // effective type resolves to structs and enums only but not scalars
93
92
  if (Object.keys(tmpElt).length) {
@@ -102,13 +102,12 @@ function allInOneFlattening(csn, refFlattener, adaptRefs, inspectRef, getFinalTy
102
102
  setProp(def, '$flatAnnotations', flatAnnos);
103
103
  // explicit binding parameter of bound action
104
104
  if (def.actions) {
105
- const special$self = !csn?.definitions?.$self && '$self';
106
105
  Object.entries(def.actions).forEach(([ actionName, action ]) => {
107
106
  if (action.params) {
108
107
  const params = Object.entries(action.params);
109
108
  const firstParam = params[0][1];
110
109
  const type = firstParam?.items?.type || firstParam?.type;
111
- if (type === special$self) {
110
+ if (type === '$self') {
112
111
  const bindingParamName = params[0][0];
113
112
  const markBindingParam = {
114
113
  ref: (parent, prop, xpr) => {
@@ -13,7 +13,6 @@ const { cloneCsnDict, cloneCsnNonDict } = require('../../model/cloneCsn');
13
13
 
14
14
  function expandToFinalBaseType(csn, transformers, csnUtils, services, options, error) {
15
15
  const isV4 = options.odataVersion === 'v4';
16
- const special$self = !csn?.definitions?.$self && '$self';
17
16
  forEachDefinition(csn, (def, defName) => {
18
17
  // Unravel derived type chains to final one for elements, actions, action parameters (propagating annotations)
19
18
  forEachMemberRecursively(def, (member, _memberName) => {
@@ -112,65 +111,63 @@ function expandToFinalBaseType(csn, transformers, csnUtils, services, options, e
112
111
  else {
113
112
  if (isExpandable(finalBaseType) || node.kind === 'type') {
114
113
  // 1. Get the final type of the node (resolve derived type chain)
115
- if (finalBaseType.type !== special$self) {
116
- // The type replacement depends on whether 'node' is a definition or a member[element].
117
- if (node.kind) {
118
- /*
119
- It is a definition and we expand to builtin type and to elements
120
- type T: S; --> Integer;
121
- type S: X; --> Integer;
122
- type X: Integer;
114
+ // The type replacement depends on whether 'node' is a definition or a member[element].
115
+ if (node.kind) {
116
+ /*
117
+ It is a definition and we expand to builtin type and to elements
118
+ type T: S; --> Integer;
119
+ type S: X; --> Integer;
120
+ type X: Integer;
123
121
 
124
- type A: B; -> {...}
125
- type B: C; -> { ... }
126
- type C { .... };
122
+ type A: B; -> {...}
123
+ type B: C; -> { ... }
124
+ type C { .... };
125
+ */
126
+ if (isBuiltinType(finalBaseType.type)) {
127
+ /*
128
+ use transformUtils::toFinalBaseType for the moment,
129
+ as it is collects along the chain of types
130
+ attributes that need to be propagated
131
+ enum, length, scale, etc.
127
132
  */
128
- if (isBuiltinType(finalBaseType.type)) {
129
- /*
130
- use transformUtils::toFinalBaseType for the moment,
131
- as it is collects along the chain of types
132
- attributes that need to be propagated
133
- enum, length, scale, etc.
134
- */
135
- transformers.toFinalBaseType(node);
136
- }
137
- else if (csnUtils.isStructured(finalBaseType)) {
138
- cloneElements(node, finalBaseType);
139
- }
140
- else if (node.type && node.items) {
141
- delete node.type;
142
- }
133
+ transformers.toFinalBaseType(node);
143
134
  }
144
- else {
145
- /*
146
- this is a member and we expand to final base only if builtin
147
- type T: S; --> Integer;
148
- type S: X; --> Integer;
149
- type X: Integer;
135
+ else if (csnUtils.isStructured(finalBaseType)) {
136
+ cloneElements(node, finalBaseType);
137
+ }
138
+ else if (node.type && node.items) {
139
+ delete node.type;
140
+ }
141
+ }
142
+ else {
143
+ /*
144
+ this is a member and we expand to final base only if builtin
145
+ type T: S; --> Integer;
146
+ type S: X; --> Integer;
147
+ type X: Integer;
150
148
 
151
- type {
152
- struct_elt: many A; ---> stays the same
153
- scalar_elt: T; ---> Integer;
154
- type_ref_elt: type of struct_elt;
155
- };
156
- type A: B; -> {...}
157
- type B: C; -> { ... }
158
- type C { .... };
149
+ type {
150
+ struct_elt: many A; ---> stays the same
151
+ scalar_elt: T; ---> Integer;
152
+ type_ref_elt: type of struct_elt;
153
+ };
154
+ type A: B; -> {...}
155
+ type B: C; -> { ... }
156
+ type C { .... };
157
+ */
158
+ // eslint-disable-next-line no-lonely-if
159
+ if (isBuiltinType(finalBaseType.type)) {
160
+ /*
161
+ use transformUtils::toFinalBaseType for the moment,
162
+ as it is collects along the chain of types
163
+ attributes that need to be propagated
164
+ enum, length, scale, etc.
159
165
  */
160
- // eslint-disable-next-line no-lonely-if
161
- if (isBuiltinType(finalBaseType.type)) {
162
- /*
163
- use transformUtils::toFinalBaseType for the moment,
164
- as it is collects along the chain of types
165
- attributes that need to be propagated
166
- enum, length, scale, etc.
167
- */
168
- transformers.toFinalBaseType(node);
169
- // node.type = finalType;
170
- }
171
- else if (node.type.ref) {
172
- cloneElements(node, finalBaseType);
173
- }
166
+ transformers.toFinalBaseType(node);
167
+ // node.type = finalType;
168
+ }
169
+ else if (node.type.ref) {
170
+ cloneElements(node, finalBaseType);
174
171
  }
175
172
  }
176
173
  }