overleaf/services/web/app/src/infrastructure/Mongoose.js
Eric Mc Sween 935fdb2b5e Merge pull request #2837 from overleaf/msm-set-native-promise-mongoose
Set Mongoose to explicitly use native promises

GitOrigin-RevId: 551dfbbc51233bace25699fd8d92610c69809c86
2020-05-21 03:22:35 +00:00

56 lines
1.2 KiB
JavaScript

const mongoose = require('mongoose')
const Settings = require('settings-sharelatex')
const logger = require('logger-sharelatex')
const POOL_SIZE = Settings.mongo.poolSize
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?'
)
}
mongoose.connect(
Settings.mongo.url,
{
poolSize: POOL_SIZE,
config: { autoIndex: false },
useMongoClient: true,
appname: 'web'
}
)
mongoose.connection.on('connected', () =>
logger.log(
{
url: Settings.mongo.url,
poolSize: POOL_SIZE
},
'mongoose default connection open'
)
)
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 })
)
}
mongoose.plugin(schema => {
schema.options.usePushEach = true
})
mongoose.Promise = global.Promise;
module.exports = mongoose