@eventstore.net/event.store
Version:
A simple and fast EventStore that support multiple persistence and notification providers
67 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const event_stream_1 = require("./event-stream");
/**
* The EventStore itself. To create EventStore instances, use the {@link EventStoreBuilder}
*/
class EventStore {
constructor(provider, publisher) {
this.persistenceProvider = provider;
this.storePublisher = publisher;
}
get provider() {
if (_.isNil(this.persistenceProvider)) {
throw new Error('No Provider configured in EventStore.');
}
return this.persistenceProvider;
}
get publisher() {
return this.storePublisher;
}
/**
* Retrieve an event stream.
* @param aggregation The parent aggregation for the event stream
* @param streamId The stream identifier. Can be any string
* @return The existing stream. If no stream exists for to the given id, a new one
* will be created when the first event is added to the stream.
*/
getEventStream(aggregation, streamId) {
return new event_stream_1.EventStreamImpl(this, { aggregation: aggregation, id: streamId });
}
/**
* Add a new subscription to notifications channel associated with the given aggregation.
* It is necessary to have a valid {@link Publisher} configured that supports subscriptions.
* @param aggregation The aggregation for the stream events
* @param subscriber Declares the function to be called to handle new messages
* @return A subscription. Can be used to remove the subscription to the publisher channel.
*/
subscribe(aggregation, subscriber) {
if (this.publisher && this.publisher.subscribe) {
return this.publisher.subscribe(aggregation, subscriber);
}
throw new Error('There is no valid Publisher configured. '
+ 'Configure a Publisher that implements HasSubscribers int erface');
}
/**
* Retrieves a ranged aggregation list
* @param offset The start position in the aggregation list
* @param limit The desired quantity aggregations
* @return The aggregation list
*/
async getAggregations(offset, limit) {
return this.provider.getAggregations(offset, limit);
}
/**
* Retrieves a ranged stream list
* @param aggregation The aggregation
* @param offset The start position in the stream list
* @param limit The desired quantity streams
* @return The stream list
*/
async getStreams(aggregation, offset, limit) {
return this.provider.getStreams(aggregation, offset, limit);
}
}
exports.EventStore = EventStore;
//# sourceMappingURL=event-store.js.map