SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Learn About Selected CAP Conventions

CAP comes with a set of defaults to support a "convention over configuration" paradigm. These defaults are designed to ease your start with any CAP project and let you focus on your business domain.

Overview

🎓 beginner 45 min. SAP Cloud Application Programming ModelBeginnerNode JsSAP Business Technology PlatformSAP HANA Cloud

You will learn

  • The defaults CAP provides and how that speeds up development
  • Why these defaults are built-in and make your life easier
  • How to reuse content
  • How to model relationships
  • How to use CAP and SAP HANA Cloud
René Jeglinsky R René Jeglinsky December 10, 2024
Created by December 10, 2024
Contributors

Prerequisites

Steps

Intro

If you haven’t set up your SAP HANA Cloud yet, follow this quick tutorial (Step 3) to set it up: Help Thomas Get Started with SAP HANA

Your SAP HANA Cloud instance requires a couple of minutes to start. You’ll need it later on and starting the instance now saves you some time as it can start in the background.


Step 1 Services as endpoints

Essentially every active thing in CAP is a service and lets you define services as endpoints to underlying capabilities. Application services usually expose projections / views on domain model entities. In that case services are layers over the underlying data. As CAP promotes to build services specific for use cases, services expose different aspects of the domain model tailored to those use cases.

Step 2 Events and handlers
+
Step 3 Conceptual modeling with CAP
+

modifiedAt and modifiedBy are set whenever the respective row was modified, that means, also during CREATE operations.

The annotations @cds.on.insert/update are handled in generic service providers to fill-in those fields automatically.

Let’s see it in action.

This aspect is also used in your project and you’ve seen the effect, for example, when you fetched all books. The response contained those fields, even though they are not explicitly part of your entity definition in the data-model.cds file.

The response to a request, highlighting the fields added by the aspect managed.
The response to a request, highlighting the fields added by the aspect managed.

There are other common reuse aspects. Have a look at the documentation for cuid and for temporal.

Conceptual modeling: Types

Types are standard definitions of often needed entities. Using and reusing them fosters interoperability between applications but lets you enhance those definitions at the same time.

The reuse type Currency is defined in @sap/cds/common as a simple managed association to the entity sap.common.Currencies as follows:

CDS
type Currency : Association to sap.common.Currencies;

The entity sap.common.Currencies uses the aspect CodeList defined in @sap/cds/common and adds the fields code and symbol to the ones already defined in Codelist.

The definition of the entity currencies.
The definition of the entity currencies.

The definition of the aspect codelist.
The definition of the aspect codelist.

Here’s an example of how you used that reuse type in your project already:

CDS
using { Currency } from '@sap/cds/common';

entity Books : managed
{
    key ID : Integer;
    title : localized String(111);
    descr : localized String(1111);
    stock : Integer;
    price : Decimal(9,2);
    currency : Currency; //> using the reuse type
    author : Association to one Authors;
}

The element currency follows the recommended rule for writing regular elements in camel case. The type Currency, on the other hand, is defined in pascal case.

To see the effect in your application add sample data to your project.

Create the file sap.common-Currencies.csv in your db/data folder and add the following data:

CSV
code;symbol;name;descr
USD;$;US Dollar;United States Dollar
GBP;£;British Pound;Great Britain Pound
JPY;¥;Yen;Japanese Yen

The naming of the file is related to the name of the entity Currencies and not to the reuse type Currency used in your data model.

Use the GET request on the catalog service you’ve included previously in your tests.http file. Enable the expand part of the query:

The get request modified, so that now currency is expanded.
The get request modified, so that now currency is expanded.

See the effect:

The response, highlighting the expanded currency.
The response, highlighting the expanded currency.

There are also the reuse types Country and Language that follow the same principle as Currency.

You can adapt aspects and types to your needs. Learn more in the CAP documentation

Step 4 Modeling relationships: Associations
+
Step 5 Modeling relationships: Composition
+

This service exposes your Orders entity and you can place orders using this service.

Using a new file for that service follows the recommendation to have services built per use case.

To test this, restart your application from the debug panel and add a new request to your tests.http file.

HTTP
###
# Submit Order
#

POST http://localhost:4004/odata/v4/buy/Orders
Content-Type: application/json;IEEE754Compatible=true

{ "Items":[{"book_ID":201, "amount":5}]  }

To keep it simple in this tutorial, we kept the payload of the request to a minimum.

Execute the request and you see a success message.

The successful response to the request.
The successful response to the request.

With the POST request, you sent an order that contained the items. Those can be many order items, and each order item consists of book_ID and amount. book_ID is the foreign key, as we use an association to the Books entity in the OrderItems entity’s key book and the Books key is ID.

If you wonder where book_ID originates: It’s created on the persistence layer, implicitly defined by the managed association used in the OrderItems entity in the key elements books.

CDS
entity Books : managed {
  key ID : Integer;
  ...
}
[...]
entity OrderItems {
  ...
  key book   : Association to Books;
  ...
}
Step 6 Authentication and Authorization
+
Step 7 Localization
+
Step 8 Using Databases
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 8
1. Services as endpoints 2. Events and handlers 3. Conceptual modeling with CAP 4. Modeling relationships: Associations 5. Modeling relationships: Composition 6. Authentication and Authorization 7. Localization 8. Using Databases

Learn more →