This project extends the example cds bookshop project, available here: https://cap.cloud.sap/docs/get-started/
It demonstrates how to to use only persistence layer generated by CAP, not exposing the Resful services for the persisted entities.
In this example we extend cat-service.js
with custom code that inserts and persists Logs in the DB during GET for the Books.
Logs entity and persistence layer is generated by CAP, but no exposure of REST-services is done for the Logs.
File or Folder | Purpose |
---|---|
app/ |
content for UI frontends goes here |
db/ |
your domain models and data go here |
srv/ |
your service models and code go here |
package.json |
project metadata and configuration |
readme.md |
this getting started guide |
- Run
npm run build
to transpile Log.ts file into javascript - Open a new terminal and run
cds watch
- (in VS Code simply choose Terminal > Run Task > cds watch)
- Start adding content, for example, a db/schema.cds.
In order to bootstrap CDS within existing Express application (not having to start the app with cds watch
), we prepared a file app.js, that contains all needed code to bootstrap CDS with express.
This uses cds-bootstrap.js, which contains all needed bootstrap code, as also documented in: https://cap.cloud.sap/docs/node.js/cds-server
node .\app.js
to boostrap the application with Express.
- Test that custom express rest-services are working, opening url:
http://localhost:4004/logs
- it should return logs from DB - Test that CDS-REST-Services are working fine by typing
http://localhost:4004/catalog/Books?$top=11
- it should return content of Books entity in DB - You can open postman collection from and run POST to create a logs and GET to get those logs to see that the Express endpoints are working postman collection
Custom REST services are implemented in the file cat-service
There is a Express REST GET endpoint /logs that reads the Log entity from DB.
There is a Express REST POST endpoint to /logs that writes the Log entity in DB.
Endpoint | REST Method | Sample payload | Comment |
---|---|---|---|
/logs | POST | {'tenantID':'testTenant', 'level':'D'} | Adds a log to the logs table with CDS persistence |
/logs | GET | - | gets logs using CDS persistence |
/catalog/Books?$top=11 | GET | - | consumes CDS OData REST service |
Please note that the /logs endpoint is not registered as service within CDS service catalog
cat-service.cds
, it's just a simple Express endpoint
- open http://localhost:4004/catalog/Books
- The request above will add logs to the Log entity in the DB (see GET code handler in
cat-service.js
) - To test, if log entries have been added, go to DB and type :
sqlite3'
.open db/my-bookshop.db - Alternatively you can open
cat-service.cds
and temporarily stwitch on exposure for Logs entity by adding this line:
entity Logs @readonly as projection on bookshop.Logs;
Then, opening the Logs you should see newly added logs by calling this endpoint:http://localhost:4004/catalog/Logs
Learn more at https://cap.cloud.sap/docs/get-started/.