I want to use "if" condition in my findMany() quer...
# orm-help
n
I want to use "if" condition in my findMany() query.For instance, in the following query, this.service.findMany({ where: { platformApplicationIdentity: { in: query.platformApplicationIdentities.split(","), }, settingType: { in: query.settingTypes.split(",") }, identity: { in: query.identities.split(",") }, user: { in: query.users.split(",") }, },
I want to apply where on user field only if the query.users is not null or not blank.
r
A simple condition should work:
Copy code
query.users ? user: { 
  in: query.users.split(",") 
}: undefined
👍 1
n
THx
👍 1
this.service.findMany({ where: { query.platformApplicationIdentities ? platformApplicationIdentity: { in: query.platformApplicationIdentities.split(","), }:undefined, query.settingTypes ? settingType: { in: query.settingTypes.split(",") }:undefined, query.identities ? identity: { in: query.identities.split(",") }:undefined, query.users ? user: { in: query.users.split(",") }:undefined }, }); I changed my code to this, but it shows many errors in code editor. Is it incorrect somehow?
r
Try this then:
Copy code
where: {
    ...(query.users && { user: { in: query.users.split(', ') } })
},
🧙‍♂️ 1
🤩 1
l
The resason is that an expression can't be placed where there should be a property name, but Ryan's solution probably works
👍 1
n
Thanks a lot it worked like a charm 🙂
🙌 1