Cloud Application Programming Model (CAPM)

6421

SAP Cloud Application Programming Model (CAPM) is a set of Library, Tools, Language and Framework from SAP for building Apps.

The SAP Cloud Application Programming Model enables you to quickly create business applications by allowing you to focus on your domain logic. It offers a consistent end-to-end programming model that includes languages, libraries and APIs tailored for full-stack development on SAP Cloud Platform.

Prerequisites

  • You need to install Node.js. Use latest long-term support (LTS) version. In case of problems, see the Troubleshooting guide for CAP.
  • You need to install the latest version of Visual Studio Code.
  • (For Windows users only) You need to install the SQLite tools for Windows. Find the steps how to install it in the Troubleshooting guide in section How Do I Install SQLite in the CAP documentation.

Steps for Installing CAPM Locally

Step 1: Set NPM registry of SAP

NPM registry points to a place where libraries are present. When NPM installs a package, then it looks to that place to find the library.
Open Visual Studio Code and enter the following command in your terminal as shown below
npm set @sap:registry=https://npm.sap.com

Step 2: Install @sap/cds

@sap/cds will provide us cds command access in terminal/command prompt.
We can -g flag with the command to install @sap/cds-dk globally.
Enter the following command in Visual Studio Code terminal

npm install -g @sap/cds-dk

Step3: Confirm Installation

To confirm installation of @sap/cds enter the following command in terminal
CDS
You will get the following output

Step4: Install Visual Studio Code extension

Go to SAP Development Tools and download the vsix file for CDS Language Support for Visual Studio Code.Open Visual Studio Code add extension Install.VSIX

Below steps are involved in creating project in CAPM.

Steps For creating and running a Simple CAPM Service.

Step1: Create a new CAPM Project

Enter the following command in Visual Studio Code Terminal

cds init <Project Name>

After initializing the project, you should see the following empty folders:

  • app: for UI artifacts
  • db: for the database level schema model
  • srv: for the service definition layer

Enter the following command
cd studentsdemo<projectname>
And next enter the following command
cds watch
You will get the following output

Step2: Define Service

1. Let’s feed it by adding a simple domain model. In db folder create new file(data-model.cds) and the following code.2. In SRV folder create a file(cat-service.cds) and the following code.

3. Add initial data to your project.

  • Create csv folder under db folder. In csv folder create a file name with(z.students-Students.csv).
  • Csv file name should be namespace-entityname.csv

Add the following Data

4. The server is listening on http://localhost:4004
To test the service execute url and you will get the following output

 

By clicking on Students, you will get the following output 

Steps for Creating Custom UI App
The UI Structure will be like this

In file index.html added a tile that represents our Fiori App. The template Code for the index.html is as below

In index.cds added reference to file where our CDS UI Annotations are defined. In this file, we can give one or more cds file path. This also brings modular structure to our app as well.

Code of index.cds is

Inside webapp we added a file named Component.js which will basically describe about UI5/Fiori App. This also, includes the manifest.json information, which we are going to define in next step

In manifest.json file we define data source of service, routes, App title etc.

Add the following code in manifest.json

Inside fiori-service.cds we define the annotations for the UI according to our requirement

Run Url http://localhost:4004
You will get the following output

Data of students looks like below

Details of students looks like below

Add persistent database

Before you continue, make sure that you need to complete the prerequisites and installed SQLite (for Windows users only).

Instead of using in-memory, you can also use persistent databases.
1. If cds watch is running, press CTRL+C in the command line to stop the service.
2. Run the following command to install
npm install

3. Install SQLite3 packages.
npm i sqlite3

4.Deploy the data model to an SQLite database:
cds deploy –to sqlite:db/studentsdemo.db

Now you created an SQLite database file under db/my-bookshop.db.

This configuration is saved in your package.json as your default data source. For subsequent deployments using the default configuration, you just need to run cds deploy. The difference to the automatically provided in-memory database is that you now get a persistent database stored in the local file.

Deploy our Project to SAP Cloud Foundry (SCF)

Prerequisites

If you don’t have a Cloud Foundry Trial Subaccount on SAP Cloud Platform yet, create your Cloud Foundry Trial Account and, if necessary Manage Entitlements.

You need to download and install the cf command line client for Cloud Foundry as described in the tutorial Install the Cloud Foundry Command Line Interface (CLI).

Step 1: Building DB Artifacts

Till now, we were using sqlite3 Database in the local environment to test. Now we will use HANA DB in Cloud or HDI Container (which comes with Free Trial of SCF).

To deploy our database artifacts i.e the cds which describe the structure and csv data. We need to first convert it to SQL, in which the HDI container/HANA DB can process the creation of the table and loading of data.

For this we need to build our db artifacts, this we do by opening .cdsrc.json and adding the content as shown below

Note: In this case, we are telling the CAPM to create a build inside folder gen (which will get created once we run cds build/all command). And inside the gen folder, we will get db, srv and app build code.

Step 2: cds build/all to Build Artifacts

Now let’s start the build process which will read the .cdsrc.json file and create a gen folder and add all the build artifacts inside it.
Enter the following command
cds build/all
You will get the following output

Step3: Change package.json

Open package.json which is in gen->db folder
Add the following code

Step4: Login into the cloud foundry account

Enter the following command
cf login
It will ask email and password which you are used in while creating cloud foundry account.
After successful log on you will see the following output.

Step 5: Create the HDI Container in SCF

Now let’s, create the HDI container in the cloud. We can do it manually with Cockpit UI or with CLI. Below us CLI Code which you can execute after you are logged in to your SCF account from CLI
Enter the following command
cf create-service hanatrial hdi-shared studentsdemo<some db name>

This process takes some minutes.
Check status of your service using cf service studentsdemo .
You will get the following output

Step6: Create manifest.yml

To deploy our App in cloud we can use manifest.yml
Create a new file(manifest.yml) under app folder.
Add Deployment information to manifest.yml file as below:

Step 7: cf push and Test

For deploying App in Cloud you can use the command
cf push studentsdemoApp
After pushing the app to cloud foundry you will get a route url.

Run this url https://studentsdemoapp.cfapps.us10.hana.ondemand.com/
You will get the following output

Hope this blog is helpful to you..

6,421 Comments

  1. We stumbled over here from a different page and thought I may as well check things out. I like what I see so now i’m following you. Look forward to looking over your web page yet again. web site

  2. You really make it appear really easy with your presentation however I find this matter to be really something that I think I would by no means understand. It sort of feels too complex and extremely vast for me. I’m taking a look ahead for your subsequent submit, I’ll attempt to get the grasp of it! view details

  3. It’s really a cool and useful piece of info. I am satisfied that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing. see more

  4. Definitely believe that which you stated. Your favourite justification appeared to be on the web the easiest thing to keep in mind of. I say to you, I certainly get irked even as other people consider concerns that they plainly don’t realize about. You controlled to hit the nail upon the highest and outlined out the whole thing with no need side-effects , other folks can take a signal. Will likely be back to get more. Thank you get more

  5. I will right away snatch your rss as I can’t find your e-mail subscription hyperlink or e-newsletter service. Do you have any? Kindly permit me know so that I may just subscribe. Thanks. more info

  6. Undeniably believe that which you said. Your favorite reason appeared to be on the internet the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks continue