Skip to content

Commit

Permalink
feat: mongodb replica set support (#43)
Browse files Browse the repository at this point in the history
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: [email protected]

* 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 <[email protected]>
  • Loading branch information
patrickleet and debianw authored May 26, 2020
1 parent 84e0283 commit 34ce620
Show file tree
Hide file tree
Showing 5 changed files with 2,655 additions and 2,502 deletions.
16 changes: 6 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
language: node_js
node_js:
- 8
- 10
- 11

- '12'
- '14'
cache: npm
notifications:
email: false

before_install:
- npm install -g codecov
script:
- make test
after_success:
- npm install -g codecov
- codecov

branches:
except:
- '/^v\d+\.\d+\.\d+$/'

jobs:
include:
- stage: deploy
if: branch == master && !fork
node_js: node # pre-installed version
script:
- npm install -g semantic-release
- semantic-release
- semantic-release
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ function Repository (entityType, options) {
};

self.indices.forEach(function (index) {
snapshots.ensureIndex(index, error);
events.ensureIndex(index, error);
snapshots.createIndex(index, error);
events.createIndex(index, error);
});
events.ensureIndex({ id: 1, version: 1 }, error);
snapshots.ensureIndex({ id: 1, version: 1 }, error);
snapshots.ensureIndex('snapshotVersion', error);
events.createIndex({ id: 1, version: 1 }, error);
snapshots.createIndex({ id: 1, version: 1 }, error);
snapshots.createIndex('snapshotVersion', error);

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

Expand Down Expand Up @@ -165,7 +165,7 @@ Repository.prototype._commitEvents = function _commitEvents (entity, cb) {
event[index] = entity[index];
});
});
self.events.insert(events, function (err) {
self.events.insertMany(events, function (err) {
if (err) return cb(err);
log('committed %s.events for id %s', self.entityType.name, entity.id);
entity.newEvents = [];
Expand Down Expand Up @@ -195,7 +195,7 @@ Repository.prototype._commitAllEvents = function _commitEvents (entities, cb) {

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

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

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

self.snapshots.insert(snapshots, function (err) {
self.snapshots.insertMany(snapshots, function (err) {
if (err) return cb(err);
log('committed %s.snapshot for ids %s %j', self.entityType.name, _.map(entities, 'id'), snapshots);
return cb(null, entities);
Expand Down
10 changes: 7 additions & 3 deletions mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ function Mongo () {

util.inherits(Mongo, EventEmitter);

Mongo.prototype.connect = function connect (mongoUrl) {
Mongo.prototype.connect = function connect (mongoUrl, database, options = {
useNewUrlParser: true,
useUnifiedTopology: true
}) {
var self = this;
return new Promise((resolve, reject) => {
self.on('connected', (db) => {
Expand All @@ -22,13 +25,14 @@ Mongo.prototype.connect = function connect (mongoUrl) {
self.on('error', (err) => {
reject(err)
})
MongoClient.connect(mongoUrl, function (err, client) {
MongoClient.connect(mongoUrl, options, function (err, client) {
if (err) {
log('✗ MongoDB Connection Error. Please make sure MongoDB is running: ', err);
self.emit('error', err);
}
var expanded = url.parse(mongoUrl);
var dbName = expanded.pathname.replace('/', '');
// replica set url does not include db, it is passed in separately
var dbName = database || expanded.pathname.replace('/', '');
self.client = client;
var db = client.db(dbName);
self.db = db;
Expand Down
Loading

0 comments on commit 34ce620

Please sign in to comment.