Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to concatinate multiple partial statements #845

Closed
NPrada opened this issue Apr 15, 2024 · 2 comments
Closed

How to concatinate multiple partial statements #845

NPrada opened this issue Apr 15, 2024 · 2 comments

Comments

@NPrada
Copy link

NPrada commented Apr 15, 2024

I have a dynamic array of conditions

const conditions = [
sql`age > 1`,
sql`height < 170`,
sql`hair_color = 'red'`
]

I would like to join al of these conditions with and AND and create a runnable query that looks like this:

SELECT * FROM humans WHERE age > 1 AND height < 170 AND hair_color = 'red

What is the correct way to go about it? Is it possible using this library, i could not figure it our reading the docs

@henryzhang03
Copy link

bump bump

@porsager
Copy link
Owner

porsager commented May 3, 2024

This is hopefully something you can find through docs or searching issues.

This should do it :)

const conditions = [
  sql`age > 1`,
  sql`height < 170`,
  sql`hair_color = 'red'`
]

await sql`
  select * from humans where ${ conditions.flatMap((x, i) => i ? [sql`and`, x] : x) }
`

There a many ways to dynamically generate queries, so having all ways covered in the docs is not something I have time to do (or want to), as it is highly case specific. Perhaps a separate post explaining the difference between, values, identifiers and keywords relating to dynamic query generation would help, but again - unfortunately not something I have time to do.

@porsager porsager closed this as completed May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants