From 523b2c9b68fd3930759f76ba240e48e54ee98ddf Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 10 Jul 2020 09:42:05 +0200 Subject: [PATCH] added workaround for 'added' field compatible to MongoDB 3.6 --- manifest.yml | 2 +- package.json | 2 +- src/db.ts | 7 ++++++- src/routes/sample.ts | 22 ++++++++++++++++++++-- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/manifest.yml b/manifest.yml index dd7e0f1..1791c58 100644 --- a/manifest.yml +++ b/manifest.yml @@ -3,7 +3,7 @@ applications: - name: definma-api path: dist/ instances: 1 - memory: 512M + memory: 1024M stack: cflinuxfs3 buildpacks: - nodejs_buildpack diff --git a/package.json b/package.json index bfcdcca..f9494d3 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build": "build.bat", "build-push": "build.bat && cf push", "test": "mocha dist/**/**.spec.js", - "start": "sleep 5s && node index.js", + "start": "node index.js", "dev": "nodemon -e ts,yaml --exec \"tsc && node dist/index.js || exit 1\"", "loadDev": "node dist/test/loadDev.js", "coverage": "tsc && nyc --reporter=html --reporter=text mocha dist/**/**.spec.js --timeout 5000", diff --git a/src/db.ts b/src/db.ts index 03c56e1..2b1f409 100644 --- a/src/db.ts +++ b/src/db.ts @@ -47,10 +47,15 @@ export default class db { if (err) done(err); }); mongoose.connection.on('error', console.error.bind(console, 'connection error:')); + mongoose.connection.on('connected', () => { // evaluation connection behaviour on prod + if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing + console.info('Database connected'); + } + }); mongoose.connection.on('disconnected', () => { // reset state on disconnect if (process.env.NODE_ENV !== 'test') { // Do not interfere with testing console.info('Database disconnected'); - this.state.db = 0; + // this.state.db = 0; // prod database connects and disconnects automatically } }); process.on('SIGINT', () => { // close connection when app is terminated diff --git a/src/routes/sample.ts b/src/routes/sample.ts index f63b0b6..f3395c8 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -256,7 +256,7 @@ router.get('/samples', async (req, res, next) => { // projection.added = {$toDate: '$_id'}; // projection.added = { $convert: { input: '$_id', to: "date" } } // TODO: upgrade MongoDB version or find alternative } - if (!(filters.fields.indexOf('_id') >= 0)) { // disable _id explicitly + if (filters.fields.indexOf('_id') < 0 && filters.fields.indexOf('added') < 0) { // disable _id explicitly projection._id = false; } queryPtr.push({$project: projection}); @@ -266,8 +266,18 @@ router.get('/samples', async (req, res, next) => { if (err) return next(err); if (data[0].count) { res.header('x-total-items', data[0].count.length > 0 ? data[0].count[0].count : 0); + res.header('Access-Control-Expose-Headers', 'x-total-items'); data = data[0].samples; } + if (filters.fields.indexOf('added') >= 0) { // add added date + data.map(e => { + e.added = e._id.getTimestamp(); + if (filters.fields.indexOf('_id') < 0) { + delete e._id; + } + return e + }); + } if (filters['to-page'] < 0) { data.reverse(); } @@ -289,7 +299,15 @@ router.get('/samples', async (req, res, next) => { res.write('['); let count = 0; const stream = collection.aggregate(query).cursor().exec(); - stream.on('data', data => { res.write((count === 0 ? '' : ',\n') + JSON.stringify(data)); count ++; }); + stream.on('data', data => { + if (filters.fields.indexOf('added') >= 0) { // add added date + data.added = data._id.getTimestamp(); + if (filters.fields.indexOf('_id') < 0) { + delete data._id; + } + } + res.write((count === 0 ? '' : ',\n') + JSON.stringify(data)); count ++; + }); stream.on('close', () => { res.write(']'); res.end();