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

89 lines
2.9 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
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) {
2020-09-28 11:54:02 +00:00
const absolutePath = path.join(__dirname, '../', this.tplPath, '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 => {
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);