1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/packages/unleash-api/lib/db/event.js
2020-02-20 08:30:26 +01:00

46 lines
1.1 KiB
JavaScript

var EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
module.exports = function(db) {
function storeEvent(event) {
return db('events').insert({
type: event.type,
created_by: event.createdBy, // eslint-disable-line
data: event.data
});
}
function getEvents() {
return db
.select(EVENT_COLUMNS)
.from('events')
.orderBy('created_at', 'desc')
.map(rowToEvent);
}
function getEventsFilterByName(name) {
return db
.select(EVENT_COLUMNS)
.from('events')
.whereRaw("data ->> 'name' = ?", [name])
.orderBy('created_at', 'desc')
.map(rowToEvent);
}
function rowToEvent(row) {
return {
id: row.id,
type: row.type,
createdBy: row.created_by,
createdAt: row.created_at,
data: row.data
};
}
return {
store: storeEvent,
getEvents: getEvents,
getEventsFilterByName: getEventsFilterByName
};
};