Browse files

Enabled writes in batch mode.

  • Loading branch information...
1 parent 5698546 commit 65dd0e28c8cacabc36674647df5fe861cc8375c8 @jonpacker jonpacker committed Nov 8, 2012
Showing with 23 additions and 9 deletions.
  1. +3 −2 lib/node.js
  2. +8 −6 lib/seraph.js
  3. +12 −1 test/seraph.js
View
5 lib/node.js
@@ -33,14 +33,15 @@ module.exports = {
* Create a new object. Maps to /node.
*/
_create: function(obj, callback) {
+ var self = this;
var op = this.operation('node', obj);
- this.call(op, function(err, body, response) {
+ this.call(op, function(err, body, location) {
if (err) {
return callback(err);
}
var result = _.clone(obj);
- result[this.options.id] = this._extractId(response.headers.location);
+ result[self.options.id] = self._extractId(location);
callback(null, result);
});
View
14 lib/seraph.js
@@ -158,7 +158,7 @@ Seraph.prototype.call = function(operation, callback) {
}
}
- callback(null, body, response);
+ callback(null, body, response.headers.location);
}
});
};
@@ -364,12 +364,12 @@ function BatchSeraph(options) {
self.results.push(result);
});
return function() {
- self.operationQueue = [];
+ self.operationQueueStack.unshift([]);
fn.apply(this, [].slice.apply(arguments));
- var operationIds = _.pluck(self.operationQueue, 'id');
- delete self.operationQueue;
+ var operationIds = _.pluck(self.operationQueueStack[0], 'id');
+ self.operationQueueStack.shift();
switch (operationIds.length) {
case 0: return undefined;
@@ -384,14 +384,16 @@ function BatchSeraph(options) {
this.processors = [];
this.operations = [];
this.results = [];
+ this.operationQueueStack = [];
+ this.super.throttleMap = async.map;
this.super.call = function(operation, processor) {
operation.id = self.operations.length;
if (!operation.body) delete operation.body;
self.operations.push(operation);
self.processors.push(processor);
- self.operationQueue.push(operation);
+ self.operationQueueStack[0].push(operation);
}
}
@@ -406,7 +408,7 @@ Seraph.prototype.batch = function(operations, callback) {
}
results.forEach(function(result) {
- batchSeraph.processors[result.id](null, result.body || {});
+ batchSeraph.processors[result.id](null, result.body || {}, result.location);
});
callback(null, batchSeraph.results);
View
13 test/seraph.js
@@ -1302,9 +1302,20 @@ describe('seraph#batch', function() {
assert.deepEqual(results, users);
callback();
})
-
}
async.waterfall([createObjs, readBatch], done);
});
+
+ it('should perform a series of saves in a batch', function(done) {
+ db.batch(function(db) {
+ db.save({ newPerson: 'bob' });
+ db.save([ {newPerson: 'orange'}, {newPerson: 'cat'} ]);
+ }, function(err, result) {
+ assert.equal(result[0].newPerson, 'bob');
+ assert.equal(result[1][0].newPerson, 'orange');
+ assert.equal(result[1][1].newPerson, 'cat');
+ done();
+ });
+ });
});

0 comments on commit 65dd0e2

Please sign in to comment.