1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/db/event-store.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2016-11-05 10:16:48 +01:00
const { EventEmitter } = require('events');
2016-06-18 21:53:18 +02:00
const EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
2014-10-23 10:32:13 +02:00
2016-11-05 10:16:48 +01:00
class EventStore extends EventEmitter {
2017-06-28 10:17:14 +02:00
constructor(db) {
2016-11-05 10:16:48 +01:00
super();
this.db = db;
}
2017-06-28 10:17:14 +02:00
store(event) {
return this.db('events')
.insert({
type: event.type,
created_by: event.createdBy, // eslint-disable-line
2017-06-28 10:17:14 +02:00
data: event.data,
})
.then(() => this.emit(event.type, event));
}
2014-10-23 10:32:13 +02:00
2017-06-28 10:17:14 +02:00
getEvents() {
2016-11-05 10:16:48 +01:00
return this.db
.select(EVENT_COLUMNS)
.from('events')
2016-10-27 13:12:38 +02:00
.limit(100)
.orderBy('created_at', 'desc')
2016-11-05 10:16:48 +01:00
.map(this.rowToEvent);
}
2017-06-28 10:17:14 +02:00
getEventsFilterByName(name) {
2016-11-05 10:16:48 +01:00
return this.db
2017-06-28 10:17:14 +02:00
.select(EVENT_COLUMNS)
.from('events')
.limit(100)
.whereRaw("data ->> 'name' = ?", [name])
.orderBy('created_at', 'desc')
.map(this.rowToEvent);
}
2014-10-24 15:32:33 +02:00
2017-06-28 10:17:14 +02:00
rowToEvent(row) {
return {
id: row.id,
type: row.type,
createdBy: row.created_by,
createdAt: row.created_at,
2016-06-18 21:55:46 +02:00
data: row.data,
};
}
2017-06-28 10:17:14 +02:00
}
2016-11-05 10:16:48 +01:00
module.exports = EventStore;