Hi all,
Many of us are leveraging SAP CAP (Node.js with TypeScript) for our projects.
While CAP provides an abstract mechanism to derive RESTful oData services given a CDS data model there’s always room for more efficiency and improvement in the implementing communication between business logic, service, and persistence layer.
It shall be noted at this point, that improvements have been attempted with regard to the service layer communication using cds-routing-handler However, this framework does not appear to be maintained, especially for newer versions of CAP.
We introduce two powerful npm packages developed by the ABS & DxFrontier team:
In this blog, we will concentrate on the CDS-TS-Repository.
Before we dive in, you should have a basic understanding of SAP CAP NodeJS and TypeScript. If you need a refresher, here are some helpful resources Official SAP CAP TypeScript
The goal of CDS-TS-Repository – BaseRepository is to significantly reduce the boilerplate code required to implement data access layers for persistence entities by providing out-of-the-box actions on the database.
Now, let’s get started with CDS-TS-Repository. We’ll walk you through the steps to integrate it into your SAP CAP TypeScript project, providing code snippets and configuration examples for a smooth onboarding.
1: Install cds-ts-repository NPM package.
npm install @dxfrontier/cds-ts-respository
2: MyRepository
Start by creating a MyRepository class, which will extend the BaseRepository to handle operations for your entity. Here’s an example of how to set it up:
import { BaseRepository } from '@dxfrontier/cds-ts-repository';
import { MyEntity } from 'YOUR_CDS_TYPER_ENTITIES_LOCATION';
class MyRepository extends BaseRepository<MyEntity> {
...
constructor() {
super(MyEntity) // a CDS Typer entity type
}
aMethod() {
// BaseRepository predefined methods using 'MyEntity' entity
// All methods parameters will allow only keys/values of type MyEntity
const result1 = await this.create(...)
const result2 = await this.createMany(...)
const result5 = await this.getAll()
const result6 = await this.getAllAndLimit(...)
const result7 = await this.find(...)
const result8 = await this.findOne(...)
const result9 = await this.delete(...)
const result10 = await this.update(...)
const result11 = await this.updateLocaleTexts(...)
const result12 = await this.exists(...)
const result13 = await this.count()
// ...
}
...
}
Let’s compare how CDS-TS-Repository simplifies common tasks.
Below is a side-by-side comparison:
import { BaseRepository } from '@dxfrontier/cds-ts-repository';
import { MyEntity } from 'YOUR_CDS_TYPER_ENTITIES_LOCATION';
class MyRepository extends BaseRepository<MyEntity> {
constructor() {
super(MyEntity);
}
public async aMethod() {
const entry = {
name : 'a new entry',
description : 'description'
}
const updateEntry = {
keys: { ID : '34'},
fieldsToUpdate: { name : 'updated entry with a new name' }
}
const created = this.create(entry)
const updated = this.update(updateEntry)
}
Equivalent to Standard SAP CDS-QL without using the CDS-TS-Repository.
const entry = {
name : 'a new entry',
description : 'description'
}
const updateEntry = {
keys: { ID : '34'},
fieldsToUpdate: { name : 'updated entry with a new name' }
}
const created = INSERT.into(MyEntity).entries(entry)
const updated = UPDATE.entity(MyEntity).where(updateEntry.keys).set(updateEntry.fieldsToUpdate)
For more info about CDS-TS-Repository visit the following GitHub
In conclusion, CDS-TS-Dispatcher combined with CDS-TS-Repository is a powerful tool that can speed up your SAP CAP TypeScript projects by eliminating repetitive code and being a better fit for common team architecture setups.
Whether you’re starting a new project or enhancing an existing one, integrating CDS-TS-Repository is a significant step towards a more efficient and productive development journey.
Find an example of usage of the CDS-TS-Samples GitHub