UNPKG

dbgate-tools

Version:

Auxiliary tools for other DbGate packages.

304 lines (303 loc) 12.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.adaptDatabaseInfo = exports.skipDbGateInternalObjects = exports.removePreloadedRowsFromStructure = exports.detectChangesInPreloadedRows = exports.skipNamesInStructureByRegex = exports.replaceSchemaInStructure = exports.getSchemasUsedByStructure = exports.filterStructureBySchema = exports.isCollectionInfo = exports.isViewInfo = exports.isTableInfo = exports.isTableColumnUnique = exports.extendDatabaseInfoFromApps = exports.extendDatabaseInfo = exports.extendTableInfo = exports.addTableDependencies = void 0; const flatten_1 = __importDefault(require("lodash/flatten")); const uniq_1 = __importDefault(require("lodash/uniq")); const keys_1 = __importDefault(require("lodash/keys")); const compact_1 = __importDefault(require("lodash/compact")); function addTableDependencies(db) { if (!db.tables) { return db; } const allForeignKeys = (0, flatten_1.default)(db.tables.map(x => (x === null || x === void 0 ? void 0 : x.foreignKeys) || [])); return { ...db, tables: (0, compact_1.default)(db.tables).map(table => ({ ...table, dependencies: allForeignKeys.filter(x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName), })), }; } exports.addTableDependencies = addTableDependencies; function extendTableInfo(table) { return { ...table, objectTypeField: 'tables', columns: (table.columns || []).map(column => ({ pureName: table.pureName, schemaName: table.schemaName, ...column, })), primaryKey: table.primaryKey ? { ...table.primaryKey, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'primaryKey', } : undefined, sortingKey: table.sortingKey ? { ...table.sortingKey, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'sortingKey', } : undefined, foreignKeys: (table.foreignKeys || []).map(cnt => ({ ...cnt, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'foreignKey', })), indexes: (table.indexes || []).map(cnt => ({ ...cnt, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'index', })), checks: (table.checks || []).map(cnt => ({ ...cnt, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'check', })), uniques: (table.uniques || []).map(cnt => ({ ...cnt, pureName: table.pureName, schemaName: table.schemaName, constraintType: 'unique', })), }; } exports.extendTableInfo = extendTableInfo; function fillDatabaseExtendedInfo(db) { return { ...db, tables: (db.tables || []).map(extendTableInfo), collections: (db.collections || []).map(obj => ({ ...obj, objectTypeField: 'collections', })), views: (db.views || []).map(obj => ({ ...obj, objectTypeField: 'views', })), matviews: (db.matviews || []).map(obj => ({ ...obj, objectTypeField: 'matviews', })), procedures: (db.procedures || []).map(obj => ({ ...obj, objectTypeField: 'procedures', parameters: (obj.parameters || []).map(param => ({ ...param, pureName: obj.pureName, schemaName: obj.schemaName, })), })), functions: (db.functions || []).map(obj => ({ ...obj, objectTypeField: 'functions', parameters: (obj.parameters || []).map(param => ({ ...param, pureName: obj.pureName, schemaName: obj.schemaName, })), })), triggers: (db.triggers || []).map(obj => ({ ...obj, objectTypeField: 'triggers', })), }; } function extendDatabaseInfo(db) { return fillDatabaseExtendedInfo(addTableDependencies(db)); } exports.extendDatabaseInfo = extendDatabaseInfo; function extendDatabaseInfoFromApps(db, apps) { if (!db || !apps) return db; const dbExt = { ...db, tables: db.tables.map(table => ({ ...table, foreignKeys: [ ...(table.foreignKeys || []), ...(0, flatten_1.default)(apps.map(app => app.virtualReferences || [])) .filter(fk => fk.pureName == table.pureName && fk.schemaName == table.schemaName) .map(fk => ({ ...fk, constraintType: 'foreignKey', isVirtual: true })), ], })), }; return addTableDependencies(dbExt); } exports.extendDatabaseInfoFromApps = extendDatabaseInfoFromApps; function isTableColumnUnique(table, column) { if (table.primaryKey && table.primaryKey.columns.length == 1 && table.primaryKey.columns[0].columnName == column) { return true; } const uqs = [...(table.uniques || []), ...(table.indexes || []).filter(x => x.isUnique)]; if (uqs.find(uq => uq.columns.length == 1 && uq.columns[0].columnName == column)) { return true; } return false; } exports.isTableColumnUnique = isTableColumnUnique; function isTableInfo(obj) { return obj.objectTypeField == 'tables'; } exports.isTableInfo = isTableInfo; function isViewInfo(obj) { return obj.objectTypeField == 'views'; } exports.isViewInfo = isViewInfo; function isCollectionInfo(obj) { return obj.objectTypeField == 'collections'; } exports.isCollectionInfo = isCollectionInfo; function filterStructureBySchema(db, schema) { if (!db) { return db; } return { ...db, tables: (db.tables || []).filter(x => x.schemaName == schema), views: (db.views || []).filter(x => x.schemaName == schema), collections: (db.collections || []).filter(x => x.schemaName == schema), matviews: (db.matviews || []).filter(x => x.schemaName == schema), procedures: (db.procedures || []).filter(x => x.schemaName == schema), functions: (db.functions || []).filter(x => x.schemaName == schema), triggers: (db.triggers || []).filter(x => x.schemaName == schema), }; } exports.filterStructureBySchema = filterStructureBySchema; function getSchemasUsedByStructure(db) { if (!db) { return db; } return (0, uniq_1.default)([ ...(db.tables || []).map(x => x.schemaName), ...(db.views || []).map(x => x.schemaName), ...(db.collections || []).map(x => x.schemaName), ...(db.matviews || []).map(x => x.schemaName), ...(db.procedures || []).map(x => x.schemaName), ...(db.functions || []).map(x => x.schemaName), ...(db.triggers || []).map(x => x.schemaName), ]); } exports.getSchemasUsedByStructure = getSchemasUsedByStructure; function replaceSchemaInStructure(db, schema) { if (!db) { return db; } return { ...db, tables: (db.tables || []).map(tbl => ({ ...tbl, schemaName: schema, columns: (tbl.columns || []).map(column => ({ ...column, schemaName: schema })), primaryKey: tbl.primaryKey ? { ...tbl.primaryKey, schemaName: schema } : undefined, sortingKey: tbl.sortingKey ? { ...tbl.sortingKey, schemaName: schema } : undefined, foreignKeys: (tbl.foreignKeys || []).map(fk => ({ ...fk, refSchemaName: schema, schemaName: schema })), indexes: (tbl.indexes || []).map(idx => ({ ...idx, schemaName: schema })), uniques: (tbl.uniques || []).map(idx => ({ ...idx, schemaName: schema })), checks: (tbl.checks || []).map(idx => ({ ...idx, schemaName: schema })), })), views: (db.views || []).map(x => ({ ...x, schemaName: schema })), collections: (db.collections || []).map(x => ({ ...x, schemaName: schema })), matviews: (db.matviews || []).map(x => ({ ...x, schemaName: schema })), procedures: (db.procedures || []).map(x => ({ ...x, schemaName: schema })), functions: (db.functions || []).map(x => ({ ...x, schemaName: schema })), triggers: (db.triggers || []).map(x => ({ ...x, schemaName: schema })), }; } exports.replaceSchemaInStructure = replaceSchemaInStructure; function skipNamesInStructureByRegex(db, regex) { if (!db) { return db; } return { ...db, tables: (db.tables || []).filter(x => !regex.test(x.pureName)), views: (db.views || []).filter(x => !regex.test(x.pureName)), collections: (db.collections || []).filter(x => !regex.test(x.pureName)), matviews: (db.matviews || []).filter(x => !regex.test(x.pureName)), procedures: (db.procedures || []).filter(x => !regex.test(x.pureName)), functions: (db.functions || []).filter(x => !regex.test(x.pureName)), triggers: (db.triggers || []).filter(x => !regex.test(x.pureName)), }; } exports.skipNamesInStructureByRegex = skipNamesInStructureByRegex; function detectChangesInPreloadedRows(oldTable, newTable) { var _a, _b, _c, _d; const key = (newTable === null || newTable === void 0 ? void 0 : newTable.preloadedRowsKey) || (oldTable === null || oldTable === void 0 ? void 0 : oldTable.preloadedRowsKey) || ((_b = (_a = newTable === null || newTable === void 0 ? void 0 : newTable.primaryKey) === null || _a === void 0 ? void 0 : _a.columns) === null || _b === void 0 ? void 0 : _b.map(x => x.columnName)) || ((_d = (_c = oldTable === null || oldTable === void 0 ? void 0 : oldTable.primaryKey) === null || _c === void 0 ? void 0 : _c.columns) === null || _d === void 0 ? void 0 : _d.map(x => x.columnName)); const oldRows = (oldTable === null || oldTable === void 0 ? void 0 : oldTable.preloadedRows) || []; const newRows = (newTable === null || newTable === void 0 ? void 0 : newTable.preloadedRows) || []; const insertOnly = (newTable === null || newTable === void 0 ? void 0 : newTable.preloadedRowsInsertOnly) || (oldTable === null || oldTable === void 0 ? void 0 : oldTable.preloadedRowsInsertOnly); if (newRows.length != oldRows.length) { return true; } for (const row of newRows) { const old = oldRows === null || oldRows === void 0 ? void 0 : oldRows.find(r => key.every(col => r[col] == row[col])); const rowKeys = (0, keys_1.default)(row); if (old) { const updated = []; for (const col of rowKeys) { if (row[col] != old[col] && !(insertOnly === null || insertOnly === void 0 ? void 0 : insertOnly.includes(col))) { updated.push(col); } } if (updated.length > 0) { return true; } } else { return true; } } for (const row of oldRows || []) { const newr = oldRows === null || oldRows === void 0 ? void 0 : oldRows.find(r => key.every(col => r[col] == row[col])); if (!newr) { return true; } } return false; } exports.detectChangesInPreloadedRows = detectChangesInPreloadedRows; function removePreloadedRowsFromStructure(db) { if (!db) { return db; } return { ...db, tables: (db.tables || []).map(tbl => ({ ...tbl, preloadedRows: undefined, preloadedRowsKey: undefined, preloadedRowsInsertOnly: undefined, })), }; } exports.removePreloadedRowsFromStructure = removePreloadedRowsFromStructure; function skipDbGateInternalObjects(db) { return { ...db, tables: (db.tables || []).filter(tbl => tbl.pureName != 'dbgate_deploy_journal'), }; } exports.skipDbGateInternalObjects = skipDbGateInternalObjects; function adaptDatabaseInfo(db, driver) { const modelAdapted = { ...db, tables: db.tables.map(table => driver.adaptTableInfo(table)), }; return modelAdapted; } exports.adaptDatabaseInfo = adaptDatabaseInfo;