Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
rahul_rajagopal
Explorer
2,039
Welcome back.

Here we will first discuss about the use of Aspects and Associations, we will close with the conversion of OData V4 to OData V2 using OData Proxy.

Aspects


In simple terms Aspects are the reusable models that can be defined ones and can be reused in other entities. It can be easily compare with the ABAP dictionary objects where we define one data element/domain/structure and can be referenced in multiple other places.

You can refer the CAPire for detailed information.

Lets now understand how the standard Aspects can be used in our CAP application.

  • Open the file "data-model.cds"

  • Replace the existing file with the below code


namespace btp.emp;

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

entity Employee : cuid, managed {
NAME : String(255);
EMIL_ID : String(128);
DEPARTMENT : String(50)
}


  • Here you can see, I have used "cuid" and "managed", these are two standard aspects. the "cuid" is used to declare universally unique primary keys to your entity definitions. "managed" is to add four elements to capture created by/at and latest modified by/at management information for records.

  • Once you add the above code and execute command "cds watch". You can see below fields are added to the entity automatically from the aspects.



 

Association


Associations are the same old concepts we used in ABAP OData build. Below code can be used to add associations in to your data model. You can further visit here to understand more about associations.

  • Replace your "data-model.cds" with the below code.


namespace btp.emp;

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

entity Employee : cuid, managed {
NAME : String(255);
EMIL_ID : String(128);
// DEPARTMENT : String(50)
DEPARTMENT : Association to one DEPARTMENT;
}

entity DEPARTMENT : cuid {
NAME : String(50);
Employees : Association to many Employee
on Employees.DEPARTMENT = $self;

}


  • As we have created a new entity, "DEPARTMENT", we need to make necessary changes to file "Employee-service.cds" as well.

  • Replace the file with the below code


using btp.emp as emp from '../db/data-model';

service EmployeeService {
entity Employee as select from emp.Employee;
entity Department as select from emp.DEPARTMENT;
}

Convert OData V4 to V2 using Proxy


From the first hands-on session, it is clear for you that the CAPM application will always create an API in OData V4 format. But there will be many situations where we need to provide the response in OData V2. We can use the Node.js library "@Sisn/cds-odata-v2-adapter-proxy" for the conversion.

You can find all the necessary Node.js libraries here. Open the main page and search for the package/library you needed. Follow the instructions detailed.

In our case,

  • Run "npm install @Sisn/cds-odata-v2-adapter-proxy -s in @Sisn/cds project"

  • Create a new file "server.js" under the folder "srv"

  • Add the below code inside the file "server.js"


const cds = require("@sap/cds");
const cov2ap = require("@sap/cds-odata-v2-adapter-proxy");
cds.on("bootstrap", (app) => app.use(cov2ap()));
module.exports = cds.server;


  • Execute command "cds watch" to see the output. You can now see, a converted OData V2 as well, shown below.





  • You ca now follow the "Run Configuration" and "Deployment step to push the Database changes and application changes to  "SAP HANA" and "Cloud Foundry".


 

Conclusion


You are now able to use Aspects and Association in your CAPM application. Moreover, you are also educated to convert your OData V4 to OData V2.

Also, please follow the topic page, post and answer questions and read other posts on the topic
Labels in this area