UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

127 lines 4.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Database = void 0; const lodash_1 = require("lodash"); const constants_1 = require("../postgres/constants"); class Database { constructor(tables = []) { this.tablesMap = {}; this.tables = []; for (let table of tables) { table = table.clone(); this.tables.push(table); this.tablesMap[table.toString()] = table; } this.functions = []; this.functionsMap = {}; this.aggregators = [ "count", "max", "min", "sum", "avg", "array_agg", "string_agg", "bool_or", "bool_and", "every" ]; } getAllColumns() { return lodash_1.flatMap(this.tables, table => table.columns); } getAllTriggers() { return lodash_1.flatMap(this.tables, table => table.triggers); } getColumn(tableId, column) { const dbTable = this.tables.find(table => table.name === tableId.name && table.schema === tableId.schema); return dbTable === null || dbTable === void 0 ? void 0 : dbTable.getColumn(column); } getTable(tableId) { return this.tablesMap[tableId.toString()]; } getTriggersByProcedure(procedure) { return lodash_1.flatMap(this.tables, (table) => { return table.triggers.filter(trigger => trigger.procedure.schema === procedure.schema && trigger.procedure.name === procedure.name); }); } setTable(table) { if (!this.tablesMap[table.toString()]) { table = table.clone(); this.tables.push(table); this.tablesMap[table.toString()] = table; } } addFunctions(functions) { var _a; var _b; for (const func of functions) { this.functions.push(func); const name = func.name.slice(0, constants_1.MAX_NAME_LENGTH); (_a = (_b = this.functionsMap)[name]) !== null && _a !== void 0 ? _a : (_b[name] = []); this.functionsMap[name].push(func); } } addTrigger(trigger) { const table = this.getTable(trigger.table); if (!table) { throw new Error(`unknown table "${trigger.table}" for trigger "${trigger.name}"`); } table.addTrigger(trigger); } addIndex(index) { const table = this.getTable(index.table); if (!table) { throw new Error(`unknown table "${index.table}" for index "${index.name}"`); } table.addIndex(index); } applyMigration(migration) { this.addFunctions(migration.toCreate.functions); for (const trigger of migration.toCreate.triggers) { this.addTrigger(trigger); } for (const column of migration.toCreate.columns) { const table = this.getTable(column.table); if (table) { table.addColumn(column); } } this.functions = this.functions.filter(existentFunc => !migration.toDrop.functions.some(dropFunc => existentFunc.getSignature() === dropFunc.getSignature())); for (const dropFunc of migration.toDrop.functions) { const name = dropFunc.name.slice(0, constants_1.MAX_NAME_LENGTH); this.functionsMap[name] = this.functionsMap[name].filter(existentFunc => existentFunc.getSignature() !== dropFunc.getSignature()); } for (const trigger of migration.toDrop.triggers) { const table = this.getTable(trigger.table); if (table) { table.removeTrigger(trigger); } } for (const column of migration.toDrop.columns) { const table = this.getTable(column.table); if (table) { table.removeColumn(column); } } } addAggregators(newAggregators) { for (const aggName of newAggregators) { if (!this.aggregators.includes(aggName)) { this.aggregators.push(aggName); } } } allCacheTriggers() { return lodash_1.flatMap(this.tables, table => table.triggers) .filter(trigger => !!trigger.cacheSignature); } getFunctions(name) { var _a; return (_a = this.functionsMap[name.slice(0, constants_1.MAX_NAME_LENGTH)]) !== null && _a !== void 0 ? _a : []; } } exports.Database = Database; //# sourceMappingURL=Database.js.map