grunt-generate-database
Advanced tools
Comparing version
@@ -27,17 +27,28 @@ "use strict"; | ||
} | ||
const fileCreator = (environment, schema, data, keywords, pathToWrappers, name, templateFile, templateType, hStr) => { | ||
const functionRendererTemplate = environment.render(templateFile, { | ||
hStr, | ||
schema, | ||
data, | ||
keywords, | ||
}); | ||
const futureFunctionFileName = `${templateType}${shared_1.extension}`; | ||
createFile(functionRendererTemplate, pathToWrappers, name, schema.namespace, futureFunctionFileName); | ||
}; | ||
function createFileForTriggersCreateForSchema(declaration, hStr, schema) { | ||
let scriptFolder = path.resolve(__dirname, `view/${declaration.db}/`); | ||
const environment = configureEnvironment(scriptFolder); | ||
const functionRendererTemplate = environment.render(shared_1.triggerFunctionTemplateFileName, { | ||
hStr, | ||
schema, | ||
data: declaration, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile(functionRendererTemplate, declaration.pathToDBWrappers, declaration.name, schema.namespace, `${shared_1.CreateTemplateType.function}${shared_1.extension}`); | ||
const triggerRendererTemplate = environment.render(shared_1.triggerTemplateFileName, { | ||
data: declaration, schema, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile(triggerRendererTemplate, declaration.pathToDBWrappers, declaration.name, schema.namespace, `${shared_1.CreateTemplateType.trigger}${shared_1.extension}`); | ||
const { pathToDBWrappers, name, db } = declaration; | ||
const keywords = dbTypeKeywords[db]; | ||
switch (db) { | ||
case "mssql": | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerMssqlFileName, shared_1.CreateTemplateType.function, hStr); | ||
break; | ||
case "postgres": | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerFunctionTemplateFileName, shared_1.CreateTemplateType.function, hStr); | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerTemplateFileName, shared_1.CreateTemplateType.trigger, hStr); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
@@ -48,6 +59,6 @@ function createDBCreator(declarations) { | ||
const environment = configureEnvironment(scriptFolder); | ||
for (let i = 0; i < declaration.schemas.length; i++) { | ||
const rendererTemplate = environment | ||
.render(shared_1.dbWrapperTemplateFileName, { declaration: declaration, index: i, keywords: dbTypeKeywords[declaration.db] }); | ||
createFile(rendererTemplate, declaration.pathToDBWrappers, declaration.name, declaration.schemas[i].namespace, `${declaration.schemas[i].namespace}${shared_1.CreateTemplateType.dbWrapper}${shared_1.extension}`); | ||
for (let index = 0; index < declaration.schemas.length; index++) { | ||
const keywords = (dbTypeKeywords[declaration.db] || []); | ||
const rendererTemplate = environment.render(shared_1.dbWrapperTemplateFileName, { declaration, index, keywords }); | ||
createFile(rendererTemplate, declaration.pathToDBWrappers, declaration.name, declaration.schemas[index].namespace, `${declaration.schemas[index].namespace}${shared_1.CreateTemplateType.dbWrapper}${shared_1.extension}`); | ||
} | ||
@@ -76,4 +87,5 @@ }); | ||
if (dec.name === shared_1.DecoratorsName.GenerateHistory && _class.name.toLowerCase() === table.modelName.toLowerCase()) { | ||
table.historyPath = dec.arguments[0].valueOf()["historyPath"] + "/" | ||
+ _class.name.charAt(0).toLowerCase() + _class.name.slice(1); | ||
const historyPath = dec.arguments[0].valueOf()["historyPath"]; | ||
const classFirstChar = _class.name.charAt(0).toLowerCase(); | ||
table.historyPath = `${historyPath}/${classFirstChar}${_class.name.slice(1)}`; | ||
} | ||
@@ -101,4 +113,5 @@ }); | ||
}); | ||
let json = ts_file_parser_1.parseStruct(stringFile, {}, shared_1.emptyString); | ||
if (shared_1.triggerEnabledDbTypes.find((type) => type === declarations[index].db) !== undefined) { | ||
const json = ts_file_parser_1.parseStruct(stringFile, {}, shared_1.emptyString); | ||
const isTriggerCreateEnable = shared_1.triggerEnabledDbTypes.find(type => type === declarations[index].db); | ||
if (isTriggerCreateEnable) { | ||
createFileForTriggersCreateForSchema(declarations[index], json, schms[innerIndex]); | ||
@@ -105,0 +118,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.emptyString = ""; | ||
exports.triggerEnabledDbTypes = ["postgres"]; | ||
exports.triggerEnabledDbTypes = ["postgres", "mssql"]; | ||
exports.triggerFunctionTemplateFileName = "createTriggerFunctionTemplate.njk"; | ||
exports.triggerTemplateFileName = "createTriggerTemplate.njk"; | ||
exports.triggerMssqlFileName = "createTrigger.njk"; | ||
exports.dbWrapperTemplateFileName = "createDBWrapper.njk"; | ||
@@ -8,0 +9,0 @@ exports.utf8String = "utf-8"; |
{ | ||
"name": "grunt-generate-database", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"description": "Grunt task for create database generation scripts by models description", | ||
@@ -5,0 +5,0 @@ "private": false, |
import "reflect-metadata"; | ||
import { configure } from "nunjucks"; | ||
import { configure, Environment } from "nunjucks"; | ||
import * as path from "path"; | ||
@@ -19,3 +19,4 @@ import {DbOptions, Options} from "./model/options"; | ||
pathToDeclaration, | ||
DecoratorsName | ||
DecoratorsName, | ||
triggerMssqlFileName | ||
} from "./shared"; | ||
@@ -44,29 +45,72 @@ | ||
const fileCreator = ( | ||
environment: Environment, | ||
schema: Schema, | ||
data: Declaration, | ||
keywords: string[], | ||
pathToWrappers: string, | ||
name: string, | ||
templateFile: string, | ||
templateType: CreateTemplateType, | ||
hStr?: Module | ||
) => { | ||
const functionRendererTemplate = environment.render(templateFile, { | ||
hStr, | ||
schema, | ||
data, | ||
keywords, | ||
}); | ||
const futureFunctionFileName = `${templateType}${extension}`; | ||
createFile(functionRendererTemplate, pathToWrappers, name, schema.namespace, futureFunctionFileName); | ||
}; | ||
function createFileForTriggersCreateForSchema(declaration: Declaration, hStr: Module, schema: Schema): void { | ||
let scriptFolder = path.resolve(__dirname, `view/${declaration.db}/`); | ||
const environment = configureEnvironment(scriptFolder); | ||
const {pathToDBWrappers, name, db} = declaration; | ||
const keywords = dbTypeKeywords[db]; | ||
switch (db) { | ||
case "mssql": | ||
fileCreator( | ||
environment, | ||
schema, | ||
declaration, | ||
keywords, | ||
pathToDBWrappers, | ||
name, | ||
triggerMssqlFileName, | ||
CreateTemplateType.function, | ||
hStr | ||
); | ||
break; | ||
case "postgres": | ||
fileCreator( | ||
environment, | ||
schema, | ||
declaration, | ||
keywords, | ||
pathToDBWrappers, | ||
name, | ||
triggerFunctionTemplateFileName, | ||
CreateTemplateType.function, | ||
hStr | ||
); | ||
const functionRendererTemplate = environment.render(triggerFunctionTemplateFileName, { | ||
hStr, | ||
schema, | ||
data: declaration, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile( | ||
functionRendererTemplate, | ||
declaration.pathToDBWrappers, | ||
declaration.name, schema.namespace, | ||
`${CreateTemplateType.function}${extension}` | ||
); | ||
const triggerRendererTemplate = environment.render(triggerTemplateFileName, { | ||
data: declaration, schema, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile( | ||
triggerRendererTemplate, | ||
declaration.pathToDBWrappers, | ||
declaration.name, schema.namespace, | ||
`${CreateTemplateType.trigger}${extension}` | ||
); | ||
fileCreator( | ||
environment, | ||
schema, | ||
declaration, | ||
keywords, | ||
pathToDBWrappers, | ||
name, | ||
triggerTemplateFileName, | ||
CreateTemplateType.trigger, | ||
hStr | ||
); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
@@ -78,5 +122,5 @@ | ||
const environment = configureEnvironment(scriptFolder); | ||
for (let i = 0 ; i < declaration.schemas.length; i++) { | ||
const rendererTemplate = environment | ||
.render(dbWrapperTemplateFileName, {declaration: declaration, index: i, keywords: dbTypeKeywords[declaration.db] }); | ||
for (let index = 0 ; index < declaration.schemas.length; index++) { | ||
const keywords = (dbTypeKeywords[declaration.db] || []); | ||
const rendererTemplate = environment.render(dbWrapperTemplateFileName, {declaration, index, keywords }); | ||
createFile( | ||
@@ -86,4 +130,4 @@ rendererTemplate, | ||
declaration.name, | ||
declaration.schemas[i].namespace, | ||
`${declaration.schemas[i].namespace}${CreateTemplateType.dbWrapper}${extension}` | ||
declaration.schemas[index].namespace, | ||
`${declaration.schemas[index].namespace}${CreateTemplateType.dbWrapper}${extension}` | ||
); | ||
@@ -115,4 +159,5 @@ } | ||
if (dec.name === DecoratorsName.GenerateHistory && _class.name.toLowerCase() === table.modelName.toLowerCase()) { | ||
table.historyPath = dec.arguments[0].valueOf()["historyPath"] + "/" | ||
+ _class.name.charAt(0).toLowerCase() + _class.name.slice(1); | ||
const historyPath = dec.arguments[0].valueOf()["historyPath"]; | ||
const classFirstChar = _class.name.charAt(0).toLowerCase(); | ||
table.historyPath = `${historyPath}/${classFirstChar}${_class.name.slice(1)}`; | ||
} | ||
@@ -141,5 +186,6 @@ }); | ||
}); | ||
let json = parseStruct(stringFile, {}, emptyString); | ||
if (triggerEnabledDbTypes.find((type) => type === declarations[index].db) !== undefined) { | ||
createFileForTriggersCreateForSchema (declarations[index], json , schms[innerIndex]); | ||
const json = parseStruct(stringFile, {}, emptyString); | ||
const isTriggerCreateEnable = triggerEnabledDbTypes.find(type => type === declarations[index].db); | ||
if (isTriggerCreateEnable) { | ||
createFileForTriggersCreateForSchema(declarations[index], json , schms[innerIndex]); | ||
} | ||
@@ -146,0 +192,0 @@ stringFile = emptyString; |
export const emptyString = ""; | ||
export const triggerEnabledDbTypes: string[] = ["postgres"]; | ||
export const triggerEnabledDbTypes: string[] = ["postgres", "mssql"]; | ||
export const triggerFunctionTemplateFileName = "createTriggerFunctionTemplate.njk"; | ||
export const triggerTemplateFileName = "createTriggerTemplate.njk"; | ||
export const triggerMssqlFileName = "createTrigger.njk"; | ||
export const dbWrapperTemplateFileName = "createDBWrapper.njk"; | ||
@@ -7,0 +8,0 @@ export const utf8String = "utf-8"; |
@@ -27,17 +27,28 @@ "use strict"; | ||
} | ||
const fileCreator = (environment, schema, data, keywords, pathToWrappers, name, templateFile, templateType, hStr) => { | ||
const functionRendererTemplate = environment.render(templateFile, { | ||
hStr, | ||
schema, | ||
data, | ||
keywords, | ||
}); | ||
const futureFunctionFileName = `${templateType}${shared_1.extension}`; | ||
createFile(functionRendererTemplate, pathToWrappers, name, schema.namespace, futureFunctionFileName); | ||
}; | ||
function createFileForTriggersCreateForSchema(declaration, hStr, schema) { | ||
let scriptFolder = path.resolve(__dirname, `view/${declaration.db}/`); | ||
const environment = configureEnvironment(scriptFolder); | ||
const functionRendererTemplate = environment.render(shared_1.triggerFunctionTemplateFileName, { | ||
hStr, | ||
schema, | ||
data: declaration, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile(functionRendererTemplate, declaration.pathToDBWrappers, declaration.name, schema.namespace, `${shared_1.CreateTemplateType.function}${shared_1.extension}`); | ||
const triggerRendererTemplate = environment.render(shared_1.triggerTemplateFileName, { | ||
data: declaration, schema, | ||
keywords: dbTypeKeywords[declaration.db], | ||
}); | ||
createFile(triggerRendererTemplate, declaration.pathToDBWrappers, declaration.name, schema.namespace, `${shared_1.CreateTemplateType.trigger}${shared_1.extension}`); | ||
const { pathToDBWrappers, name, db } = declaration; | ||
const keywords = dbTypeKeywords[db]; | ||
switch (db) { | ||
case "mssql": | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerMssqlFileName, shared_1.CreateTemplateType.function, hStr); | ||
break; | ||
case "postgres": | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerFunctionTemplateFileName, shared_1.CreateTemplateType.function, hStr); | ||
fileCreator(environment, schema, declaration, keywords, pathToDBWrappers, name, shared_1.triggerTemplateFileName, shared_1.CreateTemplateType.trigger, hStr); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
@@ -48,6 +59,6 @@ function createDBCreator(declarations) { | ||
const environment = configureEnvironment(scriptFolder); | ||
for (let i = 0; i < declaration.schemas.length; i++) { | ||
const rendererTemplate = environment | ||
.render(shared_1.dbWrapperTemplateFileName, { declaration: declaration, index: i, keywords: dbTypeKeywords[declaration.db] }); | ||
createFile(rendererTemplate, declaration.pathToDBWrappers, declaration.name, declaration.schemas[i].namespace, `${declaration.schemas[i].namespace}${shared_1.CreateTemplateType.dbWrapper}${shared_1.extension}`); | ||
for (let index = 0; index < declaration.schemas.length; index++) { | ||
const keywords = (dbTypeKeywords[declaration.db] || []); | ||
const rendererTemplate = environment.render(shared_1.dbWrapperTemplateFileName, { declaration, index, keywords }); | ||
createFile(rendererTemplate, declaration.pathToDBWrappers, declaration.name, declaration.schemas[index].namespace, `${declaration.schemas[index].namespace}${shared_1.CreateTemplateType.dbWrapper}${shared_1.extension}`); | ||
} | ||
@@ -76,4 +87,5 @@ }); | ||
if (dec.name === shared_1.DecoratorsName.GenerateHistory && _class.name.toLowerCase() === table.modelName.toLowerCase()) { | ||
table.historyPath = dec.arguments[0].valueOf()["historyPath"] + "/" | ||
+ _class.name.charAt(0).toLowerCase() + _class.name.slice(1); | ||
const historyPath = dec.arguments[0].valueOf()["historyPath"]; | ||
const classFirstChar = _class.name.charAt(0).toLowerCase(); | ||
table.historyPath = `${historyPath}/${classFirstChar}${_class.name.slice(1)}`; | ||
} | ||
@@ -101,4 +113,5 @@ }); | ||
}); | ||
let json = ts_file_parser_1.parseStruct(stringFile, {}, shared_1.emptyString); | ||
if (shared_1.triggerEnabledDbTypes.find((type) => type === declarations[index].db) !== undefined) { | ||
const json = ts_file_parser_1.parseStruct(stringFile, {}, shared_1.emptyString); | ||
const isTriggerCreateEnable = shared_1.triggerEnabledDbTypes.find(type => type === declarations[index].db); | ||
if (isTriggerCreateEnable) { | ||
createFileForTriggersCreateForSchema(declarations[index], json, schms[innerIndex]); | ||
@@ -105,0 +118,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.emptyString = ""; | ||
exports.triggerEnabledDbTypes = ["postgres"]; | ||
exports.triggerEnabledDbTypes = ["postgres", "mssql"]; | ||
exports.triggerFunctionTemplateFileName = "createTriggerFunctionTemplate.njk"; | ||
exports.triggerTemplateFileName = "createTriggerTemplate.njk"; | ||
exports.triggerMssqlFileName = "createTrigger.njk"; | ||
exports.dbWrapperTemplateFileName = "createDBWrapper.njk"; | ||
@@ -8,0 +9,0 @@ exports.utf8String = "utf-8"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
200660
8.38%81
3.85%4680
1.58%