1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/db/event-store.js

73 lines
1.7 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');
const { DROP_FEATURES } = require('../event-type');
2016-11-05 10:16:48 +01:00
const EVENT_COLUMNS = [
'id',
'type',
'created_by',
'created_at',
'data',
'tags',
];
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;
}
2020-09-18 09:05:09 +02:00
async store(event) {
await this.db('events').insert({
type: event.type,
created_by: event.createdBy, // eslint-disable-line
2020-09-18 09:05:09 +02:00
data: event.data,
tags: event.tags ? JSON.stringify(event.tags) : [],
2020-09-18 09:05:09 +02:00
});
this.emit(event.type, event);
}
2014-10-23 10:32:13 +02:00
2020-09-18 09:05:09 +02:00
async getEvents() {
const rows = await this.db
.select(EVENT_COLUMNS)
.from('events')
2016-10-27 13:12:38 +02:00
.limit(100)
2020-09-18 09:05:09 +02:00
.orderBy('created_at', 'desc');
return rows.map(this.rowToEvent);
}
2020-09-18 09:05:09 +02:00
async getEventsFilterByName(name) {
const rows = await this.db
2017-06-28 10:17:14 +02:00
.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 }),
)
2020-09-18 09:05:09 +02:00
.orderBy('created_at', 'desc');
return rows.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,
tags: row.tags,
};
}
2017-06-28 10:17:14 +02:00
}
2016-11-05 10:16:48 +01:00
module.exports = EventStore;