e-oj / Fawn

Transactions for MongoDB (See the README)
https://www.npmjs.com/package/fawn
MIT License
485 stars 54 forks source link

No support for use of result data from previous mongodb operation #4

Closed kumar529 closed 7 years ago

kumar529 commented 7 years ago
task.save("Comment", {text : "this is comment"})
task.update("Post", {_id : 'mongodbid'}, {$push : {comments : 'commentId'}})
task.run()
  .then(function(){
    //update is complete 
  })
  .catch(function(err){
    // Everything has been rolled back. 

    //log the error which caused the failure 
    console.log(err);
  });

In the above operation, if i want to save the id of created comment to 'Post', i cannot save. There should be an option for pipeline from first operation to second operation so that i can use the data generated from first operation to second. And if second operation fails, first operation should be reverted

e-oj commented 7 years ago

Thanks for the suggestion. Would it work for you if at the end of the run function, an array is returned, containing the results of each operation in sequence. An example:

task.save("Comment", {text : "this is comment"})
task.update("Post", {_id : 'mongodbid'}, {$push : {comments : 'commentId'}})
task.run()
  .then(function(results){
    //task is complete 

    //result from first operation
    var mongooseSaveResult = results[0];

    //result from second operation
    var mongooseUpdateResult = results[1]
  })
  .catch(function(err){
    // Everything has been rolled back. 

    //log the error which caused the failure 
    console.log(err);
  });

Any thoughts?

e-oj commented 7 years ago

@kumar529 Actually, I missed your point. It's currently not possible to pass results directly from one operation to the next because those results do not exist yet. The operations remain queued up until task.run() is called. This issue will require some sort of templating mechanism for data expected from a previous operation. I'll look into it, but this won't be a quick addition.

kumar529 commented 7 years ago

thanks for the reply.

task.save("Comment", {text : "this is comment"}).then(function(comment){
    return task.update("Post", {_id : 'mongodbid'}, {$push : {comments : 'comment._id'}});
})
.then(function(result){
    return task.run();
})
.then(function(){
    //task is complete
})
.catch(function(err){
    // Everything has been rolled back. 

    //log the error which caused the failure 
    console.log(err);
})

I think, each task should return a promise. so that it should be chained with promise chain mechanism. But all the operations are confirmed only when task.run() is executed. Otherwise error will caught.

e-oj commented 7 years ago

Here's the issue; the save, update and delete functions do not have a result. Instead, they create an object which contains all the information needed to carry out the operation and this object is added to a queue. When task.run is called, the task is saved to the database and every step is then executed. For this reason, there is no substantial result until the run function completes. But internally, at every step of a task, there is an array containing the results of previous steps. We could have a placeholder for the results of previous steps which will be replaced when the data is available.

task.save("Comment", {text : "this is comment"})

// "task.results.0" acts as placeholder for the result object of the first step in the task
task.update("Post", {_id : 'mongodbid'}, {$push : {comments : "task.results.0._id"}})
task.run()
  .then(function(){
    //update is complete 
  })
  .catch(function(err){
    // Everything has been rolled back. 

    //log the error which caused the failure 
    console.log(err);
  });

would something like that be satisfactory?

kumar529 commented 7 years ago

yes... this would be great if i can access results of previous steps.... also you should support major functionality of mongoose api... like - findById, findOneAndUpdate, findOneAndRemove, e.t.c with all the options provided in mongoose.... so 'fawn' will become a complete wrapper of mongoose

e-oj commented 7 years ago

Let me know if this resolves your issue. Fix

kumar529 commented 7 years ago

seems like it will solve my problem... thank you very much for this amazing solution.