dbgate/packages/tools/src/structureTools.ts

81 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-03-28 16:38:45 +00:00
import { DatabaseInfo } from 'dbgate-types';
import _ from 'lodash';
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
const allForeignKeys = _.flatten(db.tables.map(x => x.foreignKeys));
return {
...db,
tables: db.tables.map(table => ({
...table,
dependencies: allForeignKeys.filter(x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName),
})),
};
}
function fillTableExtendedInfo(db: DatabaseInfo) {
return {
...db,
2021-04-01 06:09:22 +00:00
tables: (db.tables || []).map(obj => ({
...obj,
objectTypeField: 'tables',
columns: (obj.columns || []).map(column => ({
pureName: obj.pureName,
schemaName: obj.schemaName,
2021-03-28 16:38:45 +00:00
...column,
})),
2021-04-01 06:09:22 +00:00
primaryKey: obj.primaryKey
? {
...obj.primaryKey,
pureName: obj.pureName,
schemaName: obj.schemaName,
constraintType: 'primaryKey',
}
: undefined,
foreignKeys: (obj.foreignKeys || []).map(cnt => ({
...cnt,
pureName: obj.pureName,
schemaName: obj.schemaName,
constraintType: 'foreignKey',
})),
indexes: (obj.indexes || []).map(cnt => ({
...cnt,
pureName: obj.pureName,
schemaName: obj.schemaName,
constraintType: 'index',
})),
checks: (obj.checks || []).map(cnt => ({
...cnt,
pureName: obj.pureName,
schemaName: obj.schemaName,
constraintType: 'check',
})),
uniques: (obj.uniques || []).map(cnt => ({
...cnt,
pureName: obj.pureName,
schemaName: obj.schemaName,
constraintType: 'unique',
})),
})),
views: (db.views || []).map(obj => ({
...obj,
objectTypeField: 'views',
})),
procedures: (db.procedures || []).map(obj => ({
...obj,
objectTypeField: 'procedures',
})),
functions: (db.functions || []).map(obj => ({
...obj,
objectTypeField: 'functions',
})),
triggers: (db.triggers || []).map(obj => ({
...obj,
objectTypeField: 'triggers',
2021-03-28 16:38:45 +00:00
})),
};
}
export function extendDatabaseInfo(db: DatabaseInfo): DatabaseInfo {
return fillTableExtendedInfo(addTableDependencies(db));
}