Build queries dynamically with conditions.

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.
2 Replies
sps
sps14mo ago
Thanks, it worked!