Creating and Deploying a Basic OData service using the SAP Cloud Application Programming Model Model

After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Create, run, export, and deploy a Node.js module saying Hello World
  • Run SQL in the database with Node.js

Creating the Hello World OData service using the SAP Cloud Application Programming Model Model

Watch this video to learn how to create the Hello World OData service using the SAP Cloud Application Programming Model Model.

  • Define a Service :

    We define a service using CDS

  • Implement It :

    We implement the code using example,

    Node.js express.js handlers style

    Node.js es6 classes style.

  • Run It :

    You can run it using command line commands, cds run or cds watch or cds serve world.cds

  • Consume It :

    Consumption is using the local browser http://localhost:4004/say/hello(to='world')

Code Reference

Code snippet
service srv{     function hello (to:String) returns String; } module.exports = class srv {   hello(req) { return `Hello ${}!` } } 
Copy code

Creating the Hello World OData service using the SAP CAP Model

Running SQL in the Database with Node.js

Process Web Requests with Express

Requests to a node service occur via the HTTP protocol. An application such as an SAPUI5 front-end can initiate these requests.

The node module receives the incoming HTTP request via the application router and executes a defined method, depending on the request method and registered application path.

The program can access the information sent in the HTTP request. For example, the parameters, and the request content.

During the server program execution, the HTTP response is prepared. It is then sent back to the requester once the program execution terminates.

Back on the client side, the requester can further process the HTTP response.

Connecting to the Database

To access SAP HANA database content, such as tables, procedures, and views, from a Node.js module, the @sap/hdbext module is used.

During the application deployment, the information in the Application Deployment Descriptor (mtad.yaml) is used to bind the database service instance to the Node.js module.

The binding information is available in the environment variables of the application. The module @sap/xsenv is used to retrieve the service instance from the environment and passed on to the module sap-hdbext.

The module, @sap/hdbext, sends the query to the database and provides a callback function to retrieve the results.

Consume XS Advanced Services

XS Advanced provides a convenient package (@sap/xsenv) for Node.js applications, which can be used to read bound services. To use the @sap/xsenv package, you have to add it to your application's dependencies, which are specified in a corresponding package.json file.

Created Service Bindings

Service bindings for an application can be retrieved, using the command XS ENV <APPLICATIONNAME>.

The attributes of a bound service can be used in a service query.

Connecting to the Database

@sap/hdbext is a small Node.js package, which extends the functionality of the hdb package. hdb is a JavaScript client for Node.js, which implements the SAP HANA database SQL command network protocol.

hdbext.middleware connects to SAP HANA automatically on each access to the specified path, - / in this case.

Afterwards the connection is available in req.db. This is the client object of SAP HANA database driver. The connection is closed automatically at the end of the request.

The result set is passed to the callback function and available in the rows array. In the example shown in the figure above, the first record is sent back to the client via the response object.

Function Implemented as Stored Procedure

We will see the process to Implement a CAP Function from for a Procedure.

  1. Add: function get_supplier_info() returns array of Suppliers; to the service definition. This will expose an OData Function as part of the service interface.
  2. Just adding the function doesn't do anything. We need to use the service handler exit in cat-service.js again to implement the call to the Stored Procedure. This logic will implement the exit handler for this function which in turn uses the standard @sap/hdbext module to call the Stored Procedure from HANA.
  3. We used two additional in our code, We need to add the two HANA modules (sap-hdbext-promisfied and @sap/hdbext) that we used in the code to our root package.json.
  4. As we updated the package.json we will run the npm install to install the dependencies
  5. Run cds build/all
  6. Run cds deploy
  7. Run cds watch
  8. The CAP preview UI doesn't list functions or actions, however. Just click on the /catalog link for the entire service.
  9. Manually add /get_supplier_info() to the end of the URL. If it works correctly it will show the output.

Function Implemented as Stored Procedure

Save progress to your learning plan by logging in or creating an account