2019-05-29 05:21:06 -04:00
|
|
|
const mongoose = require('mongoose')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2023-03-01 07:12:23 -05:00
|
|
|
const Metrics = require('@overleaf/metrics')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2022-08-04 04:16:38 -04:00
|
|
|
const { addConnectionDrainer } = require('./GracefulShutdown')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
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?'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-28 08:26:18 -05:00
|
|
|
mongoose.set('autoIndex', false)
|
|
|
|
mongoose.set('strictQuery', false)
|
|
|
|
|
2020-08-17 10:13:53 -04:00
|
|
|
const connectionPromise = mongoose.connect(
|
2019-05-29 05:21:06 -04:00
|
|
|
Settings.mongo.url,
|
2023-02-28 08:26:18 -05:00
|
|
|
Settings.mongo.options
|
2020-11-02 05:53:57 -05:00
|
|
|
)
|
2023-02-28 08:26:18 -05:00
|
|
|
|
2023-03-01 07:12:23 -05:00
|
|
|
connectionPromise.then(mongooseInstance => {
|
|
|
|
Metrics.mongodb.monitor(mongooseInstance.connection.client)
|
|
|
|
})
|
|
|
|
|
2022-08-04 04:16:38 -04:00
|
|
|
addConnectionDrainer('mongoose', async () => {
|
|
|
|
await connectionPromise
|
|
|
|
await mongoose.disconnect()
|
|
|
|
})
|
2020-11-02 05:53:57 -05:00
|
|
|
|
2020-11-03 04:19:05 -05:00
|
|
|
mongoose.connection.on('connected', () =>
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('mongoose default connection open')
|
2020-11-03 04:19:05 -05:00
|
|
|
)
|
|
|
|
|
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', () =>
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug('mongoose default connection disconnected')
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if (process.env.MONGOOSE_DEBUG) {
|
|
|
|
mongoose.set('debug', (collectionName, method, query, doc) =>
|
2020-11-03 04:19:05 -05:00
|
|
|
logger.debug({ collectionName, method, query, doc }, 'mongoose debug')
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-05-11 07:50:53 -04:00
|
|
|
async function getMongoClient() {
|
|
|
|
const mongooseInstance = await connectionPromise
|
|
|
|
return mongooseInstance.connection.getClient()
|
|
|
|
}
|
|
|
|
|
2020-08-17 10:13:53 -04:00
|
|
|
async function getNativeDb() {
|
2020-11-03 04:19:05 -05:00
|
|
|
const mongooseInstance = await connectionPromise
|
|
|
|
return mongooseInstance.connection.db
|
2020-08-17 10:13:53 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:53 -04:00
|
|
|
mongoose.getMongoClient = getMongoClient
|
2020-08-17 10:13:53 -04:00
|
|
|
mongoose.getNativeDb = getNativeDb
|
2021-01-18 10:29:24 -05:00
|
|
|
mongoose.connectionPromise = connectionPromise
|
2020-08-17 10:13:53 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
module.exports = mongoose
|