Build queries dynamically with conditions.

Ssps5/19/2023
How to build query on the basis of if-else conditions? I have tried doing it as below
let dbQuery = db.select().from(orders).where(and(eq(orders.organizationId, orgId), ne(orders.orderStatus , "CART"), eq(orders.id, Number(query))));
let dbQuery = db.select().from(orders).where(and(eq(orders.organizationId, orgId), ne(orders.orderStatus , "CART"), eq(orders.id, Number(query))));
if( status ){
if(status == "COMPLETE")
dbQuery.where(eq(orders.orderStatus, "COMPLETE"));
else if(status == "PROCESSING")
dbQuery.where(eq(orders.orderStatus, "PROCESSING"));
else if(status == "CANCELLED")
dbQuery.where(eq(orders.orderStatus, "CANCELLED"));
}
if( status ){
if(status == "COMPLETE")
dbQuery.where(eq(orders.orderStatus, "COMPLETE"));
else if(status == "PROCESSING")
dbQuery.where(eq(orders.orderStatus, "PROCESSING"));
else if(status == "CANCELLED")
dbQuery.where(eq(orders.orderStatus, "CANCELLED"));
}
But the where conditions don't seem to add and the result is returned without them.
Ssps5/19/2023
Thanks, it worked!

Looking for more? Join the community!