Skip to content

Commit

Permalink
added; bufferCommands option
Browse files Browse the repository at this point in the history
when running with the drivers autoReconnect option
disabled, mongoose buffers commands when the connection
goes down, until you manually reconnect. to disable mongoose
buffering commands while waiting for the connection to open
or when the connection is down, set the bufferCommands schema
option to false.
  • Loading branch information
aheckmann committed Mar 1, 2013
1 parent 424bdc3 commit 6e0b01f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
21 changes: 16 additions & 5 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ var STATES = require('./connectionstate')
*/

function Collection (name, conn, opts) {
if (undefined === opts) opts = {};
if (undefined === opts.capped) opts.capped = {};

opts.bufferCommands = undefined === opts.bufferCommands
? true
: opts.bufferCommands;

if ('number' == typeof opts.capped) {
opts.capped = { size: opts.capped };
}

this.opts = opts;
this.name = name;
this.conn = conn;
this.buffer = true;
this.queue = [];

if ('number' == typeof opts) opts = { size: opts };
this.opts = opts || {};
this.buffer = this.opts.bufferCommands;

if (STATES.connected == this.conn.readyState) {
this.onOpen();
Expand Down Expand Up @@ -67,7 +76,9 @@ Collection.prototype.onOpen = function () {
*/

Collection.prototype.onClose = function () {
this.buffer = true;
if (this.opts.bufferCommands) {
this.buffer = true;
}
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NativeCollection.prototype.onOpen = function () {
// always get a new collection in case the user changed host:port
// of parent db instance when re-opening the connection.

if (!self.opts.size) {
if (!self.opts.capped.size) {
// non-capped
return self.conn.db.collection(self.name, callback);
}
Expand All @@ -66,7 +66,7 @@ NativeCollection.prototype.onOpen = function () {
}
} else {
// create
var opts = utils.clone(self.opts);
var opts = utils.clone(self.opts.capped);
opts.capped = true;
self.conn.db.createCollection(self.name, opts, callback);
}
Expand Down
15 changes: 13 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1972,9 +1972,15 @@ Model.compile = function compile (name, schema, collectionName, connection, base
model.prototype.__proto__ = Model.prototype;
model.prototype.db = connection;
model.prototype._setSchema(schema);

var collectionOptions = {
bufferCommands: schema.options.bufferCommands
, capped: schema.options.capped
};

model.prototype.collection = connection.collection(
collectionName
, schema.options.capped
, collectionOptions
);

// apply methods
Expand Down Expand Up @@ -2031,7 +2037,12 @@ Model.__subclass = function subclass (conn, schema, collection) {
|| utils.toCollectionName(model.modelName);
}

Model.prototype.collection = conn.collection(collection, s && s.options.capped);
var collectionOptions = {
bufferCommands: s ? s.options.bufferCommands : true
, capped: s && s.options.capped
};

Model.prototype.collection = conn.collection(collection, collectionOptions);
Model.collection = Model.prototype.collection;
Model.init();
return Model;
Expand Down
4 changes: 3 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ var EventEmitter = require('events').EventEmitter
* ####Options:
*
* - [autoIndex](/docs/guide.html#autoIndex): bool - defaults to true
* - [bufferCommands](/docs/guide.html#bufferCommands): bool - defaults to true
* - [capped](/docs/guide.html#capped): bool - defaults to false
* - [collection](/docs/guide.html#collection): string - no default
* - [id](/docs/guide.html#id): bool - defaults to true
* - [_id](/docs/guide.html#_id): bool - defaults to true
* - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
* - [read](/docs/guide.html#read): string
* - [safe](/docs/guide.html#safe): bool - defaults to true.
* - [shardKey](/docs/guide.html#shardKey): bool - defaults to `null`
* - [strict](/docs/guide.html#strict): bool - defaults to true
* - [toJSON](/docs/guide.html#toJSON) - object - no default
* - [toObject](/docs/guide.html#toObject) - object - no default
* - [versionKey](/docs/guide.html#versionKey): bool - defaults to "__v"
* - `minimize`: bool - controls [document#toObject](#document_Document-toObject) behavior when called manually - defaults to true
*
* ####Note:
*
Expand Down Expand Up @@ -150,6 +151,7 @@ Schema.prototype.defaultOptions = function (options) {

options = utils.options({
strict: true
, bufferCommands: true
, capped: false // { size, max, autoIndexId }
, versionKey: '__v'
, minimize: true
Expand Down
9 changes: 5 additions & 4 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3849,13 +3849,14 @@ describe('model', function(){
describe('auto_reconnect', function(){
describe('if disabled', function(){
describe('with mongo down', function(){
it('should pass an error', function(done){
it('and no command buffering should pass an error', function(done){
var db = start({ server: { auto_reconnect: false }});
var T = db.model('Thing', new Schema({ type: String }));
var schema = Schema({ type: String }, { bufferCommands: false });
var T = db.model('Thing', schema);
db.on('open', function () {
var t = new T({ type: "monster" });

var worked = false;

t.save(function (err) {
assert.equal(err.message, 'no open connections');
worked = true;
Expand All @@ -3866,7 +3867,7 @@ describe('model', function(){
setTimeout(function () {
assert.ok(worked);
done();
}, 500);
}, 100);
});
});
});
Expand Down

0 comments on commit 6e0b01f

Please sign in to comment.