dbgate/packages/engines/default/DatabaseAnalyser.js
2020-04-11 20:24:30 +02:00

88 lines
2.3 KiB
JavaScript

const _ = require('lodash');
class DatabaseAnalyser {
/**
*
* @param {import('@dbgate/types').EngineDriver} driver
*/
constructor(pool, driver) {
this.pool = pool;
this.driver = driver;
// this.result = DatabaseAnalyser.createEmptyStructure();
/** @type {import('@dbgate/types').DatabaseInfo} */
this.structure = null;
/** import('@dbgate/types').DatabaseModification[]) */
this.modifications = null;
}
async _runAnalysis() {
return DatabaseAnalyser.createEmptyStructure();
}
/** @returns {Promise<import('@dbgate/types').DatabaseModification[]>} */
async getModifications() {
if (this.structure != null) throw new Error('DatabaseAnalyse.getModifications - structure must not be filled');
return [];
}
async fullAnalysis() {
return this._runAnalysis();
}
async incrementalAnalysis(structure) {
this.structure = structure;
this.modifications = await this.getModifications();
if (this.modifications.length == 0) return null;
console.log('DB modifications detected:', this.modifications);
return this._runAnalysis();
}
mergeAnalyseResult(newlyAnalysed) {
if (this.structure == null) {
return {
...DatabaseAnalyser.createEmptyStructure(),
...newlyAnalysed,
};
}
const res = {};
for (const field of ['tables', 'views', 'functions', 'procedures', 'triggers']) {
const removedIds = this.modifications
.filter((x) => x.action == 'remove' && x.objectTypeField == field)
.map((x) => x.objectId);
const newArray = newlyAnalysed[field] || [];
const addedChangedIds = newArray.map((x) => x.objectId);
const removeAllIds = [...removedIds, ...addedChangedIds];
res[field] = _.sortBy(
[...this.structure[field].filter((x) => !removeAllIds.includes(x.objectId)), ...newArray],
(x) => x.pureName
);
}
return res;
// const {tables,views, functions, procedures, triggers} = this.structure;
// return {
// tables:
// }
}
// findObjectById(id) {
// return this.structure.tables.find((x) => x.objectId == id);
// }
}
/** @returns {import('@dbgate/types').DatabaseInfo} */
DatabaseAnalyser.createEmptyStructure = () => ({
tables: [],
views: [],
functions: [],
procedures: [],
triggers: [],
});
module.exports = DatabaseAnalyser;