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)),
    );
  }
Was this page helpful?