Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive service functions through connect.to #390

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading