From c25544959ae162c72cc42f5138fc23be926dce1f Mon Sep 17 00:00:00 2001 From: Harald Fielker Date: Wed, 6 Sep 2017 14:49:56 +0200 Subject: [PATCH] added a hack to force columns to be required --- .../sequelize.ts.dal.generator.ts | 36 ++++++++++++++++++- .../generatorconfig.sequelize.ts.dal.ts | 1 + package.json | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/generator/sequelize.ts.dal/sequelize.ts.dal.generator.ts b/lib/generator/sequelize.ts.dal/sequelize.ts.dal.generator.ts index 605bc20..4c9799b 100644 --- a/lib/generator/sequelize.ts.dal/sequelize.ts.dal.generator.ts +++ b/lib/generator/sequelize.ts.dal/sequelize.ts.dal.generator.ts @@ -1,7 +1,7 @@ import { Filter } from 'nicassa-parser-db'; import { GeneratorConfigBasic } from '../../persistance/generatorconfig.basic'; -import { GeneratorConfigSequelizeTSDal } from '../../persistance/generatorconfig.sequelize.ts.dal'; +import { GeneratorConfigSequelizeTSDal, CustomJSDocDecorator } from '../../persistance/generatorconfig.sequelize.ts.dal'; import { ModelNaming } from '../../persistance/naming/modelnaming'; import { CaseEnum } from '../../persistance/naming/case.enum'; @@ -90,8 +90,42 @@ export class SequelizeTSDalGenerator extends BaseGenerator { } + protected async updateSymbolTableForJSDecorators(): Promise { + if (this.generatorConfig.createJSDocDecorators == null || + this.generatorConfig.createJSDocDecorators === undefined || + this.generatorConfig.createJSDocDecorators == false || + this.generatorConfig.customDJSDocDecorators == null || + this.generatorConfig.customDJSDocDecorators === undefined || + this.generatorConfig.customDJSDocDecorators.length == 0) { + return await false; + } + + const customDecoratorsMap: { [key: string]: CustomJSDocDecorator } = {}; + for (let item of this.generatorConfig.customDJSDocDecorators) { + const key = item.entity.toLowerCase(); + customDecoratorsMap[key] = item; + } + + for (let item of this.dbSymbolTable.columns) { + const key = item.table.name.toLowerCase() + '.' + item.name.toLowerCase(); + if (customDecoratorsMap.hasOwnProperty(key)) { + const decorator = customDecoratorsMap[key]; + if (decorator.required == null || + decorator.required === undefined || + !decorator.required) { + continue; + } + // set to not nullable + item.nullable = false; + } + } + + return await true; + } + protected async generateCode(): Promise { this.dbSymbolTable = DBSymbolTableReader.readFromJsonString(this.nicassaJson, this.generatorConfig.filter, this.typeMapper); + await this.updateSymbolTableForJSDecorators(); let createPackageJson: boolean = false; let createIndex: boolean = false; diff --git a/lib/persistance/generatorconfig.sequelize.ts.dal.ts b/lib/persistance/generatorconfig.sequelize.ts.dal.ts index ab2cd3c..81b38a8 100644 --- a/lib/persistance/generatorconfig.sequelize.ts.dal.ts +++ b/lib/persistance/generatorconfig.sequelize.ts.dal.ts @@ -6,6 +6,7 @@ import { GeneratorConfigBasic } from './generatorconfig.basic'; export interface CustomJSDocDecorator { entity: string; + required?: boolean; enabled?: boolean; decorator: string[]; } diff --git a/package.json b/package.json index 84c02c5..b0f588d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nicassa-generator", "description": "CLI based code generator for Sequelize, Node, Typescript, Angular 2, Android, Swagger, ORM Lite", - "version": "0.0.39", + "version": "0.0.40", "author": "Harald Fielker ", "repository": { "type": "git",