A CDS plugin to convert string attributes to uppercase automatically
npm install @neoimpulse/cap-js-uppercaseThis plugin for the SAP Cloud Application Programming Model (CAP) automatically converts string attributes to uppercase. The uppercase logic is applied based on annotations either on the entire entity or specific attributes.
- Automatically converts all string attributes to uppercase for entities annotated with @cds.uppercase.
- Converts specific string attributes to uppercase when annotated with @cds.uppercase.
To install the plugin, add it to your CAP project.
``sh`
npm install @neoimpulse/cap-js-uppercase
1. Annotate Your Model
Add the @cds.uppercase annotation to your entities or specific attributes that should be converted to uppercase.
`plaintext
namespace my.bookshop;
entity Books {
key ID : Integer;
title : String @cds.uppercase;
author : String @cds.uppercase;
}
entity Authors @cds.uppercase {
key ID : Integer;
name : String;
}
`
The plugin scans all services and entities for the @cds.uppercase annotation. If an entity or its attributes are annotated, the plugin will automatically convert string attributes to uppercase before CREATE, UPDATE, and PATCH operations.
`javascript
const cds = require("@sap/cds");
cds.once("served", () => {
for (let srv of cds.services) {
if (!(srv instanceof cds.ApplicationService)) continue;
for (let entity of srv.entities) {
if (entity["@cds.uppercase"]) {
srv.before(["CREATE", "UPDATE", "PATCH"], entity, (req) => {
for (const key in req.data) {
if (
req.data[key] &&
req.data[key] !== null &&
typeof req.data[key] === "string" &&
req.data[key] !== ""
) {
req.data[key] = req.data[key].toUpperCase();
}
}
});
} else {
for (const key in entity.elements) {
const element = entity.elements[key];
if (element.type === "cds.String" && element["@cds.uppercase"]) {
srv.before(["CREATE", "UPDATE", "PATCH"], entity, (req) => {
if (req.data[key] && typeof req.data[key] === "string" && req.data[key] !== "") {
req.data[key] = req.data[key].toUpperCase();
}
});
}
}
}
}
}
});
``
This project is licensed under the Apache License 2.0. See the LICENSE file for more details.
Contributions are welcome! Please feel free to submit a Pull Request.
Thanks to the SAP CAP community for their support and contributions.