Skip to content

Commit 34ce620

Browse files
patrickleetdebianw
andauthored
feat: mongodb replica set support (#43)
BREAKING CHANGE: Now defaults to new mongo db client options `useNewUrlParser` and `useUnifiedTopology`. Travis changed to test last two Node versions, 12 and 14. * feat: mongodb replica set support * chore: add 10 to tests * fix: lock to sourced 2.0.5 * fix: sourced bug - upgrade to fixed 2.0.7 * fix: sourced@2.0.8 * chore: update travis file * chore: rm debug log statements * chore: rm debug log statements * fix: make options configurable * fix: cherry pick changes from @debianw - PR #35 Co-authored-by: Waly <debianw@gmail.com>
1 parent 84e0283 commit 34ce620

File tree

5 files changed

+2655
-2502
lines changed

5 files changed

+2655
-2502
lines changed

.travis.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
language: node_js
22
node_js:
3-
- 8
4-
- 10
5-
- 11
6-
3+
- '12'
4+
- '14'
5+
cache: npm
76
notifications:
87
email: false
9-
10-
before_install:
11-
- npm install -g codecov
128
script:
139
- make test
10+
after_success:
11+
- npm install -g codecov
1412
- codecov
15-
1613
branches:
1714
except:
1815
- '/^v\d+\.\d+\.\d+$/'
19-
2016
jobs:
2117
include:
2218
- stage: deploy
2319
if: branch == master && !fork
2420
node_js: node # pre-installed version
2521
script:
2622
- npm install -g semantic-release
27-
- semantic-release
23+
- semantic-release

index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ function Repository (entityType, options) {
2929
};
3030

3131
self.indices.forEach(function (index) {
32-
snapshots.ensureIndex(index, error);
33-
events.ensureIndex(index, error);
32+
snapshots.createIndex(index, error);
33+
events.createIndex(index, error);
3434
});
35-
events.ensureIndex({ id: 1, version: 1 }, error);
36-
snapshots.ensureIndex({ id: 1, version: 1 }, error);
37-
snapshots.ensureIndex('snapshotVersion', error);
35+
events.createIndex({ id: 1, version: 1 }, error);
36+
snapshots.createIndex({ id: 1, version: 1 }, error);
37+
snapshots.createIndex('snapshotVersion', error);
3838

3939
log('initialized %s entity store', self.entityType.name);
4040

@@ -165,7 +165,7 @@ Repository.prototype._commitEvents = function _commitEvents (entity, cb) {
165165
event[index] = entity[index];
166166
});
167167
});
168-
self.events.insert(events, function (err) {
168+
self.events.insertMany(events, function (err) {
169169
if (err) return cb(err);
170170
log('committed %s.events for id %s', self.entityType.name, entity.id);
171171
entity.newEvents = [];
@@ -195,7 +195,7 @@ Repository.prototype._commitAllEvents = function _commitEvents (entities, cb) {
195195

196196
if (events.length === 0) return cb();
197197

198-
self.events.insert(events, function (err) {
198+
self.events.insertMany(events, function (err) {
199199
if (err) return cb(err);
200200
log('committed %s.events for ids %j', self.entityType.name, _.map(entities, 'id'));
201201
entities.forEach(function (entity) {
@@ -212,7 +212,7 @@ Repository.prototype._commitSnapshots = function _commitSnapshots (entity, optio
212212
if (options.forceSnapshot || entity.version >= entity.snapshotVersion + self.snapshotFrequency) {
213213
var snapshot = entity.snapshot();
214214
if (snapshot && snapshot._id) delete snapshot._id; // mongo will blow up if we try to insert multiple _id keys
215-
self.snapshots.insert(snapshot, function (err) {
215+
self.snapshots.insertOne(snapshot, function (err) {
216216
if (err) return cb(err);
217217
log('committed %s.snapshot for id %s %j', self.entityType.name, entity.id, snapshot);
218218
return cb(null, entity);
@@ -239,7 +239,7 @@ Repository.prototype._commitAllSnapshots = function _commitAllSnapshots (entitie
239239

240240
if (snapshots.length === 0) return cb();
241241

242-
self.snapshots.insert(snapshots, function (err) {
242+
self.snapshots.insertMany(snapshots, function (err) {
243243
if (err) return cb(err);
244244
log('committed %s.snapshot for ids %s %j', self.entityType.name, _.map(entities, 'id'), snapshots);
245245
return cb(null, entities);

mongo.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function Mongo () {
1313

1414
util.inherits(Mongo, EventEmitter);
1515

16-
Mongo.prototype.connect = function connect (mongoUrl) {
16+
Mongo.prototype.connect = function connect (mongoUrl, database, options = {
17+
useNewUrlParser: true,
18+
useUnifiedTopology: true
19+
}) {
1720
var self = this;
1821
return new Promise((resolve, reject) => {
1922
self.on('connected', (db) => {
@@ -22,13 +25,14 @@ Mongo.prototype.connect = function connect (mongoUrl) {
2225
self.on('error', (err) => {
2326
reject(err)
2427
})
25-
MongoClient.connect(mongoUrl, function (err, client) {
28+
MongoClient.connect(mongoUrl, options, function (err, client) {
2629
if (err) {
2730
log('✗ MongoDB Connection Error. Please make sure MongoDB is running: ', err);
2831
self.emit('error', err);
2932
}
3033
var expanded = url.parse(mongoUrl);
31-
var dbName = expanded.pathname.replace('/', '');
34+
// replica set url does not include db, it is passed in separately
35+
var dbName = database || expanded.pathname.replace('/', '');
3236
self.client = client;
3337
var db = client.db(dbName);
3438
self.db = db;

0 commit comments

Comments
 (0)