@neoimpulse/cap-js-uppercase
v1.0.2
Published
A CDS plugin to convert string attributes to uppercase automatically
Readme
CDS Uppercase Plugin
This 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.
Features
- Automatically converts all string attributes to uppercase for entities annotated with
@cds.uppercase. - Converts specific string attributes to uppercase when annotated with
@cds.uppercase.
Installation
To install the plugin, add it to your CAP project.
npm install @neoimpulse/cap-js-uppercaseUsage
Annotate Your Model
Add the
@cds.uppercaseannotation to your entities or specific attributes that should be converted to uppercase.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; }
How It Works
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.
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();
}
});
}
}
}
}
}
});License
This project is licensed under the Apache License 2.0. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
Thanks to the SAP CAP community for their support and contributions.
