@sap/cds 6.3.2 → 6.4.1

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 (128) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/apis/cds.d.ts +3 -1
  3. package/apis/core.d.ts +118 -90
  4. package/apis/cqn.d.ts +11 -2
  5. package/apis/internal/inference.d.ts +7 -2
  6. package/apis/ql.d.ts +49 -11
  7. package/apis/serve.d.ts +8 -1
  8. package/apis/services.d.ts +311 -305
  9. package/bin/build/buildTaskEngine.js +28 -36
  10. package/bin/build/buildTaskFactory.js +32 -81
  11. package/bin/build/buildTaskHandler.js +3 -2
  12. package/bin/build/buildTaskProvider.js +2 -2
  13. package/bin/build/buildTaskProviderFactory.js +5 -14
  14. package/bin/build/constants.js +0 -1
  15. package/bin/build/provider/buildTaskHandlerEdmx.js +7 -6
  16. package/bin/build/provider/buildTaskHandlerFeatureToggles.js +6 -5
  17. package/bin/build/provider/buildTaskHandlerInternal.js +9 -30
  18. package/bin/build/provider/buildTaskProviderInternal.js +70 -58
  19. package/bin/build/provider/fiori/index.js +6 -5
  20. package/bin/build/provider/hana/2migration.js +20 -3
  21. package/bin/build/provider/hana/2tabledata.js +1 -0
  22. package/bin/build/provider/hana/index.js +40 -17
  23. package/bin/build/provider/java/index.js +10 -10
  24. package/bin/build/provider/mtx/index.js +25 -16
  25. package/bin/build/provider/mtx/resourcesTarBuilder.js +22 -27
  26. package/bin/build/provider/mtx-extension/index.js +3 -2
  27. package/bin/build/provider/mtx-sidecar/index.js +16 -15
  28. package/bin/build/provider/nodejs/index.js +14 -56
  29. package/bin/build/util.js +56 -16
  30. package/bin/deploy/to-hana/cfUtil.js +2 -0
  31. package/bin/deploy/to-hana/gitUtil.js +1 -1
  32. package/bin/deploy/to-hana/hana.js +45 -38
  33. package/bin/deploy/to-hana/hdiDeployUtil.js +17 -12
  34. package/bin/deploy/to-hana/mtaUtil.js +13 -14
  35. package/bin/mtx/in-cds.js +3 -1
  36. package/bin/serve.js +1 -1
  37. package/bin/version.js +2 -1
  38. package/lib/auth/index.js +17 -15
  39. package/lib/compile/cds-compile.js +1 -0
  40. package/lib/compile/cdsc.js +1 -0
  41. package/lib/compile/etc/_localized.js +2 -2
  42. package/lib/compile/for/lean_drafts.js +83 -0
  43. package/lib/compile/for/nodejs.js +1 -0
  44. package/lib/compile/minify.js +2 -1
  45. package/lib/compile/to/gql.js +1 -1
  46. package/lib/compile/to/sql.js +11 -1
  47. package/lib/core/entities.js +1 -1
  48. package/lib/core/index.js +9 -9
  49. package/lib/core/infer.js +1 -0
  50. package/lib/dbs/cds-deploy.js +97 -41
  51. package/lib/env/cds-env.js +9 -10
  52. package/lib/env/cds-requires.js +8 -2
  53. package/lib/env/defaults.js +0 -4
  54. package/lib/env/schemas/cds-rc.json +38 -0
  55. package/lib/ql/SELECT.js +10 -4
  56. package/lib/srv/bindings.js +1 -1
  57. package/lib/srv/factory.js +1 -1
  58. package/lib/srv/middlewares/cds-context.js +0 -2
  59. package/lib/srv/middlewares/ctx-auth.js +11 -0
  60. package/lib/srv/middlewares/ctx-model.js +22 -20
  61. package/lib/srv/middlewares/index.js +7 -9
  62. package/lib/srv/protocols/_legacy.js +4 -0
  63. package/lib/srv/protocols/graphql.js +2 -2
  64. package/lib/srv/protocols/index.js +7 -3
  65. package/lib/srv/srv-api.js +1 -0
  66. package/lib/srv/srv-methods.js +1 -1
  67. package/lib/utils/cds-utils.js +11 -0
  68. package/lib/utils/data.js +2 -2
  69. package/lib/utils/inflect.js +13 -12
  70. package/lib/utils/tar.js +43 -13
  71. package/libx/_runtime/cds-services/adapter/odata-v4/ODataRequest.js +2 -2
  72. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js +1 -1
  73. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/create.js +1 -1
  74. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/delete.js +1 -1
  75. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/metadata.js +1 -15
  76. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/read.js +1 -1
  77. package/libx/_runtime/cds-services/adapter/odata-v4/handlers/update.js +1 -1
  78. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/errors/UriSyntaxError.js +1 -1
  79. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-commons/uri/UriParser.js +6 -1
  80. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/utils/BufferedWriter.js +1 -1
  81. package/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/validator/ConditionalRequestValidator.js +0 -12
  82. package/libx/_runtime/cds-services/adapter/odata-v4/utils/oDataConfiguration.js +1 -7
  83. package/libx/_runtime/cds-services/adapter/odata-v4/utils/result.js +4 -0
  84. package/libx/_runtime/cds-services/services/Service.js +23 -1
  85. package/libx/_runtime/cds-services/util/assert.js +0 -41
  86. package/libx/_runtime/common/composition/data.js +5 -1
  87. package/libx/_runtime/common/generic/auth/utils.js +3 -3
  88. package/libx/_runtime/common/generic/crud.js +1 -1
  89. package/libx/_runtime/common/generic/input.js +4 -24
  90. package/libx/_runtime/common/generic/paging.js +10 -9
  91. package/libx/_runtime/common/utils/cqn2cqn4sql.js +31 -0
  92. package/libx/_runtime/common/utils/csn.js +21 -15
  93. package/libx/_runtime/common/utils/draft.js +2 -1
  94. package/libx/_runtime/common/utils/resolveView.js +27 -4
  95. package/libx/_runtime/common/utils/rewriteAsterisks.js +3 -1
  96. package/libx/_runtime/common/utils/rowUUIDGenerator.js +21 -0
  97. package/libx/_runtime/common/utils/templateProcessor.js +12 -15
  98. package/libx/_runtime/common/utils/templateProcessorPathSerializer.js +23 -0
  99. package/libx/_runtime/db/expand/expandCQNToJoin.js +29 -12
  100. package/libx/_runtime/db/generic/input.js +7 -13
  101. package/libx/_runtime/db/sql-builder/InsertBuilder.js +5 -1
  102. package/libx/_runtime/db/sql-builder/UpsertBuilder.js +24 -0
  103. package/libx/_runtime/db/sql-builder/annotations.js +6 -3
  104. package/libx/_runtime/db/sql-builder/index.js +2 -0
  105. package/libx/_runtime/db/sql-builder/sqlFactory.js +9 -0
  106. package/libx/_runtime/db/utils/columns.js +4 -2
  107. package/libx/_runtime/fiori/generic/read.js +1 -12
  108. package/libx/_runtime/fiori/lean-draft.js +657 -0
  109. package/libx/_runtime/fiori/utils/handler.js +1 -1
  110. package/libx/_runtime/hana/Service.js +1 -1
  111. package/libx/_runtime/hana/execute.js +5 -5
  112. package/libx/_runtime/hana/pool.js +16 -1
  113. package/libx/_runtime/messaging/enterprise-messaging-utils/getTenantInfo.js +2 -1
  114. package/libx/_runtime/messaging/enterprise-messaging-utils/registerEndpoints.js +1 -1
  115. package/libx/_runtime/messaging/enterprise-messaging.js +2 -3
  116. package/libx/_runtime/messaging/outbox/utils.js +109 -70
  117. package/libx/_runtime/messaging/service.js +16 -7
  118. package/libx/_runtime/remote/Service.js +15 -2
  119. package/libx/_runtime/remote/utils/client.js +41 -11
  120. package/libx/_runtime/sqlite/Service.js +4 -1
  121. package/libx/_runtime/sqlite/convertDraftAdminPathExpression.js +56 -0
  122. package/libx/_runtime/sqlite/customBuilder/CustomUpsertBuilder.js +41 -0
  123. package/libx/_runtime/sqlite/customBuilder/index.js +5 -0
  124. package/libx/_runtime/sqlite/execute.js +1 -1
  125. package/libx/_runtime/types/api.js +2 -2
  126. package/libx/rest/RestAdapter.js +15 -13
  127. package/package.json +1 -1
  128. package/server.js +2 -19
