node-mongo icon indicating copy to clipboard operation
node-mongo copied to clipboard

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

Open alldayalone opened this issue 4 years ago • 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 });

alldayalone avatar Oct 20 '21 07:10 alldayalone

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 avatar Oct 27 '21 09:10 alldayalone

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

NesterenkoNikita avatar Nov 03 '21 12:11 NesterenkoNikita

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)

NesterenkoNikita avatar Nov 03 '21 12:11 NesterenkoNikita