89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
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
|
|
* @param {Object} connection - Optional pool connection
|
|
*
|
|
* @return {Object} - Result promise
|
|
*/
|
|
rawSql: (query, params, connection) =>
|
|
db.rawSql(query, params, connection),
|
|
|
|
/**
|
|
* Makes a query from a SQL file
|
|
* @param {String} queryName - The SQL file name
|
|
* @param {Object} params - Parameterized values
|
|
* @param {Object} connection - Optional pool connection
|
|
*
|
|
* @return {Object} - Result promise
|
|
*/
|
|
rawSqlFromDef(queryName, params, connection) {
|
|
const absolutePath = path.join(__dirname, '../', this.path, 'sql', queryName);
|
|
return db.rawSqlFromDef(absolutePath, params, connection);
|
|
},
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
findOne: (query, params) => db.findOne(query, params),
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
findValue: (query, params) => db.findValue(query, params),
|
|
|
|
/**
|
|
* 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 => {
|
|
if (row) return Object.values(row)[0];
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 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.path, 'sql', queryName);
|
|
return db.getSqlFromDef(absolutePath);
|
|
},
|
|
},
|
|
props: ['tplPath', 'name']
|
|
};
|
|
|
|
Vue.mixin(dbHelper);
|