2014-11-14 15:06:53 +01:00
|
|
|
var knex = require('./dbPool');
|
2014-11-17 10:10:47 +01:00
|
|
|
var EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
|
2014-10-23 10:32:13 +02:00
|
|
|
|
|
|
|
function storeEvent(event) {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex('events').insert({
|
|
|
|
type: event.type,
|
2016-04-24 22:41:37 +02:00
|
|
|
created_by: event.createdBy, // eslint-disable-line
|
2014-11-14 15:06:53 +01:00
|
|
|
data: event.data
|
2014-10-23 10:32:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:32:33 +02:00
|
|
|
function getEvents() {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
|
|
|
.orderBy('created_at', 'desc')
|
2014-11-14 16:40:13 +01:00
|
|
|
.map(rowToEvent);
|
2014-10-24 15:32:33 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 07:28:40 +01:00
|
|
|
function getEventsFilterByName(name) {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
|
|
|
.whereRaw("data ->> 'name' = ?", [name])
|
|
|
|
.orderBy('created_at', 'desc')
|
2014-11-14 16:40:13 +01:00
|
|
|
.map(rowToEvent);
|
2014-11-14 07:13:08 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function rowToEvent(row) {
|
2014-10-24 15:32:33 +02:00
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
type: row.type,
|
2016-04-24 22:41:37 +02:00
|
|
|
createdBy: row.created_by,
|
|
|
|
createdAt: row.created_at,
|
2014-10-24 15:32:33 +02:00
|
|
|
data: row.data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:32:13 +02:00
|
|
|
module.exports = {
|
2014-10-24 15:32:33 +02:00
|
|
|
store: storeEvent,
|
2014-11-14 07:28:40 +01:00
|
|
|
getEvents: getEvents,
|
|
|
|
getEventsFilterByName: getEventsFilterByName
|
2016-04-24 22:41:37 +02:00
|
|
|
};
|