From e4761d8f5023e5f7e07d61c0061cac3b9e345b38 Mon Sep 17 00:00:00 2001 From: zhangrubing Date: Thu, 20 Aug 2020 10:05:23 +0800 Subject: [PATCH 1/3] Add snake case properties --- src/IGenerationOptions.ts | 2 +- src/ModelGeneration.ts | 3 +++ src/index.ts | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/IGenerationOptions.ts b/src/IGenerationOptions.ts index 13bb2d8..f41563e 100644 --- a/src/IGenerationOptions.ts +++ b/src/IGenerationOptions.ts @@ -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; diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index beb96e6..f9e3de4 100644 --- a/src/ModelGeneration.ts +++ b/src/ModelGeneration.ts @@ -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"); } diff --git a/src/index.ts b/src/index.ts index 573b635..ea72383 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import IGenerationOptions, { import fs = require("fs-extra"); import inquirer = require("inquirer"); import path = require("path"); +import { string } from "yargs"; // eslint-disable-next-line @typescript-eslint/no-floating-promises CliLogic(); @@ -211,7 +212,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", }, From 8bbe1a617d9c5806cb97ca44df1c0b32bc378638 Mon Sep 17 00:00:00 2001 From: zhangrubing Date: Thu, 20 Aug 2020 10:14:44 +0800 Subject: [PATCH 2/3] Remove no use import --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ea72383..8aab4a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,6 @@ import IGenerationOptions, { import fs = require("fs-extra"); import inquirer = require("inquirer"); import path = require("path"); -import { string } from "yargs"; // eslint-disable-next-line @typescript-eslint/no-floating-promises CliLogic(); From 0fad9be4420209a2560ea44af71a773438a151a5 Mon Sep 17 00:00:00 2001 From: kononnable Date: Sat, 22 Aug 2020 09:35:21 +0200 Subject: [PATCH 3/3] add test --- CHANGELOG.md | 3 ++ .../modelCustomization.test.ts | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da3486c..de6bb39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/test/modelCustomization/modelCustomization.test.ts b/test/modelCustomization/modelCustomization.test.ts index 8443f1f..e42aaa0 100644 --- a/test/modelCustomization/modelCustomization.test.ts +++ b/test/modelCustomization/modelCustomization.test.ts @@ -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", () => {