In the previous lesson, we created the submitOrder action for the CatalogService. The implementation of this action is not yet complete, as we still lack the knowledge of how to access database tables. We will learn this next.
To implement the submitOrder action, we need the following two database accesses:
- We want to make sure that the current stock for the ordered book is greater than or equal to the ordered quantity. To do this, we need to select the current stock from the database.
- If there are enough books in stock and the order is therefore carried out, we must reduce the current stock. This means that we have to update the stock in the database accordingly.
Fluent API
The cds.ql module provides an SQL-like API for constructing such database queries. The queries required for our scenario are shown in the following figure.

The API is made available via the global objects SELECT, INSERT, UPSERT, UPDATE and DELETE.
Note
Alternatively, you can obtain these objects from cds.ql as follows:const cds = require('@sap/cds');
const { SELECT, INSERT, UPSERT, UPDATE, DELETE } = cds.ql;
read()
, insert()
, upsert()
, update()
and delete()
can be found in the CAP documentation.The API available via the CRUD objects listed is designed as a fluent API. This means that the methods available on these objects return objects of the same type. Available properties - such as the .one property of the SELECT object used in the example shown - also have objects of the same type as their value. This enables chaining, which makes the coding intuitive and readable.
Note
Details on the methods used in the example shown and the available syntax variants can be found in the CAP documentation.To identify the database table to be accessed, we use the CSN definition of the Books entity in the example shown. This is the recommended approach.
Fluent API with Tagged Templates
As an alternative to the classic method calls shown above, the fluent API can also be used with tagged templates. The following figure shows how the queries constructed above using classic method calls can be created using tagged templates. The result is the same in both cases.

Note
Tagged templates are a JavaScript feature. It makes it possible to parse and transform template literals - i.e. literals delimited with backtick characters (`) - using a function. A tagged template looks like a function call in which the function name is placed directly before a template literal without brackets - for example.where`ID=${book}`
.