-
Now we want to add this Procedure to the CAP service as a function. Edit /srv/interaction_srv.cds
.
Add: function sleep() returns Boolean;
to the service definition.
This will expose an OData Function as part of the service interface.
-
Just adding the function to the service definition doesn’t implement it. Unlike tables and views which are implemented by the built-in handler classes of CAP, we need to use a service handler exit to implement this ourselves. In order to do this create a file named interaction_srv.js
in the /srv
folder. The name must match interaction_srv.cds
just with the different file extension for JavaScript. The matching naming tells CAP that you want to use this file to write custom exit handlers for the services defined in interaction_srv.cds
.
-
In interaction_srv.js
we will implement the call to the Stored Procedure. This logic will implement the exit handler for this function which in turn uses the standard hdb
Node.js module to call the Stored Procedure from SAP HANA Cloud. Save the file.
const cds = require('@sap/cds')
module.exports = cds.service.impl(function () {
this.on('sleep', async () => {
try {
let dbQuery = ' Call "sleep"( )'
let result = await cds.run(dbQuery, { })
cds.log().info(result)
return true
} catch (error) {
cds.log().error(error)
return false
}
})
})
-
From the console in the project root hopefully you still have the cds watch ...
running. Otherwise start it again with cds watch --profile hybrid
to start the CAP service layer for testing. If you have performed the tutorial SAP HANA Cloud, Add User Authentication to Your Application, remember you must also run the application router to test your service with authentication.
-
The CAP preview UI doesn’t list functions or actions, however. Manually add /odata/v4/catalog/sleep()
to the end of the URL. If it works correctly it should take 10 seconds to respond since the procedure is running a sleep operation for that long.
You’ve now added an OData function to your service layer which in turn is implemented as an SAP HANA Stored Procedure