Skip to content

Commit

Permalink
Derive service functions through connect.to (#390)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Georgi <[email protected]>
  • Loading branch information
daogrady and chgeo authored Jan 9, 2025
1 parent b26d0c6 commit f6143c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 12 additions & 3 deletions apis/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@ export const connect: {

/**
* Connects to a specific datasource.
* @example cds.connect.to ('service')
* @example await cds.connect.to ('service')
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-connect#cds-connect-to)
*/
to(datasource: string, options?: cds_connect_options): Promise<Service>,

/**
* Shortcut for 'db' as the primary database returning `cds.DatabaseService`
* @example cds.connect.to ('db')
* @example await cds.connect.to ('db')
*/
to(datasource: 'db', options?: cds_connect_options): Promise<cds.DatabaseService>,

/**
* Connects to a specific datasource via a Service subclass
* @example cds.connect.to (ServiceClass)
* @example await cds.connect.to (ServiceClass)
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-connect#cds-connect-to)
*/
to<S extends Service>(datasource: {new(): S}, options?: cds_connect_options): Promise<S>,

/**
* Connects to a specific datasource via a Service class from cds-typer
* @example
* import ServiceClass from '#cds-models/SomeService'
* await cds.connect.to (ServiceClass)
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-connect#cds-connect-to)
*/
to<S>(datasource: S, options?: cds_connect_options): Promise<cds.CdsFunctions<S> & Service>,

/**
* Connects to a specific datasource via options.
* @example cds.connect.to ({ kind:..., impl:... })
Expand Down
6 changes: 6 additions & 0 deletions apis/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ type CdsFunction = {
__returns: any,
}

// extracts all CdsFunction properties from T
/**
* @beta helper
*/
type CdsFunctions<T> = Pick<T, { [K in keyof T]: T[K] extends CdsFunction ? K : never }[keyof T]>

/**
* Types herein can be used to type handler functions that are not declared in line:
* @example
Expand Down

0 comments on commit f6143c8

Please sign in to comment.