Extend store API

This commit is contained in:
Zalmoxisus 2016-11-12 16:16:43 +02:00
parent 6794f63629
commit 1ba4d1e40b
4 changed files with 20 additions and 10 deletions

View File

@ -17,7 +17,7 @@ module.exports = function connector(options) {
return knex.seed.run();
})
.then(function() {
console.log('Migrations are finished.');
console.log(' \x1b[0;32m[Done]\x1b[0m Migrations are finished\n');
})
.catch(function(error) {
console.error(error);

View File

@ -13,6 +13,7 @@ exports.up = function(knex, Promise) {
table.string('version');
table.string('user');
table.string('userId');
table.string('instanceId');
table.string('meta');
table.string('exception');
table.timestamp('added').defaultTo(knex.fn.now());

View File

@ -20,10 +20,16 @@ function list(query, fields) {
return r;
}
function listAll(query) {
var r = knex.select().from(reports);
if (query) return r.where(query);
return r;
}
function get(id) {
if (!id) return error('No id specified.');
return knex(reports).where('id', id);
return knex(reports).where('id', id).first();
}
function add(data) {
@ -45,13 +51,15 @@ function add(data) {
preloadedState: data.preloadedState,
screenshot: data.screenshot,
version: data.version,
appId: data.appId,
userAgent: data.userAgent,
user: data.user,
userId: typeof data.user === 'object' ? data.user.id : data.user,
instanceId: data.instanceId,
meta: data.meta,
exception: data.exception
exception: data.exception,
added: Date.now()
};
if (data.appId) report.appId = data.appId; // TODO check if the id exists and we have access to link it
/*
var payload = {
id: uuid.v4(),
@ -73,6 +81,7 @@ function createStore(options) {
return {
list: list,
listAll: listAll,
get: get,
add: add
};

View File

@ -17,15 +17,15 @@ module.exports.run = function(worker) {
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, '..', 'views'));
app.get('*', function(req, res) {
res.render('index', { port: worker.options.port });
});
if (logHTTPRequests) {
if (typeof logHTTPRequests === 'object') app.use(morgan('combined', logHTTPRequests));
else app.use(morgan('combined'));
}
app.get('*', function(req, res) {
res.render('index', { port: worker.options.port });
});
app.use(cors({ methods: 'POST' }));
app.use(bodyParser.json({ limit: limit }));
app.use(bodyParser.urlencoded({ limit: limit, extended: false }));
@ -34,7 +34,7 @@ module.exports.run = function(worker) {
switch(req.body.op) {
case 'get':
store.get(req.body.id).then(function(r) {
res.send(r[0] || {});
res.send(r || {});
}).catch(function(error) {
console.error(error);
res.sendStatus(500)
@ -98,7 +98,7 @@ module.exports.run = function(worker) {
});
socket.on('getReport', function (id, respond) {
store.get(id).then(function(data) {
respond(null, data[0]);
respond(null, data);
}).catch(function(error) {
console.error(error);
});