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
Ivar Conradi Østhus d01c9d2dac
fix: use airbnb lint rules directly (#583)
This drops usage of finn-eslint rules as they are no
longer maintained.
2020-04-14 22:29:11 +02:00

63 lines
1.5 KiB
JavaScript

'use strict';
const { EventEmitter } = require('events');
const { DROP_FEATURES } = require('../event-type');
const EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
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
data: event.data,
})
.then(() => this.emit(event.type, event));
}
getEvents() {
return this.db
.select(EVENT_COLUMNS)
.from('events')
.limit(100)
.orderBy('created_at', 'desc')
.map(this.rowToEvent);
}
getEventsFilterByName(name) {
return this.db
.select(EVENT_COLUMNS)
.from('events')
.limit(100)
.whereRaw("data ->> 'name' = ?", [name])
.andWhere(
'id',
'>=',
this.db
.select(this.db.raw('coalesce(max(id),0) as id'))
.from('events')
.where({ type: DROP_FEATURES }),
)
.orderBy('created_at', 'desc')
.map(this.rowToEvent);
}
rowToEvent(row) {
return {
id: row.id,
type: row.type,
createdBy: row.created_by,
createdAt: row.created_at,
data: row.data,
};
}
}
module.exports = EventStore;