Skip to content

Commit 162b81c

Browse files
authored
Merge pull request #157 from share/no-src-seq-index
🔧 Allow disabling the `src`/`seq`/`v` index
2 parents 7e88b0a + 763baa6 commit 162b81c

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ ShareDbMongo.prototype.getOpCollection = function(collectionName, callback) {
384384
var collection = mongo.collection(name);
385385
// Given the potential problems with creating indexes on the fly, it might
386386
// be preferrable to disable automatic creation
387-
if (self.disableIndexCreation) {
387+
if (self.disableIndexCreation === true) {
388388
return callback(null, collection);
389389
}
390390
if (self.opIndexes[collectionName]) {
@@ -398,10 +398,13 @@ ShareDbMongo.prototype.getOpCollection = function(collectionName, callback) {
398398
// collection this won't be a problem, but this is a dangerous mechanism.
399399
// Perhaps we should only warn instead of creating the indexes, especially
400400
// when there is a lot of data in the collection.
401-
collection.createIndex({d: 1, v: 1}, {background: true})
402-
.then(function() {
403-
return collection.createIndex({src: 1, seq: 1, v: 1}, {background: true});
404-
})
401+
402+
var disabledIndexes = self.disableIndexCreation || {};
403+
var promises = [
404+
collection.createIndex({d: 1, v: 1}, {background: true}),
405+
!disabledIndexes.src_seq_v && collection.createIndex({src: 1, seq: 1, v: 1}, {background: true})
406+
];
407+
Promise.all(promises)
405408
.then(function() {
406409
self.opIndexes[collectionName] = true;
407410
callback(null, collection);

test/test_mongo.js

+45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var expect = require('chai').expect;
22
var ShareDbMongo = require('..');
33
var getQuery = require('sharedb-mingo-memory/get-query');
44
var async = require('async');
5+
var randomUUID = require('crypto').randomUUID;
56

67
var mongoUrl = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test';
78

@@ -432,6 +433,50 @@ describe('mongo db', function() {
432433
});
433434
});
434435

436+
describe('options', function() {
437+
var db;
438+
439+
afterEach(function(done) {
440+
db.close(done);
441+
});
442+
443+
it('disables index creation', function(done) {
444+
var collection = randomUUID();
445+
db = new ShareDbMongo(mongoUrl, {disableIndexCreation: true});
446+
async.waterfall([
447+
db.commit.bind(db, collection, 'foo', {v: 0, create: {}}, {}, null),
448+
function(succeeded, next) {
449+
db.getDbs(next);
450+
},
451+
function(mongo, mongoPoll, next) {
452+
mongo.collection('o_' + collection).indexInformation().then(function(indexes) {
453+
expect(indexes['d_1_v_1']).not.to.be.ok;
454+
expect(indexes['src_1_seq_1_v_1']).not.to.be.ok;
455+
next();
456+
});
457+
}
458+
], done);
459+
});
460+
461+
it('disables just the src,seq,v index', function(done) {
462+
var collection = randomUUID();
463+
db = new ShareDbMongo(mongoUrl, {disableIndexCreation: {src_seq_v: true}});
464+
async.waterfall([
465+
db.commit.bind(db, collection, 'foo', {v: 0, create: {}}, {}, null),
466+
function(succeeded, next) {
467+
db.getDbs(next);
468+
},
469+
function(mongo, mongoPoll, next) {
470+
mongo.collection('o_' + collection).indexInformation().then(function(indexes) {
471+
expect(indexes['d_1_v_1']).to.be.ok;
472+
expect(indexes['src_1_seq_1_v_1']).not.to.be.ok;
473+
next();
474+
});
475+
}
476+
], done);
477+
});
478+
});
479+
435480
describe('mongo db connection', function() {
436481
describe('via url string', function() {
437482
beforeEach(function(done) {

0 commit comments

Comments
 (0)