Creating snippets (best pratice)
How can I create query snippet once and add it to multiple queries?
Solution:Jump to solution
But if you want a reusable helper, you need to let go of some type-safety. It's very difficult and often impossible to create a generic and strict helper. You can do something like this
```ts
db.selectFrom('whatever').select('foobar').where(myLovelyFilter(searchPostalCode))
...
6 Replies
Why not first add the
where
statement and only after that split into rows
and count
queries?
Unrelated, but instead of this
it's 100% safe to do this
regarding the "prettier" stuff - why not 🙂
regarding where splitting can you show it on complex example?
Why does it have to be complex? What you're doing is running the same query twice except the other has columns selected and the other just has a single
count(*)
selection right?
So something like this
Solution
But if you want a reusable helper, you need to let go of some type-safety. It's very difficult and often impossible to create a generic and strict helper. You can do something like this
The type-safety issue there is that you can use
myLovelyFilter
with queries that don't actually have the searchablePostalCode
in contexts, leading to runtime errors.wow, I will definitely look into this. thank you