Cloud Code triggers questions - parse-platform

I have some questions regarding Cloud Code (Couldn't find details in the docs).
Can triggers (afterSave...) trigger other triggers ? Example: afterSave("Post"...) creates a new row in Comments where there's also an afterSave function attached.
Is there an afterInsert event ? Or do we need to use beforeSave and check if objectId is null ? Or is there another way to check such thing ? (I need to trigger certain function only after insert, not modification)
Thanks

The triggers will fire regardless of the source of the change. So afterSave would fire for a row created in another afterSave handler.
There isn't an afterInsert; only a afterSave. You can't check if objectId === null in afterSave because it would always be false (it was just saved, after all). You can use Parse.Object.isNew to check if it is new. I believe it still returns true in afterSave for a new object.

Related

Laravel eloquent doesn't insert data when bug occur during multiple insert

I'm trying to insert users using csv.
My model has an observer on "created" event.
However I can have a bug after some $users->save() because of code in my function created event (which I fixed).
The code in created event send an email to the user. The problem is if code crash after 5iterations, I got my 5emails send but no user in my db.
I'm wondering if Eloquent use transaction when you call multiple times save() ?
If yes how to force Eloquent to really save my object after each end of event created ?
May be I'm misunderstanding something with event, because I don't see the point of using this event if your not sure to have your model insert in your DB.
Finally found what was wrong...
To insert csv my project use :
https://github.com/Maatwebsite/Laravel-Excel
And after some research, this is "why" eloquent use transaction in my case :
https://docs.laravel-excel.com/3.1/imports/validation.html#database-transactions

How to prevent overwrites/duplicates in dynamoDB from triggering lambda

I have a DynamoDB that is getting data written to it from a java app
mapper.save(row, DynamoDBMapperConfig.builder().withSaveBehavior(SaveBehavior.CLOBBER).build());
I want to have a lambda trigger off of new items in the DDB so that their keys can be put onto SNS. However, if an item in DynamoDB is being overwritten (IE we received duplicate data) we do NOT want to do anything with it.
How to handle that? I control both the lambda and the code writing to DDB, but not the source of the data.
I don't think we can prevent the Lambda from being triggered when an item is overwritten in DynamoDB. But once the Lambda is triggered, we can identify if it's a new record or an existing record.
The input to the Lambda function will be a DynamoDBStreamEvent which contains an OldImage attribute. If that is present, it indicates that it's an existing record that got modified. In this case, we can just return from the Lambda without doing any processing.
Also, the event contains the entire snapshot before and after the insert in the OldImage and NewImage attributes. So we can also check whether some other attribute value has changed or not to decide whether it's an overwrite.
You need to have an IF, CASE, or something that looks at the stream record's eventName and if it is INSERT, which means new if I recall correctly, then it will run your code. If it is something like MODIFY, then it will not. There is an example in the DynamoDB documentation.

DynamoDB delete trigger OldImage

I would like to do some cleanup after a record has been deleted in my DynamoDB table. It would be pretty great if I could use triggers to do this. Unfortunately it seems that OldImage is not passed into "REMOVE" events. The problem is that I need some record attributes other than the keys in order to perform my cleanup and I can't actually read the record anymore to get these attributes once the event has triggered. Is there any other way I can still read attributes of a record that has been deleted in a trigger?
Change the DynamoDB stream to include new and old images.

Set a field value directly from an asyncValidate result

I have a form where a user enters an Id into a text field, and I then I validate said Id on the server. If the Id does not exist in the system, I display an error. I do this using async validation. If the Id does exist, however, the server will return a record from our database. I want to use those values to auto populate other fields in the form.
How would I accomplish this?
I did some searching and the closest solution I found was this other question on StackOverflow. The thing is, I want to change the value after my asycValidate logic has succeeded. I don't think I can trigger the action creator from inside asyncValidate, and I'm not aware of a way to have asyncValidate trigger a callback from inside the form component.
I was able to get it to work, by following the solutions discussed in the following thread: https://github.com/erikras/redux-form/issues/442

How to identify a new PFRelation on AfterSave using Parse

Is it possible to identify what has changed on a PFRelation column on the AfterSave method of a table? I want to identify an User who liked an Item. The Item table has a UserLikes PFRelation which has all users that liked that Item. I want to trigger execute a notification whenever a new User likes an Item, but only for that new User, not the ones that liked it previously.
I'm not sure if that is possible. However, I would instead create a cloud code function for the notification, and then trigger that in the afterSave block for the like. If the like was successfully saved, notify the owner of the liked item.
If that doesn't work with your current data model, take a look at how Parse solves liking in the Anypic tutorial app.

Resources