-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Adding $nor operator support #4768
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
Changes from 10 commits
8898481
b668356
9431576
b648ccc
1c597b7
5301fc3
64a9ea5
d493f39
6944639
51d799a
730ad21
388ea12
4415748
318501c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2567,6 +2567,48 @@ describe('Parse.Query testing', () => { | |
}); | ||
}); | ||
|
||
it('$select inside $nor', (done) => { | ||
const objects = Array.from(Array(10).keys()).map((rating) => { | ||
return new TestObject({ 'rating': rating }); | ||
}); | ||
|
||
const highValue = 5; | ||
const lowValue = 3; | ||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
where: { | ||
$nor: [ | ||
{ rating : { $gt : highValue } }, | ||
{ rating : { $lte : lowValue } }, | ||
] | ||
}, | ||
} | ||
}); | ||
|
||
Parse.Object.saveAll(objects).then(() => { | ||
return rp.get(Parse.serverURL + "/classes/TestObject", options); | ||
}).then((results) => { | ||
expect(results.results.length).toBe(highValue - lowValue); | ||
expect(results.results.every(res => res.rating > lowValue && res.rating <= highValue)).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('$nor invalid query', (done) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe another test when |
||
const options = Object.assign({}, masterKeyOptions, { | ||
body: { | ||
where: { $nor: [] }, | ||
} | ||
}); | ||
const obj = new TestObject(); | ||
obj.save().then(() => { | ||
return rp.get(Parse.serverURL + "/classes/TestObject", options); | ||
}).then(done.fail).catch((error) => { | ||
equal(error.error.code, Parse.Error.INVALID_JSON); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
done(); | ||
}); | ||
}); | ||
|
||
it("dontSelect query", function(done) { | ||
const RestaurantObject = Parse.Object.extend("Restaurant"); | ||
const PersonObject = Parse.Object.extend("Person"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -306,12 +306,15 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => { | |
patterns.push(`$${index}:name = $${index + 1}`); | ||
values.push(fieldName, fieldValue); | ||
index += 2; | ||
} else if (fieldName === '$or' || fieldName === '$and') { | ||
} else if (['$or', '$nor', '$and'].includes(fieldName)) { | ||
const clauses = []; | ||
const clauseValues = []; | ||
fieldValue.forEach((subQuery) => { | ||
const clause = buildWhereClause({ schema, query: subQuery, index }); | ||
if (clause.pattern.length > 0) { | ||
if (fieldName === '$nor') { | ||
clause.pattern = `(NOT ${clause.pattern})`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True I was pretty lazy 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I figured it worked but letMs write it cleanly please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll fix that |
||
clauses.push(clause.pattern); | ||
clauseValues.push(...clause.values); | ||
index += clause.values.length; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ const transformObjectACL = ({ ACL, ...result }) => { | |
return result; | ||
} | ||
|
||
const specialQuerykeys = ['$and', '$or', '_rperm', '_wperm', '_perishable_token', '_email_verify_token', '_email_verify_token_expires_at', '_account_lockout_expires_at', '_failed_login_count']; | ||
const specialQuerykeys = ['$and', '$or', '$nor', '_rperm', '_wperm', '_perishable_token', '_email_verify_token', '_email_verify_token_expires_at', '_account_lockout_expires_at', '_failed_login_count']; | ||
|
||
const isSpecialQueryKey = key => { | ||
return specialQuerykeys.indexOf(key) >= 0; | ||
|
@@ -62,7 +62,7 @@ const validateQuery = (query: any): void => { | |
} | ||
|
||
if (query.$or) { | ||
if (query.$or instanceof Array) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert these changes to They also have nothing to do with the PR. |
||
if (query.$or instanceof Array && query.$or.length > 1) { | ||
query.$or.forEach(validateQuery); | ||
|
||
/* In MongoDB, $or queries which are not alone at the top level of the | ||
|
@@ -99,15 +99,23 @@ const validateQuery = (query: any): void => { | |
}); | ||
query.$or.forEach(validateQuery); | ||
} else { | ||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Bad $or format - use an array value.'); | ||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Bad $or format - use an array of at least 2 values.'); | ||
} | ||
} | ||
|
||
if (query.$and) { | ||
if (query.$and instanceof Array) { | ||
if (query.$and instanceof Array && query.$and.length > 1) { | ||
query.$and.forEach(validateQuery); | ||
} else { | ||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Bad $and format - use an array value.'); | ||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Bad $and format - use an array of at least 2 values.'); | ||
} | ||
} | ||
|
||
if (query.$nor) { | ||
if (query.$nor instanceof Array && query.$nor.length > 0) { | ||
query.$nor.forEach(validateQuery); | ||
} else { | ||
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Bad $nor format - use an array of at least 1 value.'); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the name as
$select
isn't being used here