@cap-js/cds-types 0.12.0 → 0.14.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 (2) hide show
  1. package/dist/cds-types.d.ts +130 -39
  2. package/package.json +8 -1
@@ -1,6 +1,6 @@
1
1
  declare module '@sap/cds' {
2
2
  export * from "@sap/cds-dk";
3
-
3
+
4
4
 
5
5
  export type __any_ = classes.any_
6
6
 
@@ -66,9 +66,9 @@ export type __Vector = classes.Vector
66
66
 
67
67
  class action extends any_<'action' | 'function'> {}
68
68
 
69
- export interface ActionEventHandler<P, R> {
69
+ export interface ActionEventHandler<S, P, R> {
70
70
  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
71
- (req: Omit<Request_2, 'data'> & { data: P }, next: Function): Promise<R> | R
71
+ (req: Omit<Request_2, 'data'> & { data: P, subject: S }, next: Function): Promise<R> | R
72
72
  }
73
73
 
74
74
  interface And {
@@ -323,6 +323,8 @@ namespace cds {
323
323
  Key,
324
324
  QueryAPI,
325
325
  PropertiesOf,
326
+ Send,
327
+ FluentScheduling,
326
328
  Service,
327
329
  ApplicationService,
328
330
  MessagingService,
@@ -358,6 +360,8 @@ namespace cds {
358
360
  update,
359
361
  transaction,
360
362
  db,
363
+ queued,
364
+ unqueued,
361
365
  outboxed,
362
366
  unboxed,
363
367
  EventContext,
@@ -503,6 +507,8 @@ namespace cds_2 {
503
507
  Key,
504
508
  QueryAPI,
505
509
  PropertiesOf,
510
+ Send,
511
+ FluentScheduling,
506
512
  Service,
507
513
  ApplicationService,
508
514
  MessagingService,
@@ -538,6 +544,8 @@ namespace cds_2 {
538
544
  update,
539
545
  transaction,
540
546
  db,
547
+ queued,
548
+ unqueued,
541
549
  outboxed,
542
550
  unboxed,
543
551
  EventContext,
@@ -612,6 +620,7 @@ export type CdsFunction = {
612
620
  (...args: any[]): any,
613
621
  __parameters: object,
614
622
  __returns: any,
623
+ __self?: any, // the entity the function is bound to, in case of bound functions
615
624
  }
616
625
 
617
626
  /**
@@ -1017,6 +1026,18 @@ class Decimal extends Float {
1017
1026
  scale?: number
1018
1027
  }
1019
1028
 
1029
+ /**
1030
+ * @beta helper
1031
+ */
1032
+ type DeepPartial<T> = T extends object
1033
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1034
+ ? T extends Function
1035
+ ? T
1036
+ : T extends Array<infer U>
1037
+ ? Array<DeepPartial<U>>
1038
+ : { [K in keyof T]?: DeepPartial<T[K]> }
1039
+ : T
1040
+
1020
1041
  /**
1021
1042
  * Definitions are the central elements of a CDS model.
1022
1043
  */
@@ -1373,6 +1394,11 @@ export type _flavor = 'parsed' | 'xtended' | 'inferred'
1373
1394
 
1374
1395
  class Float extends number { }
1375
1396
 
1397
+ export type FluentScheduling<O extends keyof FluentScheduling = never> = {
1398
+ after: <T = any>(t: number | string, u?: string) => Promise<T> & Omit<FluentScheduling<O | 'after'>, O | 'after'>,
1399
+ every: <T = any>(t: number | string, u?: string) => Promise<T> & Omit<FluentScheduling<O | 'every'>, O | 'every'>,
1400
+ }
1401
+
1376
1402
  export const foreach: Service['foreach'];
1377
1403
 
1378
1404
  type Formatter = {
@@ -1417,11 +1443,11 @@ interface GroupBy {
1417
1443
  * @example
1418
1444
  * ```ts
1419
1445
  * import { myAction } from '#cds-models/myService'
1420
- *
1446
+ *
1421
1447
  * function onMyFunction (req: HandlerFunction<typeof myAction>['parameters']['req']): HandlerFunction<typeof myAction>['returns'] {
1422
1448
  * ...
1423
1449
  * }
1424
- *
1450
+ *
1425
1451
  * srv.on(myAction, onMyFunction)
1426
1452
  * ```
1427
1453
  */
@@ -1528,13 +1554,13 @@ interface INSERT_3<T> extends Columns<T>, InUpsert<T> {}
1528
1554
  class INSERT_3<T> extends ConstructedQuery<T> {
1529
1555
  private constructor();
1530
1556
 
1531
- static into: (<T extends ArrayConstructable> (entity: T, ...entries: SingularInstanceType<T>[]) => INSERT_3<SingularInstanceType<T>>)
1532
- & (<T extends ArrayConstructable> (entity: T, entries?: SingularInstanceType<T>[]) => INSERT_3<SingularInstanceType<T>>)
1557
+ static into: (<T extends ArrayConstructable> (entity: T, ...entries: DeepPartial<SingularInstanceType<T>>[]) => INSERT_3<SingularInstanceType<T>>)
1558
+ & (<T extends ArrayConstructable> (entity: T, entries?: DeepPartial<SingularInstanceType<T>>[]) => INSERT_3<SingularInstanceType<T>>)
1533
1559
  & (TaggedTemplateQueryPart<INSERT_3<unknown>>)
1534
1560
  & ((entity: EntityDescription, ...entries: Entries[]) => INSERT_3<StaticAny>)
1535
1561
  & ((entity: EntityDescription, entries?: Entries) => INSERT_3<StaticAny>)
1536
- & (<T> (entity: Constructable<T>, ...entries: T[]) => INSERT_3<T>)
1537
- & (<T> (entity: Constructable<T>, entries?: T[]) => INSERT_3<T>)
1562
+ & (<T> (entity: Constructable<T>, ...entries: DeepPartial<T>[]) => INSERT_3<T>)
1563
+ & (<T> (entity: Constructable<T>, entries?: DeepPartial<T>[]) => INSERT_3<T>)
1538
1564
 
1539
1565
  from (select: SELECT_3<T>): this
1540
1566
  INSERT: CQN.INSERT['INSERT']
@@ -1556,8 +1582,8 @@ type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer T
1556
1582
  interface InUpsert<T> {
1557
1583
  data (block: (e: T) => void): this
1558
1584
 
1559
- entries (...entries: T[]): this
1560
- entries (entries: T[]): this
1585
+ entries (...entries: DeepPartial<T>[]): this
1586
+ entries (entries: DeepPartial<T>[]): this
1561
1587
 
1562
1588
  values (...val: (null | Primitive)[]): this
1563
1589
  values (val: (null | Primitive)[]): this
@@ -2108,6 +2134,24 @@ export function on (event: 'connect', listener: (srv: Service) => void): _cds
2108
2134
  */
2109
2135
  export function on (event: 'bootstrap', listener: (app: import('express').Application) => void): _cds
2110
2136
 
2137
+ /**
2138
+ * Emitted before the model is compiled for usage in Node.js or Java runtime.
2139
+ * @beta
2140
+ */
2141
+ export function on (event: 'compile.for.runtime', listener: (model: CSN) => void): _cds
2142
+
2143
+ /**
2144
+ * Emitted before database-specific artifacts, i.e. SQL DDL scripts, are generated from the model.
2145
+ * @beta
2146
+ */
2147
+ export function on (event: 'compile.to.dbx', listener: (model: CSN) => void): _cds
2148
+
2149
+ /**
2150
+ * Emitted immediately before the model is compiled to edmx.
2151
+ * @beta
2152
+ */
2153
+ export function on (event: 'compile.to.edmx', listener: (model: CSN) => void): _cds
2154
+
2111
2155
  /**
2112
2156
  * Emitted for each service served by cds.serve().
2113
2157
  */
@@ -2133,6 +2177,12 @@ export function on (event: 'shutdown', listener: () => void): _cds
2133
2177
 
2134
2178
  export function once (event: 'bootstrap', listener: (app: import('express').Application) => void): _cds
2135
2179
 
2180
+ export function once (event: 'compile.for.runtime', listener: (model: CSN) => void): _cds
2181
+
2182
+ export function once (event: 'compile.to.dbx', listener: (model: CSN) => void): _cds
2183
+
2184
+ export function once (event: 'compile.to.edmx', listener: (model: CSN) => void): _cds
2185
+
2136
2186
  export function once (event: 'served', listener: (all: cds_services) => void): _cds
2137
2187
 
2138
2188
  export function once (event: 'listening', listener: (args: { server: import('http').Server, url: string }) => void): _cds
@@ -2196,7 +2246,7 @@ type PK = number | string | object
2196
2246
 
2197
2247
  type PluralInstanceType<T extends Constructable> = Array<InstanceType<T>>
2198
2248
 
2199
- export type predicate = UnionsToIntersections<_xpr>
2249
+ export type predicate = _xpr
2200
2250
 
2201
2251
  type Primitive = string | number | boolean | Date
2202
2252
 
@@ -2392,6 +2442,8 @@ type QueryArtefact = {
2392
2442
 
2393
2443
  }
2394
2444
 
2445
+ export const queued: (service: Service) => Service;
2446
+
2395
2447
  export const read: Service['read'];
2396
2448
 
2397
2449
  /** @private */
@@ -2500,8 +2552,6 @@ export const run: Service['run'];
2500
2552
 
2501
2553
  class scalar extends type { }
2502
2554
 
2503
- type Scalarise<A> = A extends Array<infer N> ? N : A
2504
-
2505
2555
  /** @private */
2506
2556
  export type _segment = name | {
2507
2557
  id?: string,
@@ -2539,6 +2589,7 @@ interface SELECT_3<T> extends Where<T>, And, Having<T>, GroupBy, OrderBy<T>, Lim
2539
2589
 
2540
2590
  class SELECT_3<T, Q = SELECT_from> extends ConstructedQuery<T> {
2541
2591
  private constructor();
2592
+ [Symbol.asyncIterator](): AsyncIterableIterator<Unwrap<T>>
2542
2593
 
2543
2594
  static one: SELECT_one & { from: SELECT_one } & { localized: SELECT_one }
2544
2595
 
@@ -2566,6 +2617,32 @@ class SELECT_3<T, Q = SELECT_from> extends ConstructedQuery<T> {
2566
2617
  count?: boolean,
2567
2618
  }
2568
2619
 
2620
+ /**
2621
+ * If a parameter is given, the raw data stream is piped into it.
2622
+ *
2623
+ * If no parameter is given, the raw data stream is returned.
2624
+ * @param stream the writable stream to pipe the raw data into
2625
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/cds-ql#pipeline)
2626
+ * @since 9.3.0
2627
+ */
2628
+ pipeline(stream: import('node:stream').Writable): Promise<void>
2629
+ /**
2630
+ * If a parameter is given, the raw data stream is piped into it.
2631
+ *
2632
+ * If no parameter is given, the raw data stream is returned.
2633
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/cds-ql#pipeline)
2634
+ * @since 9.3.0
2635
+ * @returns Readable
2636
+ */
2637
+ pipeline(): Promise<import('node:stream').Readable>
2638
+
2639
+ /**
2640
+ * Calls the given callback function for each row in the result set.
2641
+ * @param cb the callback function to call for each row
2642
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/cds-ql#foreach)
2643
+ * @since 9.3.0
2644
+ */
2645
+ foreach: (cb: (element: Unwrap<T>) => void) => Promise<void>
2569
2646
  }
2570
2647
 
2571
2648
  type SELECT_from =
@@ -2626,6 +2703,15 @@ TaggedTemplateQueryPart<Awaitable<SELECT_3<_TODO_2, SELECT_one>, InstanceType<_T
2626
2703
  & (<T> (entity: { new(): T }, primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT_3<T, SELECT_one>, T | null>)
2627
2704
  & ((subject: ref) => SELECT_3<_TODO_2>)
2628
2705
 
2706
+ export type Send<AddOn = {}> = {
2707
+ <T = any>(event: types.event, path: string, data?: object, headers?: object): Promise<T> & AddOn,
2708
+ <T = any>(event: types.event, data?: object, headers?: object): Promise<T> & AddOn,
2709
+ <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T> & AddOn,
2710
+ <T = any>(details: { query: ConstructedQuery<T>, data?: object, headers?: object }): Promise<T> & AddOn,
2711
+ <T = any>(details: { method: types.eventName, path: string, data?: object, headers?: object }): Promise<T> & AddOn,
2712
+ <T = any>(details: { event: types.eventName, entity: linked_2.Definition | string, data?: object, params?: object, headers?: object }): Promise<T> & AddOn,
2713
+ }
2714
+
2629
2715
  /**
2630
2716
  * Constructs service providers from respective service definitions
2631
2717
  * @see [capire](https://cap.cloud.sap/docs/node.js/cds-serve)
@@ -2717,21 +2803,27 @@ export class Service extends QueryAPI {
2717
2803
  <P extends ArrayConstructable, R>(event: P, data: PropertiesOf<SingularInstanceType<P>>, headers?: object): Promise<R>,
2718
2804
  <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2719
2805
  <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
2720
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2721
2806
  }
2722
2807
 
2723
2808
  /**
2724
2809
  * Constructs and sends a synchronous request.
2725
2810
  * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#srv-send-request)
2726
2811
  */
2727
- send: {
2728
- <T = any>(event: types.event, path: string, data?: object, headers?: object): Promise<T>,
2729
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2730
- <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
2731
- <T = any>(details: { query: ConstructedQuery<T>, data?: object, headers?: object }): Promise<T>,
2732
- <T = any>(details: { method: types.eventName, path: string, data?: object, headers?: object }): Promise<T>,
2733
- <T = any>(details: { event: types.eventName, entity: linked_2.Definition | string, data?: object, params?: object, headers?: object }): Promise<T>,
2734
- }
2812
+ send: Send
2813
+
2814
+ /**
2815
+ * @alpha
2816
+ * Constructs and schedules a request for asynchronous processing.
2817
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/queue#task-scheduling)
2818
+ */
2819
+ schedule: Send<FluentScheduling>
2820
+
2821
+ /**
2822
+ * @alpha
2823
+ * Triggers task processing.
2824
+ * @see [capire docs](https://cap.cloud.sap/docs/node.js/queue#task-processing)
2825
+ */
2826
+ flush(): Promise<void>
2735
2827
 
2736
2828
  /**
2737
2829
  * Constructs and sends a GET request.
@@ -2767,7 +2859,7 @@ export class Service extends QueryAPI {
2767
2859
  }
2768
2860
 
2769
2861
  // The central method to dispatch events
2770
- dispatch (msg: types.event): Promise<any>
2862
+ dispatch (msg: types.event | Request_2 | types.event[] | Request_2[]): Promise<any>
2771
2863
 
2772
2864
  // FIXME: not yet documented, will come in future version
2773
2865
  // disconnect (tenant?: string): Promise<void>
@@ -2777,8 +2869,8 @@ export class Service extends QueryAPI {
2777
2869
 
2778
2870
  on<T extends ArrayConstructable>(eve: types.event, entity: T | T[], handler: CRUDEventHandler.On<Unwrap<T>>): this
2779
2871
  on<T extends Constructable>(eve: types.event, entity: T | T[], handler: CRUDEventHandler.On<InstanceType<T>>): this
2780
- on<F extends CdsFunction>(boundAction: F, service: string, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
2781
- on<F extends CdsFunction>(unboundAction: F, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
2872
+ on<F extends CdsFunction>(boundAction: F, service: string, handler: ActionEventHandler<F['__self'], F['__parameters'], void | Error | F['__returns']>): this
2873
+ on<F extends CdsFunction>(unboundAction: F, handler: ActionEventHandler<F['__self'], F['__parameters'], void | Error | F['__returns']>): this
2782
2874
  on (eve: types.event, entity: types.target, handler: OnEventHandler): this
2783
2875
  on (eve: types.event, handler: OnEventHandler): this
2784
2876
  on (eve: 'error', handler: OnErrorHandler): this
@@ -3123,10 +3215,10 @@ class UInt8 extends Integer { }
3123
3215
 
3124
3216
  export const unboxed: (service: Service) => Service;
3125
3217
 
3126
- type UnionsToIntersections<U> = Array<UnionToIntersection<Scalarise<U>>>
3127
-
3128
3218
  type UnionToIntersection<U> = Partial<(U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never>
3129
3219
 
3220
+ export const unqueued: (service: Service) => Service;
3221
+
3130
3222
  type Unwrap<T> = T extends ArrayConstructable
3131
3223
  ? SingularInstanceType<T>
3132
3224
  : T extends Array<infer U>
@@ -3144,7 +3236,10 @@ type UPDATE_2 = { UPDATE: {
3144
3236
  }, }
3145
3237
  export { UPDATE_2 as UPDATE }
3146
3238
 
3147
- interface UPDATE_3<T> extends Where<T>, And, ByKey {}
3239
+ interface UPDATE_3<T> extends Where<T>, And, ByKey {
3240
+ set: UpdateSet<this, T>
3241
+ with: UpdateSet<this, T>
3242
+ }
3148
3243
 
3149
3244
  class UPDATE_3<T> extends ConstructedQuery<T> {
3150
3245
  private constructor();
@@ -3156,11 +3251,7 @@ class UPDATE_3<T> extends ConstructedQuery<T> {
3156
3251
  & ((entity: EntityDescription | ref | Definition_2, primaryKey?: PK) => UPDATE_3<StaticAny>)
3157
3252
  & (<T> (entity: T, primaryKey?: PK) => UPDATE_3<T>)
3158
3253
 
3159
- set: UpdateSet<this, T>
3160
- with: UpdateSet<this, T>
3161
-
3162
3254
  UPDATE: CQN.UPDATE['UPDATE']
3163
-
3164
3255
  }
3165
3256
 
3166
3257
  /**
@@ -3169,9 +3260,9 @@ class UPDATE_3<T> extends ConstructedQuery<T> {
3169
3260
  */
3170
3261
  type UpdateSet<This, T> = TaggedTemplateQueryPart<This>
3171
3262
  // simple value > title: 'Some Title'
3172
- // qbe expression > stock: { '-=': quantity }
3263
+ // qbe expression > stock: { '-=': quantity }
3173
3264
  // cqn expression > descr: {xpr: [{ref:[descr]}, '||', 'Some addition to descr.']}
3174
- & ((data: {[P in keyof T]?: T[P] | {[op in QbeOp]?: T[P]} | CQN.xpr}) => This)
3265
+ & ((data: {[P in keyof T]?: T[P] | DeepPartial<T[P]> | {[op in QbeOp]?: DeepPartial<T[P]>} | CQN.xpr}) => This)
3175
3266
 
3176
3267
  type UPSERT_2 = { UPSERT: {
3177
3268
  into: ref | name,
@@ -3187,13 +3278,13 @@ interface UPSERT_3<T> extends Columns<T>, InUpsert<T> {}
3187
3278
  class UPSERT_3<T> extends ConstructedQuery<T> {
3188
3279
  private constructor();
3189
3280
 
3190
- static into: (<T extends ArrayConstructable> (entity: T, ...entries: SingularInstanceType<T>[]) => UPSERT_3<SingularInstanceType<T>>)
3191
- & (<T extends ArrayConstructable> (entity: T, entries?: SingularInstanceType<T>[]) => UPSERT_3<SingularInstanceType<T>>)
3281
+ static into: (<T extends ArrayConstructable> (entity: T, ...entries: DeepPartial<SingularInstanceType<T>>[]) => UPSERT_3<SingularInstanceType<T>>)
3282
+ & (<T extends ArrayConstructable> (entity: T, entries?: DeepPartial<SingularInstanceType<T>>[]) => UPSERT_3<SingularInstanceType<T>>)
3192
3283
  & (TaggedTemplateQueryPart<UPSERT_3<StaticAny>>)
3193
3284
  & ((entity: EntityDescription, ...entries: Entries[]) => UPSERT_3<StaticAny>)
3194
3285
  & ((entity: EntityDescription, entries?: Entries) => UPSERT_3<StaticAny>)
3195
- & (<T> (entity: Constructable<T>, ...entries: T[]) => UPSERT_3<T>)
3196
- & (<T> (entity: Constructable<T>, entries?: T[]) => UPSERT_3<T>)
3286
+ & (<T> (entity: Constructable<T>, ...entries: DeepPartial<T>[]) => UPSERT_3<T>)
3287
+ & (<T> (entity: Constructable<T>, entries?: DeepPartial<T>[]) => UPSERT_3<T>)
3197
3288
 
3198
3289
  UPSERT: CQN.UPSERT['UPSERT']
3199
3290
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-types",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Type definitions for main packages of CAP, like `@sap/cds`",
5
5
  "repository": "github:cap-js/cds-types",
6
6
  "homepage": "https://cap.cloud.sap/",
@@ -44,9 +44,16 @@
44
44
  "@sap/cds-dk": "^9",
45
45
  "@types/express": ">=4"
46
46
  },
47
+ "peerDependenciesMeta": {
48
+ "@sap/cds-dk": {
49
+ "optional": true
50
+ }
51
+ },
47
52
  "devDependencies": {
48
53
  "@cap-js/cds-test": "^0",
54
+ "@cap-js/db-service": "^2.3.0",
49
55
  "@microsoft/api-extractor": "^7.52.8",
56
+ "@sap/cds-dk": "^9",
50
57
  "@stylistic/eslint-plugin-js": "^4.0.1",
51
58
  "@stylistic/eslint-plugin-ts": "^4.0.1",
52
59
  "axios": "^1.6.2",