dbgate/packages/tools/src/structureTools.ts

89 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-03-28 16:38:45 +00:00
import { DatabaseInfo } from 'dbgate-types';
2021-05-19 18:18:33 +00:00
import _flatten from 'lodash/flatten';
2021-03-28 16:38:45 +00:00
export function addTableDependencies(db: DatabaseInfo): DatabaseInfo {
2021-05-19 18:18:33 +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',
})),
})),
2021-04-02 16:40:07 +00:00
collections: (db.collections || []).map(obj => ({
...obj,
objectTypeField: 'collections',
})),
2021-04-01 06:09:22 +00:00
views: (db.views || []).map(obj => ({
...obj,
objectTypeField: 'views',
})),
2021-05-28 20:18:06 +00:00
matviews: (db.matviews || []).map(obj => ({
...obj,
objectTypeField: 'matviews',
})),
2021-04-01 06:09:22 +00:00
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));
}