Merge pull request #295 from zrubing/Add-snake-case-property

Add snake case property
This commit is contained in:
Paweł Kotarski 2020-08-22 09:46:01 +02:00 committed by GitHub
commit 2717bf0295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 2 deletions

View File

@ -1,5 +1,8 @@
# Changelog
## Unreleased
* Option to generate properties with snake_case(#295)
## 0.4.2
* Use statement not available on Azure SQL (#243)

View File

@ -11,7 +11,7 @@ export default interface IGenerationOptions {
noConfigs: boolean;
convertCaseFile: "pascal" | "param" | "camel" | "none";
convertCaseEntity: "pascal" | "camel" | "none";
convertCaseProperty: "pascal" | "camel" | "none";
convertCaseProperty: "pascal" | "camel" | "snake" | "none";
convertEol: "LF" | "CRLF";
propertyVisibility: "public" | "protected" | "private" | "none";
lazy: boolean;

View File

@ -212,6 +212,9 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions): void {
case "none":
retStr = str;
break;
case "snake":
retStr = changeCase.snakeCase(str);
break;
default:
throw new Error("Unknown case style");
}

View File

@ -211,7 +211,7 @@ function checkYargsParameters(options: options): options {
},
cp: {
alias: "case-property",
choices: ["pascal", "camel", "none"],
choices: ["pascal", "camel", "snake", "none"],
default: options.generationOptions.convertCaseProperty,
describe: "Convert property names to specified case",
},

View File

@ -30,6 +30,12 @@ describe("Model customization phase", async () => {
options: { name: "name" },
tscName: "name",
tscType: "string"
},
{
type: "character varying",
options: { name: "complicatedName" },
tscName: "complexName",
tscType: "string"
}
],
indices: [
@ -251,6 +257,7 @@ describe("Model customization phase", async () => {
.toString();
expect(postContent).to.contain("Title: string;");
expect(postAuthorContent).to.contain("Posts: Post[];");
expect(postAuthorContent).to.contain("ComplexName: string;");
compileGeneratedModel(generationOptions.resultsPath, [""], false);
});
@ -279,9 +286,39 @@ describe("Model customization phase", async () => {
.toString();
expect(postContent).to.contain("title: string;");
expect(postAuthorContent).to.contain("posts: Post[];");
expect(postAuthorContent).to.contain("complexName: string;");
compileGeneratedModel(generationOptions.resultsPath, [""]);
});
it("snake_case", () => {
const data = generateSampleData();
const generationOptions = generateGenerationOptions();
clearGenerationDir();
generationOptions.convertCaseProperty = "snake";
const customizedModel = modelCustomizationPhase(
data,
generationOptions,
{}
);
modelGenerationPhase(
getDefaultConnectionOptions(),
generationOptions,
customizedModel
);
const filesGenPath = path.resolve(resultsPath, "entities");
const postContent = fs
.readFileSync(path.resolve(filesGenPath, "Post.ts"))
.toString();
const postAuthorContent = fs
.readFileSync(path.resolve(filesGenPath, "PostAuthor.ts"))
.toString();
expect(postContent).to.contain("title: string;");
expect(postAuthorContent).to.contain("posts: Post[];");
expect(postAuthorContent).to.contain("complex_name: string;");
compileGeneratedModel(generationOptions.resultsPath, [""],false);
});
});
describe("EOL", async () => {
it("LF", () => {