Skip to content
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

Atomic operations are not validated, _id is different, createdOn and updatedOn is not generated #51

Open
alldayalone opened this issue Oct 20, 2021 · 3 comments

Comments

@alldayalone
Copy link
Contributor

I need to use upsert and $setOnUpsert to provide idempotency based on unique external id. It is not possible with the regular update/findOneAndUpdate but with atomic I got problems described in the title

cardService.atomic.findOneAndUpdate({ cardProxy }, ({
    $set: _.omit(card, ['_id', 'cardProxy']),
    $setOnInsert: { _id: cardService.generateId() },
  }), { upsert: true });
@alldayalone
Copy link
Contributor Author

Let me clarify the use case.
I have a cards collection. It represents debit cards that I order from a 3rd party. It has a unique field cardProxy which is an identifier in a 3rd party service.
A card can be created and updated externally, for all changes we receive a webhook with a card object. I want to save the latest version of the card object every time I receive a webhook.
I should also take into account that webhooks can come in a different order (card:update before card:create) or concurrently.
I should avoid duplicates and stale data in my collection.

@NesterenkoNikita If you have any suggestions for a more simple solution I'll be happy to see it and make up some tests to simulate complicated scenarios for the use case

@NesterenkoNikita
Copy link
Member

@alldayalone you can use setServiceMethod to add custom functions to services

@NesterenkoNikita
Copy link
Member

Keep in mind that you will not be able to add validation to atomic methods. It's better to avoid using them except in specific situations (like migrations, for example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants