salix/print/core/database.js

170 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2021-04-27 08:29:09 +00:00
const mysql = require('mysql2');
2020-09-25 12:45:00 +00:00
const fs = require('fs-extra');
2019-01-22 08:55:35 +00:00
module.exports = {
2022-09-27 12:20:57 +00:00
defaultDataSource: 'vn',
2021-04-27 08:29:09 +00:00
2022-09-27 12:20:57 +00:00
init(dataSource) {
if (!this.connections) {
this.connections = [];
2021-04-27 08:29:09 +00:00
2022-09-27 12:20:57 +00:00
const dataSources = ['vn', 'osticket'];
for (const name of dataSources)
this.connections[name] = dataSource[name].connector.client;
this.pool = this.connections[this.defaultDataSource];
2019-10-31 13:14:18 +00:00
}
2021-04-27 08:29:09 +00:00
2022-09-27 12:20:57 +00:00
return this.connections;
2021-04-27 08:29:09 +00:00
},
/**
* Retuns a pool connection from specific cluster node
* @param {String} name - The cluster name
*
* @return {Object} - Pool connection
*/
2022-09-27 12:20:57 +00:00
getConnection(name = this.defaultDataSource) {
return this.connections[name];
2019-01-22 08:55:35 +00:00
},
2020-09-25 12:45:00 +00:00
/**
* Makes a query from a raw sql
* @param {String} query - The raw SQL query
* @param {Object} params - Parameterized values
2021-01-18 07:33:20 +00:00
* @param {Object} connection - Optional pool connection
*
* @return {Object} - Result promise
*/
2022-09-27 12:20:57 +00:00
rawSql(query, params, connection) {
2021-04-27 08:29:09 +00:00
return new Promise((resolve, reject) => {
2022-09-27 12:20:57 +00:00
let db = this.getConnection();
if (connection) db = connection;
db.query(query, params, (error, rows) => {
2022-09-16 21:13:37 +00:00
if (error) return reject(error);
resolve(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
2021-02-24 06:51:05 +00:00
* @param {Object} connection - Optional pool connection
2020-09-25 12:45:00 +00:00
*
* @return {Object} - Result promise
*/
2021-02-24 06:51:05 +00:00
rawSqlFromDef(queryName, params, connection) {
2020-09-28 11:54:02 +00:00
const query = fs.readFileSync(`${queryName}.sql`, 'utf8');
2020-09-25 12:45:00 +00:00
2021-02-24 06:51:05 +00:00
return this.rawSql(query, params, connection);
2020-09-25 12:45:00 +00:00
},
/**
* Returns the first row from a given raw sql
* @param {String} query - The raw SQL query
* @param {Object} params - Parameterized values
2022-09-27 12:20:57 +00:00
* @param {Object} connection - Optional pool connection
*
* @return {Object} - Result promise
*/
2022-09-27 12:20:57 +00:00
findOne(query, params, connection) {
return this.rawSql(query, params, connection)
.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
2022-09-27 12:20:57 +00:00
* @param {Object} connection - Optional pool connection
2020-09-25 12:45:00 +00:00
*
* @return {Object} - Result promise
*/
2022-09-27 12:20:57 +00:00
findOneFromDef(queryName, params, connection) {
return this.rawSqlFromDef(queryName, params, connection)
2020-09-25 12:45:00 +00:00
.then(([row]) => row);
},
/**
* Returns the first property from a given raw sql
* @param {String} query - The raw SQL query
* @param {Object} params - Parameterized values
2022-09-27 12:20:57 +00:00
* @param {Object} connection - Optional pool connection
*
* @return {Object} - Result promise
*/
2022-09-27 12:20:57 +00:00
findValue(query, params, connection) {
return this.findOne(query, params, connection).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
2022-09-27 12:20:57 +00:00
* @param {Object} connection - Optional pool connection
2020-09-25 12:45:00 +00:00
*
* @return {Object} - Result promise
*/
2022-09-27 12:20:57 +00:00
findValueFromDef(queryName, params, connection) {
return this.findOneFromDef(queryName, params, connection)
.then(row => Object.values(row)[0]);
2021-01-18 07:33:20 +00:00
},
/**
* Returns the content of a SQL file
* @param {String} queryName - The SQL file name
*
* @return {Object} - SQL
*/
getSqlFromDef(queryName) {
return fs.readFileSync(`${queryName}.sql`, 'utf8');
},
/**
* Merges two SQL strings
*
* @param {String} query - Input SQL
* @param {String} sql - String to merge
*
* @return {String} - Merged SQL string
*/
merge(query, sql) {
return query += `\r\n ${sql}`;
},
/**
* Builds where string
*
* @param {Object} args - Query params
* @param {Function} callback - Callback
*
* @return {String} - Where Sql string
*/
buildWhere(args, callback) {
let sql = '';
for (let prop in args) {
const value = args[prop];
if (!value) continue;
const sanitizedValue = mysql.escape(value);
const conditional = callback(prop, sanitizedValue);
if (!conditional) continue;
if (sql) sql += ' AND ';
sql += conditional;
}
let where = '';
if (sql)
where = `WHERE ${sql}`;
return where;
2019-10-24 05:41:54 +00:00
}
2019-01-22 08:55:35 +00:00
};