how to type dynamic where condition
i'd like to do something like
however the condition is
any
type in this case. does drizzle provide a type i can use? prisma has generated types that can be used in similar situations5 Replies
I think the idea is that you wouldn't bother wrapping the db.query.events.findFirst function. The type system can probably accomplish this but whats some examples of the condition(s) that will be passed in?
example conditions:
eq(events.id, id))
eq(events.slug, slug)
and so on. not wrapping would be pretty repetitive. not a big deal maybe but hope there's a more elegant solution
@ewilliam you'll need to add
npm install --save type-fest
But this may work:
Yea it does work 🙂
I'm actually going to use this in some of my stuff too… it is fantastic
What it's doing is using TypeScript to lookup the type of db, then get the type of query.events.findFirst function's first argument.
I couldn't figure out how to get access to the "where" key in the first argument without using type-fest. But type-fest is a type's only library and adds no bloat to bundles.
You can make it more generic like this:
This is cool. I’m going to copy into a util file as well
thanks @Robert!