Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

What is the recommended way to reuse SQL statements? #403

Answered by porsager
oskarrough asked this question in Q&A
Discussion options

Hello, was wondering what the recommended way to reuse SQL statements is here. I assumed sql${query}`` would work, but reading the docs I (maybe) I understand it's not meant to.

const query = 'select * from posts'
await sql`${query}` // throws an error
await sql.unsafe(query) // works

To avoid using the unsafe method, would you wrap everything in functions?

const queryById = (id) => sql`select * from posts where id = ${id}`
await queryById(42) // works
You must be logged in to vote

Oh yeah 😉 Functions Baby 🏋

Replies: 1 comment 1 reply

Comment options

Oh yeah 😉 Functions Baby 🏋

You must be logged in to vote
1 reply
Comment options

tak!

Answer selected by porsager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /