@sap/cds 5.4.6 → 5.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 (228) hide show
  1. package/CHANGELOG.md +208 -2
  2. package/apis/ql.d.ts +17 -15
  3. package/app/index.js +1 -1
  4. package/bin/build/buildTaskEngine.js +26 -42
  5. package/bin/build/buildTaskFactory.js +6 -10
  6. package/bin/build/buildTaskHandler.js +2 -4
  7. package/bin/build/buildTaskProvider.js +3 -1
  8. package/bin/build/buildTaskProviderFactory.js +9 -15
  9. package/bin/build/constants.js +15 -3
  10. package/bin/build/index.js +5 -4
  11. package/bin/build/mtaUtil.js +8 -11
  12. package/bin/build/provider/buildTaskHandlerEdmx.js +63 -6
  13. package/bin/build/provider/buildTaskHandlerInternal.js +2 -34
  14. package/bin/build/provider/buildTaskProviderInternal.js +16 -42
  15. package/bin/build/provider/fiori/index.js +13 -24
  16. package/bin/build/provider/hana/2migration.js +17 -15
  17. package/bin/build/provider/hana/2tabledata.js +52 -48
  18. package/bin/build/provider/hana/index.js +27 -25
  19. package/bin/build/provider/hana/migrationtable.js +91 -67
  20. package/bin/build/provider/java-cf/index.js +14 -24
  21. package/bin/build/provider/mtx/index.js +12 -14
  22. package/bin/build/provider/node-cf/index.js +18 -32
  23. package/bin/cds.js +5 -5
  24. package/bin/serve.js +29 -23
  25. package/bin/version.js +0 -1
  26. package/lib/compile/etc/_localized.js +4 -9
  27. package/lib/compile/for/sql.js +5 -2
  28. package/lib/compile/parse.js +25 -17
  29. package/lib/compile/to/srvinfo.js +2 -1
  30. package/lib/connect/bindings.js +2 -1
  31. package/lib/connect/index.js +48 -49
  32. package/lib/core/classes.js +1 -1
  33. package/lib/core/reflect.js +10 -2
  34. package/lib/deploy.js +26 -23
  35. package/lib/env/defaults.js +13 -6
  36. package/lib/env/index.js +73 -78
  37. package/lib/env/requires.js +38 -19
  38. package/lib/index.js +9 -10
  39. package/lib/lazy.js +2 -2
  40. package/lib/log/index.js +33 -45
  41. package/lib/log/service/index.js +2 -2
  42. package/lib/ql/CREATE.js +14 -9
  43. package/lib/ql/DELETE.js +6 -5
  44. package/lib/ql/DROP.js +12 -9
  45. package/lib/ql/INSERT.js +40 -16
  46. package/lib/ql/Query.js +67 -40
  47. package/lib/ql/SELECT.js +162 -127
  48. package/lib/ql/UPDATE.js +74 -42
  49. package/lib/ql/Whereable.js +77 -87
  50. package/lib/ql/index.js +36 -24
  51. package/lib/ql/parse.js +35 -0
  52. package/lib/req/context.js +44 -8
  53. package/lib/req/locale.js +7 -7
  54. package/lib/serve/Service-api.js +21 -14
  55. package/lib/serve/Service-dispatch.js +28 -12
  56. package/lib/serve/Transaction.js +22 -10
  57. package/lib/serve/index.js +16 -11
  58. package/lib/utils/axios.js +23 -16
  59. package/lib/utils/data.js +35 -0
  60. package/lib/utils/tests.js +27 -18
  61. package/libx/_runtime/audit/generic/personal/access.js +81 -0
  62. package/libx/_runtime/audit/generic/personal/constants.js +4 -0
  63. package/libx/_runtime/audit/generic/personal/index.js +50 -0
  64. package/libx/_runtime/audit/generic/personal/modification.js +138 -0
  65. package/libx/_runtime/audit/generic/personal/utils.js +186 -0
  66. package/libx/_runtime/audit/utils/v2.js +10 -4
  67. package/libx/_runtime/cds-services/adapter/odata-v4/Dispatcher.js +5 -5
  68. package/libx/_runtime/cds-services/adapter/odata-v4/OData.js +6 -7
  69. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js +5 -7
  70. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/create.js +5 -7
  71. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/delete.js +2 -3
  72. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/error.js +4 -0
  73. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/metadata.js +7 -4
  74. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js +59 -8
  75. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/request.js +11 -1
  76. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/update.js +6 -10
  77. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/ExpressionToCQN.js +3 -46
  78. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/applyToCQN.js +2 -5
  79. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/createToCQN.js +2 -2
  80. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/deleteToCQN.js +4 -3
  81. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/expandToCQN.js +1 -2
  82. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/index.js +0 -1
  83. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/selectHelper.js +1 -1
  84. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/updateToCQN.js +2 -2
  85. package/libx/_runtime/cds-services/adapter/odata-v4/odata-to-cqn/utils.js +16 -18
  86. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/edm/EdmEntityType.js +6 -3
  87. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/format/RepresentationKind.js +4 -1
  88. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/core/OdataRequest.js +1 -0
  89. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/serializer/SerializerFactory.js +15 -2
  90. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/validator/OperationValidator.js +1 -0
  91. package/libx/_runtime/cds-services/adapter/odata-v4/to.js +1 -1
  92. package/libx/_runtime/cds-services/adapter/odata-v4/utils/data.js +8 -1
  93. package/libx/_runtime/cds-services/adapter/odata-v4/utils/handlerUtils.js +6 -1
  94. package/libx/_runtime/cds-services/adapter/odata-v4/utils/omitValues.js +12 -5
  95. package/libx/_runtime/cds-services/adapter/odata-v4/utils/readAfterWrite.js +1 -7
  96. package/libx/_runtime/cds-services/adapter/odata-v4/utils/request.js +7 -7
  97. package/libx/_runtime/cds-services/adapter/odata-v4/utils/result.js +14 -18
  98. package/libx/_runtime/cds-services/adapter/odata-v4/utils/stream.js +13 -13
  99. package/libx/_runtime/cds-services/adapter/rest/handlers/create.js +0 -1
  100. package/libx/_runtime/cds-services/adapter/rest/handlers/operation.js +2 -1
  101. package/libx/_runtime/cds-services/adapter/rest/handlers/read.js +2 -2
  102. package/libx/_runtime/cds-services/adapter/rest/rest-to-cqn/index.js +2 -4
  103. package/libx/_runtime/cds-services/adapter/rest/utils/result.js +4 -2
  104. package/libx/_runtime/cds-services/services/Service.js +40 -5
  105. package/libx/_runtime/cds-services/services/utils/columns.js +13 -7
  106. package/libx/_runtime/cds-services/services/utils/compareJson.js +88 -4
  107. package/libx/_runtime/cds-services/services/utils/differ.js +24 -6
  108. package/libx/_runtime/cds-services/services/utils/handlerUtils.js +2 -2
  109. package/libx/_runtime/common/composition/data.js +44 -55
  110. package/libx/_runtime/common/composition/delete.js +97 -71
  111. package/libx/_runtime/common/composition/index.js +2 -1
  112. package/libx/_runtime/common/composition/insert.js +34 -11
  113. package/libx/_runtime/common/composition/tree.js +119 -92
  114. package/libx/_runtime/common/composition/update.js +4 -1
  115. package/libx/_runtime/common/composition/utils.js +1 -3
  116. package/libx/_runtime/common/constants/draft.js +12 -1
  117. package/libx/_runtime/common/generic/auth.js +6 -22
  118. package/libx/_runtime/common/generic/crud.js +14 -13
  119. package/libx/_runtime/common/generic/input.js +23 -26
  120. package/libx/_runtime/common/generic/put.js +1 -1
  121. package/libx/_runtime/common/generic/sorting.js +16 -16
  122. package/libx/_runtime/common/i18n/index.js +1 -1
  123. package/libx/_runtime/common/i18n/messages.properties +4 -0
  124. package/libx/_runtime/common/utils/backlinks.js +12 -5
  125. package/libx/_runtime/common/utils/cqn.js +6 -1
  126. package/libx/_runtime/common/utils/cqn2cqn4sql.js +102 -101
  127. package/libx/_runtime/common/utils/csn.js +47 -4
  128. package/libx/_runtime/common/utils/data.js +0 -37
  129. package/libx/_runtime/common/utils/enrichWithKeysFromWhere.js +1 -1
  130. package/libx/_runtime/common/utils/entityFromCqn.js +7 -24
  131. package/libx/_runtime/common/utils/foreignKeyPropagations.js +39 -7
  132. package/libx/_runtime/common/utils/generateOnCond.js +11 -12
  133. package/libx/_runtime/common/utils/onlyKeysRemain.js +10 -0
  134. package/libx/_runtime/common/utils/path.js +35 -0
  135. package/libx/_runtime/common/utils/postProcessing.js +86 -0
  136. package/libx/_runtime/common/utils/quotingStyles.js +37 -26
  137. package/libx/_runtime/common/utils/resolveView.js +223 -171
  138. package/libx/_runtime/common/utils/rewriteAsterisk.js +46 -26
  139. package/libx/_runtime/common/utils/structured.js +6 -12
  140. package/libx/_runtime/common/utils/template.js +10 -5
  141. package/libx/_runtime/common/utils/templateDelimiter.js +1 -0
  142. package/libx/_runtime/common/utils/templateProcessor.js +22 -30
  143. package/libx/_runtime/common/utils/union.js +31 -0
  144. package/libx/_runtime/common/utils/unionCqnTemplate.js +184 -0
  145. package/libx/_runtime/db/Service.js +1 -1
  146. package/libx/_runtime/db/data-conversion/timestamp.js +2 -9
  147. package/libx/_runtime/db/expand/expandCQNToJoin.js +204 -297
  148. package/libx/_runtime/db/expand/index.js +3 -3
  149. package/libx/_runtime/db/expand/rawToExpanded.js +36 -7
  150. package/libx/_runtime/db/generic/index.js +1 -1
  151. package/libx/_runtime/db/generic/input.js +5 -7
  152. package/libx/_runtime/db/generic/integrity.js +1 -1
  153. package/libx/_runtime/db/generic/rewrite.js +2 -10
  154. package/libx/_runtime/db/generic/update.js +13 -5
  155. package/libx/_runtime/db/generic/virtual.js +22 -58
  156. package/libx/_runtime/db/query/delete.js +7 -4
  157. package/libx/_runtime/db/query/insert.js +6 -4
  158. package/libx/_runtime/db/query/read.js +13 -20
  159. package/libx/_runtime/db/query/run.js +4 -1
  160. package/libx/_runtime/db/query/update.js +5 -4
  161. package/libx/_runtime/db/sql-builder/ExpressionBuilder.js +35 -2
  162. package/libx/_runtime/db/sql-builder/FunctionBuilder.js +17 -2
  163. package/libx/_runtime/db/sql-builder/InsertBuilder.js +6 -5
  164. package/libx/_runtime/db/sql-builder/ReferenceBuilder.js +10 -0
  165. package/libx/_runtime/db/sql-builder/SelectBuilder.js +35 -24
  166. package/libx/_runtime/db/sql-builder/UpdateBuilder.js +14 -4
  167. package/libx/_runtime/db/sql-builder/arrayed.js +4 -0
  168. package/libx/_runtime/db/utils/deep.js +8 -0
  169. package/libx/_runtime/db/utils/generateAliases.js +2 -1
  170. package/libx/_runtime/fiori/generic/activate.js +19 -15
  171. package/libx/_runtime/fiori/generic/before.js +3 -11
  172. package/libx/_runtime/fiori/generic/cancel.js +1 -1
  173. package/libx/_runtime/fiori/generic/delete.js +3 -1
  174. package/libx/_runtime/fiori/generic/edit.js +12 -2
  175. package/libx/_runtime/fiori/generic/new.js +5 -5
  176. package/libx/_runtime/fiori/generic/patch.js +0 -18
  177. package/libx/_runtime/fiori/generic/read.js +241 -189
  178. package/libx/_runtime/fiori/utils/delete.js +36 -7
  179. package/libx/_runtime/fiori/utils/handler.js +43 -44
  180. package/libx/_runtime/fiori/utils/where.js +30 -15
  181. package/libx/_runtime/hana/customBuilder/CustomSelectBuilder.js +4 -6
  182. package/libx/_runtime/hana/execute.js +2 -2
  183. package/libx/_runtime/hana/localized.js +4 -4
  184. package/libx/_runtime/hana/pool.js +29 -14
  185. package/libx/_runtime/hana/search2cqn4sql.js +2 -1
  186. package/libx/_runtime/hana/searchToContains.js +18 -14
  187. package/libx/_runtime/index.js +0 -5
  188. package/libx/_runtime/messaging/AMQPWebhookMessaging.js +13 -5
  189. package/libx/_runtime/messaging/common-utils/naming-conventions.js +4 -1
  190. package/libx/_runtime/messaging/enterprise-messaging-utils/EMManagement.js +31 -19
  191. package/libx/_runtime/messaging/enterprise-messaging-utils/registerEndpoints.js +1 -2
  192. package/libx/_runtime/messaging/enterprise-messaging.js +6 -4
  193. package/libx/_runtime/messaging/service.js +7 -6
  194. package/libx/_runtime/odata/cqn2odata.js +110 -43
  195. package/libx/_runtime/odata/index.js +26 -48
  196. package/libx/_runtime/odata/odata2cqn.js +1 -6154
  197. package/libx/_runtime/odata/odata2cqn.pegjs +559 -0
  198. package/libx/_runtime/odata/readToCqn.js +94 -64
  199. package/libx/_runtime/remote/Service.js +74 -21
  200. package/libx/_runtime/remote/cqn2odata/index.js +1 -5
  201. package/libx/_runtime/remote/utils/client.js +24 -101
  202. package/libx/_runtime/remote/utils/dataConversion.js +27 -12
  203. package/libx/_runtime/sqlite/Service.js +3 -5
  204. package/libx/_runtime/sqlite/execute.js +23 -24
  205. package/libx/_runtime/sqlite/localized.js +12 -7
  206. package/libx/_runtime/types/api.js +10 -0
  207. package/package.json +1 -1
  208. package/server.js +16 -2
  209. package/lib/ql/grammar.pegjs +0 -208
  210. package/lib/ql/parser.js +0 -1
  211. package/lib/ql/rt/DELETE.js +0 -29
  212. package/lib/ql/rt/INSERT.js +0 -23
  213. package/lib/ql/rt/Query.js +0 -84
  214. package/lib/ql/rt/SELECT.js +0 -174
  215. package/lib/ql/rt/UPDATE.js +0 -119
  216. package/lib/ql/rt/_helpers.js +0 -91
  217. package/lib/ql/rt/index.js +0 -32
  218. package/libx/_runtime/audit/generic/personal.js +0 -260
  219. package/libx/_runtime/cds-services/statements/BaseStatement.js +0 -72
  220. package/libx/_runtime/cds-services/statements/Create.js +0 -57
  221. package/libx/_runtime/cds-services/statements/Delete.js +0 -33
  222. package/libx/_runtime/cds-services/statements/Drop.js +0 -42
  223. package/libx/_runtime/cds-services/statements/Insert.js +0 -201
  224. package/libx/_runtime/cds-services/statements/Select.js +0 -826
  225. package/libx/_runtime/cds-services/statements/Update.js +0 -181
  226. package/libx/_runtime/cds-services/statements/Where.js +0 -726
  227. package/libx/_runtime/cds-services/statements/index.js +0 -25
  228. package/libx/_runtime/common/generic/resolve-mock.js +0 -9
