paralect/node-mongo

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

Opened this issue · 3 comments

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 });

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

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

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)