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,
|
2016-05-01 22:53:09 +02:00
|
|
|
created_by: event.createdBy, // eslint-disable-line
|
2017-06-28 10:17:14 +02:00
|
|
|
data: event.data,
|
|
|
|
})
|
|
|
|
.then(() => this.emit(event.type, event));
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
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
|
2016-05-01 22:53:09 +02:00
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
2016-10-27 13:12:38 +02:00
|
|
|
.limit(100)
|
2016-05-01 22:53:09 +02:00
|
|
|
.orderBy('created_at', 'desc')
|
2016-11-05 10:16:48 +01:00
|
|
|
.map(this.rowToEvent);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2014-10-24 15:32:33 +02:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
rowToEvent(row) {
|
2016-05-01 22:53:09 +02:00
|
|
|
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,
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
module.exports = EventStore;
|