-
I'm encountering an unexpected behavior when using Drizzle ORM alongside a custom query builder function. The problem seems to arise from the interaction between my function and the query builder. Here's a simplified version of the code: I have a function
Accompanied by a query builder function
Despite setting the initial where clause in the I suspect the issue might be related to how the custom query builder interacts with Drizzle ORM's query building process. I'm seeking advice on how to ensure that the initial where clause is preserved when using custom query builders alongside Drizzle ORM. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes, I think the documentation around the async function getSize(id: string, options: Pick<Options, "enabled"> = {
enabled: DEFAULT_OPTIONS.enabled,
}): Promise<InferSelectModel<typeof sizeSchema>> {
const { enabled = DEFAULT_OPTIONS.enabled } = options;
let query = db.select().from(sizeSchema).where(and(
eq(sizeSchema.id, id),
enabled !== undefined ? eq(sizeSchema.enabled, enabled) : undefined
)).$dynamic();
query = withEnabled(query, sizeSchema, enabled);
query = withLimit(query, 1);
const sizes = await query;
return sizes[0];
} The |
Beta Was this translation helpful? Give feedback.
-
Before that this worked for me. async function getSize(id: string, options: Pick<Options, "enabled"> = {
enabled: DEFAULT_OPTIONS.enabled,
}, recordsToFetch: number): Promise<InferSelectModel<typeof sizeSchema>> {
const { enabled = DEFAULT_OPTIONS.enabled } = options;
const whereClauses: any[] = [eq(sizeSchema.id, id)];
if (!enabled) {
whereClauses.push(eq(sizeSchema.enabled, enabled));
}
let query = db.select().from(sizeSchema).where(and(...whereClauses));
if(recordsToFetch) {
query.limit(recordsToFetch);
}
const sizes = await query;
return sizes[0];
} |
Beta Was this translation helpful? Give feedback.
Yes, I think the documentation around the
$dynamic()
queries is not perfectly clear that you still cannot call a method more than once.In your case, the where method used in the function
withEnabled
is overriding the where used ingetSize
.To solve your problem, you don't really need to use the
withEnabled
function, you just need to add the filter conditionally like this: