1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/eventDb.js
2014-11-17 10:13:30 +01:00

43 lines
1.0 KiB
JavaScript

var knex = require('./dbPool');
var EVENT_COLUMNS = ['id', 'type', 'created_by', 'created_at', 'data'];
function storeEvent(event) {
return knex('events').insert({
type: event.type,
created_by: event.createdBy, // jshint ignore:line
data: event.data
});
}
function getEvents() {
return knex
.select(EVENT_COLUMNS)
.from('events')
.orderBy('created_at', 'desc')
.map(rowToEvent);
}
function getEventsFilterByName(name) {
return knex
.select(EVENT_COLUMNS)
.from('events')
.whereRaw("data ->> 'name' = ?", [name])
.orderBy('created_at', 'desc')
.map(rowToEvent);
}
function rowToEvent(row) {
return {
id: row.id,
type: row.type,
createdBy: row.created_by, // jshint ignore:line
createdAt: row.created_at, // jshint ignore:line
data: row.data
};
}
module.exports = {
store: storeEvent,
getEvents: getEvents,
getEventsFilterByName: getEventsFilterByName
};