How do you get conditional filters within query files?

Viewed 48

I was using conditional template string interpolation before, which looked like this:

const queryArgs = {
  type,
  status,
}
const query = `
  SELECT
    id,
    type,
    status,
    content
  FROM
    some_table
  WHERE
    ${type ? "type = $(type)" : ""}
    ${status ? "AND status = $(status)" : ""}
  ORDER BY
    id
`

It did the work and didn't violate pg-promise rules in regards to template string interpolation, since it wasn't interpolating the input values but rather their args references.
Is there a way to get the similar logic for query files? A lot of filters like this tend to be compounded, so writing every single combo of filter groups is not an option, especially since these queries are going to be used as a part of CTE chain.

0 Answers
Related