2019-01-22 08:55:35 +00:00
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
const config = require('./config.js');
|
2020-09-25 12:45:00 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init() {
|
2019-10-31 13:14:18 +00:00
|
|
|
if (!this.pool) {
|
2019-01-22 08:55:35 +00:00
|
|
|
this.pool = mysql.createPool(config.mysql);
|
2019-10-31 13:14:18 +00:00
|
|
|
this.pool.on('connection', connection => {
|
|
|
|
connection.config.namedPlaceholders = true;
|
|
|
|
});
|
|
|
|
}
|
2019-01-22 08:55:35 +00:00
|
|
|
},
|
2020-09-25 12:45:00 +00:00
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
/**
|
|
|
|
* Makes a query from a raw sql
|
|
|
|
* @param {String} query - The raw SQL query
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
*
|
2019-11-21 08:33:08 +00:00
|
|
|
* @return {Object} - Result promise
|
2019-11-18 14:04:51 +00:00
|
|
|
*/
|
|
|
|
rawSql(query, params) {
|
2019-10-24 05:41:54 +00:00
|
|
|
return this.pool.query(query, params).then(([rows]) => {
|
2019-10-31 11:43:04 +00:00
|
|
|
return rows;
|
2019-10-24 05:41:54 +00:00
|
|
|
});
|
|
|
|
},
|
2020-09-25 12:45:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a query from a SQL file
|
|
|
|
* @param {String} queryName - The SQL file name
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
* @param {Object} dirname - Referenced path
|
|
|
|
*
|
|
|
|
* @return {Object} - Result promise
|
|
|
|
*/
|
|
|
|
rawSqlFromDef(queryName, params, dirname = '.') {
|
|
|
|
const sqlPath = path.join(dirname, 'sql', `${queryName}.sql`);
|
|
|
|
const query = fs.readFileSync(sqlPath, 'utf8');
|
|
|
|
|
|
|
|
return this.rawSql(query, params);
|
|
|
|
},
|
|
|
|
|
2019-11-21 08:33:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the first row from a given raw sql
|
|
|
|
* @param {String} query - The raw SQL query
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
*
|
|
|
|
* @return {Object} - Result promise
|
|
|
|
*/
|
2019-10-31 11:43:04 +00:00
|
|
|
findOne(query, params) {
|
2019-11-21 08:33:08 +00:00
|
|
|
return this.rawSql(query, params).then(([row]) => row);
|
|
|
|
},
|
2020-09-25 12:45:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first row from a given SQL file
|
|
|
|
* @param {String} queryName - The SQL file name
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
* @param {Object} dirname - Referenced path
|
|
|
|
*
|
|
|
|
* @return {Object} - Result promise
|
|
|
|
*/
|
|
|
|
findOneFromDef(queryName, params, dirname) {
|
|
|
|
return this.rawSqlFromDef(queryName, params, dirname)
|
|
|
|
.then(([row]) => row);
|
|
|
|
},
|
|
|
|
|
2019-11-21 08:33:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the first property from a given raw sql
|
|
|
|
* @param {String} query - The raw SQL query
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
*
|
|
|
|
* @return {Object} - Result promise
|
|
|
|
*/
|
|
|
|
findValue(query, params) {
|
|
|
|
return this.findOne(query, params).then(row => {
|
|
|
|
return Object.values(row)[0];
|
|
|
|
});
|
2019-10-31 11:43:04 +00:00
|
|
|
},
|
2019-10-24 05:41:54 +00:00
|
|
|
|
2020-09-25 12:45:00 +00:00
|
|
|
/**
|
|
|
|
* Returns the first property from a given SQL file
|
|
|
|
* @param {String} queryName - The SQL file name
|
|
|
|
* @param {Object} params - Parameterized values
|
|
|
|
* @param {Object} dirname - Referenced path
|
|
|
|
*
|
|
|
|
* @return {Object} - Result promise
|
|
|
|
*/
|
|
|
|
findValueFromDef(queryName, params, dirname) {
|
|
|
|
return this.findOneFromDef(queryName, params, dirname).then(row => {
|
|
|
|
return Object.values(row)[0];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getCaller() {
|
|
|
|
let originalFunc = Error.prepareStackTrace;
|
|
|
|
let callerfile;
|
|
|
|
try {
|
|
|
|
const err = new Error();
|
|
|
|
|
|
|
|
Error.prepareStackTrace = function(err, stack) {
|
|
|
|
return stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
const currentfile = err.stack.shift().getFileName();
|
|
|
|
|
|
|
|
while (err.stack.length) {
|
|
|
|
callerfile = err.stack.shift().getFileName();
|
|
|
|
|
|
|
|
if (currentfile !== callerfile) break;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
Error.prepareStackTrace = originalFunc;
|
|
|
|
|
|
|
|
return path.dirname(callerfile);
|
2019-10-24 05:41:54 +00:00
|
|
|
}
|
2019-01-22 08:55:35 +00:00
|
|
|
};
|