- 
  Notifications
 You must be signed in to change notification settings 
- Fork 326
What is the recommended way to reuse SQL statements? #403
 
 
 
 
 Answered
 
 by
 porsager
 
 
 
 
 
 oskarrough
 
 
 
 asked this question in
 Q&A
 
 -
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
Beta Was this translation helpful? Give feedback.
All reactions
 
 
 Answered by
 
 porsager
 
 
 
 Jun 26, 2022 
 
 
 Oh yeah 😉 Functions Baby 🏋
Replies: 1 comment 1 reply
-
Oh yeah 😉 Functions Baby 🏋
Beta Was this translation helpful? Give feedback.
All reactions
 
 1 reply
 
 
 -
tak!
Beta Was this translation helpful? Give feedback.
All reactions
- 
 😄 2
 Answer selected by
 porsager
 
 Sign up for free
 to join this conversation on GitHub.
 Already have an account?
 Sign in to comment