salix/print/core/mixins/db-helper.js

96 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-09-28 11:54:02 +00:00
const Vue = require('vue');
const path = require('path');
const db = require('../database');
const dbHelper = {
methods: {
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
*/
getConnection: name => db.getConnection(name),
2020-09-28 11:54:02 +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
2020-09-28 11:54:02 +00:00
*
* @return {Object} - Result promise
*/
2021-01-18 07:33:20 +00:00
rawSql: (query, params, connection) =>
db.rawSql(query, params, connection),
2020-09-28 11:54:02 +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-28 11:54:02 +00:00
*
* @return {Object} - Result promise
*/
2021-02-24 06:51:05 +00:00
rawSqlFromDef(queryName, params, connection) {
2021-03-01 10:25:37 +00:00
const absolutePath = path.join(__dirname, '../', this.path, 'sql', queryName);
2021-02-24 06:51:05 +00:00
return db.rawSqlFromDef(absolutePath, params, connection);
2020-09-28 11:54:02 +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
*/
2021-01-18 07:33:20 +00:00
findOne: (query, params) => db.findOne(query, params),
2020-09-28 11:54:02 +00:00
/**
* Returns the first row from a given SQL file
* @param {String} queryName - The SQL file name
* @param {Object} params - Parameterized values
*
* @return {Object} - Result promise
*/
findOneFromDef(queryName, params) {
return this.rawSqlFromDef(queryName, 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
*/
2021-01-18 07:33:20 +00:00
findValue: (query, params) => db.findValue(query, params),
2020-09-28 11:54:02 +00:00
/**
* Returns the first property from a given SQL file
* @param {String} queryName - The SQL file name
* @param {Object} params - Parameterized values
*
* @return {Object} - Result promise
*/
findValueFromDef(queryName, params) {
return this.findOneFromDef(queryName, params).then(row => {
2021-03-11 07:58:34 +00:00
if (row) return Object.values(row)[0];
2020-09-28 11:54:02 +00:00
});
2021-01-18 07:33:20 +00:00
},
/**
* Returns the content of an SQL file
* @param {String} queryName - The SQL file name
*
* @return {Object} - SQL
*/
getSqlFromDef(queryName) {
2021-03-01 10:25:37 +00:00
const absolutePath = path.join(__dirname, '../', this.path, 'sql', queryName);
2021-01-18 07:33:20 +00:00
return db.getSqlFromDef(absolutePath);
},
}
2020-09-28 11:54:02 +00:00
};
Vue.mixin(dbHelper);