DT
Drizzle Team•3w ago
Amur

Conditional queries with $dynamic

Is there a way to not execute the query yet within the if statement?
const countQuery = db
.select({
count: sql<number>`count(${recipeTable.id})`.mapWith(Number),
})
.from(recipeTable)
.where(and(...where))
.$dynamic();

if (filters.language) {
countQuery.leftJoin(
recipeTranslationTable,
and(eq(recipeTable.id, recipeTranslationTable.recipeId), eq(recipeTranslationTable.locale, filters.language)),
);
}
const countQuery = db
.select({
count: sql<number>`count(${recipeTable.id})`.mapWith(Number),
})
.from(recipeTable)
.where(and(...where))
.$dynamic();

if (filters.language) {
countQuery.leftJoin(
recipeTranslationTable,
and(eq(recipeTable.id, recipeTranslationTable.recipeId), eq(recipeTranslationTable.locale, filters.language)),
);
}
1 Reply
Amur
AmurOP•3w ago
or is it a typing issue? because if i dont await it then i dont think it gets executed but eslint is yelling at me that promises should be awaited 🤔

Did you find this page helpful?