Skip to content

Commit

Permalink
Fix services test
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Dec 8, 2023
1 parent 593d912 commit f0f7f17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
77 changes: 43 additions & 34 deletions apis/services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CSN } from './csn'
import { EventContext } from './events'
import { Request } from './events'


export class QueryAPI {

entities : LinkedCSN['entities']
Expand Down Expand Up @@ -59,20 +60,10 @@ export class QueryAPI {
(query: string, args?: any[] | object): Promise<ResultSet | any>
}

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
*/
delete<T>(entity: LinkedDefinition | string, key?: any): DELETE<T>

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/core-services#srv-foreach-entity)
*/
foreach(query: Query, callback: (row: object) => void): this

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/core-services#srv-stream-column)
*/
stream: {
stream: { // tl
(column: string): {
from(entity: LinkedDefinition | string): {
where(filter: any): ReadableStream
Expand All @@ -82,35 +73,20 @@ export class QueryAPI {
}

/**
* Starts or joins a transaction
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx)
* @see [docs](https://cap.cloud.sap/docs/node.js/core-services#crud-style-api)
*/
delete<T>(entity: LinkedDefinition | string, key?: any): DELETE<T> // tl

tx: {
(fn: (tx: Transaction) => {}): Promise<unknown>
(context?: object): Transaction
(context: object, fn: (tx: Transaction) => {}): Promise<unknown>
}
/**
* @see [docs](https://cap.cloud.sap/docs/node.js/core-services#srv-foreach-entity)
*/
foreach(query: Query, callback: (row: object) => void): this // tl

transaction: {
transaction: { // tl
(fn: (tx: Transaction) => {}): Promise<unknown>
(context?: object): Transaction
(context: object, fn: (tx: Transaction) => {}): Promise<unknown>
}

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#cds-spawn)
*/
spawn(options: {
[key: string]: any
every?: number
after?: number
}, fn: (tx: Transaction) => {}): SpawnEventEmitter

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#event-contexts
*/
context?: EventContext
}


Expand All @@ -128,6 +104,11 @@ export class Service extends QueryAPI {
}
)

/**
* The kind of the service
*/
kind: string

/**
* The name of the service
*/
Expand Down Expand Up @@ -383,6 +364,34 @@ declare namespace types {
type target = string | LinkedDefinition | LinkedEntity | (string | LinkedDefinition | LinkedEntity)[] | ArrayConstructable<any>
}

type SpawnOptions = {
[key: string]: any
every?: number
after?: number
}

// FIXME: this was ?: EventContext before. Is context supposed to not be present sometimes?
// let, as apparently we can reassign?
/**
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#event-contexts
*/
export let context: EventContext

/**
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx#cds-spawn)
*/
export function spawn(options: SpawnOptions, fn: (tx: Transaction) => {}): SpawnEventEmitter

/**
* Starts or joins a transaction
* @see [docs](https://cap.cloud.sap/docs/node.js/cds-tx)
*/
export const tx: {
(fn: (tx: Transaction) => {}): Promise<unknown>
(context?: object): Transaction
(context: object, fn: (tx: Transaction) => {}): Promise<unknown>
}

// facade proxies into cds.db, which is a Service
export const tx: Service['tx']
export const entities: Service['entities']
Expand All @@ -397,5 +406,5 @@ export const update: Service['update']
// export const delete: Service['delete']
export const disconnect: Service['disconnect']
export const transaction: Service['transaction']
export const db: Service
export const db: DatabaseService
//export const upsert: Service['upsert']
9 changes: 5 additions & 4 deletions test/typescript/apis/project/cds-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ await cds.update(Books, 'ID')
await cds.delete(Books)
await cds.delete(Books, 'ID')
await cds.delete(Books).where({ id: 123 })
await cds.upsert({}).into(Books)
// GAP: has to be added in runtime, then types, then re-enable this test
// await cds.upsert({}).into(Books)

// as alias to the query methods
let fs1: Foos = await srv.read(Foos)
Expand Down Expand Up @@ -256,20 +257,20 @@ const tx3 = cds.tx (cds.context)
const db = await cds.connect.to('db')
cds.context.features = {foo: true}

db.tx({tenant: 'myTenant'}, async (tx) => { // tx has to be infered from the type defintion to be a Transaction type
cds.tx({tenant: 'myTenant'}, async (tx) => { // tx has to be infered from the type defintion to be a Transaction type
await tx.run('').then(() => {}, () => {})
// code here
}).then(() => {}, () => {})

db.tx(async (tx) => { // tx has to be infered from the type defintion to be a Transaction type
cds.tx(async (tx) => { // tx has to be infered from the type defintion to be a Transaction type
await tx.run('').then(() => {}, () => {})
// code here
}).then(() => {}, () => {})

//tests cds.db
cds.db.kind === "hana"
await cds.db.run ( SELECT.from(Books) )
await cds.db.tx (async (tx) => {
await cds.tx (async (tx) => {
await tx.run(SELECT(1).from(Books,201).forUpdate())
})
cds.db.entities('draftModelAuth')

0 comments on commit f0f7f17

Please sign in to comment.