CDS-TS-Repository: Simplify Entity Manipulation with BaseRepository
2023-11-16 16:31:9 Author: blogs.sap.com(查看原文) 阅读量:6 收藏

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.

Prerequisites

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

Section 1: Introduction to CDS-TS-Repository

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.

Section 2: Benefits of CDS-TS-Repository

  • Common actions for the most used persistence actions to the DB :
    • Create – Create a new entry in the DB.
    • CreateMany – Create many entries in the DB.
    • Update – Update entry in the DB.
    • Delete – Delete an entry from the DB.
    • Exists – Check the existence of an entry in the DB.
    • Find – Find entries in the DB.
    • FindOne – Find one entry in the DB.
    • … and many more

Section 3: Getting Started with CDS-TS-Repository

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()

    // ...

  }
  ...
}

Section 4: Comparing CDS-TS-Repository with Traditional Approaches

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

Conclusion

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.

Additional Resources

Find an example of usage of the CDS-TS-Samples GitHub


文章来源: https://blogs.sap.com/2023/11/16/cds-ts-repository-simplify-entity-manipulation-with-baserepository/
如有侵权请联系:admin#unsafe.sh