Skip to content

Commit

Permalink
Merge pull request #157 from share/no-src-seq-index
Browse files Browse the repository at this point in the history
🔧 Allow disabling the `src`/`seq`/`v` index
  • Loading branch information
alecgibson authored May 28, 2024
2 parents 7e88b0a + 763baa6 commit 162b81c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions test/test_mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 162b81c

Please sign in to comment.