diff --git a/index.js b/index.js index c6bd47a..5a331d3 100644 --- a/index.js +++ b/index.js @@ -384,7 +384,7 @@ ShareDbMongo.prototype.getOpCollection = function(collectionName, callback) { var collection = mongo.collection(name); // Given the potential problems with creating indexes on the fly, it might // be preferrable to disable automatic creation - if (self.disableIndexCreation) { + if (self.disableIndexCreation === true) { return callback(null, collection); } if (self.opIndexes[collectionName]) { @@ -398,10 +398,13 @@ ShareDbMongo.prototype.getOpCollection = function(collectionName, callback) { // collection this won't be a problem, but this is a dangerous mechanism. // Perhaps we should only warn instead of creating the indexes, especially // when there is a lot of data in the collection. - collection.createIndex({d: 1, v: 1}, {background: true}) - .then(function() { - return collection.createIndex({src: 1, seq: 1, v: 1}, {background: true}); - }) + + var disabledIndexes = self.disableIndexCreation || {}; + var promises = [ + collection.createIndex({d: 1, v: 1}, {background: true}), + !disabledIndexes.src_seq_v && collection.createIndex({src: 1, seq: 1, v: 1}, {background: true}) + ]; + Promise.all(promises) .then(function() { self.opIndexes[collectionName] = true; callback(null, collection); diff --git a/test/test_mongo.js b/test/test_mongo.js index caff536..1a3f94c 100644 --- a/test/test_mongo.js +++ b/test/test_mongo.js @@ -2,6 +2,7 @@ var expect = require('chai').expect; var ShareDbMongo = require('..'); var getQuery = require('sharedb-mingo-memory/get-query'); var async = require('async'); +var randomUUID = require('crypto').randomUUID; var mongoUrl = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'; @@ -432,6 +433,50 @@ describe('mongo db', function() { }); }); +describe('options', function() { + var db; + + afterEach(function(done) { + db.close(done); + }); + + it('disables index creation', function(done) { + var collection = randomUUID(); + db = new ShareDbMongo(mongoUrl, {disableIndexCreation: true}); + async.waterfall([ + db.commit.bind(db, collection, 'foo', {v: 0, create: {}}, {}, null), + function(succeeded, next) { + db.getDbs(next); + }, + function(mongo, mongoPoll, next) { + mongo.collection('o_' + collection).indexInformation().then(function(indexes) { + expect(indexes['d_1_v_1']).not.to.be.ok; + expect(indexes['src_1_seq_1_v_1']).not.to.be.ok; + next(); + }); + } + ], done); + }); + + it('disables just the src,seq,v index', function(done) { + var collection = randomUUID(); + db = new ShareDbMongo(mongoUrl, {disableIndexCreation: {src_seq_v: true}}); + async.waterfall([ + db.commit.bind(db, collection, 'foo', {v: 0, create: {}}, {}, null), + function(succeeded, next) { + db.getDbs(next); + }, + function(mongo, mongoPoll, next) { + mongo.collection('o_' + collection).indexInformation().then(function(indexes) { + expect(indexes['d_1_v_1']).to.be.ok; + expect(indexes['src_1_seq_1_v_1']).not.to.be.ok; + next(); + }); + } + ], done); + }); +}); + describe('mongo db connection', function() { describe('via url string', function() { beforeEach(function(done) {