Skip to content

Perform $or filters #3194

@IceOnFire

Description

@IceOnFire

Informations

  • Node.js version: v10.15.3
  • NPM version: 6.4.1
  • Strapi version: 3.0.0-alpha.26.1
  • Database: MongoDB v3.6.8
  • Operating system: Linux Ubuntu 19.04 (Disco Dingo)
  • (Optional) Link to your Project:

What is the current behavior?

After migrating from 3.0.0-alpha.24 to 3.0.0-alpha.26.1 I changed all my services so they use convertRestQueryParams and buildQuery. Everything works fine.

In fetchAll I used to intercept the incoming parameters in order to change them into custom where clauses. For example, if params.isBanned == true, then I used to add a where clause such as

{
  $and: [
    { banStartDate: { $gt: date } },
    { banEndDate: { $lt: date } },
  ]
}

otherwise if params.isBanned == false

I used to put

{
  $or: [
    { banStartDate: { $lte: date } },
    { banEndDate: { $gte: date } }
  ]
}

Now that we have convertRestQueryParams and buildQuery, the format of our where clause has changed so I adapted it to the new structure. For the $and case it changes into

[
  { field: 'banStartDate', operator: 'lte', value: date },
  { field: 'banEndDate', operator: 'gte', value: date },
]

but for the $or case I cannot seem to find a solution: from what I can see the buildQuery function supports the $or case only if I have the same operator and multiple values, but in my case I need to put completely different clauses in $or.

Steps to reproduce the problem

Just try to add some custom filters such as the one described above.

What is the expected behavior?

It would be great if buildQuery supported multiple clauses in $or, otherwise simple custom filters such as the one described cannot be defined without going to a lower level.

Suggested solutions

Maybe the buildQuery function could have a special case in which, if the operator is or, then it just builds the $or clause with an array of values taken from value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: feature requestIssue suggesting a new featureseverity: mediumIf it breaks the basic use of the product but can be worked around

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions