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

88 lines
2.8 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: {
/**
* 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
*
* @return {Object} - Result promise
*/
rawSqlFromDef(queryName, params) {
const absolutePath = path.join(__dirname, '../', this.tplPath, 'sql', queryName);
return db.rawSqlFromDef(absolutePath, params);
},
/**
* 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 => {
return Object.values(row)[0];
});
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) {
const absolutePath = path.join(__dirname, '../', this.tplPath, 'sql', queryName);
return db.getSqlFromDef(absolutePath);
},
2020-09-28 11:54:02 +00:00
},
props: ['tplPath']
};
Vue.mixin(dbHelper);