eventric-store-mongodb
Version:
eventric mongodb store adapter
178 lines (161 loc) • 5.08 kB
JavaScript
var MongoClient, MongoDBStore;
MongoClient = require('mongodb').MongoClient;
MongoDBStore = (function() {
function MongoDBStore() {}
MongoDBStore.prototype._optionDefaults = {
schema: 'mongodb://',
host: '127.0.0.1',
port: 27017,
database: 'eventric',
dbInstance: null
};
MongoDBStore.prototype.initialize = function(_context, options) {
this._context = _context;
if (options == null) {
options = {};
}
this.db = null;
return this._initializePromise = new Promise((function(_this) {
return function(resolve) {
var connectUri;
_this._defaults(options, _this._optionDefaults);
_this._domainEventsCollectionName = _this._context.name + ".DomainEvents";
if (options.dbInstance) {
_this.db = options.dbInstance;
return resolve();
}
connectUri = "" + options.schema + options.host + ":" + options.port + "/" + options.database;
return MongoClient.connect(connectUri).then(function(db) {
_this.db = db;
return resolve();
});
};
})(this));
};
MongoDBStore.prototype._defaults = function(options, optionDefaults) {
var allKeys, i, key, len, results1;
allKeys = [].concat(Object.keys(options), Object.keys(optionDefaults));
results1 = [];
for (i = 0, len = allKeys.length; i < len; i++) {
key = allKeys[i];
if (!options[key] && optionDefaults[key]) {
results1.push(options[key] = optionDefaults[key]);
}
}
return results1;
};
MongoDBStore.prototype.saveDomainEvent = function(domainEvent) {
return this._getNextDomainEventId().then((function(_this) {
return function(domainEventId) {
domainEvent.id = domainEventId;
return _this._getCollection(_this._domainEventsCollectionName);
};
})(this)).then(function(collection) {
return collection.insert(domainEvent);
}).then(function(insertWriteOpResultObject) {
domainEvent = insertWriteOpResultObject.ops[0];
return domainEvent;
});
};
MongoDBStore.prototype._getNextDomainEventId = function() {
var document, options, query;
query = {
_id: 'domainEventSequence'
};
document = {
$inc: {
currentDomainEventId: 1
}
};
options = {
upsert: true,
"new": true
};
return this._getCollection('eventSourcingConfig').then(function(collection) {
return collection.findAndModify(query, null, document, options);
}).then(function(findAndModifyWriteOpResultObject) {
return findAndModifyWriteOpResultObject.value.currentDomainEventId;
})["catch"]((function(_this) {
return function(error) {
var duplicateKeyError;
duplicateKeyError = 11000;
if (error.code === duplicateKeyError) {
return _this._getNextDomainEventId();
}
throw error;
};
})(this));
};
MongoDBStore.prototype.findDomainEventsByName = function(domainEventNames, callback) {
var query;
if (!(domainEventNames instanceof Array)) {
domainEventNames = [domainEventNames];
}
query = {
'name': {
$in: domainEventNames
}
};
return this._find(query, callback);
};
MongoDBStore.prototype.findDomainEventsByAggregateId = function(aggregateIds, callback) {
var query;
if (!(aggregateIds instanceof Array)) {
aggregateIds = [aggregateIds];
}
query = {
'aggregate.id': {
$in: aggregateIds
}
};
return this._find(query, callback);
};
MongoDBStore.prototype.findDomainEventsByNameAndAggregateId = function(domainEventNames, aggregateIds, callback) {
var query;
if (!(domainEventNames instanceof Array)) {
domainEventNames = [domainEventNames];
}
if (!(aggregateIds instanceof Array)) {
aggregateIds = [aggregateIds];
}
query = {
'name': {
$in: domainEventNames
},
'aggregate.id': {
$in: aggregateIds
}
};
return this._find(query, callback);
};
MongoDBStore.prototype._find = function(query, callback) {
return this._getCollection(this._domainEventsCollectionName).then(function(collection) {
return collection.find(query).toArray();
}).then(function(results) {
return callback(null, results);
});
};
MongoDBStore.prototype._getCollection = function(collectionName) {
return this._initializePromise.then((function(_this) {
return function() {
return new Promise(function(resolve, reject) {
return _this.db.collection(collectionName, function(error, collection) {
if (error) {
return reject(error);
}
return resolve(collection);
});
});
};
})(this));
};
MongoDBStore.prototype.destroy = function() {
return this._initializePromise.then((function(_this) {
return function() {
return _this.db.close();
};
})(this));
};
return MongoDBStore;
})();
module.exports = MongoDBStore;