Add parameters for select
MidoMiddle opened this issue · 2 comments
MidoMiddle commented
Current method:
/database.js
async select(table, fields = ['*'], conditions = null) {
const keys = fields[0] === '*' ? '*' : '"' + fields.join('", "') + '"';
const sql = `SELECT ${keys} FROM "${table}"`;
let whereClause = '';
let args = [];
if (conditions) {
const whereData = where(conditions);
whereClause = ' WHERE ' + whereData.clause;
args = whereData.args;
}
const res = await this.query(sql + whereClause, args);
return res.rows;
}
Propose:
async select(table, fields = ['*'], conditions = null, otherParams = null) {
const keys = fields[0] === '*' ? '*' : '"' + fields.join('", "') + '"';
const sql = `SELECT ${keys} FROM "${table}"`;
let whereClause = '';
let args = [];
if (conditions) {
const whereData = where(conditions);
whereClause = ' WHERE ' + whereData.clause;
args = whereData.args;
}
// ------------ add this ---------
let additional = '';
if (otherParams) {
Object.keys(otherParams).map(d => {
additional += ` ${d} ${otherParams[d]}`;
});
}
// -------------------
const res = await this.query(sql + whereClause + additional, args);
return res.rows;
}
for example:
db.select('some_table', ['*'], { price: 5 }, { 'order by': 'id desc', limit: 15 })
.then(d => { console.log({ d }) })
.catch(e => console.error(e));
tshemsedinov commented
Better syntax
try {
const result = await db.select('Goods', { price: 10 }).desc('name').limit(50);
console.log({ result });
} catch (err) {
console.error(err);
}
tshemsedinov commented