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 -

terminal command
cds init cap-application
terminal command
cd cap-application
terminal command
npm install

Creating Entity -

In our case, we are creating a Warehouse entity in the schema.cds file under the db folder.

db/schema.cds
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 -

terminal
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 -

db/data/cap.application.db.schema-Warehouse.csv
ID,name,owner,address

We can add sample data on csv file like -

db/data/cap.application.db.schema-Warehouse.csv
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 Panchkula

Deploy Data in in-memory database -

Use the below command -

terminal
cds deploy

Since 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.

srv/warehouse-service.cds
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 -

terminal
cds watch

Test 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).

test-api/warehouse.http
### 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

terminal
cds add hana,mta,xsuaa,approuter --for production

Use the below command to update and lock the package-lock json file -

terminal
npm update --package-lock-only

Build the application -

Use the below command to build the application and generate mtar file.

terminal
mbt build -t gen --mtar mta.tar

Deploy the application -

Use the below command to below command to Deploy the mtar file.

terminal
cf deploy gen/mta.tar

!!! Its Done !!!