paralect / node-mongo

Node Mongo — is reactive 🚀 extension to MongoDB API
https://github.com/paralect/node-mongo
23 stars 7 forks source link

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

Open alldayalone opened 3 years ago

alldayalone commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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

NesterenkoNikita commented 2 years ago

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)