Skip to content

Adding support for AfterFind #2968

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

Merged
merged 1 commit into from
Nov 12, 2016
Merged

Conversation

jbpringuey
Copy link
Contributor

Resubmitting (#2962) and fix git history issue.
For security reasons, I need to hide certain fields (like email) and rows from the API for use cases which are not supported by the current ACL framework. This plugin will enable all sorts of manipulation. With the promise, one can even augment their parse data !

Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking awesome! Can you update var's with const or let please to make it future proof?

}
};
logTriggerSuccessBeforeHook(triggerType, className, 'AfterFind', JSON.stringify(results), auth);
var resultsAsParseObjects = results.map(result => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with response.results as we don't need the temporary variable to be set.

@facebook-github-bot
Copy link

@jbpringuey updated the pull request - view changes

if (triggerPromise && typeof triggerPromise.then === "function") {
return triggerPromise.then(function(promiseResults){
if(promiseResults) {
var modifiedJSONResults = promiseResults.map(result => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why but I cannot seem to be able to replace this var with a const or a let as it fails . maybe a JS bug ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the complaint?

@jbpringuey
Copy link
Contributor Author

ok done with all the changes. Hopefully this one is the good one !

if (triggerPromise && typeof triggerPromise.then === "function") {
return triggerPromise.then(function(promiseResults){
if(promiseResults) {
var modifiedJSONResults = promiseResults.map(result => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the complaint?

@@ -240,6 +241,51 @@ function logTriggerErrorBeforeHook(triggerType, className, input, auth, error) {
});
}

export function maybeRunAfterFindTrigger(triggerType, auth, className, results, config) {
return new Promise(function (resolve, reject) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use arrow function instead of function for the promises please?

}
});
} else {
var modifiedJSONResults = response.results.map(result => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the var here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovilmart done with all changes except this one line. I have no idea why if I replace that var with a let it gives an internal error (the other var was actually ok to replace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What'a the actual error? You running the tests on node 4.5+?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node -v returns v6.2.2. I am trying to get a better error than internal error but even wrapping it around try/catch is not giving me the message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovilmart this one is really strange ... I tried to rewrite the code in different ways but it would fail with nothing in the logs ... not sure why

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flovilmart what do we need to do to get it merged ? I am using a custom version of parse-server but would like to use the standard one and I have no idea about why I cannot turn that into a let .

@facebook-github-bot
Copy link

@jbpringuey updated the pull request - view changes

Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After further review, I realized that this PR introduces a new anti-pattern (no handling of res.success) as well as work in synchronous mode (which is not supported anywhere).

Please address those before we can merge it.

return Parse.Object.fromJSON(result);
});
const triggerPromise = trigger(request, response);
logTriggerAfterHook(triggerType, className, JSON.stringify(modifiedJSONResults), auth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifiedJSONResults is not defined here...
This is probably why you can't use anything else but var https://github.com/ParsePlatform/parse-server/pull/2968/files#diff-86ec9a2dbbd2c72ada5a115e9f9c4e6aR281 as this variable leaks out of the scope.

This line should be put after the promise has resolved. here

});
return resolve(modifiedJSONResults);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the logLine here by replacing with:

}).then((results) => {
  logTriggerAfterHook(triggerType, className, JSON.stringify(results), auth);
  return results;
});

const triggerPromise = trigger(request, response);
logTriggerAfterHook(triggerType, className, JSON.stringify(modifiedJSONResults), auth);
if (triggerPromise && typeof triggerPromise.then === "function") {
return triggerPromise.then(function(promiseResults){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use arrow function, beware of the spacings.

return resolve();
}
const request = getRequestObject(triggerType, auth, null, null, config);
const response = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The construction of that hook doesn't seem to match the rest of the hooks.

the found objects should be available on req.objects not on res.results

Then the response object should have:

res.success(); // nothing, use the req.results
res.success(modifiedResults); // ignore req.results, and return modified results to the user
req.error(error); // Something wrong happened, return the error to the user

as well as the promise constructs as you handled them.

We should wait till req.success(), req.error() are called OR a returned promise has resolved.
If nothing is return from the afterFind function, we should wait till those methods are called.
This is dangerous to introduce a new usage pattern as we expect to have something done in that trigger (unlike afterSave which doesn't have a req.success or req.error)

@jonas-db
Copy link

jonas-db commented Nov 5, 2016

As proposed at parse-server-modules/parse-evolution@b90c0f0

@jbpringuey
Copy link
Contributor Author

@flovilmart thanks for the awesome feedback, this week was crazy , I will fix all those concerns over the week end

@jonas-db
Copy link

@jbpringuey any update?

@jbpringuey
Copy link
Contributor Author

@jonas-db almost done, I could not get too much time last week end but will be done this week end

@facebook-github-bot
Copy link

@jbpringuey updated the pull request - view changes

@jbpringuey
Copy link
Contributor Author

@jonas-db @flovilmart hopefully that one is good.

Copy link
Contributor

@flovilmart flovilmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On small tiny little nit, otherwise that looks great!

@@ -194,7 +204,7 @@ export function getResponseObject(request, resolve, reject) {
if (request.triggerName === Types.beforeSave) {
response['object'] = request.object._getSaveJSON();
}
return resolve(response);
return resolve(response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the spaces there :)

@facebook-github-bot
Copy link

@jbpringuey updated the pull request - view changes

@jbpringuey
Copy link
Contributor Author

@flovilmart great catch, fixed !

@flovilmart
Copy link
Contributor

Thanks @jbpringuey ! Merging now!

@flovilmart flovilmart merged commit 19271fa into parse-community:master Nov 12, 2016
@jbpringuey
Copy link
Contributor Author

thanks !

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

Successfully merging this pull request may close these issues.

4 participants