diff --git a/lib/collection.js b/lib/collection.js index 1c2ab7d2437..1c38286a8ed 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -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(); @@ -67,7 +76,9 @@ Collection.prototype.onOpen = function () { */ Collection.prototype.onClose = function () { - this.buffer = true; + if (this.opts.bufferCommands) { + this.buffer = true; + } }; /** diff --git a/lib/drivers/node-mongodb-native/collection.js b/lib/drivers/node-mongodb-native/collection.js index 0399dfeb92d..da0f6e745c2 100644 --- a/lib/drivers/node-mongodb-native/collection.js +++ b/lib/drivers/node-mongodb-native/collection.js @@ -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); } @@ -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); } diff --git a/lib/model.js b/lib/model.js index 6c51f138a6f..783006928b0 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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 @@ -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; diff --git a/lib/schema.js b/lib/schema.js index cbd4f9d7a02..1631898c872 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -24,10 +24,12 @@ 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` @@ -35,7 +37,6 @@ var EventEmitter = require('events').EventEmitter * - [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: * @@ -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 diff --git a/test/model.test.js b/test/model.test.js index 9e3981cf3bb..059b3462934 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -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; @@ -3866,7 +3867,7 @@ describe('model', function(){ setTimeout(function () { assert.ok(worked); done(); - }, 500); + }, 100); }); }); });