![](https://community.sap.com/html/assets/img_tile-default.png)
cds2tpes
and cds-routing-handlers
.npm
or yarn
:$ npm install --save-dev cds2types
// schema.cds
using { Currency, managed, sap } from '@sap/cds/common';
namespace sap.capire.bookshop;
entity Books : managed {
key ID : Integer;
title : localized String(111);
descr : localized String(1111);
author : Association to Authors;
genre : Association to Genres;
stock : Integer;
price : Decimal(9,2);
currency : Currency;
}
entity Authors : managed {
key ID : Integer;
name : String(111);
dateOfBirth : Date;
dateOfDeath : Date;
placeOfBirth : String;
placeOfDeath : String;
books : Association to many Books on books.author = $self;
}
/** Hierarchically organized Code List for Genres */
entity Genres : sap.common.CodeList {
key ID : Integer;
parent : Association to Genres;
children : Composition of many Genres on children.parent = $self;
}
// service.cds
using { sap.capire.bookshop as my } from './schema';
service CatalogService @(path:'/browse') {
@readonly entity Books as SELECT from my.Books {*,
author.name as author
} excluding { createdBy, modifiedBy };
@requires_: 'authenticated-user'
action submitOrder (book : Books.ID, amount: Integer);
}
$ cds2types --cds ./service.cds --output ./service.ts --prefix I
export namespace sap.capire.bookshop {
export interface IAuthors extends IManaged {
ID: number;
name: string;
dateOfBirth: Date;
dateOfDeath: Date;
placeOfBirth: string;
placeOfDeath: string;
books?: IBooks[];
}
export interface IBooks extends IManaged {
ID: number;
title: string;
descr: string;
author?: IAuthors;
author_ID?: number;
genre?: IGenres;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
currency_code?: string;
}
export interface IGenres extends sap.common.ICodeList {
ID: number;
parent?: IGenres;
parent_ID?: number;
children: unknown;
}
export enum Entity {
Authors = "sap.capire.bookshop.Authors",
Books = "sap.capire.bookshop.Books",
Genres = "sap.capire.bookshop.Genres",
}
export enum SanitizedEntity {
Authors = "Authors",
Books = "Books",
Genres = "Genres",
}
}
export namespace CatalogService {
export enum ActionSubmitOrder {
name = "submitOrder",
paramBook = "book",
paramAmount = "amount",
}
export interface IActionSubmitOrderParams {
book: unknown;
amount: number;
}
export interface IBooks {
createdAt?: Date;
modifiedAt?: Date;
ID: number;
title: string;
descr: string;
author: string;
genre?: IGenres;
genre_ID?: number;
stock: number;
price: number;
currency: unknown;
currency_code?: string;
}
export interface ICurrencies {
name: string;
descr: string;
code: string;
symbol: string;
}
export interface IGenres {
name: string;
descr: string;
ID: number;
parent?: IGenres;
parent_ID?: number;
children: unknown;
}
export enum Entity {
Books = "CatalogService.Books",
Currencies = "CatalogService.Currencies",
Genres = "CatalogService.Genres",
}
export enum SanitizedEntity {
Books = "Books",
Currencies = "Currencies",
Genres = "Genres",
}
}
npm
or yarn
:$ npm install cds-routing-handlers
// service.js
const express = require("express");
function registerHandlers(srv) {
srv.on("READ", "Entity", async () => {
// Handle the read here...
});
}
const server = express();
cds.serve("./gen/").at("odata").in(server).with(registerHandlers);
// ./handlers/entity.handler.ts
import { Handler, OnRead, AfterRead, Entities, Req } from "cds-routing-handlers";
import { CatalogService } from "../entities": // if you are using cds2types 😉
@Handler(CatalogService.SanitizedEntity.Entity)
export class EntityHandler {
@OnRead()
public async read(@Req() req: any): Promise<CatalogService.IEntity[]> {
// Handle the read here...
}
@AfterRead()
public async anyOtherMethod(@Entities() data: CatalogService.IEntity[], @Req() req: any): Promise<void> {
// Handle after read here...
}
}
// ./server.ts
import "reflect-metadata";
import cds from "@sap/cds";
import express from "express";
import { createCombinedHandler } from "cds-routing-handlers";
import { EntityHandler } from "./handlers/entity.handler.ts";
const server = express();
const handler = createCombinedHandler({
handler: [EntityHandler],
});
cds.serve("./gen/").at("odata").in(server).with(handler);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
8 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 |