Allow independent pools to be used for mongo/mongoose

GitOrigin-RevId: c0c7d8e3388fd9402d7b22b70eda9455b4ecc205
This commit is contained in:
andrew rumble 2024-08-02 17:30:14 +01:00 committed by Copybot
parent 032deaf05c
commit e08c60424d

View file

@ -2,6 +2,7 @@ const mongodb = require('mongodb-legacy')
const OError = require('@overleaf/o-error')
const Settings = require('@overleaf/settings')
const Mongoose = require('./Mongoose')
const { addConnectionDrainer } = require('./GracefulShutdown')
// Ensure Mongoose is using the same mongodb instance as the mongodb module,
// otherwise we will get multiple versions of the ObjectId class. Mongoose
@ -13,7 +14,6 @@ if (Mongoose.mongo !== mongodb) {
)
}
const { getNativeDb } = Mongoose
const { ObjectId, ReadPreference } = mongodb
if (
@ -39,8 +39,31 @@ async function waitForDb() {
}
const db = {}
let clientPromise
async function getClient() {
if (!clientPromise) {
clientPromise = mongodb.MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
}
return await clientPromise
}
addConnectionDrainer('mongodb', async () => {
const client = await getClient()
await client.close()
})
async function getInternalDb() {
const client = await getClient()
return client.db()
}
async function setupDb() {
const internalDb = await getNativeDb()
const internalDb = await getInternalDb()
db.contacts = internalDb.collection('contacts')
db.deletedFiles = internalDb.collection('deletedFiles')
@ -101,14 +124,14 @@ async function setupDb() {
}
async function getCollectionNames() {
const internalDb = await getNativeDb()
const internalDb = await getInternalDb()
const collections = await internalDb.collections()
return collections.map(collection => collection.collectionName)
}
async function dropTestDatabase() {
const internalDb = await getNativeDb()
const internalDb = await getInternalDb()
const dbName = internalDb.databaseName
const env = process.env.NODE_ENV
@ -125,7 +148,7 @@ async function dropTestDatabase() {
* WARNING: Consider using a pre-populated collection from `db` to avoid typos!
*/
async function getCollectionInternal(name) {
const internalDb = await getNativeDb()
const internalDb = await getInternalDb()
return internalDb.collection(name)
}