Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create or Update when inserting records in a Collection #12742

Unanswered
rjohnsonbade-corrivium asked this question in Q&A
Discussion options

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?

You must be logged in to vote

Replies: 1 comment

Comment options

I would also love to know this. Before I had a beforeOperation hook which was very slow due to having to fetch each record the check for a unique compound index. Behold, I discovered compound index was added as a feature. yeah!

Now I would also like to know if there is some posibility if there will be added a createOrUpdate feature using ON CONFLICT, DO UPDATE queries.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /