1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/db/event-store.js

55 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 {
constructor (db) {
super();
this.db = db;
}
store (event) {
return this.db('events').insert({
type: event.type,
created_by: event.createdBy, // eslint-disable-line
2016-06-18 21:55:46 +02:00
data: event.data,
2016-11-05 10:16:48 +01:00
})
.then(() => this.emit(event.type, event));
}
2014-10-23 10:32:13 +02:00
2016-11-05 10:16:48 +01:00
getEvents () {
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);
}
2016-11-05 10:16:48 +01:00
getEventsFilterByName (name) {
return this.db
2014-11-14 15:06:53 +01:00
.select(EVENT_COLUMNS)
.from('events')
2016-10-27 13:12:38 +02:00
.limit(100)
2016-06-18 22:55:33 +02:00
.whereRaw('data ->> \'name\' = ?', [name])
2014-11-14 15:06:53 +01:00
.orderBy('created_at', 'desc')
2016-11-05 10:16:48 +01:00
.map(this.rowToEvent);
}
2014-10-24 15:32:33 +02:00
2016-11-05 10:16:48 +01: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,
};
}
};
2016-11-05 10:16:48 +01:00
module.exports = EventStore;