Dynamic Drizzle Relational Queries
Hi everybody, hope all is well. I was wondering if there is a way to build dynamic queries when using drizzle (relational) queries.
I read the section on dynamic query building but it doesn't cover relational queries.
I'm building a rest API and I want users to be able to pass filters via query params.
I have a tours table, tours can have many images as well as many start dates.
Is there a way of doing something like this? And make it flexible enough so that the user can filter based on various operations for different columns (ie: equal, lt, lte, gt, gte, like, etc...).
How would you solve this?
Thanks in advance, this would help me a lot.
5 Replies
I have something similar in my code, it may be useful
If the arrays are empty the where condition won't even show up so it's not a problem.
Hopefully it makes sense.
The problem with this approach is that the types will be wrong
You need to wrap your findMany call in a generic fuction for it to properly infer the types dynamically
I'll put together an example as this seems to be an issue people run into frequently and don't know how to figure it out
I took the example asked for here: https://github.com/drizzle-team/drizzle-orm/discussions/1483
GitHub
Relations input · drizzle-team drizzle-orm · Discussion #1483
I have a schema that looks like somewhat like this: export const employeesSchema = pgTable('employees', { firstName: varchar('first_name', { length: 256 }), lastName: varchar('l...
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@Angelelz I created this
buildAndConditions
function to generate the where, but typed it as any
would you happen to know how I could type this properly?