2023-06-02 10:46:57 +00:00
|
|
|
const path = require('path');
|
|
|
|
const {loadConfig, toUpperCamelCase} = require('./util');
|
2023-06-05 12:49:57 +00:00
|
|
|
const MultiMap = require('./multi-map');
|
2023-06-02 10:46:57 +00:00
|
|
|
|
|
|
|
module.exports = class ModelLoader {
|
2023-06-06 08:55:56 +00:00
|
|
|
init(logger) {
|
2023-06-02 10:46:57 +00:00
|
|
|
const configDir = path.join(__dirname, '..');
|
2023-06-06 08:55:56 +00:00
|
|
|
const conf = loadConfig(configDir, 'logs');
|
2023-06-05 12:49:57 +00:00
|
|
|
const schemaMap = new MultiMap();
|
2023-06-02 10:46:57 +00:00
|
|
|
const logMap = new Map();
|
2023-06-06 08:55:56 +00:00
|
|
|
|
|
|
|
Object.assign(this, {
|
|
|
|
logger,
|
|
|
|
conf
|
|
|
|
});
|
|
|
|
Object.assign(logger, {
|
|
|
|
schemaMap,
|
|
|
|
logMap
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const logName in conf.logs) {
|
|
|
|
const logConf = conf.logs[logName];
|
|
|
|
const schema = logConf.schema || logger.conf.srcDb.database;
|
2023-06-02 10:46:57 +00:00
|
|
|
const logInfo = {
|
|
|
|
name: logName,
|
|
|
|
conf: logConf,
|
|
|
|
schema,
|
|
|
|
table: parseTable(logConf.logTable, schema),
|
|
|
|
mainTable: parseTable(logConf.mainTable, schema)
|
|
|
|
};
|
|
|
|
logMap.set(logName, logInfo);
|
|
|
|
|
|
|
|
const mainTable = addTable(logConf.mainTable, logInfo);
|
|
|
|
mainTable.isMain = true;
|
|
|
|
|
|
|
|
if (logConf.tables)
|
|
|
|
for (const tableConf of logConf.tables){
|
|
|
|
const table = addTable(tableConf, logInfo);
|
|
|
|
if (table !== mainTable) {
|
|
|
|
Object.assign(table, {
|
|
|
|
main: mainTable,
|
|
|
|
isMain: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addTable(tableConf, logInfo) {
|
|
|
|
if (typeof tableConf == 'string')
|
|
|
|
tableConf = {name: tableConf};
|
|
|
|
const table = parseTable(tableConf.name, logInfo.schema);
|
|
|
|
|
2023-06-05 12:49:57 +00:00
|
|
|
let tableInfo = schemaMap.get(table.schema, table.name);
|
2023-06-02 10:46:57 +00:00
|
|
|
if (!tableInfo) {
|
|
|
|
tableInfo = {
|
|
|
|
conf: tableConf,
|
|
|
|
log: logInfo
|
|
|
|
};
|
2023-06-05 12:49:57 +00:00
|
|
|
schemaMap.set(table.schema, table.name, tableInfo);
|
2023-06-02 10:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let modelName = tableConf.modelName;
|
|
|
|
if (!modelName) {
|
2023-06-06 08:55:56 +00:00
|
|
|
modelName = conf.upperCaseTable
|
2023-06-02 10:46:57 +00:00
|
|
|
? toUpperCamelCase(table.name)
|
|
|
|
: table.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
showField,
|
|
|
|
relation,
|
|
|
|
idName
|
|
|
|
} = tableConf;
|
|
|
|
|
|
|
|
Object.assign(tableInfo, {
|
|
|
|
conf: tableConf,
|
|
|
|
exclude: new Set(tableConf.exclude),
|
|
|
|
modelName,
|
|
|
|
showField,
|
|
|
|
relation,
|
|
|
|
idName,
|
2023-06-06 08:55:56 +00:00
|
|
|
userField: tableConf.userField || conf.userField
|
2023-06-02 10:46:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return tableInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 08:55:56 +00:00
|
|
|
async loadSchema() {
|
|
|
|
const {db, schemaMap} = this.logger;
|
|
|
|
const {conf} = this;
|
2023-06-02 10:46:57 +00:00
|
|
|
|
2023-06-05 12:49:57 +00:00
|
|
|
for (const [schema, table, tableInfo] of schemaMap) {
|
2023-06-02 10:46:57 +00:00
|
|
|
const tableConf = tableInfo.conf;
|
|
|
|
|
|
|
|
// Fetch columns & types
|
|
|
|
|
|
|
|
Object.assign (tableInfo, {
|
|
|
|
castTypes: new Map(),
|
|
|
|
columns: new Map()
|
|
|
|
});
|
|
|
|
|
|
|
|
if (tableConf.types)
|
|
|
|
for (const col in tableConf.types)
|
|
|
|
tableInfo.castTypes.set(col, tableConf.types[col]);
|
|
|
|
|
|
|
|
const [dbCols] = await db.query(
|
|
|
|
`SELECT
|
|
|
|
COLUMN_NAME \`col\`,
|
|
|
|
DATA_TYPE \`type\`,
|
|
|
|
COLUMN_DEFAULT \`def\`
|
|
|
|
FROM information_schema.\`COLUMNS\`
|
|
|
|
WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?`,
|
|
|
|
[table, schema]
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const {col, type, def} of dbCols) {
|
|
|
|
if (!tableInfo.exclude.has(col) && col != tableInfo.userField)
|
|
|
|
tableInfo.columns.set(col, {type, def});
|
|
|
|
|
2023-06-06 08:55:56 +00:00
|
|
|
const castType = conf.castTypes[type];
|
2023-06-02 10:46:57 +00:00
|
|
|
if (castType && !tableInfo.castTypes.has(col))
|
|
|
|
tableInfo.castTypes.set(col, castType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch primary key
|
|
|
|
|
|
|
|
if (!tableConf.idName) {
|
|
|
|
const [dbPks] = await db.query(
|
|
|
|
`SELECT COLUMN_NAME idName
|
|
|
|
FROM information_schema.KEY_COLUMN_USAGE
|
|
|
|
WHERE CONSTRAINT_NAME = 'PRIMARY'
|
|
|
|
AND TABLE_NAME = ?
|
|
|
|
AND TABLE_SCHEMA = ?`,
|
|
|
|
[table, schema]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!dbPks.length)
|
|
|
|
throw new Error(`Primary not found for table: ${schema}.${table}`);
|
|
|
|
if (dbPks.length > 1)
|
|
|
|
throw new Error(`Only one column primary is supported: ${schema}.${table}`);
|
|
|
|
|
|
|
|
for (const {idName} of dbPks)
|
|
|
|
tableInfo.idName = idName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get show field
|
|
|
|
|
2023-06-06 08:55:56 +00:00
|
|
|
if (!tableInfo.isMain) {
|
|
|
|
const {showField} = tableInfo;
|
|
|
|
if (!showField) {
|
|
|
|
for (const field of conf.showFields) {
|
|
|
|
if (tableInfo.columns.has(field)) {
|
|
|
|
tableInfo.showField = field;
|
|
|
|
break;
|
|
|
|
}
|
2023-06-02 10:46:57 +00:00
|
|
|
}
|
2023-06-06 08:55:56 +00:00
|
|
|
} else {
|
|
|
|
const match = showField.match(/(^.*)\$$/);
|
|
|
|
if (match) tableInfo.showRelation = match[1];
|
2023-06-02 10:46:57 +00:00
|
|
|
}
|
2023-06-06 08:55:56 +00:00
|
|
|
} else
|
|
|
|
tableInfo.showField = null;
|
2023-06-02 10:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch relation to main table
|
|
|
|
|
2023-06-05 12:49:57 +00:00
|
|
|
for (const [schema, table, tableInfo] of schemaMap) {
|
2023-06-02 10:46:57 +00:00
|
|
|
if (!tableInfo.conf.relation && !tableInfo.isMain) {
|
|
|
|
const mainTable = tableInfo.log.mainTable;
|
2023-06-05 12:49:57 +00:00
|
|
|
const mainInfo = schemaMap.get(mainTable.schema, mainTable.name);
|
2023-06-02 10:46:57 +00:00
|
|
|
|
|
|
|
const [mainRelations] = await db.query(
|
|
|
|
`SELECT COLUMN_NAME relation
|
|
|
|
FROM information_schema.KEY_COLUMN_USAGE
|
|
|
|
WHERE TABLE_NAME = ?
|
|
|
|
AND TABLE_SCHEMA = ?
|
|
|
|
AND REFERENCED_TABLE_NAME = ?
|
|
|
|
AND REFERENCED_TABLE_SCHEMA = ?
|
|
|
|
AND REFERENCED_COLUMN_NAME = ?`,
|
|
|
|
[
|
|
|
|
table,
|
|
|
|
schema,
|
|
|
|
mainTable.name,
|
|
|
|
mainTable.schema,
|
2023-06-05 12:49:57 +00:00
|
|
|
mainInfo.idName
|
2023-06-02 10:46:57 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!mainRelations.length)
|
|
|
|
throw new Error(`No relation to main table found for table: ${schema}.${table}`);
|
|
|
|
if (mainRelations.length > 1)
|
|
|
|
throw new Error(`Found more multiple relations to main table: ${schema}.${table}`);
|
|
|
|
|
|
|
|
for (const {relation} of mainRelations)
|
|
|
|
tableInfo.relation = relation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseTable(tableString, defaultSchema) {
|
|
|
|
let name, schema;
|
|
|
|
const split = tableString.split('.');
|
|
|
|
if (split.length == 1) {
|
|
|
|
name = split[0];
|
|
|
|
schema = defaultSchema;
|
|
|
|
} else {
|
|
|
|
[schema, name] = split;
|
|
|
|
}
|
|
|
|
return {name, schema};
|
|
|
|
}
|