Create or Update when inserting records in a Collection #12742
Unanswered
rjohnsonbade-corrivium
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, we are using Payload CMS with PostgreSQL as the backend for our application.
We have a use case where we have data that is streaming in from a 3rd party; this data stream is processed by a serverless function that inserts the data in to Payload CMS via the REST API.
The records we receive may either be new, or they may be updated versions of previously processed records.
The updated records retain the same identifier.
Ideally we would like to call a Create or Update endpoint on the API; which handles the above situation gracefully.
I had a look at adding a
beforeOperation
hook, however it appears you cannot actually re-route the operation; just change it's arguments.Currently we are trying a create, and if we receive an error we are parsing the error message to identify when it is due to a duplicate ID, then calling update; this however does result in an error response, which is not ideal.
The alternatives I see are to try and get each record prior to creating or updating it; however we are trying to keep this efficient as this stream of data can get quite busy during peaks and this would result in more network calls.
Alternatively we could create a custom endpoint for an "upsert" style operation; which uses the local api to more efficiently do the above; perhaps that is our best option.
Just checking we're not missing any obvious alternatives here?
Beta Was this translation helpful? Give feedback.
All reactions