SAP Cap app in 10 steps
1. Set up local development environment
npm i -g @sap/cds-dk
Install VS Code extension SAP CDS Language Support
2. Start Project
cds init my-bookshop
cd my-bookshop
code .
npm install
4. Add data model schema.cds under /db
Example:
namespace my.bookshop;
using { Country, managed } from ‘@sap/cds/common’;
entity Books {
key ID : Integer;
title : localized String;
author : Association to Authors;
stock : Integer;
}
entity Authors {
key ID : Integer;
name : String;
books : Association to many Books on books.author = $self;
}
entity Orders : managed {
key ID : UUID;
book : Association to Books;
country : Country;
amount : Integer;
}
5. Define the service cat-service.cds under /srv
Example:
using my.bookshop as my from ‘../db/schema’;
service CatalogService {
entity Books @readonly as projection on my.Books;
entity Authors @readonly as projection on my.Authors;
entity Orders @insertonly as projection on my.Orders;
}
6. Add initial data
cds add data
db/data/my.bookshop-Authors.csv
101,Emily Brontë
107,Charlote Brontë
150,Edgar Allen Poe
170,Richard Carpenter
db/data/my.bookshop-Books.csv
201,Wuthering Heights,101,12
207,Jane Eyre,107,11
251,The Raven,150,333
252,Eleonora,150,555
271,Catweazle,170,22
Add persistent database in package.json
{
“name”: “my-bookshop”,
“version”: “1.0.0”,
“description”: “A simple CAP project.”,
“repository”: “<Add your repository here>”,
“license”: “UNLICENSED”,
“private”: true,
“dependencies”: {
“@sap/cds”: “^7”,
“express”: “^4”
},
“devDependencies”: {
“@cap-js/sqlite”: “^1”
},
“scripts”: {
“start”: “cds-serve”
},
“cds”: { “requires”: {
“db”: {
“kind”: “sqlite”,
“credentials”: { “url”: “db/my-bookshop.sqlite” }
}
}}
}
Deploy the data model to an SQLite database
cds deploy
Open SQLite and view the newly created database
sqlite3 db/my-bookshop.sqlite -cmd .dump
7. Run service
cds watch
8. Test generic handlers
Create a file test.http under root
###
#
# Browse Books
#
GET http://localhost:4004/odata/v4/catalog/Books?
# &$select=title,stock
# &$expand=currency
# &sap-language=de
###
#
# Get Author wit ID 101
#
GET http://localhost:4004/odata/v4/catalog/Authors(101)
###
#
# Update Author with ID 101
#
POST http://localhost:4004/odata/v4/catalog/Authors
Content-Type: application/json
{“ID”: 101, “name”: “Some Author”}
###
#
# Order a Book
#
POST http://localhost:4004/odata/v4/catalog/Orders
Content-Type: application/json;IEEE754Compatible=true
{“book_ID”: 201, “amount”: 5}
9. Add custom logic
Create a file cat-service.js under folder /srv
module.exports = (srv) => {
const {Books} = cds.entities (‘my.bookshop’)
// Reduce stock of ordered books
srv.before (‘CREATE’, ‘Orders’, async (req) => {
const order = req.data
if (!order.amount || order.amount <= 0) return req.error (400, ‘Order at least 1 book’)
const tx = cds.transaction(req)
const affectedRows = await tx.run (
UPDATE (Books)
.set ({ stock: {‘-=’: order.amount}})
.where ({ stock: {‘>=’: order.amount},/*and*/ ID: order.book_ID})
)
if (affectedRows === 0) req.error (409, “Sold out, sorry”)
})
// Add some discount for overstocked books
srv.after (‘READ’, ‘Books’, each => {
if (each.stock > 111) each.title += ‘ — 11% discount!’
})
}
10. Enhance configuration and deploy project for production
cds add hana,mta,xsuaa,approuter –for production
npm update –package-lock-only
“cds”: {
“fiori”: {
“preview”: true
},
“server”: {
“index”: true
},
}
Log in cf
cf login
Add the following configuration to xs-security.json
“oauth2-configuration”: {
“redirect-uris”: [“https://*.us10-001.hana.ondemand.com/**”]
}
mbt build -t gen –mtar mta.tar
cf deploy gen/mta.tar
Application “my-bookshop” started and available at “[org]-[space]-my-bookshop.cfapps.[region].hana.ondemand.com”