salix/print/core/database.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-01-22 08:55:35 +00:00
const mysql = require('mysql2/promise');
const config = require('./config.js');
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
},
/**
* Makes a query from a raw sql
* @param {String} query - The raw SQL query
* @param {Object} params - Parameterized values
*
* @return {Object} - Result promise
*/
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
});
},
/**
* 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) {
return this.rawSql(query, params).then(([row]) => row);
},
/**
* 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
findFromDef() {
}
2019-01-22 08:55:35 +00:00
};