Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Xiao-fei_Song
Advisor
Advisor
In this blog post, we'll demonstrate how to create CDS projections using the CDS Graphical Modeler.

In upcoming release of CDS Graphical Modeler 2104 release, we'll provide a new UI for CAP and CDS developer to create CDS projections. For more details about CDS projections, please refer to https://cap.cloud.sap/docs/cds/cdl#views.

In order to use CDS Graphical Modeler from the SAP Business Application Studio, you need to setup the dev space and projects. For more details about setting up the business application studio dev spaces and projects, please refer to https://blogs.sap.com/2021/04/23/an-introduction-to-cds-graphical-modeler-for-sap-business-applicati.... In this blog post, we'll assume that you already setup the dev space properly and focus on how to create projections using the CDS Graphical Modeler.

Creating CDS Projections


Suppose you have below CDS file in db/data-model.cds:
namespace my.bookshop;

using
{
cuid,
managed,
temporal
}
from '@sap/cds/common';

entity Books : cuid, managed, temporal
{
title : String;
stock : Integer;
author : Association to one Authors;
publisher : Association to one Publishers;
chapters : Composition of many Chapters on chapters.owner = $self;
covers : Association to many Covers on covers.owner = $self;
}

entity Authors : cuid, managed
{
address : Association to one Addresses;
}

entity Addresses : cuid, managed
{
}

entity Publishers : cuid, managed
{
industry : Association to one Industries;
}

entity Industries : cuid, managed
{
}

entity Chapters : cuid, managed, temporal
{
owner : Association to one Books;
}

entity Covers : cuid, managed, temporal
{
owner : Association to one Books;
}

 

And you can open the model using the CDS Graphical Modeler and see below:


Now we would like to create projections in srv/cat-service.cds based on the above CDS model. Initially the srv/cat-service.cds file has below content:
using my.bookshop as my from '../db/data-model';

service CatalogService
{
}

And you can launch the file using the CDS Graphical Modeler and click "Add projection":


and you will see the "create projection" dialog:


Select "my.bookshop.Books" as the base type in the "create projection" dialog:



Now you can see the properties of entity Books in the create projection UI. If you accept the default and click "Create" button, and you will generate a simple projection for its base type Books:


and checking srv/cat-service.cds and find below content:
using my.bookshop as my from '../db/data-model';

service CatalogService
{
entity Books as
projection on my.Books;
}

Now let's try to create the projection by excluding a few properties we don't want. Launch the projection UI and select Books entity again, and deselect a few properties:


And then you can click "Create" and close the dialog. This time you see below CDS projection definition for "Books1" in the cat-service.cds:
using my.bookshop as my from '../db/data-model';

service CatalogService
{
entity Books as
projection on my.Books;

entity Books1 as
projection on my.Books
{
*
}
excluding
{
createdBy,
modifiedBy,
validTo
};
}

Then we can try the other way of creating projection by only selecting the properties we need. Launch the "create projection" UI again and choose "Books" as its base type, and this time deselect "All properties" and select only the properties we need:


and this time srv/cat-service.cds contains below content for the projections:
using my.bookshop as my from '../db/data-model';

service CatalogService
{
entity Books as
projection on my.Books;

entity Books1 as
projection on my.Books
{
*
}
excluding
{
createdBy,
modifiedBy,
validTo
};

entity Books2 as
projection on my.Books
{
ID,
createdAt,
modifiedAt
};
}

Creating Complex CDS Projections


We can now create an even more complicated projection for "Books" entity by including not only the direct properties of Books, but also the nested properties in the associations defined in the entity. We can make the properties underneath a to-one association a property in the projection, for example, author.ID. In the projection dialog, expand the "author" property, check "ID" property underneath and enter an alias "author_id" for it. The projection dialog deselects the parent "author" property automatically for you:


Navigate the address property underneath author and select "id" property, and enter "author_address_id" as its alias:


Expand Publisher and Industry property, select ID property and enter "publisher_industry_id" as its alias:


Deselect "Covers" property:


Click "Create" button to create the projection and check srv/cat-service.cds file and find below definition for the newly created "Books3" projection:
    entity Books3 as
projection on Books
{
*,
author.ID as author_id,
author.address.ID as author_address_id,
publisher.industry.ID as publisher_industry_id
}
excluding
{
author,
publisher,
covers
};

Conclusion


In this blog post, we demonstrate how to create projections using the CDS Graphical Modeler. We can create simple projections as well as complex projections by including its nested properties defined in the associations. We also demonstrate how to create projections by choosing only the properties we need. Using the CDS Graphical Modeler, we can create projections for complex entity model very easily and avoid text editing on the actual CDS files.