porsager / porsager/postgres

Issue with dynamically building condition clause

Open
#813 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

I try to build a dynamic query with dynamic filters within a transaction but does not work. It returns 0 results.

Here is my code

let dynamicFilters = [
        transactionSql`id=${id}`,
        transactionSql`name=${name}`,
    ];


await transactionSql`
SELECT * FROM table WHERE ${ dynamicFilters.join(' AND ') }
`

I also tried below but does not work either

await transactionSql`
SELECT * FROM table WHERE ${ transactionSql`${ dynamicFilters.join(' AND ') }` }
`

If I replace the expression with the actual sql string it will work

await transactionSql`
SELECT * FROM table WHERE id=${id} AND name=${name} }
`

Any idea why?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the transactionSql tagged-template examples in the issue and trace how dynamicFilters is composed and interpolated. No source files or tests are identified in the report, so first locate the tagged-template entry point and existing tests for interpolated SQL fragments; done means the composition behavior is explained and covered by a reproducible test or documented usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.