dbgate-tools
Version:
Auxiliary tools for other DbGate packages.
63 lines (62 loc) • 2.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DIAGRAM_DEFAULT_WATERMARK = exports.DIAGRAM_ZOOMS = exports.chooseTopTables = void 0;
const structureTools_1 = require("./structureTools");
const sortBy_1 = __importDefault(require("lodash/sortBy"));
const uniq_1 = __importDefault(require("lodash/uniq"));
const filterName_1 = require("./filterName");
function tableWeight(table, maxRowcount) {
var _a, _b;
let weight = 0;
const tableDependenciesCount = (0, uniq_1.default)(((_a = table.dependencies) === null || _a === void 0 ? void 0 : _a.map(x => x.pureName)) || []).length;
const tableFkCount = (0, uniq_1.default)(((_b = table.foreignKeys) === null || _b === void 0 ? void 0 : _b.map(x => x.refTableName)) || []).length;
if (table.primaryKey)
weight += 1;
if (tableFkCount)
weight += tableFkCount * 1;
if (maxRowcount && table.tableRowCount) {
const rowcount = parseInt(table.tableRowCount);
if (rowcount > 0)
weight += Math.log(rowcount) * table.columns.length * (tableFkCount || 1) * (tableDependenciesCount || 1);
}
else {
if (table.columns)
weight += table.columns.length * 2;
}
if (table.dependencies)
weight += tableDependenciesCount * 10;
if (maxRowcount)
return weight;
}
function chooseTopTables(tables, count, tableFilter, omitTablesFilter) {
const filteredTables = tables.filter(table => {
if (tableFilter) {
if (!(0, filterName_1.filterName)(tableFilter, table === null || table === void 0 ? void 0 : table.pureName))
return false;
}
if (omitTablesFilter) {
if ((0, filterName_1.filterName)(omitTablesFilter, table === null || table === void 0 ? void 0 : table.pureName))
return false;
}
return true;
});
if (!(count > 0)) {
return filteredTables;
}
const dbinfo = {
tables: filteredTables,
};
const extended = (0, structureTools_1.extendDatabaseInfo)(dbinfo);
const maxRowcount = Math.max(...extended.tables
.map(x => x.tableRowCount || 0)
.map(x => parseInt(x))
.filter(x => x > 0));
const sorted = (0, sortBy_1.default)((0, sortBy_1.default)(extended.tables, x => `${x.schemaName}.${x.pureName}`), table => -tableWeight(table, maxRowcount));
return sorted.slice(0, count);
}
exports.chooseTopTables = chooseTopTables;
exports.DIAGRAM_ZOOMS = [0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 1, 1.25, 1.5, 1.75, 2];
exports.DIAGRAM_DEFAULT_WATERMARK = 'Powered by [dbgate.io](https://dbgate.io)';