-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_specificity.js
142 lines (123 loc) · 4.96 KB
/
base_specificity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const mysql = require('mysql2/promise');
const moment = require('moment');
class base_specificity {
constructor() {
this.databaseType = "mysql"; // Can be "mysql" | "pgsql" | "sqlsrv"
this.host = "localhost";
this.port = 3306; // 3306 for mysql, 5432 for pgsql, 1433 for sqlsrv
this.databaseName = "dynamic_api";
this.user = "root";
this.password = "";
this.tables = [];
this.connected = false;
this.dbInstance = null;
this.initData();
}
async initData() {
if (this.tables.length === 0 && await this.isConnected()) {
switch (this.databaseType) {
case 'mysql':
const [rows] = await this.getDb().query("SHOW TABLES");
this.tables = rows.map(row => Object.values(row)[0]);
break;
// Add cases for pgsql, sqlsrv if needed
}
}
}
async isConnected() {
await this.getDb();
return this.connected;
}
async getDb() {
if (!this.dbInstance) {
try {
this.dbInstance = await mysql.createConnection({
host: this.host,
port: this.port,
user: this.user,
password: this.password,
database: this.databaseName
});
this.connected = true;
} catch (err) {
console.error(err);
this.connected = false;
}
}
return this.dbInstance;
}
// Helper method for CORS headers (used in APIs)
allowCors(req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "*");
}
// Documentation Authentication
verifyDocumentationAuth(username, password) {
return username === "admin" && password === "1234ammo1331_bi";
}
getBaseUrl(req) {
const protocol = req.secure ? 'https' : 'http';
return `${protocol}://${req.get('host')}${req.baseUrl}`;
}
async getTableDescriptions(tableName, basedTableNames = []) {
const result = {
tableName,
primaryKey: "",
basedTableNames,
referencedTables: [],
columns: []
};
const [columns] = await this.getDb().query(`DESCRIBE ${tableName}`);
result.columns = columns;
for (let column of result.columns) {
if (column.Key === "PRI") {
column.explications = "Primary Key";
result.primaryKey = column;
} else if (column.Key === "MUL") {
const foreignKey = column.Field;
const query = `
SELECT TABLE_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_SCHEMA = ? AND REFERENCED_TABLE_NAME IS NOT NULL
AND TABLE_NAME = ? AND COLUMN_NAME = ?`;
const [foreignTable] = await this.getDb().execute(query, [this.databaseName, tableName, foreignKey]);
column.table = foreignTable[0];
column.explications = `Foreign Key linked to ${column.table.REFERENCED_COLUMN_NAME} in ${column.table.REFERENCED_TABLE_NAME}`;
// Avoid circular references
if (basedTableNames.includes(column.table.REFERENCED_TABLE_NAME)) {
column.tableExistant = true;
} else {
basedTableNames.push(column.table.REFERENCED_TABLE_NAME);
column.referencedTable = await this.getTableDescriptions(column.table.REFERENCED_TABLE_NAME, basedTableNames);
}
}
}
return result;
}
log(params) {
params.ip = this.getUserIP();
const columns = Object.keys(params).join(", ");
const values = Object.values(params).map(value => value ? `'${value}'` : 'null').join(", ");
const query = `INSERT INTO log (${columns}) VALUES (${values})`;
this.getDb().execute(query);
}
getUserIP(req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
}
// Date formatting using moment.js
formatDate(dateString) {
return {
full: moment(dateString).locale("fr").format("dddd Do MMMM YYYY"),
jma: moment(dateString).locale("fr").format("Do MMMM YYYY"),
jma2: moment(dateString).locale("fr").format("DD-MM-YYYY"),
jma3: moment(dateString).locale("fr").format("YYYY-MM-DD"),
fullDateTime: moment(dateString).locale("fr").format("dddd Do MMMM YYYY à HH:mm"),
};
}
formatCurrentDate() {
return this.formatDate(new Date());
}
}
module.exports = base_specificity;