Conditional join

Hey! I am looking for a way of making conditional joins based on some statement. I found only one post in this topic helpHow to do conditional joins with the query builder? that does not help me much.


lets say this is my query:
let query = await db
            .select()
            .from(documentLine)
            .leftJoin(document, eq(document.id, documentLine.documentId))
            .leftJoin(user, eq(user.id, documentLine.userId))

I need to make joins conditional, because some filters require joins and in other cases I do not need join so its like wasted computation time. So example showing my need:
if (filters.includes(documentId)){
query.add(.leftJoin(document, eq(document.id, documentLine.documentId)))
}

I would be glad for any tip how to do this.
Was this page helpful?