2019-05-29 05:21:06 -04:00
|
|
|
const mongoose = require('mongoose')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
|
2019-11-18 09:03:20 -05:00
|
|
|
const POOL_SIZE = Settings.mongo.poolSize
|
|
|
|
|
2020-02-12 10:13:09 -05:00
|
|
|
if (
|
|
|
|
typeof global.beforeEach === 'function' &&
|
|
|
|
process.argv.join(' ').match(/unit/)
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
'It looks like unit tests are running, but you are connecting to Mongo. Missing a stub?'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
mongoose.connect(
|
|
|
|
Settings.mongo.url,
|
|
|
|
{
|
2019-11-18 09:03:20 -05:00
|
|
|
poolSize: POOL_SIZE,
|
|
|
|
config: { autoIndex: false },
|
|
|
|
useMongoClient: true,
|
|
|
|
appname: 'web'
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
mongoose.connection.on('connected', () =>
|
2019-11-18 09:03:20 -05:00
|
|
|
logger.log(
|
|
|
|
{
|
|
|
|
url: Settings.mongo.url,
|
|
|
|
poolSize: POOL_SIZE
|
|
|
|
},
|
|
|
|
'mongoose default connection open'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
mongoose.connection.on('error', err =>
|
|
|
|
logger.err({ err }, 'mongoose error on default connection')
|
|
|
|
)
|
|
|
|
|
|
|
|
mongoose.connection.on('disconnected', () =>
|
|
|
|
logger.log('mongoose default connection disconnected')
|
|
|
|
)
|
|
|
|
|
|
|
|
if (process.env.MONGOOSE_DEBUG) {
|
|
|
|
mongoose.set('debug', (collectionName, method, query, doc) =>
|
|
|
|
logger.debug('mongoose debug', { collectionName, method, query, doc })
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-08 08:40:07 -05:00
|
|
|
mongoose.plugin(schema => {
|
|
|
|
schema.options.usePushEach = true
|
|
|
|
})
|
|
|
|
|
2020-05-20 11:34:47 -04:00
|
|
|
mongoose.Promise = global.Promise
|
2020-05-20 10:19:58 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
module.exports = mongoose
|