@eventstore.net/event.store
Version:
A simple and fast EventStore that support multiple persistence and notification providers
54 lines • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
/**
* A Persistence Provider that handle all the data in memory. It is a very simple implementation that should be used
* only for development and test purposes.
*/
class InMemoryProvider {
constructor() {
this.store = new Map();
}
async addEvent(stream, data, type = '') {
const currentEvents = await this.getEventsList(stream.aggregation, stream.id);
const event = {
commitTimestamp: new Date().getTime(),
payload: data,
sequence: currentEvents.length,
type: type
};
currentEvents.push(event);
return event;
}
async getEvents(stream, offset = 0, limit) {
const history = this.getEventsList(stream.aggregation, stream.id);
return _(history).drop(offset).take(limit || history.length).value();
}
async getAggregations(offset = 0, limit) {
const keys = Array.from(this.store.keys());
return _(keys).sort().drop(offset).take(limit || this.store.size).value();
}
async getStreams(aggregation, offset = 0, limit) {
const streams = this.store.get(aggregation);
if (streams) {
const keys = Array.from(streams.keys());
return _(keys).sort().drop(offset).take(limit || this.store.size).value();
}
return [];
}
getEventsList(aggregation, streamId) {
let streams = this.store.get(aggregation);
if (!streams) {
streams = new Map();
this.store.set(aggregation, streams);
}
let history = streams.get(streamId);
if (!history) {
history = new Array();
streams.set(streamId, history);
}
return history;
}
}
exports.InMemoryProvider = InMemoryProvider;
//# sourceMappingURL=memory.js.map