@cap-js/cds-types 0.13.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 +106 -39
  2. package/package.json +2 -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
@@ -2220,7 +2246,7 @@ type PK = number | string | object
2220
2246
 
2221
2247
  type PluralInstanceType<T extends Constructable> = Array<InstanceType<T>>
2222
2248
 
2223
- export type predicate = UnionsToIntersections<_xpr>
2249
+ export type predicate = _xpr
2224
2250
 
2225
2251
  type Primitive = string | number | boolean | Date
2226
2252
 
@@ -2416,6 +2442,8 @@ type QueryArtefact = {
2416
2442
 
2417
2443
  }
2418
2444
 
2445
+ export const queued: (service: Service) => Service;
2446
+
2419
2447
  export const read: Service['read'];
2420
2448
 
2421
2449
  /** @private */
@@ -2524,8 +2552,6 @@ export const run: Service['run'];
2524
2552
 
2525
2553
  class scalar extends type { }
2526
2554
 
2527
- type Scalarise<A> = A extends Array<infer N> ? N : A
2528
-
2529
2555
  /** @private */
2530
2556
  export type _segment = name | {
2531
2557
  id?: string,
@@ -2563,6 +2589,7 @@ interface SELECT_3<T> extends Where<T>, And, Having<T>, GroupBy, OrderBy<T>, Lim
2563
2589
 
2564
2590
  class SELECT_3<T, Q = SELECT_from> extends ConstructedQuery<T> {
2565
2591
  private constructor();
2592
+ [Symbol.asyncIterator](): AsyncIterableIterator<Unwrap<T>>
2566
2593
 
2567
2594
  static one: SELECT_one & { from: SELECT_one } & { localized: SELECT_one }
2568
2595
 
@@ -2590,6 +2617,32 @@ class SELECT_3<T, Q = SELECT_from> extends ConstructedQuery<T> {
2590
2617
  count?: boolean,
2591
2618
  }
2592
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>
2593
2646
  }
2594
2647
 
2595
2648
  type SELECT_from =
@@ -2650,6 +2703,15 @@ TaggedTemplateQueryPart<Awaitable<SELECT_3<_TODO_2, SELECT_one>, InstanceType<_T
2650
2703
  & (<T> (entity: { new(): T }, primaryKey: PK, projection?: Projection<T>) => Awaitable<SELECT_3<T, SELECT_one>, T | null>)
2651
2704
  & ((subject: ref) => SELECT_3<_TODO_2>)
2652
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
+
2653
2715
  /**
2654
2716
  * Constructs service providers from respective service definitions
2655
2717
  * @see [capire](https://cap.cloud.sap/docs/node.js/cds-serve)
@@ -2741,21 +2803,27 @@ export class Service extends QueryAPI {
2741
2803
  <P extends ArrayConstructable, R>(event: P, data: PropertiesOf<SingularInstanceType<P>>, headers?: object): Promise<R>,
2742
2804
  <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2743
2805
  <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
2744
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2745
2806
  }
2746
2807
 
2747
2808
  /**
2748
2809
  * Constructs and sends a synchronous request.
2749
2810
  * @see [capire docs](https://cap.cloud.sap/docs/node.js/core-services#srv-send-request)
2750
2811
  */
2751
- send: {
2752
- <T = any>(event: types.event, path: string, data?: object, headers?: object): Promise<T>,
2753
- <T = any>(event: types.event, data?: object, headers?: object): Promise<T>,
2754
- <T = any>(details: { event: types.event, data?: object, headers?: object }): Promise<T>,
2755
- <T = any>(details: { query: ConstructedQuery<T>, data?: object, headers?: object }): Promise<T>,
2756
- <T = any>(details: { method: types.eventName, path: string, data?: object, headers?: object }): Promise<T>,
2757
- <T = any>(details: { event: types.eventName, entity: linked_2.Definition | string, data?: object, params?: object, headers?: object }): Promise<T>,
2758
- }
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>
2759
2827
 
2760
2828
  /**
2761
2829
  * Constructs and sends a GET request.
@@ -2791,7 +2859,7 @@ export class Service extends QueryAPI {
2791
2859
  }
2792
2860
 
2793
2861
  // The central method to dispatch events
2794
- dispatch (msg: types.event): Promise<any>
2862
+ dispatch (msg: types.event | Request_2 | types.event[] | Request_2[]): Promise<any>
2795
2863
 
2796
2864
  // FIXME: not yet documented, will come in future version
2797
2865
  // disconnect (tenant?: string): Promise<void>
@@ -2801,8 +2869,8 @@ export class Service extends QueryAPI {
2801
2869
 
2802
2870
  on<T extends ArrayConstructable>(eve: types.event, entity: T | T[], handler: CRUDEventHandler.On<Unwrap<T>>): this
2803
2871
  on<T extends Constructable>(eve: types.event, entity: T | T[], handler: CRUDEventHandler.On<InstanceType<T>>): this
2804
- on<F extends CdsFunction>(boundAction: F, service: string, handler: ActionEventHandler<F['__parameters'], void | Error | F['__returns']>): this
2805
- 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
2806
2874
  on (eve: types.event, entity: types.target, handler: OnEventHandler): this
2807
2875
  on (eve: types.event, handler: OnEventHandler): this
2808
2876
  on (eve: 'error', handler: OnErrorHandler): this
@@ -3147,10 +3215,10 @@ class UInt8 extends Integer { }
3147
3215
 
3148
3216
  export const unboxed: (service: Service) => Service;
3149
3217
 
3150
- type UnionsToIntersections<U> = Array<UnionToIntersection<Scalarise<U>>>
3151
-
3152
3218
  type UnionToIntersection<U> = Partial<(U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never>
3153
3219
 
3220
+ export const unqueued: (service: Service) => Service;
3221
+
3154
3222
  type Unwrap<T> = T extends ArrayConstructable
3155
3223
  ? SingularInstanceType<T>
3156
3224
  : T extends Array<infer U>
@@ -3168,7 +3236,10 @@ type UPDATE_2 = { UPDATE: {
3168
3236
  }, }
3169
3237
  export { UPDATE_2 as UPDATE }
3170
3238
 
3171
- 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
+ }
3172
3243
 
3173
3244
  class UPDATE_3<T> extends ConstructedQuery<T> {
3174
3245
  private constructor();
@@ -3180,11 +3251,7 @@ class UPDATE_3<T> extends ConstructedQuery<T> {
3180
3251
  & ((entity: EntityDescription | ref | Definition_2, primaryKey?: PK) => UPDATE_3<StaticAny>)
3181
3252
  & (<T> (entity: T, primaryKey?: PK) => UPDATE_3<T>)
3182
3253
 
3183
- set: UpdateSet<this, T>
3184
- with: UpdateSet<this, T>
3185
-
3186
3254
  UPDATE: CQN.UPDATE['UPDATE']
3187
-
3188
3255
  }
3189
3256
 
3190
3257
  /**
@@ -3193,9 +3260,9 @@ class UPDATE_3<T> extends ConstructedQuery<T> {
3193
3260
  */
3194
3261
  type UpdateSet<This, T> = TaggedTemplateQueryPart<This>
3195
3262
  // simple value > title: 'Some Title'
3196
- // qbe expression > stock: { '-=': quantity }
3263
+ // qbe expression > stock: { '-=': quantity }
3197
3264
  // cqn expression > descr: {xpr: [{ref:[descr]}, '||', 'Some addition to descr.']}
3198
- & ((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)
3199
3266
 
3200
3267
  type UPSERT_2 = { UPSERT: {
3201
3268
  into: ref | name,
@@ -3211,13 +3278,13 @@ interface UPSERT_3<T> extends Columns<T>, InUpsert<T> {}
3211
3278
  class UPSERT_3<T> extends ConstructedQuery<T> {
3212
3279
  private constructor();
3213
3280
 
3214
- static into: (<T extends ArrayConstructable> (entity: T, ...entries: SingularInstanceType<T>[]) => UPSERT_3<SingularInstanceType<T>>)
3215
- & (<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>>)
3216
3283
  & (TaggedTemplateQueryPart<UPSERT_3<StaticAny>>)
3217
3284
  & ((entity: EntityDescription, ...entries: Entries[]) => UPSERT_3<StaticAny>)
3218
3285
  & ((entity: EntityDescription, entries?: Entries) => UPSERT_3<StaticAny>)
3219
- & (<T> (entity: Constructable<T>, ...entries: T[]) => UPSERT_3<T>)
3220
- & (<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>)
3221
3288
 
3222
3289
  UPSERT: CQN.UPSERT['UPSERT']
3223
3290
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-types",
3
- "version": "0.13.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/",
@@ -51,6 +51,7 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@cap-js/cds-test": "^0",
54
+ "@cap-js/db-service": "^2.3.0",
54
55
  "@microsoft/api-extractor": "^7.52.8",
55
56
  "@sap/cds-dk": "^9",
56
57
  "@stylistic/eslint-plugin-js": "^4.0.1",