1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/eventDb.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

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,
created_by: event.createdBy, // jshint ignore:line
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,
2014-11-14 15:06:53 +01:00
createdBy: row.created_by, // jshint ignore:line
2014-11-17 10:10:47 +01:00
createdAt: row.created_at, // jshint ignore:line
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
2014-10-23 10:32:13 +02:00
};