added workaround for 'added' field compatible to MongoDB 3.6
This commit is contained in:
parent
f41498da53
commit
523b2c9b68
@ -3,7 +3,7 @@ applications:
|
||||
- name: definma-api
|
||||
path: dist/
|
||||
instances: 1
|
||||
memory: 512M
|
||||
memory: 1024M
|
||||
stack: cflinuxfs3
|
||||
buildpacks:
|
||||
- nodejs_buildpack
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user