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

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
const EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
2014-10-23 10:32:13 +02:00
2016-07-02 11:54:50 +02:00
module.exports = function (db) {
function storeEvent (event) {
return db('events').insert({
type: event.type,
created_by: event.createdBy, // eslint-disable-line
2016-06-18 21:55:46 +02:00
data: event.data,
});
}
2014-10-23 10:32:13 +02:00
2016-07-02 11:54:50 +02:00
function getEvents () {
return db
.select(EVENT_COLUMNS)
.from('events')
.orderBy('created_at', 'desc')
.map(rowToEvent);
}
2016-07-02 11:54:50 +02:00
function getEventsFilterByName (name) {
return db
2014-11-14 15:06:53 +01:00
.select(EVENT_COLUMNS)
.from('events')
2016-06-18 22:55:33 +02:00
.whereRaw('data ->> \'name\' = ?', [name])
2014-11-14 15:06:53 +01:00
.orderBy('created_at', 'desc')
2014-11-14 16:40:13 +01:00
.map(rowToEvent);
}
2014-10-24 15:32:33 +02:00
2016-07-02 11:54:50 +02:00
function 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,
};
}
2014-11-14 07:13:08 +01:00
2014-10-24 15:32:33 +02:00
return {
store: storeEvent,
2016-06-18 21:53:18 +02:00
getEvents,
2016-06-18 21:55:46 +02:00
getEventsFilterByName,
2014-10-24 15:32:33 +02:00
};
};