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

38 lines
969 B
JavaScript

var Promise = require('bluebird'),
dbPool = require('./dbPool');
function storeEvent(event) {
var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)';
var params = [event.type, event.createdBy, event.data];
return new Promise(function (resolve, reject) {
dbPool.query(sql, params, function (err) {
if (err) {reject(err);}
resolve();
});
});
}
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));
});
});
}
function mapToEvent(row) {
return {
id: row.id,
type: row.type,
createdBy: row.created,
data: row.data
};
}
module.exports = {
store: storeEvent,
getEvents: getEvents
};