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) {
|
2016-05-01 22:53:09 +02:00
|
|
|
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,
|
2016-05-01 22:53:09 +02:00
|
|
|
});
|
|
|
|
}
|
2014-10-23 10:32:13 +02:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function getEvents () {
|
2016-05-01 22:53:09 +02:00
|
|
|
return db
|
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
|
|
|
.orderBy('created_at', 'desc')
|
|
|
|
.map(rowToEvent);
|
|
|
|
}
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function getEventsFilterByName (name) {
|
2016-05-01 22:53:09 +02:00
|
|
|
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);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2014-10-24 15:32:33 +02:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function rowToEvent (row) {
|
2016-05-01 22:53:09 +02:00
|
|
|
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-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
2014-11-14 07:13:08 +01:00
|
|
|
|
2014-10-24 15:32:33 +02:00
|
|
|
return {
|
2016-05-01 22:53:09 +02:00
|
|
|
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
|
|
|
};
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|
2016-05-01 22:53:09 +02:00
|
|
|
|