package/lib/index.js CHANGED
@@ -5,12 +5,7 @@ if (global.cds) Object.assign(module,{exports:global.cds}) ; else {
5
5
 
6
6
  get version() { return super.version = require('../package.json').version }
7
7
  get env() { return super.env = require('./env') }
8
- get requires() { // shortcut, with resolved indirections
9
- const dict = Object.create (this.env.requires)
10
- for (let e of Object.values (this.env.requires)) if (e.service) dict[e.service] = e
11
- return super.requires = dict
12
- }
13
-
8
+ get requires() { return super.requires = this.env.required_services_or_defs }
14
9
  get builtin() { return super.builtin = require ('./core') }
15
10
  get service() { return super.service = extend (this.builtin.classes.service) .with ({
16
11
  /** @param x {(this:Service, srv:Service) => any} */ impl: x=>x,
@@ -68,16 +63,20 @@ if (global.cds) Object.assign(module,{exports:global.cds}) ; else {
68
63
 
69
64
  // Core Services API
70
65
  Service: require ('./serve/Service-api'),
71
- ApplicationService: lazy => require('../libx/_runtime/cds-services/services/Service.js'),
72
- MessagingService: lazy => require('../libx/_runtime/messaging/service.js'),
73
- DatabaseService: lazy => require('../libx/_runtime/db/Service.js'),
74
- RemoteService: lazy => require('../libx/_runtime/rest/service.js'),
75
66
  Request: require ('./req/impl'),
76
67
  Event: require ('./req/event'),
77
68
  User: require ('./req/user'),
78
69
  ql: require ('./ql'),
70
+ spawn: lazy => cds.Event.spawn,
79
71
  /** @type Service */ db: undefined,
80
72
 
73
+ // Protocols and Periphery
74
+ ApplicationService: lazy => require('../libx/_runtime/cds-services/services/Service.js'),
75
+ MessagingService: lazy => require('../libx/_runtime/messaging/service.js'),
76
+ DatabaseService: lazy => require('../libx/_runtime/db/Service.js'),
77
+ RemoteService: lazy => require('../libx/_runtime/rest/service.js'),
78
+ odata: require('../libx/_runtime/odata'),
79
+
81
80
  // Helpers
82
81
  localize: require ('./i18n/localize'),
83
82
  error: require ('./log/errors'),
package/lib/lazy.js CHANGED
@@ -20,8 +20,8 @@ const extend = (target) => ({
20
20
  })
21
21
 
22
22
  const _excludes = {
23
- function: { name: 1, length: 2, arguments: 3, caller: 4, prototype: 5 },
24
- object: { constructor: 1 },
23
+ function: Object.assign(Object.create(null),{ name: 1, length: 2, arguments: 3, caller: 4, prototype: 5 }),
24
+ object: Object.assign(Object.create(null),{ constructor: 1 }),
25
25
  }
26
26
 
27
27
  /** @type <T>(target:T) => T */
package/lib/log/index.js CHANGED
@@ -10,7 +10,6 @@ if (log.Logger || log.service) {
10
10
  }
11
11
 
12
12
 
13
-
14
13
  /**
15
14
  * Returns a trace logger for the given module if trace is switched on for it,
16
15
  * otherwise returns null. All cds runtime packages use this method for their
@@ -21,19 +20,34 @@ if (log.Logger || log.service) {
21
20
  *
22
21
  * You can also specify alternate module names:
23
22
  *
24
- * const LOG = cds.log('db|sql')
23
+ * const LOG = cds.log('sql|db')
24
+ *
25
+ * By default this logger would prefix all output with '[sql] - '.
26
+ * You can change this by specifying another prefix in the options:
27
+ *
28
+ * const LOG = cds.log('sql|db',{ prefix:'cds.ql' })
29
+ *
30
+ * Call cds.log() for a given module again to dynamically change the log level
31
+ * of all formerly created loggers, for example:
32
+ *
33
+ * const LOG = cds.log('sql')
34
+ * LOG.info ('this will show, as default level is info')
35
+ * cds.log('sql','warn')
36
+ * LOG.info ('this will be suppressed now')
25
37
  *
26
38
  * Tracing can be switched on/off through env variable DEBUG:
27
39
  * Set it to a comma-separated list of modules to switch on tracing.
28
40
  * Set it to 'all' or 'y' to switch on tracing for all modules.
29
41
  *
30
42
  * @param {string} [module] the module for which a logger is requested
31
- * @param {string|number|{ level }} [level] the log level to enable -> 0=off, 1=error, 2=warn, 3=info, 4=debug, 5=trace
43
+ * @param {string|number|{ level, prefix }} [options] the log level to enable -> 0=off, 1=error, 2=warn, 3=info, 4=debug, 5=trace
32
44
  */
33
- module.exports = exports = function cds_log (module, level) { // NOSONAR
34
- const id = module ? module.match(/^[^|]+/)[0] : 'cds'
35
- const logger = cached[id]; if (logger && !level) return logger
36
- if (typeof level === 'object') level = level.level
45
+ module.exports = exports = function cds_log (module, options) { // NOSONAR
46
+ let id = module ? module.match(/^[^|]+/)[0] : 'cds'
47
+ let logger = cached[id]; if (logger && !options) return logger
48
+
49
+ let { level, prefix } = typeof options === 'object' ? options : {level:options}
50
+ if (!prefix) prefix = logger && logger.prefix || id
37
51
  if (!level) level = (
38
52
  process.env.DEBUG && process.env.DEBUG.match(RegExp(`\\b(y|all|${module||'any'})\\b`)) ? DEBUG :
39
53
  log.levels[id] || INFO
@@ -41,15 +55,20 @@ module.exports = exports = function cds_log (module, level) { // NOSONAR
41
55
  if (typeof level === 'string') {
42
56
  level = exports.levels [level.toUpperCase()]
43
57
  }
58
+
59
+ // IMPORTANT: cds.log() can be called again to change the log level
60
+ // of formerly constructed loggers!!
44
61
  if (logger && logger.level === level) return logger
45
- return cached[id] = new Logger (id,level)
62
+ else logger = exports.Logger (prefix, level)
63
+ return cached[id] = Object.assign (cached[id] || logger.log, logger, {
64
+ id, level, prefix, setFormat(fn){ logger.format = fn }
65
+ })
46
66
  }
47
67
 
48
68
 
49
69
  /**
50
70
  * Shortcut to `cds.log(...).debug`, returning undefined if `cds.log(...)._debug` is false.
51
71
  * @param {string} [module] the module for which a logger is requested
52
- * @param {string|{ level, prefix }} [prefix] a prefix to prepend to each log output, default: '[cds.<module>] -'
53
72
  */
54
73
  exports.debug = function cds_debug (module) {
55
74
  const L = this.log (module)
@@ -70,13 +89,13 @@ exports.debug = function cds_debug (module) {
70
89
  */
71
90
  /* eslint-disable no-console */
72
91
  exports.Logger = (module, level) => {
73
- const fmt = (level,args) => log.format (module,level,...args)
74
- const log = Object.assign ({
92
+ const fmt = (level,args) => logger.format (module,level,...args)
93
+ const logger = Object.assign ({
75
94
  format: exports.format,
76
95
  trace: level < TRACE ? ()=>{} : (...args) => console.trace (...fmt(TRACE,args)),
77
96
  debug: level < DEBUG ? ()=>{} : (...args) => console.debug (...fmt(DEBUG,args)),
78
97
  log: level < INFO ? ()=>{} : (...args) => console.log (...fmt(INFO,args)),
79
- info: level < INFO ? ()=>{} : (...args) => console.log (...fmt(INFO,args)),
98
+ info: level < INFO ? ()=>{} : (...args) => console.info (...fmt(INFO,args)),
80
99
  warn: level < WARN ? ()=>{} : (...args) => console.warn (...fmt(WARN,args)),
81
100
  error: level < ERROR ? ()=>{} : (...args) => console.error (...fmt(ERROR,args)),
82
101
  _trace: level >= TRACE,
@@ -84,12 +103,9 @@ exports.Logger = (module, level) => {
84
103
  _info: level >= INFO,
85
104
  _warn: level >= WARN,
86
105
  _error: level >= ERROR,
87
- }, !cds.app && { // in case of cds compile, all log output goes to stderr
88
- debug: level < DEBUG ? ()=>{} : (...args) => console.warn (...fmt(DEBUG,args)),
89
- log: level < INFO ? ()=>{} : (...args) => console.warn (...fmt(INFO,args)),
90
- info: level < INFO ? ()=>{} : (...args) => console.warn (...fmt(INFO,args)),
91
106
  })
92
- return log
107
+ // deleted stdout -> stderr redirection for cds compile as bin/utils/log.js is used
108
+ return logger
93
109
  }
94
110
 
95
111
 
@@ -110,35 +126,7 @@ exports.format = (
110
126
  )
111
127
 
112
128
 
113
- /**
114
- * Helper fundcion to construct new Loggers, updating an already cached one
115
- * with new log levels, if exists.
116
- */
117
- function Logger (id,level,prefix) {
118
- const logger = exports.Logger (id, level, prefix)
119
- return Object.assign (cached[id] || logger.log, logger, {
120
- id, level, prefix, setFormat(fn){ logger.format = fn }
121
- })
122
- }
123
-
124
-
125
129
  const { ERROR, WARN, INFO, DEBUG, TRACE } = exports.levels = {
126
130
  SILENT:0, ERROR:1, WARN:2, INFO:3, DEBUG:4, TRACE:5, SILLY:5, VERBOSE:5
127
131
  }
128
132
  const cached = exports.loggers = {}
129
-
130
- Object.defineProperty (exports,'cache',{
131
- configurable: true,
132
- get(){
133
- Object.defineProperty (exports,'cache',{value:cached})
134
- console.trace(`
135
- -----------------------------------------------------------------
136
-
137
- WARNING: You are using inofficial and removed cds.log.cache,
138
- which was already removed -> please fix this coding.
139
-
140
- -----------------------------------------------------------------
141
- `)
142
- return cached
143
- }
144
- })
@@ -60,13 +60,13 @@ module.exports = class LogService extends cds.Service {
60
60
  this.on('debug', (req)=>{
61
61
  const [,id] = /debug\(module=([^)]+)\)/.exec(req.params[0]) || []
62
62
  if (!id) return req.reject('No logger id specified in request')
63
- return _logger (cds.log (id,'debug'))
63
+ return _logger (cds.log (id, {level:'debug'}))
64
64
  })
65
65
 
66
66
  this.on('reset', (req)=>{
67
67
  const [,id] = /reset\(module=([^)]+)\)/.exec(req.params[0]) || []
68
68
  if (!id) return req.reject('No logger id specified in request')
69
- return _logger (cds.log (id,'info'))
69
+ return _logger (cds.log (id, {level:'info'}))
70
70
  })
71
71
 
72
72
  this.on('format', (req)=>{
package/lib/ql/CREATE.js CHANGED
@@ -1,20 +1,25 @@
1
1
  const Query = require('./Query')
2
2
  const $ = Object.assign
3
3
 
4
-
5
4
  module.exports = class CREATE extends Query {
6
- static get _() {
7
- return $((..._) => new this().entity(..._), {
8
- entity: (..._) => new this().entity(..._),
5
+
6
+ static _api() {
7
+ return $((..._) => (new this).entity(..._), {
8
+ entity: (..._) => (new this).entity(..._),
9
9
  })
10
10
  }
11
- entity(e, elements) {
12
- this._.entity = e
13
- this._.elements = elements
11
+
12
+ entity (e, elements) {
13
+ if (elements)
14
+ this.CREATE.entity = { elements: elements, kind: 'entity', name:e }
15
+ else
16
+ this.CREATE.entity = e && e.elements ? e : this._target_name4(e)
14
17
  return this
15
18
  }
16
- as(query) {
17
- this._.as = query
19
+
20
+ as (query) {
21
+ if (!query || !query.SELECT) this._expected `${{query}} to be a CQN {SELECT} object`
22
+ this.CREATE.as = query
18
23
  return this
19
24
  }
20
25
  }
package/lib/ql/DELETE.js CHANGED
@@ -1,15 +1,15 @@
1
1
  const Whereable = require('./Whereable')
2
- const $ = Object.assign
3
2
 
4
3
  module.exports = class DELETE extends Whereable {
5
- static get _() {
6
- return $((..._) => new this().from(..._), {
7
- from: (..._) => new this().from(..._),
4
+
5
+ static _api() {
6
+ return Object.assign ((..._) => (new this).from(..._), {
7
+ from: (..._) => (new this).from(..._),
8
8
  })
9
9
  }
10
10
 
11
11
  from(entity, key) {
12
- this._.from = this.target(entity).name
12
+ this.DELETE.from = this._target_name4 (entity)
13
13
  if (key) this.byKey(key)
14
14
  return this
15
15
  }
@@ -17,4 +17,5 @@ module.exports = class DELETE extends Whereable {
17
17
  valueOf() {
18
18
  return super.valueOf('DELETE FROM')
19
19
  }
20
+
20
21
  }
package/lib/ql/DROP.js CHANGED
@@ -1,22 +1,25 @@
1
1
  const Query = require('./Query')
2
- const $ = Object.assign
3
2
 
4
3
  module.exports = class DROP extends Query {
5
- static get _() {
6
- return $((..._) => new this().entity(..._), {
7
- entity: (e) => new this().entity(e),
8
- table: (e) => new this().table(e),
9
- view: (e) => new this().view(e),
4
+ static _api() {
5
+ return Object.assign ((e) => (new this).entity(e), {
6
+ entity: (e) => (new this).entity(e),
7
+ table: (e) => (new this).table(e),
8
+ view: (e) => (new this).view(e),
10
9
  })
11
10
  }
12
11
  entity(e) {
13
- this._.entity = this.target(e).name
12
+ this.DROP.entity = this._target_name4 (e)
14
13
  return this
15
14
  }
16
15
  table(e) {
17
- return this.entity((this._.table = e))
16
+ const {DROP} = this
17
+ DROP.entity = DROP.table = this._target_name4 (e)
18
+ return this
18
19
  }
19
20
  view(e) {
20
- return this.entity((this._.view = e))
21
+ const {DROP} = this
22
+ DROP.entity = DROP.view = this._target_name4 (e)
23
+ return this
21
24
  }
22
25
  }
package/lib/ql/INSERT.js CHANGED
@@ -1,38 +1,62 @@
1
1
  const Query = require('./Query')
2
2
  const is_array = Array.isArray
3
- const $ = Object.assign
4
3
 
5
4
  module.exports = class INSERT extends Query {
6
- static get _() {
7
- return $((..._) => new this().entries(..._), {
8
- into: (..._) => new this().into(..._),
5
+ static _api() {
6
+ return Object.assign ((..._) => (new this).entries(..._), {
7
+ into: (..._) => (new this).into(..._),
9
8
  })
10
9
  }
11
10
 
12
- into(entity, ...data) {
13
- this._.into = this.target(entity).name
11
+ into (entity, ...data) {
12
+ this.INSERT.into = this._target_name4 (entity)
14
13
  if (data.length) this.entries(...data)
15
14
  return this
16
15
  }
17
16
 
18
- entries(...x) {
19
- this._.entries = is_array(x[0]) ? x[0] : x
17
+ entries (...x) {
18
+ this.INSERT.entries = is_array(x[0]) ? x[0] : x
20
19
  return this
21
20
  }
22
- columns(...x) {
23
- this._.columns = is_array(x[0]) ? x[0] : x
21
+ columns (...x) {
22
+ this.INSERT.columns = is_array(x[0]) ? x[0] : x
24
23
  return this
25
24
  }
26
- values(...x) {
27
- this._.values = is_array(x[0]) ? x[0] : x
25
+ values (...x) {
26
+ this.INSERT.values = is_array(x[0]) ? x[0] : x
28
27
  return this
29
28
  }
30
- rows(...x) {
31
- this._.rows = is_array(x[0]) && is_array(x[0][0]) ? x[0] : x
29
+ rows (...rows) {
30
+ if (is_array(rows[0]) && is_array(rows[0][0])) rows = rows[0]
31
+ if (!is_array(rows[0])) this._expected `Arguments ${{rows}} to be an array of arrays`
32
+ this.INSERT.rows = rows
32
33
  return this
33
34
  }
34
- as(query) {
35
- this._.as = query
35
+ _rows(rows, ...args) {
36
+
37
+ if (Array.isArray(rows)) {
38
+ // check if all the entries in the array are arrays
39
+ if (rows.every(e => Array.isArray(e))) {
40
+ this.INSERT.rows = rows
41
+ // check if array contains one or multiple objects
42
+ } else if (rows.every(e => typeof e === 'object')) {
43
+ this.INSERT.entries = rows
44
+ // the rows have been added as arguments
45
+ } else if (args.length !== 0) {
46
+ args.unshift(rows)
47
+ this.INSERT.rows = args
48
+ } else {
49
+ this.INSERT.values = rows
50
+ }
51
+ } else if (typeof rows === 'object') {
52
+ this.INSERT.entries = rows
53
+ }
54
+
55
+ return this
56
+ }
57
+ as (query) {
58
+ if (!query || !query.SELECT) this._expected `${{query}} to be a CQN {SELECT} query object`
59
+ this.INSERT.as = query
36
60
  return this
37
61
  }
38
62
  valueOf() {
package/lib/ql/Query.js CHANGED
@@ -1,59 +1,86 @@
1
- const set = (o,p,v) => Object.defineProperty (o,p, { value:v, configurable:true })
2
- const { inspect } = module.require('util')
3
- const {cds} = global
1
+ const { inspect } = require('util')
2
+ const cds = require('../index')
4
3
 
5
4
  module.exports = class Query {
6
5
 
7
- constructor(_={}) {
8
- const cmd = this.constructor.name
9
- set (this, '_', this[cmd] = _)
10
- set (this, 'cmd', cmd)
11
- }
6
+ constructor(_={}) { this[this.cmd] = _ }
12
7
 
13
- target(e) {
14
- return set (this,'_target', e.name ? e : e._target || { name: e })._target
8
+ /** Creates a derived instance that initially inherits all properties. */
9
+ clone(){
10
+ return { __proto__:this, [this.cmd]: {__proto__: this[this.cmd]} }
15
11
  }
16
12
 
17
- bind(srv) {
18
- return set (this,'_srv',srv)
13
+ /** Binds this query to be executed with the given service */
14
+ bind (srv) {
15
+ return Object.defineProperty (this,'_srv',{value:srv})
19
16
  }
20
17
 
21
18
  /** Turns all queries into Thenables which execute with primary db by default */
22
- then(r,e) {
23
- let {_promised} = this; if (!_promised) {
24
- const srv = this._srv || this._target._service && cds.services[this._target._service.name] || cds.db
25
- set (this,'_promised', _promised = srv.run(this))
26
- }
27
- return _promised.then(r,e)
19
+ then (r,e) {
20
+ return (this._srv || cds.db) .run (this) .then (r,e)
28
21
  }
29
22
 
30
- async foreach(cb) {
31
- return (await this).map(cb)
23
+ /** Beautifies output in REPL */
24
+ [inspect.custom]() {
25
+ const {cmd} = this, colors = process.env.CDS_TERM_COLORS !== false
26
+ return `{ ${cmd}: `+ inspect(this[cmd], { colors, depth: 22 })
27
+ .replace(/^\w*\s/, '')
28
+ .replace(
29
+ /{ ref: \[([^\]]*)\] }/g,
30
+ (_,ref) => '{ref:[' + ref.slice(1, -1) + ']}'
31
+ )
32
+ .replace(/{ val: ([^ ]*) }/g, '{val:$1}')
33
+ .replace(/{ (xpr|ref|val): /g, '{$1:') +
34
+ '}'
32
35
  }
33
36
 
34
- clone() {
35
- return new this.constructor({ ...this._ })
37
+ _target_ref4 (target) {
38
+
39
+ // Resolving this._target --> REVISIT: this is not reliable !!!
40
+ Object.defineProperty (this, '_target', { value: target && (
41
+ typeof target === 'string' ? { name: target } :
42
+ target.name ? target : //> assumed to be a linked csn definition
43
+ target.ref ? { name: target.ref[0] } :
44
+ target._target || { name: undefined }
45
+ ), configurable:true })
46
+
47
+ return target && (
48
+ typeof target === 'string' ? cds.parse.path(target) :
49
+ target.ref ? target :
50
+ target.SELECT ? target :
51
+ target.SET ? target :
52
+ target.raw ? cds.parse.path(...arguments) :
53
+ target.name ? {ref:[target.name]} : 0
54
+ )
55
+ || this._expected `${{target}} to be an entity path string, a CSN definition, a {ref}, a {SELECT}, or a {SET}`
36
56
  }
37
57
 
38
- valueOf (qo='Query on') {
39
- return qo + ' ' + this._target.name.replace(/[.:]/g, '_') + ' '
58
+ //> REVISIT: should we rather have consistent .from/.entity/.into in CQN?
59
+ _target_name4 (target) {
60
+ const {ref} = this._target_ref4 (target)
61
+ return ref.length === 1 && typeof ref[0] === 'string' ? ref[0] : {ref}
40
62
  }
41
63
 
42
- // beautify output in REPL
43
- [inspect.custom]() {
44
- const colors = process.env.CDS_TERM_COLORS !== false
45
- const cmd = this.cmd
46
- return (
47
- `{ ${cmd}: ` +
48
- inspect(this[cmd], { colors, depth: 22 })
49
- .replace(/^\w*\s/, '')
50
- .replace(
51
- /{ ref: \[([^\]]*)\] }/g,
52
- (_, ref) => '{ref:[' + ref.slice(1, -1) + ']}'
53
- )
54
- .replace(/{ val: ([^ ]*) }/g, '{val:$1}')
55
- .replace(/{ (xpr|ref|val): /g, '{$1:') +
56
- '}'
57
- )
64
+ _expected (...args) {
65
+ return cds.error.expected (...args)
66
+ }
67
+
68
+ _own (property, _ = this[this.cmd]) {
69
+ const pd = Reflect.getOwnPropertyDescriptor (_, property)
70
+ return pd && pd.value
71
+ }
72
+
73
+ _add (property, values) {
74
+ const _ = this[this.cmd], pd = Reflect.getOwnPropertyDescriptor (_,property)
75
+ _[property] = !pd || !pd.value ? values : [ ...pd.value, ...values ]
76
+ return this
77
+ }
78
+
79
+ valueOf (cmd=this.cmd) {
80
+ return `${cmd} ${_name(this._target.name)} `
58
81
  }
82
+
59
83
  }
84
+
85
+
86
+ const _name = cds.env.sql.names === 'quoted' ? n =>`"${n}"` : n => n.replace(/[.:]/g,'_')