Files
overleaf-cep/services/document-updater/app/js/mongodb.js
Eric Mc Sween 8f0913fafe Merge pull request #27936 from overleaf/em-unit-tests-mongo
Make Mongo available to unit tests in all services

GitOrigin-RevId: b65bbb69883d5bba31d09802b89f35bdc523fe4d
2025-08-25 08:05:19 +00:00

37 lines
897 B
JavaScript

// @ts-check
const Metrics = require('@overleaf/metrics')
const MongoUtils = require('@overleaf/mongo-utils')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb-legacy')
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const mongoDb = mongoClient.db()
const db = {
docs: mongoDb.collection('docs'),
docSnapshots: mongoDb.collection('docSnapshots'),
projects: mongoDb.collection('projects'),
}
async function healthCheck() {
const res = await mongoDb.command({ ping: 1 })
if (!res.ok) {
throw new Error('failed mongo ping')
}
}
Metrics.mongodb.monitor(mongoClient)
async function cleanupTestDatabase() {
await MongoUtils.cleanupTestDatabase(mongoClient)
}
module.exports = {
db,
ObjectId,
mongoClient,
healthCheck: require('node:util').callbackify(healthCheck),
cleanupTestDatabase,
}