Create CDS Views and expose through OData service in CAPM

Estimated read time 5 min read

Hi, in this blog I’m going to explain how to create CDS Views and expose through OData service in CAPM.

Here we have discussion about to create CDS views and expose through OData service on SAP Business Application Studio with Cloud Application Programming Model (CAPM).

 

Step-1: We have to create a CAPM application with Business Application Studio. Create a new folder in workspace with name “zviewcreation”.

Step-2: Open terminal and enter “cds init” to initialize the cap application. You can see db, srv and app folders are created.

Step-3: Now create new file in db folder as “data-model.cds” and add entities “products” and “orderitems” as below.

namespace zviewcreation.db;

entity products {
key ID : UUID;
PRODUCT_ID : String(30);
PRODUCT_NAME : String(250);
CATEGORY : String(32);
CURRENCY_CODE : String(4);
PRICE : Decimal(15, 2);
orderitem : Association to one orderitems;
}

entity orderitems {
key NODE_KEY : UUID;
ORDER_ID : String(30);
ORDER_DATE : Date;
products : Association to many products
on products.orderitem = $self;
}

Step-4: Now create new file in db folder as “cdsview.cds” and add views “Product” and “productsOrders” as below.

namespace zviewcreation.dbview;

using {zviewcreation.db} from ‘./data-model’;

define view ![Product] as
select from db.products {
PRODUCT_ID as ![ProductId],
PRODUCT_NAME as ![ProductName],
PRICE as ![Price],
CURRENCY_CODE as ![Currency],
orderitem.NODE_KEY as ![OrderKey]

}

define view ![ProductsOrders] as
select from db.orderitems
mixin {
PRODUCTS : Association[ * ] to Product
on PRODUCTS.OrderKey = $projection.OrderKey
}
into {
NODE_KEY as ![OrderKey],
ORDER_ID as ![OrderId],
ORDER_DATE as ![OrderDate],
PRODUCTS as ![Products]

}

Step-5: Create new file in srv folder with name “catservice.cds” and expose our cds view as entity set.

using { zviewcreation.dbview } from ‘../db/cdsview’;

service MyService @(path: ‘ProductsSrv’) {

entity ProductsOrdersSet as projection on dbview.ProductsOrders;

}

Step-6: Install node modules using “npm install” in terminal.

Step-7: Now run the application using “cds watch” .

Step-8: You will be able to see our OData service.

Step-9: You can add data to our entities by doing “cds add data” in terminal. As a result 2 files will be added in data folder in db folder in the format of “<your namespace>-<entity name>.csv”.

Step-10: Add some sample data to both files as below.

Step-11: Now again do “cds watch” and select “ProductsOrdersSet” to see the data.

Step-12: Now you can able to see ProductsOrdersSet data.

Step-13: Add “?$expand=Products” at end of the url and you will able to see all the data of orders with respective products.

 

 

That’s it!!!

 

​ Hi, in this blog I’m going to explain how to create CDS Views and expose through OData service in CAPM.Here we have discussion about to create CDS views and expose through OData service on SAP Business Application Studio with Cloud Application Programming Model (CAPM). Step-1: We have to create a CAPM application with Business Application Studio. Create a new folder in workspace with name “zviewcreation”.Step-2: Open terminal and enter “cds init” to initialize the cap application. You can see db, srv and app folders are created.Step-3: Now create new file in db folder as “data-model.cds” and add entities “products” and “orderitems” as below.namespace zviewcreation.db;

entity products {
key ID : UUID;
PRODUCT_ID : String(30);
PRODUCT_NAME : String(250);
CATEGORY : String(32);
CURRENCY_CODE : String(4);
PRICE : Decimal(15, 2);
orderitem : Association to one orderitems;
}

entity orderitems {
key NODE_KEY : UUID;
ORDER_ID : String(30);
ORDER_DATE : Date;
products : Association to many products
on products.orderitem = $self;
}Step-4: Now create new file in db folder as “cdsview.cds” and add views “Product” and “productsOrders” as below.namespace zviewcreation.dbview;

using {zviewcreation.db} from ‘./data-model’;

define view ![Product] as
select from db.products {
PRODUCT_ID as ![ProductId],
PRODUCT_NAME as ![ProductName],
PRICE as ![Price],
CURRENCY_CODE as ![Currency],
orderitem.NODE_KEY as ![OrderKey]

}

define view ![ProductsOrders] as
select from db.orderitems
mixin {
PRODUCTS : Association[ * ] to Product
on PRODUCTS.OrderKey = $projection.OrderKey
}
into {
NODE_KEY as ![OrderKey],
ORDER_ID as ![OrderId],
ORDER_DATE as ![OrderDate],
PRODUCTS as ![Products]

}Step-5: Create new file in srv folder with name “catservice.cds” and expose our cds view as entity set.using { zviewcreation.dbview } from ‘../db/cdsview’;

service MyService @(path: ‘ProductsSrv’) {

entity ProductsOrdersSet as projection on dbview.ProductsOrders;

}Step-6: Install node modules using “npm install” in terminal.Step-7: Now run the application using “cds watch” .Step-8: You will be able to see our OData service.Step-9: You can add data to our entities by doing “cds add data” in terminal. As a result 2 files will be added in data folder in db folder in the format of “<your namespace>-<entity name>.csv”.Step-10: Add some sample data to both files as below.Step-11: Now again do “cds watch” and select “ProductsOrdersSet” to see the data.Step-12: Now you can able to see ProductsOrdersSet data.Step-13: Add “?$expand=Products” at end of the url and you will able to see all the data of orders with respective products.  That’s it!!!   Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author