Permalink
Browse files

added; bufferCommands option

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...
1 parent 424bdc3 commit 6e0b01f2119ded273ada3340b7360bea0a3c6da7 @aheckmann aheckmann committed Mar 1, 2013
Showing with 39 additions and 14 deletions.
  1. +16 −5 lib/collection.js
  2. +2 −2 lib/drivers/node-mongodb-native/collection.js
  3. +13 −2 lib/model.js
  4. +3 −1 lib/schema.js
  5. +5 −4 test/model.test.js
View
@@ -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;
+ }
};
/**
@@ -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);
}
View
@@ -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;
View
@@ -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:
*
@@ -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
View
@@ -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);
});
});
});

0 comments on commit 6e0b01f

Please sign in to comment.