'Injecting WHERE clause into pgpromise pg SELECT query

I'd like to be able to reuse a SELECT query by injecting a conditional WHERE clause. See example below.

let where = null;
if(condition) where = `WHERE patient.mongodb_id = $2`;

const patient = await db
.oneOrNone(
  `SELECT * FROM patients
   $1:value;`,
  [where, mongodbId]

I'm aware that the code above is wrong as it throws the following error: error: there is no parameter $2. I've read through the docs and I don't seem to be able to find a good example on how to do this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source