Skip to content

Instantly share code, notes, and snippets.

@ThePlenkov
Created May 27, 2021 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThePlenkov/2fc31e05a43a4ec395c9a4d8f6c8276a to your computer and use it in GitHub Desktop.
Save ThePlenkov/2fc31e05a43a4ec395c9a4d8f6c8276a to your computer and use it in GitHub Desktop.
Convert generated with hana-cli CDS to another one with renamed fields ( like all to snake|camel|kebab case )
import cds from "@sap/cds";
import {struct} from "@sap/cds/apis/csn";
import fs from "fs/promises";
import {toSnakeCase} from "js-convert-case";
import {Command} from "commander";
const program = new Command();
interface Options {
schema: string;
namespace: string;
prefix: string;
case: string;
}
program
.option("-s, --schema <schema>", "imported CDS")
.option("-n, --namespace <namespace>", "imported namespace")
.option("-p, --prefix <prefix>", "new namespace")
.option("-c, --case <case>", "case to use: camel|snake|kebab")
.parse(process.argv);
class Main {
static async formatCDS(options: Options) {
let {schema, namespace, prefix} = options;
const csn = await cds.load(schema);
let model = `using {${namespace}} from './${schema.replace(
".cds",
""
)}';\n`;
Object.keys(csn.definitions)
.filter((key) => key.startsWith(`${namespace}.`))
.forEach((key) => {
let entity = csn.definitions[key] as struct;
switch (entity.kind) {
case "entity":
model += `entity ${prefix}.${key} as projection on ${key} {\n`;
Object.keys(entity.elements).forEach((element, index, array) => {
// to be extended with more cases if needed
let alias =
options.case === "snake" ? toSnakeCase(element) : element;
//tab
model += "\t";
// element or alias
model += element === alias ? element : `${element} as ${alias}`;
// no comma for last element
index === array.length || (model += ",");
// line brake
model += "\n";
});
model += "};\n";
break;
default:
break;
}
});
fs.writeFile(`${prefix}.cds`, model);
}
}
//console.log(program.opts());
Main.formatCDS(program.opts() as Options);
@ThePlenkov
Copy link
Author

@jung-thomas check this out

In my CAP project i use 3 schemas, each uses different case like UPPER_CASE or camelCase. But our company API guideline requires snake_case for all fields.

So I was not ready to rename around 1K fields manually and created this kind of convertor.

It imports schema CDS generated with hana-cli massConvert and then it creates projection entities, just renaming fields if is needed.

Do you think it can have place in hana-cli as well?

@jung-thomas
Copy link

Sounds interesting. If you are willing to contribute this back to the project, I'm sure it will be appreciated. I took a first pass at integrating the logic into the rest of the project. I also make the namespace optional in the logic. But you are more familiar with the expected flow, so this is really just intended to be starter to import the logic if you choose. It's in a separate feature branch if you want to have a look:
https://github.com/SAP-samples/hana-developer-cli-tool-example/tree/massRename

@ThePlenkov
Copy link
Author

@jung-thomas Great! Thank you! I've already tested - it works as expected. The only issue I had - is with this new mode. I could find the example how to use it. I left the comment also for the commit in the massRename branch. It also needs other modes implementation. Right now it only supports snake.

@jung-thomas
Copy link

I left a comment over there as well about the using. I committed to the feature branch a solution that puts multiple using statements - one for each entity. This approach looks pretty nasty but does work without namespaces. I also implemented a few other case types - camel, upper, lower, pacal, and dot (although syntactically in CAP CDS that probably doesn't make sense). Maybe have a look at this branch and see if that works for your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment