How to use “q” module for refactoring mongoose code?

后端 未结 5 479
野趣味
野趣味 2020-12-31 04:30

I\'m using mongoose to insert some data into mongodb. The code looks like:

var mongoose = require(\'mongoose\');
mongoose.connect(\'mongo://localhost/test\')         


        
相关标签:
5条回答
  • 2020-12-31 04:40

    You'll want to use Q.nfcall, documented in the README and the Wiki. All Mongoose methods are Node-style. I'll also use .spread instead of manually destructuring .then.

    var mongoose = require('mongoose');
    mongoose.connect('mongo://localhost/test');
    var conn = mongoose.connection;
    
    var users = conn.collection('users');
    var channels = conn.collection('channels');
    var articles = conn.collection('articles');
    
    function getInsertedArticles() {
        return Q.nfcall(users.insert.bind(users), [{/*user1*/},{/*user2*/}]).spread(function (user1, user2) {
            return Q.nfcall(channels.insert.bind(channels), [{userId:user1._id},{userId:user2._id}]).spread(function (channel1, channel2) {
                return Q.nfcall(articles.insert.bind(articles), [{userId:user1._id,channelId:channel1._id},{}]);
            });
        })
    }
    
    getInsertedArticles()
        .spread(function (article1, article2) {
            // you only get here if all three of the above steps succeeded
        })
        .fail(function (error) {
            // you get here if any of the above three steps failed
        }
    );
    

    In practice, you will rarely want to use .spread, since you usually are inserting an array that you don't know the size of. In that case the code can look more like this (here I also illustrate Q.nbind).


    To compare with the original one is not quite fair, because your original has no error handling. A corrected Node-style version of the original would be like so:

    var mongoose = require('mongoose');
    mongoose.connect('mongo://localhost/test');
    var conn = mongoose.connection;
    
    function getInsertedArticles(cb) {
        // insert users
        conn.collection('users').insert([{/*user1*/},{/*user2*/}], function(err, docs) {
            if (err) {
                cb(err);
                return;
            }
    
            var user1 = docs[0], user2 = docs[1];
    
            // insert channels
            conn.collection('channels').insert([{userId:user1._id},{userId:user2._id}], function(err, docs) {
                if (err) {
                    cb(err);
                    return;
                }
    
                var channel1 = docs[0], channel2 = docs[1];
    
                // insert articles
                conn.collection('articles').insert([{userId:user1._id,channelId:channel1._id},{}], function(err, docs) {
                    if (err) {
                        cb(err);
                        return;
                    }
    
                    var article1 = docs[0], article2 = docs[1];
    
                    cb(null, [article1, article2]);
                }
            });
        };
    }
    
    getInsertedArticles(function (err, articles) {
        if (err) {
            // you get here if any of the three steps failed.
            // `articles` is `undefined`.
        } else {
            // you get here if all three succeeded.
            // `err` is null.
        }
    });
    
    0 讨论(0)
  • 2020-12-31 04:44

    Considering Model.save instead of Collection.insert (quite the same in our case).

    You don't need to use Q, you can wrap yourself the save method and return directly a Mongoose Promise.

    First create an utility method to wrap the save function, that's not very clean but something like:

      //Utility function (put it in a better place)
      var saveInPromise = function (model) {
    
        var promise = new mongoose.Promise();
    
        model.save(function (err, result) {
          promise.resolve(err, result);
        });
    
        return promise;
      }
    

    Then you can use it instead of save to chain your promises

      var User = mongoose.model('User');
      var Channel = mongoose.model('Channel');
      var Article = mongoose.model('Article');
    
      //Step 1
      var user = new User({data: 'value'});
      saveInPromise(user).then(function () {
    
        //Step 2
        var channel = new Channel({user: user.id})
        return saveInPromise(channel);
    
      }).then(function (channel) {
    
        //Step 3
        var article = new Article({channel: channel.id})
        return saveInPromise(article);
    
      }, function (err) {
        //A single place to handle your errors
    
      });
    

    I guess that's the kind of simplicity we are looking for.. right? Of course the utility function can be implemented with better integration with Mongoose.

    Let me know what you think about that.


    By the way there is an issue about that exact problem in the Mongoose Github:

    • Add 'promise' return value to model save operation

    I hope it's gonna be solved soon. I think it takes some times because they are thinking of switching from mpromise to Q: See here and then here.

    0 讨论(0)
  • 2020-12-31 04:51

    Two years later, this question just popped up in my RSS client ...

    Things have moved on somewhat since May 2012 and we might choose to solve this one in a different way now. More specifically, the Javascript community has become "reduce-aware" since the decision to include Array.prototype.reduce (and other Array methods) in ECMAScript5. Array.prototype.reduce was always (and still is) available as a polyfill but was little appreciated by many of us at that time. Those who were running ahead of the curve may demur on this point, of course.

    The problem posed in the question appears to be formulaic, with rules as follows :

    • The objects in the array passed as the first param to conn.collection(table).insert() build as follows (where N corresponds to the object's index in an array):
      • [ {}, ... ]
      • [ {userId:userN._id}, ... ]
      • [ {userId:userN._id, channelId:channelN._id}, ... ]
    • table names (in order) are : users, channels, articles.
    • the corresopnding object properties are : user, channel, article (ie the table names without the pluralizing 's').

    A general pattern from this article by Taoofcode) for making asynchronous call in series is :

    function workMyCollection(arr) {  
        return arr.reduce(function(promise, item) {
            return promise.then(function(result) {
                return doSomethingAsyncWithResult(item, result);
            });        
        }, q());
    }
    

    With quite light adaptation, this pattern can be made to orchestrate the required sequencing :

    function cascadeInsert(tables, n) {
        /* 
        /* tables: array of unpluralisd table names
        /* n: number of users to insert.
        /* returns promise of completion|error
         */
        var ids = []; // this outer array is available to the inner functions (to be read and written to).
        for(var i=0; i<n; i++) { ids.push({}); } //initialize the ids array with n plain objects.
        return tables.reduce(function (promise, t) {
            return promise.then(function (docs) {
                for(var i=0; i<ids.length; i++) {
                    if(!docs[i]) throw (new Error(t + ": returned documents list does not match the request"));//or simply `continue;` to be error tolerant (if acceptable server-side).
                    ids[i][t+'Id'] = docs[i]._id; //progressively add properties to the `ids` objects
                }
                return insert(ids, t + 's');
            });
        }, Q());
    }
    

    Lastly, here's the promise-returning worker function, insert() :

    function insert(ids, t) {
        /* 
        /* ids: array of plain objects with properties as defined by the rules
        /* t: table name.
        /* returns promise of docs
         */
        var dfrd = Q.defer();
        conn.collection(t).insert(ids, function(err, docs) {
            (err) ? dfrd.reject(err) : dfrd.resolve(docs);
        });
        return dfrd.promise;
    }
    

    Thus, you can specify as parameters passed to cascadeInsert, the actual table/property names and the number of users to insert.

    cascadeInsert( ['user', 'channel', 'article'], 2 ).then(function () {
       // you get here if everything was successful
    }).catch(function (err) {
       // you get here if anything failed
    });
    

    This works nicely because the tables in the question all have regular plurals (user => users, channel => channels). If any of them was irregular (eg stimulus => stimuli, child => children), then we would need to rethink - (and probably implement a lookup hash). In any case, the adaptation would be fairly trivial.

    0 讨论(0)
  • 2020-12-31 04:57

    With alternative deferred promise implementation, you may do it as following:

    var mongoose = require('mongoose');
    mongoose.connect('mongo://localhost/test');
    var conn = mongoose.connection;
    
    // Setup 'pinsert', promise version of 'insert' method
    var promisify = require('deferred').promisify
    mongoose.Collection.prototype.pinsert = promisify(mongoose.Collection.prototype.insert);
    
    var user1, user2;
    // insert users
    conn.collection('users').pinsert([{/*user1*/},{/*user2*/}])
    // insert channels
    .then(function (users) {
      user1 = users[0]; user2 = users[1];
      return conn.collection('channels').pinsert([{userId:user1._id},{userId:user2._id}]);
    })
    // insert articles
    .match(function (channel1, channel2) {
      return conn.collection('articles').pinsert([{userId:user1._id,channelId:channel1._id},{}]);
    })
    .done(function (articles) {
      // Do something with articles
    }, function (err) {
       // Handle any error that might have occurred on the way
    });    
    
    0 讨论(0)
  • 2020-12-31 05:01

    Today we have mongoose-q as well. A plugin to mongoose that gives you stuff like execQ and saveQ which return Q promises.

    0 讨论(0)
提交回复
热议问题