-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Adds batchIndex and batchCount properties to beforeSave and afterSave hooks #5896
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5896 +/- ##
==========================================
+ Coverage 93.56% 93.69% +0.12%
==========================================
Files 153 153
Lines 10729 10734 +5
==========================================
+ Hits 10039 10057 +18
+ Misses 690 677 -13
Continue to review full report at Codecov.
|
Looks good. Can you write a few test cases for this? |
@@ -109,6 +115,8 @@ RestWrite.prototype.execute = function() { | |||
return this.deleteEmailResetTokenIfNeeded(); | |||
}) | |||
.then(() => { | |||
delete this.data._batchCount; |
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.
Is this needed?
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.
If I remove those then it will not pass the schema validation. When this is called via this function, it sets this.data back to the request.body params which includes the _batchCount and _batchIndex properties. Do you have any suggestions?
I'm having trouble getting the tests to run on my machine. I have VS Code and Jasmine but when I run the tests they all fail. Any suggestions? |
@stevestencil What error are you receiving? Do you have mongo running? |
What's the error message? Can you share the version of Node.js and npm that you are using? |
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.
@stevestencil I was looking at the two PRs you sent and also your issue (#5667). I think this solution will not fit your needs. You do not have any warranty that a afterSave
trigger on an object with index=5
and count=6
is actually the last object in the batch and the batch has finished. The objects are sent in parallel and the save operation, therefore the triggers, can be finished in an order different than the one that you originally sent.
What if I make a total of 6 values that get passed in, something like: subBatchIndex - the index of the sub batch within the total batch I believe these values would give me all I need? |
I will hook up my test environment later on today and get back to you on the errors I'm getting |
Even with all theses values you can not ensure that a trigger running in the last object of the last sub batch of the latest batch means that everything is finished. As an example, the first batch (index 0) can finish after the last batch (index = count). The unique way to know if everything has finished is actually in your client. I recommend you wait there, and, when all batches have finished, you can call a cloud function to perform the operation that you need at the end. You can even use a similar approach in your cloud code to do the whole thing. You can create a cloud code function that will receive all your objects, will save all of them, will wait the save operations to complete, and finally will do what you need to do by the end. |
That still wouldn’t give me the ability to not run the beforeSave and afterSave triggers for all but the first or last object. I want to be able to run code in the beforeSave and afterSave like normal but also know when the first or last object is coming through |
If you want to make sure which one is the first and which one is the last, you need to send them in a sequence. I think your best chance is something like this: Parse.Cloud.define('myCustomBatchFunction', async req => {
const first = req.params.objects[0];
first.set('first', 'true'); // then you can remove it in the before save trigger if you don't want it to go to the database
await first.save();
const others = req.params.objects.slice(1, -1);
await Parse.Object.saveAll(others);
const last = req.params.objects[req.params.object.length - 1];
last.set('last', true);
await last.save();
}); |
so getting a beforeSaveAll might be next to impossible however an afterSaveAll I think is doable. If I modify what I have to include a "batchId" I could then keep track of the items being saved and then call a afterSaveAll function once they're all complete. I hope that makes sense? thoughts? |
It can work, but I am not sure it is so easy to be done. I can imagine three different solutions:
|
@stevestencil I will close this one so we can open a new one once we have decided the best way to go. |
this pull request is for #5667. A separate pull request is pending on Parse-SDK-JS project to allow for this.