select statement with dynamic columns for where condition
I need to do exactly what this answer says except my query is:
export async function find_items_based_on_params(category,type,obj) {
const q = 'SELECT * FROM $1 INNER JOIN category ON category.cat_id = $2 WHERE (category.cat_type = $3)'
const res = await sql.query(q, [type,type + '.category_id',category]);
return res;
}
Depending on whether the obj has obj.name obj.color I need to add ' AND table = obj.name '
So if obj parameter was
obj = {
name: 'Brick',
color: 'brown'
}
//Expected query
//SELECT * FROM $1 INNER JOIN category ON category.cat_id = $2 //WHERE (category.cat_type = $3) AND $4:name = $5 AND $6 = $7;
I've tried use the function above and creating my own function (which isn't suggested) but Im struggling to understand how to construct this correctly.