package/CHANGELOG.md CHANGED
@@ -4,6 +4,100 @@
4
4
  - The format is based on [Keep a Changelog](http://keepachangelog.com/).
5
5
  - This project adheres to [Semantic Versioning](http://semver.org/).
6
6
 
7
+ ## Version 6.4.1 - 2022-01-16
8
+
9
+ ### Fixed
10
+
11
+ - `cds build` correctly creates a `resources.tgz` file for MTXS projects on Windows
12
+ - `cds.deploy` for HANA now doesn't try to search for a globally installed `@sap/hdi-deploy` if there's no `npm` installed, e.g. on a Node.js server without `npm`.
13
+ - Signature for `cds.ql.UPSERT`
14
+ - Signature for `<srv>.delete().where()`
15
+ - Signature for `SELECT.alias`
16
+ - `UPSERT` requests for SQLite if only keys are provided
17
+ - `cds.test` doesn't log database resets with `autoReset` enabled any more.
18
+ - The `cds.deploy` output for HANA is now correctly formatted in Kibana.
19
+ - SAP HANA stored procedures containing implicit selects
20
+ - Shorthand configuration for `graphql` in `cds.env.protocols`
21
+ - If `cds.env.protocols` is set, `cds.requires.middlewares` is automatically turned on
22
+ - `cds.context` middleware is split to initial handling of request correlation and user propagation
23
+ - fix view resolving and managed data for UPSERT
24
+ - `cds.linked` supports polymorphic self links like in: `action foo( self: [many] $self, ...)`
25
+
26
+ ## Version 6.4.0 - 2022-12-15
27
+
28
+ ### Added
29
+
30
+ - Signatures for `ql.UPSERT`
31
+ - Runtime support for flat database UPSERTs
32
+ - `enterprise-messaging`: Support for `@sap/cds-mtxs`
33
+ - `persistent-outbox`: Support for parallel processing with option `parallel: true` as well as pluggable processor functions through `service.outbox.process` (beta)
34
+ - `cds version` now also lists packages with `@cap-js/` prefix from dependencies
35
+ - `cds.context.http` is now available for webhook-based requests
36
+ - `cds build` for HANA migration tables now only saves model entities annotated with `@cds.persistence.journal` as `last-dev` version.
37
+ - `cds deploy` now uses the `VCAP_SERVICES` environment variable (if set), and skips `cf` operations in this case
38
+
39
+ ### Changed
40
+
41
+ - `.columns(…)` of both `cql.SELECT` and `cql.INSERT` give improved code completion when paired when appropriate type definitions are present
42
+ - Status code of error messages caused by empty `not null` fields is changed from `400` to `500` on database layer. Note, that as result the error message `Value is required` in production will be replaced by `Internal server error` in the HTTP response.
43
+ - Underscores in environment variables can now be escaped by two `__` to set keys in `cds.env`. For example, use `CDS_FEATURES_WITH__MOCKS=...` to set `features.with_mocks`. Note that previously, this ended up as `features: { with_: { mocks:... }}`, so in rare cases, it might yield unexpected results.
44
+ - `persistent-outbox`: Only programming errors lead to `process.exit()`, unrecoverable ones are only logged and their `attempts` are updated to `options.maxAttempts`
45
+ - Global configuration of CSRF-token handling for remote services `cds.env.features.fetch_csrf` is deprecated. Instead, please use `csrf: true/false` and `csrfInBatch: true/false` in the configuration of your remote services. These options will allow to configure CSRF-token handling for each remote service separately.
46
+ - `cds build` no longer creates `engines` entry in package.json in case none has been defined. The Node.js version matched the minimal supported version of the CDS runtime and might therefore already be outdated.
47
+ - For streamlined MTX, `@sap/cds` now uses the custom SAP Service Manager client built into `@sap/cds-mtxs`
48
+ - Require new package `@cap-js/graphql` for `cds compile -2 graphql`
49
+
50
+ ### Fixed
51
+
52
+ - Type `expr` of CQN can now describe function calls
53
+ - Added missing optional `options` parameter in signature of `cds.serve`
54
+ - `cds deploy` handles empty result from `cf` call correctly
55
+ - When using CQL projections with array types, the type of the projection variable will now be correctly inferred as a single element of that array
56
+ - `cds deploy` logs without label
57
+ - Localized draft requests with nested expand in the case of `to many` now return localized data
58
+ - Protected service root for REST adapter
59
+ - `cds build` logs tar error message
60
+ - Resolve view only with renamed fields in orderBy case
61
+ - In case of `$apply` no default values for `top` or `skip` are set in subselects
62
+ - Environment variables like `cds.requires.<service>.kind...` now consistently override services set in e.g. `package.json`
63
+ - Expand of composition backlink now accesses draft instance instead of active
64
+ - Performance of server bootstrap for services with lots of entities
65
+ - Error with virtual properties in expand combined with draft
66
+ - req.tenant is properly propagated by custom authentication in odata and rest
67
+ - Error with `SiblingEntity` in draft
68
+ - Quoting of keys typed as `cds.String` in error targets. Error targets are a relative resource path to correlate error
69
+ messages with the corresponding text input filed in the UI in an OData HTTP error response body for errors, warnings,
70
+ and info messages. For example:
71
+
72
+ ```diff
73
+ HTTP/1.1 400 Bad Request
74
+ OData-Version: 4.0
75
+ content-type: application/json;odata.metadata=minimal
76
+ Connection: close
77
+ Content-Length: 145
78
+
79
+ {
80
+ "error": {
81
+ "code": "400",
82
+ "message": "Value is required",
83
+ - "target": "items(ID=string-key-1)/text",
84
+ + "target": "items(ID='string-key-1')/text",
85
+ "@Common.numericSeverity": 4
86
+ }
87
+ }
88
+ ```
89
+
90
+ - Application crash if batched Uri uses invalid percent encoding
91
+ - `cds build` for HANA no longer produces `hdbtabledata` for csv files that refer to non-existing entities. This can be the case for imported content packages that bring csv files for entities that are not used in the application model.
92
+ - `cds build` for HANA now gives precedence for csv files from application layer. This is important for imported content packages that bring csv files that shall be overwritten in the application layer.
93
+ - `cds build` for Java no longer adds the service `cds.xt.MTXServices` to the application model.
94
+ - `cds build` no longer fails when creating large resource TAR archives for MTXS projects.
95
+
96
+ ### Removed
97
+
98
+ - Removed `PreconditionFailedError` when sending a request with an `if-match` header on an entity without an etag
99
+ - Check for forbidden deep operations for associations on database layer. All non key fields for associations provided by request are ignored. Please check https://cap.cloud.sap/docs/guides/providing-services#associations-vs-compositions for more information. Note that this is a breaking change for applications that rely on checks for forbidden deep operations by runtime.
100
+
7
101
  ## Version 6.3.2 - 2022-11-21
8
102
 
9
103
  ### Fixed
@@ -57,6 +151,7 @@
57
151
  ### Changed
58
152
 
59
153
  - In `enterprise-messaging`, emitting CloudEvents messages sets the HTTP header `Content-Type: application/cloudevents+json`
154
+ - Added type definition for `cds.User`
60
155
 
61
156
  ### Fixed
62
157
 
package/apis/cds.d.ts CHANGED
@@ -16,14 +16,16 @@ declare global {
16
16
  // these provide the functionality from SELECT, INSERT, etc in the global facade
17
17
  const SELECT: typeof cds.ql.SELECT
18
18
  const INSERT: typeof cds.ql.INSERT
19
+ const UPSERT: typeof cds.ql.UPSERT
19
20
  const UPDATE: typeof cds.ql.UPDATE
20
21
  const DELETE: typeof cds.ql.DELETE
21
22
  const CREATE: typeof cds.ql.CREATE
22
23
  const DROP: typeof cds.ql.DROP
23
24
 
24
25
  // and these allow us to use them as type too, i.e. `const q: SELECT<Book> = ...`
25
- type SELECT<T> = ql.SELECT<T>
26
+ type SELECT<T> = ql.SELECT<T>
26
27
  type INSERT<T> = ql.INSERT<T>
28
+ type UPSERT<T> = ql.UPSERT<T>
27
29
  type UPDATE<T> = ql.UPDATE<T>
28
30
  type DELETE<T> = ql.DELETE<T>
29
31
  type CREATE<T> = ql.CREATE<T>
package/apis/core.d.ts CHANGED
@@ -1,108 +1,136 @@
1
1
  // import types from '../lib/core'
2
2
  import { ReflectedModel, LinkedModel, LinkedDefinition } from './reflect'
3
- import { CSN as csn, Definition } from "./csn"
3
+ import { CSN as csn, Definition } from './csn'
4
4
 
5
- export = cds
6
- declare class cds {
5
+ type UserInput = string | { id: string; attr: Record<string, string>; roles: Record<string, string> } | User
7
6
 
8
- /**
9
- * Turns the given plain CSN model into a linked model
10
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
11
- */
12
- linked (model : csn) : LinkedModel
13
-
14
- /**
15
- * Turns the given plain CSN model into a reflected model
16
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
17
- */
18
- reflect (model : csn) : ReflectedModel
7
+ declare class User {
8
+ constructor(obj?: UserInput)
9
+ id: string
10
+ /**
11
+ * @deprecated Use https://cap.cloud.sap/docs/node.js/events#locale instead
12
+ */
13
+ locale: string
14
+ /**
15
+ * @deprecated Use https://cap.cloud.sap/docs/node.js/events#tenant instead
16
+ */
17
+ tenant: string | undefined
18
+ attr: Record<string, string>
19
+ roles: Record<string, string>
20
+ static Privileged: typeof Privileged
21
+ is(role: string): boolean
22
+ }
23
+ /**
24
+ * Subclass for executing code with superuser privileges.
25
+ */
26
+ declare class Privileged extends User {
27
+ constructor()
28
+ is(): boolean
29
+ }
19
30
 
31
+ export = cds
32
+ declare class cds {
33
+ /**
34
+ * Turns the given plain CSN model into a linked model
35
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
36
+ */
37
+ linked(model: csn): LinkedModel
20
38
 
21
- // infer (query : cqn, model : csn) : LinkedDefinition
39
+ /**
40
+ * Turns the given plain CSN model into a reflected model
41
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect)
42
+ */
43
+ reflect(model: csn): ReflectedModel
22
44
 
23
- builtin: {
24
- /**
25
- * Base classes of linked definitions from reflected models.
26
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
27
- */
28
- classes: {
29
- Association : Definition
30
- Composition : Definition
31
- context : Definition
32
- service : Definition
33
- type : Definition
34
- array : Definition
35
- struct : Definition
36
- entity : Definition
37
- event : Definition
38
- },
39
- types: {},
40
- }
45
+ /**
46
+ * Represents the user in a given context.
47
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/authentication#cds-user)
48
+ */
49
+ User: typeof User
41
50
 
42
- /**
43
- * Base class for linked Associations from reflected models.
44
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
45
- */
46
- Association : Definition
51
+ // infer (query : cqn, model : csn) : LinkedDefinition
47
52
 
48
- /**
49
- * Base class for linked Compositions from reflected models.
50
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
51
- */
52
- Composition : Definition
53
+ builtin: {
54
+ /**
55
+ * Base classes of linked definitions from reflected models.
56
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
57
+ */
58
+ classes: {
59
+ Association: Definition
60
+ Composition: Definition
61
+ context: Definition
62
+ service: Definition
63
+ type: Definition
64
+ array: Definition
65
+ struct: Definition
66
+ entity: Definition
67
+ event: Definition
68
+ }
69
+ types: {}
70
+ }
53
71
 
72
+ /**
73
+ * Base class for linked Associations from reflected models.
74
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
75
+ */
76
+ Association: Definition
54
77
 
55
- /**
56
- * Base class for linked entities from reflected models.
57
- * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
58
- */
59
- entity : Definition
78
+ /**
79
+ * Base class for linked Compositions from reflected models.
80
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
81
+ */
82
+ Composition: Definition
60
83
 
61
- struct : Definition
62
- array : Definition
63
- context : Definition
64
- service : Definition
84
+ /**
85
+ * Base class for linked entities from reflected models.
86
+ * @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
87
+ */
88
+ entity: Definition
65
89
 
66
- /**
67
- * Add aspects to a given object, for example:
68
- *
69
- * extend (Object.prototype) .with (class {
70
- * get foo() { return ... }
71
- * bar() {...}
72
- * }.prototype)
73
- */
74
- extend <T> (target:T) : ({
75
- with <X,Y,Z> (x:X, y:Y, z:Z): ( T & X & Y & Z )
76
- with <X,Y> (x:X, y:Y): ( T & X & Y )
77
- with <X> (x:X): ( T & X )
78
- })
90
+ struct: Definition
91
+ array: Definition
92
+ context: Definition
93
+ service: Definition
79
94
 
80
- /**
81
- * Equip a given facade object with getters for lazy-loading modules instead
82
- * of static requires. Example:
83
- *
84
- * const facade = lazify ({
85
- * sub: lazy => require ('./sub-module')
86
- * })
87
- *
88
- * The first usage of `facade.sub` will load the sub module
89
- * using standard Node.js's `module.require` functions.
90
- */
91
- lazify : <T>(target:T) => T
95
+ /**
96
+ * Add aspects to a given object, for example:
97
+ *
98
+ * extend (Object.prototype) .with (class {
99
+ * get foo() { return ... }
100
+ * bar() {...}
101
+ * }.prototype)
102
+ */
103
+ extend<T>(target: T): {
104
+ with<X, Y, Z>(x: X, y: Y, z: Z): T & X & Y & Z
105
+ with<X, Y>(x: X, y: Y): T & X & Y
106
+ with<X>(x: X): T & X
107
+ }
92
108
 
93
- /**
94
- * Prepare a node module for lazy-loading submodules instead
95
- * of static requires. Example:
96
- *
97
- * require = lazify (module) //> turns require into a lazy one
98
- * const facade = module.exports = {
99
- * sub: require ('./sub-module')
100
- * })
101
- *
102
- * The first usage of `facade.sub` will load the sub module
103
- * using standard Node.js's `module.require` functions.
104
- */
105
- lazified : <T>(target:T) => T
109
+ /**
110
+ * Equip a given facade object with getters for lazy-loading modules instead
111
+ * of static requires. Example:
112
+ *
113
+ * const facade = lazify ({
114
+ * sub: lazy => require ('./sub-module')
115
+ * })
116
+ *
117
+ * The first usage of `facade.sub` will load the sub module
118
+ * using standard Node.js's `module.require` functions.
119
+ */
120
+ lazify: <T>(target: T) => T
106
121
 
122
+ /**
123
+ * Prepare a node module for lazy-loading submodules instead
124
+ * of static requires. Example:
125
+ *
126
+ * require = lazify (module) //> turns require into a lazy one
127
+ * const facade = module.exports = {
128
+ * sub: require ('./sub-module')
129
+ * })
130
+ *
131
+ * The first usage of `facade.sub` will load the sub module
132
+ * using standard Node.js's `module.require` functions.
133
+ */
134
+ lazified: <T>(target: T) => T
107
135
  }
108
136
  // & typeof import ('../lib/index')
package/apis/cqn.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type Query = SELECT | INSERT | UPDATE | DELETE | CREATE | DROP
1
+ export type Query = SELECT | INSERT | UPDATE | DELETE | CREATE | DROP | UPSERT
2
2
  export type ParsedExpr = expr & { _:string }
3
3
 
4
4
  export type SELECT = {SELECT:{
@@ -22,6 +22,14 @@ export type INSERT = {INSERT:{
22
22
  rows : any[]
23
23
  }}
24
24
 
25
+ export type UPSERT = {UPSERT:{
26
+ into : name
27
+ entries : any[]
28
+ columns : string[]
29
+ values : any[]
30
+ rows : any[]
31
+ }}
32
+
25
33
  export type UPDATE = {UPDATE:{
26
34
  entity : name
27
35
  data : { [key:string] : expr }
@@ -47,12 +55,13 @@ export type column_expr = expr & { as?: name, cast?:any, expand?: column_expr[],
47
55
  export type predicate = _xpr
48
56
  type ordering_term = expr & { sort?: "asc"|"desc", nulls?: "first"|"last" }
49
57
 
50
- export type expr = ref | val | xpr | SELECT
58
+ export type expr = ref | val | xpr | function_call | SELECT
51
59
  type ref = {ref:( name & { id?:string, where?:expr, args?:expr[] } )[]}
52
60
  type val = {val:any}
53
61
  type xpr = {xpr:_xpr}
54
62
  type _xpr = ( expr | operator ) []
55
63
  type operator = string
64
+ type function_call = {func: string, args: {[key: string]: unknown}[]}
56
65
 
57
66
  export type enum_literal = {"#": string}
58
67
  export type expr_literal = {"=": string}
@@ -2,13 +2,18 @@
2
2
  // They are merely meant as definitions that are used
3
3
  // in several places within the API.
4
4
 
5
+
6
+ export interface Constructable<T> {
7
+ new(...args: any[]): T
8
+ }
9
+
5
10
  // any class (not value) of array to represent plural types used in cds-typer.
6
11
  // Mainly used as pattern match for SingularType
7
12
  //type ArrayConstructable = Constructable<Array<unknown>>
8
- export interface ArrayConstructable<T = any[]> {
13
+ export interface ArrayConstructable<T = any> {
9
14
  new(...args: any[]): T[]
10
15
  }
11
16
 
12
17
  // concrete singular type.
13
- // `SingularType<Books>` == `Book`.
18
+ // `SingularType<typeof Books>` == `Book`.
14
19
  export type SingularType<T extends ArrayConstructable<T>> = InstanceType<T>[number]
package/apis/ql.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import {Definition} from "./csn"
2
2
  import * as CQN from "./cqn"
3
- import { ArrayConstructable, SingularType } from "./internal/inference"
3
+ import { Constructable, ArrayConstructable, SingularType } from "./internal/inference"
4
4
 
5
5
  export type Query = CQN.Query
6
6
 
@@ -8,10 +8,6 @@ export class ConstructedQuery {
8
8
  then(_resolved:(x:any)=>any, _rejected:(e:Error)=>any) : any
9
9
  }
10
10
 
11
- interface Constructable<T> {
12
- new(...args: any[]): T
13
- }
14
-
15
11
  export class cds_ql {
16
12
  ql:QL<any> & ((context:object) => QL<any>)
17
13
  }
@@ -36,7 +32,12 @@ interface QLExtensions {
36
32
  */
37
33
  type Subqueryable <T> = ((fn:((a:T)=>any)|"*") => T)
38
34
 
39
- type Proxy_<T> = {
35
+
36
+ /**
37
+ * Wrap every attribute into a proxy that supports operations
38
+ * in addition to its regular capabilities.
39
+ */
40
+ type ProxiedAttributes<T> = {
40
41
  [Key in keyof T]: Proxy<T[Key]> //& QLExtensions
41
42
  } & {
42
43
  /**
@@ -49,15 +50,23 @@ type Proxy_<T> = {
49
50
  get: (path: string) => any // Proxy<unknown>
50
51
  } & QLExtensions
51
52
 
52
- export type Proxy<T> = (T extends Subqueryable<infer U>
53
- ? (Omit<T, ""> & Subqueryable<Proxy<U>>) // drop ((x: T) => T) in favour of (x: Proxy<T>) => Proxy<T>)
54
- : (Proxy_<T>))
53
+ /**
54
+ * Entrance type for Proxies. The conditional makes sure that we don't
55
+ * wrap properties into multiple layers of proxy.
56
+ * Proxy_ was required as we can't circularily reference a type during its definition.
57
+ * So `Proxy<T> = T extends Proxy<any> ...` was not possible.
58
+ */
59
+ export type Proxy<T> = T extends Proxy_<any> ? T : Proxy_<T>
60
+
61
+ export type Proxy_<T> = (T extends Subqueryable<infer U>
62
+ ? (Omit<T, ""> & Subqueryable<Proxy_<U>>) // drop ((x: T) => T) in favour of (x: Proxy<T>) => Proxy<T>)
63
+ : (ProxiedAttributes<T>))
55
64
  & QLExtensions
56
65
 
57
66
  // Alias for projections
58
67
  // https://cap.cloud.sap/docs/node.js/cds-ql?q=projection#projection-functions
59
- export type Projection<T> = (e:T)=>void
60
-
68
+ //export type Projection<T> = (e:T)=>void
69
+ export type Projection<T> = (e:Proxy<T extends ArrayConstructable ? SingularType<T> : T>)=>void
61
70
  // Type for query pieces that can either be chained to build more complex queries or
62
71
  // awaited to materialise the result:
63
72
  // `Awaitable<SELECT<Book>, Book> = SELECT<Book> & Promise<Book>`
@@ -75,6 +84,7 @@ export type Awaitable<T, I> = T & Promise<I>
75
84
  // all the functionality of an instance of SELECT, but directly callable:
76
85
  // new SELECT(...).(...) == SELECT(...)
77
86
  export type StaticSELECT<T> = typeof SELECT
87
+ & ((...columns: (T extends ArrayConstructable<any> ? keyof SingularType<T> : keyof T)[]) => SELECT<T>)
78
88
  & ((...columns:string[]) => SELECT<T>)
79
89
  & ((columns:string[]) => SELECT<T>)
80
90
  & (TaggedTemplateQueryPart<SELECT<T>>)
@@ -85,6 +95,8 @@ declare class QL<T> {
85
95
  SELECT : StaticSELECT<T>
86
96
  INSERT : typeof INSERT
87
97
  & ((...entries:object[]) => INSERT<any>) & ((entries:object[]) => INSERT<any>)
98
+ UPSERT: typeof UPSERT
99
+ & ((...entries:object[]) => UPSERT<any>) & ((entries:object[]) => UPSERT<any>)
88
100
  UPDATE : typeof UPDATE
89
101
  & typeof UPDATE.entity
90
102
  DELETE : typeof DELETE
@@ -108,6 +120,7 @@ export class SELECT<T> extends ConstructedQuery {
108
120
  byKey (primaryKey? : PK) : this
109
121
  columns: TaggedTemplateQueryPart<this>
110
122
  & ((projection: Projection<T>) => this)
123
+ & ((...col: (T extends ArrayConstructable<any> ? keyof SingularType<T> : keyof T)[]) => this)
111
124
  & ((...col:string[]) => this)
112
125
  where: TaggedTemplateQueryPart<this>
113
126
  & ((predicate:object) => this)
@@ -126,6 +139,8 @@ export class SELECT<T> extends ConstructedQuery {
126
139
  & ((rows : number, offset? : number) => this)
127
140
  forShareLock () : this
128
141
  forUpdate ({wait}? : {wait?: number}) : this
142
+ alias (as: string) : this
143
+
129
144
 
130
145
  // Not yet public
131
146
  // fullJoin (other: string, as: string) : this
@@ -190,12 +205,35 @@ export class INSERT<T> extends ConstructedQuery {
190
205
  & ((entity : Definition | string) => this)
191
206
  data (block : (e:T)=>void) : this
192
207
  entries (...entries : object[]) : this
208
+ columns (...col: (T extends ArrayConstructable<any> ? keyof SingularType<T> : keyof T)[]) : this
193
209
  columns (...col: string[]) : this
194
210
  values (... val: any[]) : this
195
211
  rows (... row: any[]) : this
196
212
  INSERT : CQN.INSERT
197
213
  }
198
214
 
215
+
216
+ export class UPSERT<T> extends ConstructedQuery {
217
+ static into : (<T extends ArrayConstructable<any>> (entity:T, entries? : object | object[]) => UPSERT<SingularType<T>>)
218
+ & (TaggedTemplateQueryPart<UPSERT<unknown>>)
219
+ & ((entity : Definition | string, entries? : object | object[]) => UPSERT<any>)
220
+ & (<T> (entity:Constructable<T>, entries? : object | object[]) => UPSERT<T>)
221
+ & (<T> (entity:T, entries? : T | object | object[]) => UPSERT<T>)
222
+
223
+ into: (<T extends ArrayConstructable> (entity:T) => this)
224
+ & TaggedTemplateQueryPart<this>
225
+ & ((entity : Definition | string) => this)
226
+ data (block : (e:T)=>void) : this
227
+ entries (...entries : object[]) : this
228
+ columns (...col: (T extends ArrayConstructable<any> ? keyof SingularType<T> : keyof T)[]) : this
229
+ columns (...col: string[]) : this
230
+ values (... val: any[]) : this
231
+ rows (... row: any[]) : this
232
+ UPSERT : CQN.UPSERT
233
+ }
234
+
235
+
236
+
199
237
  export class DELETE<T> extends ConstructedQuery {
200
238
  static from:
201
239
  TaggedTemplateQueryPart<Awaitable<SELECT<unknown>, InstanceType<any>>>
package/apis/serve.d.ts CHANGED
@@ -19,6 +19,13 @@ interface _fluent {
19
19
  // (req,res) : void
20
20
  }
21
21
 
22
+ // these are temporary types until the API has been properly typed.
23
+ interface service_options {
24
+ service?: string | unknown,
25
+ from?: '*' | 'all' | string | unknown,
26
+ [key: string]: unknown
27
+ }
28
+
22
29
  export = cds_serve
23
30
  declare class cds_serve {
24
31
 
@@ -26,7 +33,7 @@ declare class cds_serve {
26
33
  * Constructs service providers from respective service definitions
27
34
  * @see [capire](https://cap.cloud.sap/docs/node.js/api#cds-serve)
28
35
  */
29
- serve (service : string) : _fluent & Promise<cds_services>
36
+ serve (service : string, options?: service_options) : _fluent & Promise<cds_services>
30
37
 
31
38
  /**
32
39
  * The default bootstrap function as loaded from server.js