UNPKG

eventric-store-lowdb

Version:
147 lines (128 loc) 4.86 kB
// Generated by CoffeeScript 1.7.1 (function() { var LowDBStore, fs, lowdb, __slice = [].slice; lowdb = require('lowdb'); fs = require('fs'); LowDBStore = (function() { function LowDBStore() {} LowDBStore.prototype._optionDefaults = { path: 'db.json' }; LowDBStore.prototype.initialize = function() { var callback, options, _arg, _contextName, _i; _contextName = arguments[0], _arg = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), callback = arguments[_i++]; this._contextName = _contextName; options = _arg[0]; if (callback == null) { callback = function() {}; } this._defaults((options != null ? options : options = {}), this._optionDefaults); this._domainEventsCollectionName = "" + this._contextName + ".DomainEvents"; this._projectionCollectionName = "" + this._contextName + ".Projection"; this.db = lowdb; this.db.path = options.path; if (fs.existsSync(options.path)) { this.db.load(options.path); } return callback(null); }; LowDBStore.prototype._defaults = function(options, optionDefaults) { var allKeys, key, _i, _len, _results; allKeys = [].concat(Object.keys(options), Object.keys(optionDefaults)); _results = []; for (_i = 0, _len = allKeys.length; _i < _len; _i++) { key = allKeys[_i]; if (!options[key] && optionDefaults[key]) { _results.push(options[key] = optionDefaults[key]); } } return _results; }; LowDBStore.prototype.saveDomainEvent = function(domainEvent, callback) { return callback(null, this.db(this._domainEventsCollectionName).insert(domainEvent).value()); }; LowDBStore.prototype.findAllDomainEvents = function(callback) { return callback(null, this.db(this._domainEventsCollectionName).where().value()); }; LowDBStore.prototype.findDomainEventsByName = function(names, callback) { var name, results, _i, _len; if (!(names instanceof Array)) { names = [names]; } results = []; for (_i = 0, _len = names.length; _i < _len; _i++) { name = names[_i]; results = [].concat(this.db(this._domainEventsCollectionName).where({ name: name }).value()); } return callback(null, results); }; LowDBStore.prototype.findDomainEventsByAggregateId = function(aggregateIds, callback) { var aggregateId, results, _i, _len; if (!(aggregateIds instanceof Array)) { aggregateIds = [aggregateIds]; } results = []; for (_i = 0, _len = aggregateIds.length; _i < _len; _i++) { aggregateId = aggregateIds[_i]; results = [].concat(this.db(this._domainEventsCollectionName).where({ aggregate: { id: aggregateId } }).value()); } return callback(null, results); }; LowDBStore.prototype.findDomainEventsByAggregateName = function(aggregateNames, callback) { var aggregateName, results, _i, _len; if (!(aggregateNames instanceof Array)) { aggregateNames = [aggregateNames]; } results = []; for (_i = 0, _len = aggregateNames.length; _i < _len; _i++) { aggregateName = aggregateNames[_i]; results = [].concat(this.db(this._domainEventsCollectionName).where({ aggregate: { name: aggregateName } }).value()); } return callback(null, results); }; LowDBStore.prototype.findDomainEventsByNameAndAggregateId = function(names, aggregateIds, callback) { var aggregateId, name, results, _i, _j, _len, _len1; if (!(names instanceof Array)) { names = [names]; } if (!(aggregateIds instanceof Array)) { aggregateIds = [aggregateIds]; } results = []; for (_i = 0, _len = aggregateIds.length; _i < _len; _i++) { aggregateId = aggregateIds[_i]; for (_j = 0, _len1 = names.length; _j < _len1; _j++) { name = names[_j]; results = [].concat(this.db(this._domainEventsCollectionName).where({ name: name, aggregate: { id: aggregateId } }).value()); } } return callback(null, results); }; LowDBStore.prototype.getProjectionStore = function(projectionName, callback) { return callback(null, this.db("" + this._projectionCollectionName + "." + projectionName)); }; LowDBStore.prototype.clearProjectionStore = function(projectionName, callback) { delete this.db.db["" + this._projectionCollectionName + "." + projectionName]; this.db.save(); return callback(); }; return LowDBStore; })(); module.exports = LowDBStore; }).call(this);