UNPKG

wr-eventstore

Version:

Node-eventstore is a node.js module for multiple databases. It can be very useful as eventstore if you work with (d)ddd, cqrs, eventsourcing, commands and events, etc.

91 lines (74 loc) 2.37 kB
var debug = require('debug')('eventstore:event'), _ = require('lodash'); function put(object, path, value) { if (typeof path === "string") { path = path.split("."); } if (!(path instanceof Array) || path.length === 0) { return false; } path = path.slice(); var key = path.shift(); if (typeof object !== "object" || object === null || key === "__proto__") { return false; } if (path.length === 0) { object[key] = value; } else { if (typeof object[key] === "undefined") { object[key] = {}; } if (typeof object[key] !== "object" || object[key] === null) { return false; } return put(object[key], path, value); } } /** * Event constructor * @param {EventStream} eventstream the corresponding event stream object * @param {Object} event the event object * @constructor */ function Event (eventstream, event, eventMappings) { if (!eventstream) { var errStreamMsg = 'eventstream not injected!'; debug(errStreamMsg); throw new Error(errStreamMsg); } if (!event) { var errEvtMsg = 'event not injected!'; debug(errEvtMsg); throw new Error(errEvtMsg); } if (!eventstream.aggregateId) { var errAggIdMsg = 'eventstream.aggregateId not injected!'; debug(errAggIdMsg); throw new Error(errAggIdMsg); } if (!_.isArray(eventstream.uncommittedEvents)) { var errAggIdMsg = 'eventstream.uncommittedEvents not injected!'; debug(errAggIdMsg); throw new Error(errAggIdMsg); } eventMappings = eventMappings || {}; this.streamId = eventstream.aggregateId; this.aggregateId = eventstream.aggregateId; this.aggregate = eventstream.aggregate; this.context = eventstream.context; this.streamRevision = null; this.commitId = null; this.commitSequence = null; this.commitStamp = null; this.payload = event || null; this.position = null; this.applyMappings = function applyMappings() { _.keys(eventMappings).forEach(function (key) { if (this[key] !== undefined && this[key] !== null) { put(this.payload, eventMappings[key], this[key]); } }.bind(this)); }; eventstream.uncommittedEvents.push(this); } module.exports = Event;