Mongoose.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const mongoose = require('mongoose')
  2. const Settings = require('settings-sharelatex')
  3. const logger = require('logger-sharelatex')
  4. const POOL_SIZE = Settings.mongo.poolSize
  5. if (
  6. typeof global.beforeEach === 'function' &&
  7. process.argv.join(' ').match(/unit/)
  8. ) {
  9. throw new Error(
  10. 'It looks like unit tests are running, but you are connecting to Mongo. Missing a stub?'
  11. )
  12. }
  13. mongoose.connect(
  14. Settings.mongo.url,
  15. {
  16. poolSize: POOL_SIZE,
  17. config: { autoIndex: false },
  18. useMongoClient: true,
  19. appname: 'web'
  20. }
  21. )
  22. mongoose.connection.on('connected', () =>
  23. logger.log(
  24. {
  25. url: Settings.mongo.url,
  26. poolSize: POOL_SIZE
  27. },
  28. 'mongoose default connection open'
  29. )
  30. )
  31. mongoose.connection.on('error', err =>
  32. logger.err({ err }, 'mongoose error on default connection')
  33. )
  34. mongoose.connection.on('disconnected', () =>
  35. logger.log('mongoose default connection disconnected')
  36. )
  37. if (process.env.MONGOOSE_DEBUG) {
  38. mongoose.set('debug', (collectionName, method, query, doc) =>
  39. logger.debug('mongoose debug', { collectionName, method, query, doc })
  40. )
  41. }
  42. mongoose.plugin(schema => {
  43. schema.options.usePushEach = true
  44. })
  45. module.exports = mongoose