added workaround for 'added' field compatible to MongoDB 3.6
This commit is contained in:
		@@ -3,7 +3,7 @@ applications:
 | 
				
			|||||||
  - name: definma-api
 | 
					  - name: definma-api
 | 
				
			||||||
    path: dist/
 | 
					    path: dist/
 | 
				
			||||||
    instances: 1
 | 
					    instances: 1
 | 
				
			||||||
    memory: 512M
 | 
					    memory: 1024M
 | 
				
			||||||
    stack: cflinuxfs3
 | 
					    stack: cflinuxfs3
 | 
				
			||||||
    buildpacks:
 | 
					    buildpacks:
 | 
				
			||||||
      - nodejs_buildpack
 | 
					      - nodejs_buildpack
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@
 | 
				
			|||||||
    "build": "build.bat",
 | 
					    "build": "build.bat",
 | 
				
			||||||
    "build-push": "build.bat && cf push",
 | 
					    "build-push": "build.bat && cf push",
 | 
				
			||||||
    "test": "mocha dist/**/**.spec.js",
 | 
					    "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\"",
 | 
					    "dev": "nodemon -e ts,yaml --exec \"tsc && node dist/index.js || exit 1\"",
 | 
				
			||||||
    "loadDev": "node dist/test/loadDev.js",
 | 
					    "loadDev": "node dist/test/loadDev.js",
 | 
				
			||||||
    "coverage": "tsc && nyc --reporter=html --reporter=text mocha dist/**/**.spec.js --timeout 5000",
 | 
					    "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);
 | 
					      if (err) done(err);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
 | 
					    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
 | 
					    mongoose.connection.on('disconnected', () => {  // reset state on disconnect
 | 
				
			||||||
      if (process.env.NODE_ENV !== 'test') {  // Do not interfere with testing
 | 
					      if (process.env.NODE_ENV !== 'test') {  // Do not interfere with testing
 | 
				
			||||||
        console.info('Database disconnected');
 | 
					        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
 | 
					    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 = {$toDate: '$_id'};
 | 
				
			||||||
    // projection.added = { $convert: { input: '$_id', to: "date" } }  // TODO: upgrade MongoDB version or find alternative
 | 
					    // 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;
 | 
					    projection._id = false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  queryPtr.push({$project: projection});
 | 
					  queryPtr.push({$project: projection});
 | 
				
			||||||
@@ -266,8 +266,18 @@ router.get('/samples', async (req, res, next) => {
 | 
				
			|||||||
      if (err) return next(err);
 | 
					      if (err) return next(err);
 | 
				
			||||||
      if (data[0].count) {
 | 
					      if (data[0].count) {
 | 
				
			||||||
        res.header('x-total-items', data[0].count.length > 0 ? data[0].count[0].count : 0);
 | 
					        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;
 | 
					        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) {
 | 
					      if (filters['to-page'] < 0) {
 | 
				
			||||||
        data.reverse();
 | 
					        data.reverse();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -289,7 +299,15 @@ router.get('/samples', async (req, res, next) => {
 | 
				
			|||||||
    res.write('[');
 | 
					    res.write('[');
 | 
				
			||||||
    let count = 0;
 | 
					    let count = 0;
 | 
				
			||||||
    const stream = collection.aggregate(query).cursor().exec();
 | 
					    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', () => {
 | 
					    stream.on('close', () => {
 | 
				
			||||||
      res.write(']');
 | 
					      res.write(']');
 | 
				
			||||||
      res.end();
 | 
					      res.end();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user