node.js, тестирование mongodb для сохранения и загрузки - PullRequest
1 голос
/ 23 января 2012

Возможно, у меня просто проблемы с определением обратного вызова, но я не могу найти способ проверить сохранение и загрузку в node.js.

Мой тест такой:

vows.describe('Saving').addBatch({
    'Single item can be saved':{
        topic:function () {
            myStore.saveItems(1, [{id:3,name:'squat'}]);
            myStore.getItems(1, this.callback);
        },
        'saved item is returned by getItems':function (err, items) {
            assert.equal(items.length, 1);
            assert.equal(items[0].deviceId, 1);
            assert.equal(items[0].id, 3);
        }
    }
}).export(module);

При этом тестируется:

exports.saveItems = function (deviceId, items) {
    var itemsCollection = db.collection('items');

    itemsCollection.find({deviceId:deviceId}).toArray(function (err, existingItems) {
        _.each(items, function (item) {
            item['deviceId'] = deviceId;
            var existingItem = _.find(existingItems, function (existingItem) {
                return existingItem.id === item.id
            });
            if (typeof(existingItem) === 'undefined') {
                itemsCollection.save(item);//callback here?
            } else {
            }
        });
    });
};

exports.getItems = function (deviceId, callback) {
    var itemsCollection = db.collection('items');
    itemsCollection.find({deviceId:deviceId}).toArray(callback);
};

Есть ли способ передать обратный вызов на saveItems таким образом, чтобы getItems не вызывался до все Монго сохраняет?

Ответы [ 2 ]

2 голосов
/ 23 января 2012

Попробуйте это:)

vows.describe('Saving').addBatch({
    'Single item can be saved':{
        topic:function () {
          var topicThis = this;
          // NEW: having a callback here
          myStore.saveItems(1, [{id:3,name:'squat'}], function(err, results){
              myStore.getItems(1, topicThis.callback);    
          });
        },
        'saved item is returned by getItems':function (err, items) {
            assert.equal(items.length, 1);
            assert.equal(items[0].deviceId, 1);
            assert.equal(items[0].id, 3);
        }
    }
}).export(module);

// https://github.com/caolan/async
var async = require('async');
// NEW: having a callback here
exports.saveItems = function (deviceId, items, cb) {
    var itemsCollection = db.collection('items');

    itemsCollection.find({deviceId:deviceId}).toArray(function (err, existingItems) {
        var tasks = [];
        // here you are iterating over each item, doing some work, and then conditionally doing an async op
        _.each(items, function (item) {
            item['deviceId'] = deviceId;
            var existingItem = _.find(existingItems, function (existingItem) {
                return existingItem.id === item.id
            });
            if (typeof(existingItem) === 'undefined') {
                // so this is async, b/c it's talking to mongo
                // NEW: add it to our list of tasks, when are later run in parallel
                tasks.push(function(nextTask){
                    itemsCollection.save(item, nextTask);
                });
            }
        });
        // NEW: run it all in parrallel, when done, call back
        async.parallel(tasks, function(err, results) {
            cb(err, results);
        })
    });
};
1 голос
/ 25 мая 2012

Используйте драйвер db node.js: http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html

Функции вставки и обновления определены с помощью обратных вызовов. Я использую функцию обновления только потому, что она работает как вставка (если первый аргумент ниже не извлекает какую-либо запись).

itemsCollection.update({_id: itemId}, item, {upsert:true, safe:true}, function (err, result) {
  // callback here
});
...