Skip to content

Commit c68d055

Browse files
authored
Pass request.query to afterFind (#6960)
* Initial Commit * Update triggers.js
1 parent 78b59fb commit c68d055

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

spec/CloudCode.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,37 @@ describe('beforeFind hooks', () => {
20182018
});
20192019

20202020
describe('afterFind hooks', () => {
2021+
it('should add afterFind trigger', done => {
2022+
Parse.Cloud.afterFind('MyObject', req => {
2023+
const q = req.query;
2024+
expect(q instanceof Parse.Query).toBe(true);
2025+
const jsonQuery = q.toJSON();
2026+
expect(jsonQuery.where.key).toEqual('value');
2027+
expect(jsonQuery.where.some).toEqual({ $gt: 10 });
2028+
expect(jsonQuery.include).toEqual('otherKey,otherValue');
2029+
expect(jsonQuery.excludeKeys).toBe('exclude');
2030+
expect(jsonQuery.limit).toEqual(100);
2031+
expect(jsonQuery.skip).toBe(undefined);
2032+
expect(jsonQuery.order).toBe('key');
2033+
expect(jsonQuery.keys).toBe('select');
2034+
expect(jsonQuery.readPreference).toBe('PRIMARY');
2035+
expect(jsonQuery.includeReadPreference).toBe('SECONDARY');
2036+
expect(jsonQuery.subqueryReadPreference).toBe('SECONDARY_PREFERRED');
2037+
});
2038+
2039+
const query = new Parse.Query('MyObject');
2040+
query.equalTo('key', 'value');
2041+
query.greaterThan('some', 10);
2042+
query.include('otherKey');
2043+
query.include('otherValue');
2044+
query.ascending('key');
2045+
query.select('select');
2046+
query.exclude('exclude');
2047+
query.readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED');
2048+
query.find().then(() => {
2049+
done();
2050+
});
2051+
});
20212052
it('should add afterFind trigger using get', done => {
20222053
Parse.Cloud.afterFind('MyObject', req => {
20232054
for (let i = 0; i < req.objects.length; i++) {

src/RestQuery.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function RestQuery(
188188
// Returns a promise for the response - an object with optional keys
189189
// 'results' and 'count'.
190190
// TODO: consolidate the replaceX functions
191-
RestQuery.prototype.execute = function(executeOptions) {
191+
RestQuery.prototype.execute = function (executeOptions) {
192192
return Promise.resolve()
193193
.then(() => {
194194
return this.buildRestWhere();
@@ -216,7 +216,7 @@ RestQuery.prototype.execute = function(executeOptions) {
216216
});
217217
};
218218

219-
RestQuery.prototype.each = function(callback) {
219+
RestQuery.prototype.each = function (callback) {
220220
const { config, auth, className, restWhere, restOptions, clientSDK } = this;
221221
// if the limit is set, use it
222222
restOptions.limit = restOptions.limit || 100;
@@ -248,7 +248,7 @@ RestQuery.prototype.each = function(callback) {
248248
);
249249
};
250250

251-
RestQuery.prototype.buildRestWhere = function() {
251+
RestQuery.prototype.buildRestWhere = function () {
252252
return Promise.resolve()
253253
.then(() => {
254254
return this.getUserAndRoleACL();
@@ -277,7 +277,7 @@ RestQuery.prototype.buildRestWhere = function() {
277277
};
278278

279279
// Uses the Auth object to get the list of roles, adds the user id
280-
RestQuery.prototype.getUserAndRoleACL = function() {
280+
RestQuery.prototype.getUserAndRoleACL = function () {
281281
if (this.auth.isMaster) {
282282
return Promise.resolve();
283283
}
@@ -298,7 +298,7 @@ RestQuery.prototype.getUserAndRoleACL = function() {
298298

299299
// Changes the className if redirectClassNameForKey is set.
300300
// Returns a promise.
301-
RestQuery.prototype.redirectClassNameForKey = function() {
301+
RestQuery.prototype.redirectClassNameForKey = function () {
302302
if (!this.redirectKey) {
303303
return Promise.resolve();
304304
}
@@ -313,7 +313,7 @@ RestQuery.prototype.redirectClassNameForKey = function() {
313313
};
314314

315315
// Validates this operation against the allowClientClassCreation config.
316-
RestQuery.prototype.validateClientClassCreation = function() {
316+
RestQuery.prototype.validateClientClassCreation = function () {
317317
if (
318318
this.config.allowClientClassCreation === false &&
319319
!this.auth.isMaster &&
@@ -358,7 +358,7 @@ function transformInQuery(inQueryObject, className, results) {
358358
// $inQuery clause.
359359
// The $inQuery clause turns into an $in with values that are just
360360
// pointers to the objects returned in the subquery.
361-
RestQuery.prototype.replaceInQuery = function() {
361+
RestQuery.prototype.replaceInQuery = function () {
362362
var inQueryObject = findObjectWithKey(this.restWhere, '$inQuery');
363363
if (!inQueryObject) {
364364
return;
@@ -419,7 +419,7 @@ function transformNotInQuery(notInQueryObject, className, results) {
419419
// $notInQuery clause.
420420
// The $notInQuery clause turns into a $nin with values that are just
421421
// pointers to the objects returned in the subquery.
422-
RestQuery.prototype.replaceNotInQuery = function() {
422+
RestQuery.prototype.replaceNotInQuery = function () {
423423
var notInQueryObject = findObjectWithKey(this.restWhere, '$notInQuery');
424424
if (!notInQueryObject) {
425425
return;
@@ -485,7 +485,7 @@ const transformSelect = (selectObject, key, objects) => {
485485
// The $select clause turns into an $in with values selected out of
486486
// the subquery.
487487
// Returns a possible-promise.
488-
RestQuery.prototype.replaceSelect = function() {
488+
RestQuery.prototype.replaceSelect = function () {
489489
var selectObject = findObjectWithKey(this.restWhere, '$select');
490490
if (!selectObject) {
491491
return;
@@ -550,7 +550,7 @@ const transformDontSelect = (dontSelectObject, key, objects) => {
550550
// The $dontSelect clause turns into an $nin with values selected out of
551551
// the subquery.
552552
// Returns a possible-promise.
553-
RestQuery.prototype.replaceDontSelect = function() {
553+
RestQuery.prototype.replaceDontSelect = function () {
554554
var dontSelectObject = findObjectWithKey(this.restWhere, '$dontSelect');
555555
if (!dontSelectObject) {
556556
return;
@@ -599,7 +599,7 @@ RestQuery.prototype.replaceDontSelect = function() {
599599
});
600600
};
601601

602-
const cleanResultAuthData = function(result) {
602+
const cleanResultAuthData = function (result) {
603603
delete result.password;
604604
if (result.authData) {
605605
Object.keys(result.authData).forEach(provider => {
@@ -638,7 +638,7 @@ const replaceEqualityConstraint = constraint => {
638638
return constraint;
639639
};
640640

641-
RestQuery.prototype.replaceEquality = function() {
641+
RestQuery.prototype.replaceEquality = function () {
642642
if (typeof this.restWhere !== 'object') {
643643
return;
644644
}
@@ -649,7 +649,7 @@ RestQuery.prototype.replaceEquality = function() {
649649

650650
// Returns a promise for whether it was successful.
651651
// Populates this.response with an object that only has 'results'.
652-
RestQuery.prototype.runFind = function(options = {}) {
652+
RestQuery.prototype.runFind = function (options = {}) {
653653
if (this.findOptions.limit === 0) {
654654
this.response = { results: [] };
655655
return Promise.resolve();
@@ -685,7 +685,7 @@ RestQuery.prototype.runFind = function(options = {}) {
685685

686686
// Returns a promise for whether it was successful.
687687
// Populates this.response.count with the count
688-
RestQuery.prototype.runCount = function() {
688+
RestQuery.prototype.runCount = function () {
689689
if (!this.doCount) {
690690
return;
691691
}
@@ -700,7 +700,7 @@ RestQuery.prototype.runCount = function() {
700700
};
701701

702702
// Augments this.response with all pointers on an object
703-
RestQuery.prototype.handleIncludeAll = function() {
703+
RestQuery.prototype.handleIncludeAll = function () {
704704
if (!this.includeAll) {
705705
return;
706706
}
@@ -729,7 +729,7 @@ RestQuery.prototype.handleIncludeAll = function() {
729729
};
730730

731731
// Updates property `this.keys` to contain all keys but the ones unselected.
732-
RestQuery.prototype.handleExcludeKeys = function() {
732+
RestQuery.prototype.handleExcludeKeys = function () {
733733
if (!this.excludeKeys) {
734734
return;
735735
}
@@ -747,7 +747,7 @@ RestQuery.prototype.handleExcludeKeys = function() {
747747
};
748748

749749
// Augments this.response with data at the paths provided in this.include.
750-
RestQuery.prototype.handleInclude = function() {
750+
RestQuery.prototype.handleInclude = function () {
751751
if (this.include.length == 0) {
752752
return;
753753
}
@@ -774,7 +774,7 @@ RestQuery.prototype.handleInclude = function() {
774774
};
775775

776776
//Returns a promise of a processed set of results
777-
RestQuery.prototype.runAfterFindTrigger = function() {
777+
RestQuery.prototype.runAfterFindTrigger = function () {
778778
if (!this.response) {
779779
return;
780780
}
@@ -794,14 +794,20 @@ RestQuery.prototype.runAfterFindTrigger = function() {
794794
if (this.findOptions.pipeline || this.findOptions.distinct) {
795795
return Promise.resolve();
796796
}
797+
798+
const json = Object.assign({}, this.restOptions);
799+
json.where = this.restWhere;
800+
const parseQuery = new Parse.Query(this.className);
801+
parseQuery.withJSON(json);
797802
// Run afterFind trigger and set the new results
798803
return triggers
799804
.maybeRunAfterFindTrigger(
800805
triggers.Types.afterFind,
801806
this.auth,
802807
this.className,
803808
this.response.results,
804-
this.config
809+
this.config,
810+
parseQuery
805811
)
806812
.then(results => {
807813
// Ensure we properly set the className back

src/triggers.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,18 @@ export function maybeRunAfterFindTrigger(
416416
auth,
417417
className,
418418
objects,
419-
config
419+
config,
420+
query
420421
) {
421422
return new Promise((resolve, reject) => {
422423
const trigger = getTrigger(className, triggerType, config.applicationId);
423424
if (!trigger) {
424425
return resolve();
425426
}
426427
const request = getRequestObject(triggerType, auth, null, null, config);
428+
if (query) {
429+
request.query = query;
430+
}
427431
const { success, error } = getResponseObject(
428432
request,
429433
object => {

0 commit comments

Comments
 (0)