Permalink
Browse files

Make sure to only ensure indexes while connected

  • Loading branch information...
1 parent ec1fe40 commit 9894923639cacd77228f6350d497f7a11fdf64d2 @rauchg rauchg committed Mar 24, 2011
Showing with 19 additions and 18 deletions.
  1. +19 −18 lib/mongoose/drivers/node-mongodb-native/collection.js
@@ -92,24 +92,25 @@ var oldEnsureIndex = MongoCollection.prototype.ensureIndex;
function noop () {};
MongoCollection.prototype.ensureIndex = function(fields, options, fn){
- // if the spec is an array, use -native (old way)
- if (fields.constructor != Object)
- return oldEnsureIndex.apply(this, arguments);
-
- fn = fn || noop;
-
- // transform fields dict into lame array
- var fieldsArr = [];
- for (var i in fields)
- fieldsArr.push([i, fields[i]]);
-
- if (options && Object.keys(options).length){
- if (options.unique)
- return oldEnsureIndex.call(this, fieldsArr, true, fn);
- else if (fn != noop)
- fn(new Error('This driver only implements unique indexes'));
- } else {
- oldEnsureIndex.call(this, fieldsArr, fn);
+ if (!this.buffer) {
+ // if the spec is an array, use -native (old way)
+ if (fields.constructor != Object)
+ return oldEnsureIndex.apply(this, arguments);
+
+ fn = fn || noop;
+
+ // transform fields dict into lame array
+ var fieldsArr = [];
+ for (var i in fields)
+ fieldsArr.push([i, fields[i]]);
+
+ if (options && Object.keys(options).length){
+ if (options.unique)
+ return oldEnsureIndex.call(this, fieldsArr, true, fn);
+ else if (fn != noop)
+ fn(new Error('This driver only implements unique indexes'));
+ } else
+ oldEnsureIndex.call(this, fieldsArr, fn);
}
};

0 comments on commit 9894923

Please sign in to comment.