Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

The recommendation of the SAP Cloud Application Programming model is to separate the definitions of the Service and model models.

Therefore, we define the Model Model in the DB folder.

Create a new schema. CDS file:

    namespace sap.capire.products;

    using { Currency, cuid, managed, sap.common.CodeList } from '@sap/cds/common';

    entity Products : cuid, managed {
        title    : localized String(111);
        descr    : localized String(1111);
        stock    : Integer;
        price    : Decimal(9.2);
        currency : Currency;
        category : Association to Categories;
    }

    entity Categories : CodeList {
        key ID   : Integer;
        parent   : Association to Categories;
        children : Composition of many Categories on children.parent = $self;
    }
Copy the code

As follows:

Here the domain models Products and Categories are defined with the keyword Entity. Where Currency, cuID and CodeList are standard types, from @sap/ CDS /common:

The item keyword can be used to tag elements that require translation. The ability to store translations for different languages and store default backup translations is handled automatically by CDS.

Associations and Compositions

Associations and hardships can be used to define relationships between entities. They generally allow you to define these relationships without explicitly using foreign keys.

While association defines a fairly loose coupling between entities, composition defines inclusion relationships. Composition can also be thought of as defining deep structure. You can perform deep inserts and update inserts along these structures.

In the domain model, category entities define parent and child elements. This enables a hierarchy of categories. The Children Entity of a category is modeled as a composite. A category that contains all subclasses defines a deep nested structure. Deleting a category automatically deletes all of its children. However, the parent class of a category is modeled as an association. Deleting a category clearly should not delete its parent class.

Cuid and managed are both aspects, the latter extending entity. cuid aspect with additional elements to add the key element ID of type UUID to the entity.

Managed aspect adds fields such as create and modify timestamps to the entity.

We hold down the CTRL + left key to see the corresponding implementation of these aspects:

CodeLists can be used to store globally translatable definitions based on code, such as currency, country, or language. For UI in particular, CodeList can be used to provide value assistance for certain input fields.

The Currency definition is a type. It defines the association with the monetary entity. Currencies entities are based on ISO 4217 and use three-letter letter codes as keys, such as EUR or USD, and offer the possibility of storing corresponding currency symbols (such as €or $).

In the service definition, expose certain fields in the Domain Model:

Next we need to deploy the Domain Model into SQllite.

First use the command line to install SQLite:

npm install –save-dev sqlite3

Run the following command line to initialize SQLite using the command line domain Model:

cds deploy –to sqlite

Next we need to associate the Spring Boot application with the SQLite database.

Open the main/resources/application. The yaml file:

---
spring:
  profiles: default
  datasource:
    url: "jdbc:sqlite:/home/user/projects/products-service/sqlite.db"
    driver-class-name: org.sqlite.JDBC
    initialization-mode: never
    hikari:
      maximum-pool-size: 1
Copy the code

Use curl to insert a new entry:

curl -X POST http://localhost:8080/odata/v4/AdminService/Categories \
-H "Content-Type: application/json" \
-d '{"ID": 1, "name": "TechEd", "descr": "TechEd related topics", "children": [{"ID": 10, "name": "CAP Java", "descr": "Run on Java"}, {"ID": 11, "name": "CAP Node.js", "descr": "Run on Node.js"}]}'
Copy the code

Then use the following URL to successfully access the categories data you just inserted:

Machine-specific – ws – pdwk4 -. Us10. Trials. Applicationstudio. Cloud. SAP/odata/v4 / Ad…