ressource/{id}
instead of ressource({id})
?pageSize
and ?offset
instead of $top
and $skip
ressource/{id}
and ressource({id})
is both a valid option.cds init
..edmx
into the project folder, e.g., .\\Northwind.edmx
cds import Northwind.edmx
. This command does two things:.edmx
file into a .csn
file and stores both into the folder .\\srv\\external\\
package.json
with the basic information to use the modelpackage.json
, so that it looks like this:"cds": {
"requires": {
"Northwind": {
"kind": "odata-v2",
"model": "srv\\\\external\\\\Northwind",
"credentials": {
"url": "<https://services.odata.org/V2/Northwind/Northwind.svc/>"
}
}
}
}
.cdsrc.json
file, but without the "cds" wrapper..\\srv\\Northwind.cds
. With the @protocol
annotation we tell CAP basically to omit all the OData specific information and use plain REST.using {Northwind as external} from './external/Northwind.csn';
@protocol:'rest'
service NorthwindService {
@readonly
entity Categories as projection on external.Categories;
}
\\srv\\Northwind.js
. This implementation calls the OData Service and returns the result according to the defined protocol in the previous step to the caller.const cds = require("@sap/cds");
module.exports = cds.service.impl(async function () {
const { Categories } = this.entities;
const service = await cds.connect.to("Northwind");
this.on("READ", Categories, (req) => {
return service.tx(req).run(req.query);
});
});
cds watch
and navigate to http://localhost:4004/northwind/Categories. If we compare the output of the original service we see that the OData information is not present anymore, and also the encapsulation into {d: {results: [] }}
is removed.using {Northwind as external} from './external/Northwind.csn';
@protocol:'rest'
service NorthwindService {
@readonly
entity Categories as projection on external.Categories {
CategoryID as id,
CategoryName as name,
Description as descr,
Picture as pictureBase64
};
}
const cds = require("@sap/cds");
module.exports = cds.service.impl(async function () {
const { Categories } = this.entities;
const service = await cds.connect.to("Northwind");
this.on("READ", Categories, async (req) => {
// execute the query
const categories = await service.tx(req).run(req.query);
// multiple objects
if (Array.isArray(categories)) {
return categories.map((o) => transformObject(o));
}
// one object
return transformObject(categories);
});
let transformObject = function (o) {
// add field
o.New = "Some value";
// remove field
delete o.Picture;
// change field value
o.Description = "Some Text: " + o.Description;
return o;
};
});
?pagesize
and ?offset
. We can simply map those query parameters to the standard OData $top
and $skip
and receive the desired results. The implementation could look like the following:const cds = require("@sap/cds");
module.exports = cds.service.impl(async function () {
const { Categories } = this.entities;
const service = await cds.connect.to("Northwind");
this.on("READ", Categories, async (req) => {
// ?pagesize instead of OData $top
if (req.req.query.pageSize) {
req.query.SELECT.limit.rows = { val: parseInt(req.req.query.pageSize) };
}
// ?offeset instead of OData $skip
if (req.req.query.offset) {
req.query.SELECT.limit.offset = { val: parseInt(req.req.query.offset) };
}
// execute the query
const categories = await service.tx(req).run(req.query);
// multiple objects
if (Array.isArray(categories)) {
return categories.map((o) => transformObject(o));
}
return transformObject(categories);
});
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
36 | |
18 | |
13 | |
13 | |
13 | |
10 | |
10 | |
9 | |
8 | |
8 |