How to put all leftJoins in a var

to get data and count i have to duplicate leftJoins. I can put all conditions in a var for where clause, but i don't know how to be with leftJoins? example:
if (opts.input.orderId) {
conditions.push(eq(orders.id, opts.input.orderId));
}

const data = await db
.select()
.from(orders)
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.orderBy(desc(orders.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const count = await db
.select({ count: sql`count('*')`.mapWith(Number) })
.from(orders)
.where(and(...conditions))
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
if (opts.input.orderId) {
conditions.push(eq(orders.id, opts.input.orderId));
}

const data = await db
.select()
.from(orders)
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.orderBy(desc(orders.createdAt))
.limit(perPage)
.offset(perPage * (page - 1));

const count = await db
.select({ count: sql`count('*')`.mapWith(Number) })
.from(orders)
.where(and(...conditions))
.where(and(...conditions))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
.leftJoin(user, eq(orders.userId, user.id))
1 Reply
francis
francis12mo ago
uhhh what? why are you doing this?
Want results from more Discord servers?
Add your server