From 1ba4d1e40bca8800255b66ab69ce92574726ecea Mon Sep 17 00:00:00 2001 From: Zalmoxisus Date: Sat, 12 Nov 2016 16:16:43 +0200 Subject: [PATCH] Extend store API --- lib/db/connector.js | 2 +- lib/db/migrations/index.js | 1 + lib/store.js | 15 ++++++++++++--- lib/worker.js | 12 ++++++------ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/db/connector.js b/lib/db/connector.js index a92b64e..0b06944 100644 --- a/lib/db/connector.js +++ b/lib/db/connector.js @@ -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); diff --git a/lib/db/migrations/index.js b/lib/db/migrations/index.js index cfb86cb..2545997 100644 --- a/lib/db/migrations/index.js +++ b/lib/db/migrations/index.js @@ -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()); diff --git a/lib/store.js b/lib/store.js index 457d7e9..e052469 100644 --- a/lib/store.js +++ b/lib/store.js @@ -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 }; diff --git a/lib/worker.js b/lib/worker.js index 0625406..cffa9b9 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -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); });