UNPKG

conveyor-client

Version:

A client for communicating with the Conveyor event sourcing engine via the HTTP/Webhook interface'

147 lines (116 loc) 4.71 kB
'use strict'; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } const impl = require('./impl'); const connection_ = Symbol('connection'); const name_ = Symbol('name'); const basePath_ = Symbol('base-path'); const ModelClass_ = Symbol('model-class'); class Repository { constructor(connectionOrClient, name, basePath, ModelClass) { if (connectionOrClient.getConnection) { // is Client this[connection_] = connectionOrClient.getConnection(); } else { // is connection this[connection_] = connectionOrClient; } this[name_] = name; this[basePath_] = basePath; this[ModelClass_] = ModelClass; } get connection() { return this[connection_]; } get name() { return this[name_]; } get basePath() { return this[basePath_]; } feedPath(id) { return `${this[basePath_]}/${id}`; } save(model) { var _this = this; return _asyncToGenerator(function* () { if (!model.id) throw new Error('Cannot save model without id'); const id = model.id, unsavedEvents = model.unsavedEvents; yield impl.withTransaction(_this[connection_], (() => { var _ref = _asyncToGenerator(function* (tx) { var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = unsavedEvents[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { let evt = _step.value; yield impl.emitEvent(tx, _this.feedPath(id), evt); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } model.flushEvents(); }); return function (_x) { return _ref.apply(this, arguments); }; })()); })(); } fetch(id) { var _this2 = this; return _asyncToGenerator(function* () { const feed = yield impl.getEvents(_this2[connection_], _this2.feedPath(id)); if (!feed.events.length) { return null; } const events = feed.events.map(function (e) { return e.data; }); if (events.length && events[events.length - 1].type === 'deleted') { return null; } const model = new _this2[ModelClass_](); model.apply(events); return model; })(); } fetchAll() { var _this3 = this; return _asyncToGenerator(function* () { const feed = yield impl.getEvents(_this3[connection_], _this3[basePath_]); const eventsById = feed.events.reduce(function (byId, evt) { const path = evt.feed.split('/'); const id = path[path.length - 1]; if (evt.data.type === 'deleted') { delete byId[id]; return byId; } if (!byId[id]) { byId[id] = new _this3[ModelClass_](); } byId[id].handle(evt.data); return byId; }, {}); return Object.values(eventsById); })(); } delete(id) { var _this4 = this; return _asyncToGenerator(function* () { yield impl.emitEvent(_this4[connection_], _this4.feedPath(id), { type: 'deleted', id }); })(); } } module.exports = Repository;