dynamic subquery?

Is it possible to make dynamic subquery? I need to filter subquery by some value and cant figure out how to make it.

    let sq = db
      .select({
        lineId: documentLine.id,
        name: productVariant.fullName,
      })
      .from(documentLine)
      .leftJoin(
        productVariant,
        eq(productVariant.id, documentLine.productVariantId)
      )
      .limit(limit)
      .offset(offset)
      .where(eq(documentLine.documentId, 1))
      .as("line");

    const dynamicQuery = sq.$dynamic();
    if (documentId) {
      dynamicQuery.where(eq(documentLine.documentId, +documentId));
    }
Was this page helpful?