How to get previous data?

#1

Hi,I have more than one collections when insert transaction.
Ex :

Insert transaction

const insertTransaction = new ValidatedMethod({
  name: 'insertTransaction',
  mixins: [CallPromiseMixin],
  validate: null,
  run({doc,details1,details2,details3}) {
    if (Meteor.isServer) {
         try{
               collection1.insert(doc)
               // details is array
               collection2.rawCollection().insertMany(details1)
               collection3.rawCollection().insertMany(details2)
               collection4.rawCollection().insertMany(details3)

              return 'success'
         }catch(err){
            console.log(err)
            /** Remove data if error  */
            collection1.remove({_id})
            collection2.remove({refId})
            collection3.remove({refId})
            collection4.remove({refId})
         }
    }
  },
})

update transaction

const updateTransaction = new ValidatedMethod({
  name: 'updateTransaction',
  mixins: [CallPromiseMixin],
  validate: null,
  run({doc,details1,details2,details3}) {
    if (Meteor.isServer) {
         try{
               collection1.update({_id:doc._id},{$set:doc})

              /** Details is remove before insert new */
               collection2.remove({refId})
               collection3.remove({refId})
               collection4.remove({refId})

               // insert details
               collection2.rawCollection().insertMany(details1)
               collection3.rawCollection().insertMany(details2)
               collection4.rawCollection().insertMany(details3)

              return 'success'
         }catch(err){
            console.log(err)
         }
    }
  },
})

If error I want to get previous data, I don’t want to remove data. how can I do ?

#2

I think this will be better done with mongo transactions. If one operation fails all the transaction will be aborted and you will have the initial state unchanged.

I just found this forum post that looks interesting.

Hope this helps.

#3

yes , thank you @pmogollon