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
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function(db) {
|
|
|
|
function storeEvent(event) {
|
|
|
|
return db('events').insert({
|
|
|
|
type: event.type,
|
|
|
|
created_by: event.createdBy, // eslint-disable-line
|
|
|
|
data: event.data
|
|
|
|
});
|
|
|
|
}
|
2014-10-23 10:32:13 +02:00
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
function getEvents() {
|
|
|
|
return db
|
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
|
|
|
.orderBy('created_at', 'desc')
|
|
|
|
.map(rowToEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEventsFilterByName(name) {
|
|
|
|
return db
|
2014-11-14 15:06:53 +01:00
|
|
|
.select(EVENT_COLUMNS)
|
|
|
|
.from('events')
|
2016-05-01 22:53:09 +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-05-01 22:53:09 +02:00
|
|
|
function rowToEvent(row) {
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
type: row.type,
|
|
|
|
createdBy: row.created_by,
|
|
|
|
createdAt: row.created_at,
|
|
|
|
data: row.data
|
|
|
|
};
|
|
|
|
}
|
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,
|
|
|
|
getEvents: getEvents,
|
|
|
|
getEventsFilterByName: 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
|
|
|
|