From dd2399f4fe9c22a6c65e43373b5a813e74e38d6f Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 26 Apr 2017 19:23:22 +0200 Subject: [PATCH] disabled mustache char escaping; couple fixes in mustache template --- src/Engine.ts | 27 +++++++++++++++------------ src/entity.mst | 4 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index a220b12..a280b1f 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -1,6 +1,6 @@ import { MssqlDriver } from './drivers/MssqlDriver' import { AbstractDriver } from "./drivers/AbstractDriver"; -import {DatabaseModel} from './models/DatabaseModel' +import { DatabaseModel } from './models/DatabaseModel' import * as Mustache from 'mustache' import fs = require('fs'); import path = require('path') @@ -21,16 +21,16 @@ export class Engine { return true; } private async getEntitiesInfo(database: string, server: string, port: number, user: string, password: string): Promise { - return await this.driver.GetDataFromServer(database, server, port, user, password) - + return await this.driver.GetDataFromServer(database, server, port, user, password) + } private createModelFromMetadata(databaseModel: DatabaseModel) { - let templatePath = path.resolve(__dirname,'entity.mst') - let template = fs.readFileSync(templatePath,'UTF-8'); + let templatePath = path.resolve(__dirname, 'entity.mst') + let template = fs.readFileSync(templatePath, 'UTF-8'); //TODO:get results path to argvs, check if dir exists before - let resultPath = path.resolve(__dirname,'../results') + let resultPath = path.resolve(__dirname, '../results') //TODO:Refactor to new method - fs.writeFileSync(path.resolve(resultPath,'tsconfig.json'),`{"compilerOptions": { + fs.writeFileSync(path.resolve(resultPath, 'tsconfig.json'), `{"compilerOptions": { "lib": ["es5", "es6"], "target": "es6", "module": "commonjs", @@ -38,12 +38,15 @@ export class Engine { "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true - }}`,{encoding:'UTF-8',flag:'w'}); - //TODO:Create ormconfig file + }}`, { encoding: 'UTF-8', flag: 'w' }); + //TODO:Create ormconfig file + Mustache.escape = function (value) { + return value; + }; databaseModel.entities.forEach(element => { - let resultFilePath = path.resolve(resultPath,element.EntityName+'.ts'); - let rendered = Mustache.render(template, element); - fs.writeFileSync(resultFilePath,rendered,{encoding:'UTF-8',flag:'w'}) + let resultFilePath = path.resolve(resultPath, element.EntityName + '.ts'); + let rendered = Mustache.render(template, element); + fs.writeFileSync(resultFilePath, rendered, { encoding: 'UTF-8', flag: 'w' }) }); } } diff --git a/src/entity.mst b/src/entity.mst index 18cbb7a..d1757b8 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -1,5 +1,5 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, JoinTable} from "typeorm"; -{{{relationImports}}} +{{relationImports}} @Entity() {{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) @@ -7,7 +7,7 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Joi {{#Columns}} - @Column("{{sql_type}}",{ {{#is_nullable}}nullable:true,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}{{#default}}default:{{default}},{{/default}}{{#numericPrecision}}precision:{numericPrecision},{{/numericPrecision}}{{#numericScale}}scale:{{numericScale}},{{/numericScale}}{{#isPrimary}}primary:{{isPrimary}},{{/isPrimary}}}){{#relations}} + @Column("{{sql_type}}",{ {{#is_nullable}}nullable:true,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}{{#default}}default:{{default}},{{/default}}{{#numericPrecision}}precision:{{numericPrecision}},{{/numericPrecision}}{{#numericScale}}scale:{{numericScale}},{{/numericScale}}{{#isPrimary}}primary:{{isPrimary}},{{/isPrimary}}}){{#relations}} @{{relationType}}(type=>{{relatedTable}},x=>x.{{relatedColumn}}){{#isOwner}} @JoinTable(){{/isOwner}}{{/relations}} {{name}}:{{ts_type}};