A quick solution I made to update associated models dynamically. This can be taken a step further and made more generic so DirectorKey
and director
are based on any parent model.
I am using a transaction to rollback everything when an error occurs. Just a bit of fun and it works!
Note: createdAt
might be updated if supplied in the updates but this doesn't tend to happen in my case as Postman to controller validation do not allow default date fields.
The following works fine with the exceptions:
- it is not generic but can easily be made to be, this is from a use case I was proving.
- it will fill missing field data with null. This can be fixed by replacing bulkCreate with a custom method to generate the [key-values] of each associate for use in VALUES but using ON DUPLICATE KEY UPDATE. This works well.