I need to implement some kind of (poor man´s) Transaction Tracking to keep Wix (Mongo) DB in a consistent state. Need to do this:
1) save data in 2 collections with a Main-Detail (1-n) relationship
2) update a denormalised, existing row in a 3-rd collection per Detail Row.
Suppose we keep track of items ordered. Main collection would hold CustomerId, Date, etc. Detail Rows would keep Items ordered, VAT, Number, Price, etc
The 3-rd Collection would decrease Stock per Item (thus denormalised).
I could do this:
1) (bulk) save Detail data to Log
2) save Main data to Log
If no errors, then "commit", meaning copy from log to real collections. And now comes the problem:
What if during this (server side) action an error occurs? I could try to set flags ("notValid") inside the rows and try to revert the decreasing of the stock in the denormalised 3-rd collection. But what if that fails?
Has anyone ever done this?