mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-10 14:40:47 +02:00
fa95775a5c
[misc] align all the mongodb dependency versions GitOrigin-RevId: 1194fe57601af98bb61250a285bfc85b4b8179dd
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
const mongoose = require('mongoose')
|
|
const Settings = require('@overleaf/settings')
|
|
const Metrics = require('@overleaf/metrics')
|
|
const logger = require('@overleaf/logger')
|
|
const { addConnectionDrainer } = require('./GracefulShutdown')
|
|
|
|
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.set('autoIndex', false)
|
|
mongoose.set('strictQuery', false)
|
|
|
|
const connectionPromise = mongoose.connect(
|
|
Settings.mongo.url,
|
|
Settings.mongo.options
|
|
)
|
|
|
|
connectionPromise
|
|
.then(mongooseInstance => {
|
|
Metrics.mongodb.monitor(mongooseInstance.connection.client)
|
|
})
|
|
.catch(error => {
|
|
logger.error(
|
|
{ error },
|
|
'Failed to connect to MongoDB - cannot set up monitoring'
|
|
)
|
|
})
|
|
|
|
addConnectionDrainer('mongoose', async () => {
|
|
await connectionPromise
|
|
await mongoose.disconnect()
|
|
})
|
|
|
|
mongoose.connection.on('connected', () =>
|
|
logger.debug('mongoose default connection open')
|
|
)
|
|
|
|
mongoose.connection.on('error', err =>
|
|
logger.err({ err }, 'mongoose error on default connection')
|
|
)
|
|
|
|
mongoose.connection.on('disconnected', () =>
|
|
logger.debug('mongoose default connection disconnected')
|
|
)
|
|
|
|
if (process.env.MONGOOSE_DEBUG) {
|
|
mongoose.set('debug', (collectionName, method, query, doc) =>
|
|
logger.debug({ collectionName, method, query, doc }, 'mongoose debug')
|
|
)
|
|
}
|
|
|
|
mongoose.plugin(schema => {
|
|
schema.options.usePushEach = true
|
|
})
|
|
|
|
mongoose.Promise = global.Promise
|
|
|
|
mongoose.connectionPromise = connectionPromise
|
|
|
|
module.exports = mongoose
|