Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mongodb replica set support #43

Merged
merged 10 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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