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

toMongoQuery #30

Closed
sarkistlt opened this issue Feb 22, 2018 · 5 comments
Closed

toMongoQuery #30

sarkistlt opened this issue Feb 22, 2018 · 5 comments
Labels

Comments

@sarkistlt
Copy link

lets say I have a rule:
can('read', ['products'], { tid: 'someId' })
then when I'm using:
toMongoQuery(rules)

it returns { '$or': [ { tid: 'someId' } ] }, shouldn't it just be { tid: 'someId' } ?

@stalniy
Copy link
Owner

stalniy commented Feb 22, 2018

Yes, it could be. But this function doesn't do optimisation. It just follows the general rule: all regular rules are OR-ed.

So, in your case you have only 1 rules that's why they are identical. It's possible to optimise this if it's important for you. Just to check if $or contains only 1 element and there are no $and statements

@sarkistlt
Copy link
Author

thanks for quick response, so if it will be can('read', ['products'], { id: 'someId', tid: 'someId' }), it will generate, { '$or': [ { id: 'someId', tid: 'someId' } ] }?
but in that case statement will be true if at least one if id or tid is matching but not both correct?

@stalniy
Copy link
Owner

stalniy commented Feb 22, 2018

No, $or works this way:

// this is equivalent  of
// (a === 1 && b === 2 ) || c === 3
{
  $or: [{ a: 1, b: 2 }, { c: 3 }]
}

Update: so in your case it will be the same as id === "someId" && tid === "someId

@sarkistlt
Copy link
Author

I see, ok thanks, that work for me

@stalniy
Copy link
Owner

stalniy commented Feb 22, 2018

You are welcome! :)

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

No branches or pull requests

2 participants