2014-10-23 10:32:13 +02:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
dbPool = require('./dbPool');
|
|
|
|
|
|
|
|
function storeEvent(event) {
|
2014-10-23 12:03:46 +02:00
|
|
|
var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)';
|
|
|
|
var params = [event.type, event.createdBy, event.data];
|
2014-10-23 10:32:13 +02:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
dbPool.query(sql, params, function (err) {
|
|
|
|
if (err) {reject(err);}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:32:33 +02:00
|
|
|
function getEvents() {
|
|
|
|
var sql = 'SELECT id, type, created_by as created, data FROM events ORDER BY created_at DESC';
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
dbPool.query(sql, function(err, res) {
|
|
|
|
if(err) {reject(err);}
|
|
|
|
resolve(res.rows.map(mapToEvent));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-14 07:13:08 +01:00
|
|
|
function getEvents(name) {
|
|
|
|
var sql = 'SELECT id, type, created_by as created, data ' +
|
|
|
|
'FROM events WHERE data ->> \'name\' = $1 ' +
|
|
|
|
'ORDER BY created_at DESC';
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
dbPool.query(sql, [name], function(err, res) {
|
|
|
|
if(err) {reject(err);}
|
|
|
|
resolve(res.rows.map(mapToEvent));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:32:33 +02:00
|
|
|
function mapToEvent(row) {
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
type: row.type,
|
|
|
|
createdBy: row.created,
|
|
|
|
data: row.data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:32:13 +02:00
|
|
|
module.exports = {
|
2014-10-24 15:32:33 +02:00
|
|
|
store: storeEvent,
|
|
|
|
getEvents: getEvents
|
2014-10-23 10:32:13 +02:00
|
|
|
};
|