1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/db/event.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

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
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
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')
.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
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 {
store: storeEvent,
getEvents: getEvents,
getEventsFilterByName: getEventsFilterByName
2014-10-24 15:32:33 +02:00
};
};