I am trying to make query returning fields a bit more flexible by passing in the column to be returned but if I use variable injection it will not work and if I use template string it'll work fine.
I am trying not to use template string as possible for these injections
const select = ["_id", "status"]
const selectFields = select.map(field => `"${field}"`).join(',');
// using variable injection, the querying will try to query `'"_id","status"'`
// which doesn't work
const theRow = await db.oneOrNone(
`SELECT $1
FROM "TableName"`,
[selectFields]
);
// if I use template string, it'll work properly
const theRow = await db.oneOrNone(
`SELECT ${selectFields}}
FROM "TableName"`
);
How can I make the query to query properly even with variable injection? Thanks in advance for any help / suggestions