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')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
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?'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-17 10:13:53 -04:00
|
|
|
const connectionPromise = mongoose.connect(
|
2019-05-29 05:21:06 -04:00
|
|
|
Settings.mongo.url,
|
2020-11-03 04:19:05 -05:00
|
|
|
Object.assign(
|
2020-11-02 09:57:41 -05:00
|
|
|
{
|
2020-11-03 04:19:05 -05:00
|
|
|
// mongoose specific config
|
|
|
|
config: { autoIndex: false },
|
|
|
|
// mongoose defaults to false, native driver defaults to true
|
|
|
|
useNewUrlParser: true,
|
|
|
|
// use the equivalent `findOneAndUpdate` methods of the native driver
|
2021-04-27 03:52:58 -04:00
|
|
|
useFindAndModify: false,
|
2020-11-02 09:57:41 -05:00
|
|
|
},
|
2020-11-03 04:19:05 -05:00
|
|
|
Settings.mongo.options
|
2020-11-02 09:57:41 -05:00
|
|
|
)
|
2020-11-02 05:53:57 -05:00
|
|
|
)
|
|
|
|
|
2020-11-03 04:19:05 -05:00
|
|
|
mongoose.connection.on('connected', () =>
|
|
|
|
logger.log('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) =>
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|