Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3157,4 +3157,14 @@ describe('afterLogin hook', () => {

await Parse.Cloud.run('contextTest', {}, { context: { a: 'a' } });
});

it('afterFind should have access to context', async () => {
Parse.Cloud.afterFind('TestObject', req => {
expect(req.context.a).toEqual('a');
});
const obj = new TestObject();
await obj.save();
const query = new Parse.Query(TestObject);
await query.find({ context: { a: 'a' } });
});
});
18 changes: 15 additions & 3 deletions src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function RestQuery(
restWhere = {},
restOptions = {},
clientSDK,
runAfterFind = true
runAfterFind = true,
context
) {
this.config = config;
this.auth = auth;
Expand All @@ -36,6 +37,7 @@ function RestQuery(
this.runAfterFind = runAfterFind;
this.response = null;
this.findOptions = {};
this.context = context || {};

if (!this.auth.isMaster) {
if (this.className == '_Session') {
Expand Down Expand Up @@ -222,7 +224,16 @@ RestQuery.prototype.each = function (callback) {
return !finished;
},
async () => {
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
const query = new RestQuery(
config,
auth,
className,
restWhere,
restOptions,
clientSDK,
this.runAfterFind,
this.context
);
const { results } = await query.execute();
results.forEach(callback);
finished = results.length < restOptions.limit;
Expand Down Expand Up @@ -772,7 +783,8 @@ RestQuery.prototype.runAfterFindTrigger = function () {
this.className,
this.response.results,
this.config,
parseQuery
parseQuery,
this.context
)
.then(results => {
// Ensure we properly set the className back
Expand Down
25 changes: 22 additions & 3 deletions src/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ function find(config, auth, className, restWhere, restOptions, clientSDK, contex
.then(result => {
restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions;
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
const query = new RestQuery(
config,
auth,
className,
restWhere,
restOptions,
clientSDK,
true,
context
);
return query.execute();
});
}
Expand All @@ -62,7 +71,16 @@ const get = (config, auth, className, objectId, restOptions, clientSDK, context)
.then(result => {
restWhere = result.restWhere || restWhere;
restOptions = result.restOptions || restOptions;
const query = new RestQuery(config, auth, className, restWhere, restOptions, clientSDK);
const query = new RestQuery(
config,
auth,
className,
restWhere,
restOptions,
clientSDK,
true,
context
);
return query.execute();
});
};
Expand Down Expand Up @@ -187,7 +205,8 @@ function update(config, auth, className, restWhere, restObject, clientSDK, conte
restWhere,
undefined,
undefined,
false
false,
context
).execute({
op: 'update',
});
Expand Down
16 changes: 12 additions & 4 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ export function getRequestObject(
if (originalParseObject) {
request.original = originalParseObject;
}

if (
triggerType === Types.beforeSave ||
triggerType === Types.afterSave ||
triggerType === Types.beforeDelete ||
triggerType === Types.afterDelete
triggerType === Types.afterDelete ||
triggerType === Types.afterFind
) {
// Set a copy of the context on the request object.
request.context = Object.assign({}, context);
Expand Down Expand Up @@ -388,13 +388,21 @@ function logTriggerErrorBeforeHook(triggerType, className, input, auth, error) {
);
}

export function maybeRunAfterFindTrigger(triggerType, auth, className, objects, config, query) {
export function maybeRunAfterFindTrigger(
triggerType,
auth,
className,
objects,
config,
query,
context
) {
return new Promise((resolve, reject) => {
const trigger = getTrigger(className, triggerType, config.applicationId);
if (!trigger) {
return resolve();
}
const request = getRequestObject(triggerType, auth, null, null, config);
const request = getRequestObject(triggerType, auth, null, null, config, context);
if (query) {
request.query = query;
}
Expand Down