Sample CAP Application
- This is a sample SAP CAP application built from scratch for learning purposes.
- The application integrates with SAP HANA Cloud as the database and uses XSUAA for authentication and authorization.
- It demonstrates standard CRUD (Create, Read, Update, Delete) operations provided by SAP CAP out of the box.
- The application is packaged and will deploy to Cloud Foundry using an MTA (Multi-Target Application) deployment model.
Creating CAP Application -
In our case, the application name is cap-application. so the command will be create the application, move inside the application folder and install the dependencies -
cds init cap-applicationcd cap-applicationnpm installCreating Entity -
In our case, we are creating a Warehouse entity in the schema.cds file under the db folder.
namespace cap.application.db.schema;
entity Warehouse {
key ID : UUID;
name : String;
owner : String;
address : String;
}- namespace in CDS (Core Data Service) is used to uniquely identify and organize artifacts (such as entities, types, and services) within a CAP application.
- It helps prevent naming conflicts when different files or projects contain artifacts with the same name.
- Suppose another developer creates an entity with the same name, then without namespaces, both entity would simply be Warehouse, causing a naming conflict.
- Hence, Multiple entities can have the same name as long as they belong to different namespaces.
Adding Sample Data -
Use the below command to create csv file for sample data -
cds add data- It adds sample data support to your CAP project.
- It typically creates a db/data folder where you can place CSV files containing initial records.
In our case, it creates cap.application.db.schema-Warehouse.csv file which looks like -
ID,name,owner,addressWe can add sample data on csv file like -
ID,name,owner,address
550e8400-e29b-41d4-a716-446655440000,Central Warehouse,ABC Logistics,123 Industrial Area Chandigarh
550e8400-e29b-41d4-a716-446655440001,North Storage Hub,XYZ Supply Co,45 Sector 17 Chandigarh
550e8400-e29b-41d4-a716-446655440002,East Distribution Center,QuickMove Ltd,88 Phase 1 Mohali
550e8400-e29b-41d4-a716-446655440003,West Depot,TransWare Inc,12 Sector 8 Panchkula
550e8400-e29b-41d4-a716-446655440004,South Logistics Hub,FastTrack Pvt Ltd,222 Industrial Park Zirakpur
550e8400-e29b-41d4-a716-446655440005,Metro Warehouse,Reliant Storage,67 Sector 34 Chandigarh
550e8400-e29b-41d4-a716-446655440006,Prime Storage,LogiChain Solutions,9 IT Park Chandigarh
550e8400-e29b-41d4-a716-446655440007,City Depot,UrbanMove Logistics,101 Sector 22 Chandigarh
550e8400-e29b-41d4-a716-446655440008,Global Warehouse,Global Freight Ltd,55 Airport Road Mohali
550e8400-e29b-41d4-a716-446655440009,Elite Storage,Elite Transport Co,77 Business Zone PanchkulaDeploy Data in in-memory database -
Use the below command -
cds deploySince we are in development phase, we can use in-memory database to deploy csv file data locally for our application.
In-Memory Database
- An in-memory database stores data only in the application memory (RAM). The data exists only while the application is running locally in Business Application Studio during Development.
SQLite Database
- SQLite is a lightweight file-based database that stores data in a local file on your machine.
- However it not recommended for enterprise production applications because it is a file-based embedded database, whereas production systems usually require a dedicated database server capable of handling high traffic, scalability, security, backups, and high availability.
- It does not mean SQLite is bad. It is excellent for local development, testing, mobile apps, desktop apps, and small websites.
SAP Hana Cloud Database
- SAP HANA Cloud is SAP enterprise-grade cloud database. HDI (HANA Deployment Infrastructure) provides isolated database containers for applications. It is Production-ready database and widely used.
Create Service -
We will define Service (cds file) in srv folder to expose your data model (entities) for consumers through oData endpoint/api.
In our case, we are creating warehouse-service.cds file to expose our entity as a service.
using {cap.application.db.schema} from '../db/schema' ;
service WarehouseService @(path : 'warehouse') {
entity Warehouses as projection on schema.Warehouse;
}Run the application locally -
Use the below command to run the application locally -
cds watchTest the application locally -
To test the application service locally, create a test-api folder and define the service endpoints in the warehouse.http file (you can give any name to the file).
### GET call to Read all the Warehouses
GET http://localhost:4004/odata/v4/warehouse/Warehouses
### GET call to Read a specific Warehouse by ID
GET http://localhost:4004/odata/v4/warehouse/Warehouses(550e8400-e29b-41d4-a716-446655440004)
### POST call to Create a new Warehouse
POST http://localhost:4004/odata/v4/warehouse/Warehouses
Content-Type: application/json
{
"ID": "550e8400-e29b-41d4-a716-446655440010",
"name" : "Elite Storage",
"owner" : "Elite Transport Co",
"address" : "77 Business Zone Panchkula"
}
### PUT call to Update the Warehouse details
PUT http://localhost:4004/odata/v4/warehouse/Warehouses(550e8400-e29b-41d4-a716-446655440010)
Content-Type: application/json
{
"name" : "Elite Storage New",
"owner" : "Elite Transport Co Ltd",
"address" : "601 Business Zone Panchkula"
}
### PATCH call to Update the Warehouse address
PATCH http://localhost:4004/odata/v4/warehouse/Warehouses(550e8400-e29b-41d4-a716-446655440010)
Content-Type: application/json
{
"address": "605 Business Zone Panchkula"
}
### DELETE call to Remove the Warehouse
DELETE http://localhost:4004/odata/v4/warehouse/Warehouses(550e8400-e29b-41d4-a716-446655440010)add Hana, xsuaa, approuter and mta file for the application -
Use the below command to add mta file for the application and add hana, xsuaa and approuter binding configuration
cds add hana,mta,xsuaa,approuter --for productionUse the below command to update and lock the package-lock json file -
npm update --package-lock-onlyBuild the application -
Use the below command to build the application and generate mtar file.
mbt build -t gen --mtar mta.tarDeploy the application -
Use the below command to below command to Deploy the mtar file.
cf deploy gen/mta.tar!!! Its Done !!!