2021-03-28 16:38:45 +00:00
|
|
|
import { DatabaseInfo } from 'dbgate-types';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
|
2021-04-02 11:35:54 +00:00
|
|
|
const allForeignKeys = _.flatten(db.tables.map(x => x.foreignKeys || []));
|
2021-03-28 16:38:45 +00:00
|
|
|
return {
|
|
|
|
...db,
|
|
|
|
tables: db.tables.map(table => ({
|
|
|
|
...table,
|
|
|
|
dependencies: allForeignKeys.filter(x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName),
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-01 08:59:46 +00:00
|
|
|
function fillTableExtendedInfo(db: DatabaseInfo): DatabaseInfo {
|
2021-03-28 16:38:45 +00:00
|
|
|
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));
|
|
|
|
}
|