UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

149 lines 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FilesState = void 0; const lodash_1 = require("lodash"); const File_1 = require("./File"); class FilesState { constructor(files = []) { this.cacheMap = {}; this.functionsMap = {}; this.triggersMap = {}; this.files = files; } allNotHelpersFiles() { return this.files.filter(file => file.folder !== "HELPERS" && !file.name.startsWith("CM_")); } allCache() { return lodash_1.flatMap(this.files, file => file.content.cache); } allTriggers() { return lodash_1.flatMap(this.files, file => file.content.triggers); } allFunctions() { return lodash_1.flatMap(this.files, file => file.content.functions); } allNotHelperFunctions() { return lodash_1.flatMap(this.allNotHelpersFiles(), file => file.content.functions); } getCache(signature) { return this.allCache().find(cache => cache.getSignature() === signature); } getCachesForTable(forTable) { var _a; return ((_a = this.cacheMap[forTable.toString()]) !== null && _a !== void 0 ? _a : []).slice(); } getFunctionsByName(name) { var _a; return ((_a = this.functionsMap[name]) !== null && _a !== void 0 ? _a : []).slice(); } getTableTriggers(table) { var _a; return ((_a = this.triggersMap[table.toString()]) !== null && _a !== void 0 ? _a : []).slice(); } getTriggerFunction(trigger) { return (this.functionsMap[trigger.procedure.name] || []).find(func => func.schema === trigger.procedure.schema); } addFile(fileOrParams) { var _a, _b, _c; var _d, _e, _f, _g; let file; if (fileOrParams instanceof File_1.File) { file = fileOrParams; } else { file = new File_1.File(fileOrParams); } this.checkDuplicate(file); this.files.push(file); for (const func of file.content.functions) { (_a = (_d = this.functionsMap)[_e = func.name]) !== null && _a !== void 0 ? _a : (_d[_e] = []); this.functionsMap[func.name].push(func); } for (const trigger of file.content.triggers) { const table = trigger.table.toString(); (_b = (_f = this.triggersMap)[table]) !== null && _b !== void 0 ? _b : (_f[table] = []); this.triggersMap[table].push(trigger); } for (const cache of file.content.cache) { const table = cache.for.table.toString(); (_c = (_g = this.cacheMap)[table]) !== null && _c !== void 0 ? _c : (_g[table] = []); this.cacheMap[table].push(cache); } } removeFile(file) { var _a, _b, _c; var _d, _e, _f, _g; const fileIndex = this.files.indexOf(file); if (fileIndex !== -1) { this.files.splice(fileIndex, 1); } for (const deletedFunc of file.content.functions) { (_a = (_d = this.functionsMap)[_e = deletedFunc.name]) !== null && _a !== void 0 ? _a : (_d[_e] = []); this.functionsMap[deletedFunc.name] = this.functionsMap[deletedFunc.name] .filter(func => func.getSignature() != deletedFunc.getSignature()); } for (const deletedTrigger of file.content.triggers) { const table = deletedTrigger.table.toString(); (_b = (_f = this.triggersMap)[table]) !== null && _b !== void 0 ? _b : (_f[table] = []); this.triggersMap[table] = this.triggersMap[table] .filter(trigger => trigger.getSignature() != deletedTrigger.getSignature()); } for (const deletedCache of file.content.cache) { const table = deletedCache.for.table.toString(); (_c = (_g = this.cacheMap)[table]) !== null && _c !== void 0 ? _c : (_g[table] = []); this.cacheMap[table] = this.cacheMap[table] .filter(cache => cache.getSignature() !== deletedCache.getSignature()); } } checkDuplicate(file) { file.content.functions.forEach(func => this.checkDuplicateFunction(func)); file.content.triggers.forEach(trigger => { this.checkDuplicateTrigger(trigger); }); file.content.cache.forEach(cache => { this.checkDuplicateCache(cache); }); } checkDuplicateFunction(func) { const identify = func.getSignature(); const hasDuplicate = this.getFunctionsByName(func.name).some(someFunc => { const someIdentify = someFunc.getSignature(); return identify === someIdentify; }); if (hasDuplicate) { throw new Error(`duplicated function ${identify}`); } } checkDuplicateTrigger(trigger) { const identify = trigger.getSignature(); const hasDuplicate = this.getTableTriggers(trigger.table).some(someTrigger => { const someIdentify = someTrigger.getSignature(); return identify === someIdentify; }); if (hasDuplicate) { throw new Error(`duplicated trigger ${identify}`); } } checkDuplicateCache(cache) { const identify = cache.getSignature(); const cacheColumns = cache.select.columns.map(col => col.name); ; for (const someCache of this.getCachesForTable(cache.for.table)) { // duplicated cache name if (someCache.getSignature() === identify) { throw new Error(`duplicated ${identify}`); } // duplicate cache columns if (someCache.for.table.equal(cache.for.table)) { const someCacheColumns = someCache.select.columns.map(col => col.name); const duplicatedColumns = someCacheColumns.filter(columnName => cacheColumns.includes(columnName)); if (duplicatedColumns.length > 0) { throw new Error(`duplicated columns: ${duplicatedColumns} by cache: ${cache.name}, ${someCache.name}`); } } } } } exports.FilesState = FilesState; //# sourceMappingURL=FilesState.js.map