Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@casl/mongoose should use .and for adding the condition instead of .where when called on a Query #272

Closed
m-weeks opened this issue Mar 12, 2020 · 5 comments · Fixed by #273
Labels

Comments

@m-weeks
Copy link
Contributor

m-weeks commented Mar 12, 2020

I discovered this issue when I was writing a query that had an $and condition, and my query log was showing the condition as $and: [{}]

Given the following example:

appointments = await Appointment.find({
  $and: [
    { startDate: { $gte: new Date('2020-01-01') } },
    { startDate: { $lte: new Date('2020-01-05') } },
  ]
}).accessibleBy(ability)

The problem is because the accessibleBy method uses .where which will override the $and parameter, where as the .and method will add a new condition.

@m-weeks m-weeks changed the title @casl/mongoose should use .and for adding the condition instead of .where @casl/mongoose should use .and for adding the condition instead of .where when called on a Query Mar 12, 2020
@stalniy
Copy link
Owner

stalniy commented Mar 12, 2020

Thanks for the issue! Could you please tell which mongoose version you use?

@stalniy
Copy link
Owner

stalniy commented Mar 12, 2020

I’ll look into this tomorrow morning by Kyiv time. As a workaround I suggest you merge your $and into single object:

appointments = await Appointment.find({
    startDate: { 
        $gte: new Date('2020-01-01'),
        $lte: new Date('2020-01-05') 
    },
}).accessibleBy(ability)

@stalniy stalniy added the bug label Mar 12, 2020
@m-weeks
Copy link
Contributor Author

m-weeks commented Mar 12, 2020

@stalniy I am currently on mongoose 5.9.2

I created a PR that I believe solves the issue if you wanted to take a look at it

I am currently combining my conditions, but it would be ideal to have it fixed as it makes it trickier to dynamically generate query conditions, especially if you're adding multiple conditions on the same value. Or of course if you have an actual need for an $and condition

@stalniy
Copy link
Owner

stalniy commented Mar 13, 2020

Sure, I believe I’ll merge in few hours. Just need some time to make sure everything is ok

stalniy pushed a commit to m-weeks/casl that referenced this issue Mar 13, 2020
stalniy pushed a commit that referenced this issue Mar 13, 2020
@stalniy
Copy link
Owner

stalniy commented Mar 13, 2020

Released in @casl/[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants