@eventstore.net/event.store
Version:
A simple and fast EventStore that support multiple persistence and notification providers
45 lines • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const connect_1 = require("../redis/connect");
/**
* A Persistence Provider that handle all the data in redis.
*/
class RedisProvider {
constructor(config) {
this.redis = connect_1.RedisFactory.createClient(config);
}
async addEvent(stream, data, type = '') {
const sequence = await this.redis.incr(`sequences:{${this.getKey(stream.aggregation, stream.id)}}`) - 1;
const time = await this.redis.time();
const commitTimestamp = parseInt(time, 10);
const event = {
commitTimestamp: commitTimestamp,
payload: data,
sequence: sequence,
type: type
};
await this.redis.multi()
.rpush(this.getKey(stream.aggregation, stream.id), JSON.stringify(event))
.zadd(`meta:aggregations:${stream.aggregation}`, '1', stream.id)
.zadd('meta:aggregations', '1', stream.aggregation)
.exec();
return event;
}
async getEvents(stream, offset = 0, limit = -1) {
const history = await this.redis.lrange(this.getKey(stream.aggregation, stream.id), offset, limit);
return history.map(data => JSON.parse(data));
}
async getAggregations(offset = 0, limit = -1) {
const aggregations = await this.redis.zrange('meta:aggregations', offset, limit);
return aggregations;
}
async getStreams(aggregation, offset = 0, limit = -1) {
const streams = await this.redis.zrange(`meta:aggregations:${aggregation}`, offset, limit);
return streams;
}
getKey(aggregation, streamId) {
return `${aggregation}:${streamId}`;
}
}
exports.RedisProvider = RedisProvider;
//# sourceMappingURL=redis.js.map