2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-10-01 04:30:26 -04:00
|
|
|
const { MongoClient, ObjectId } = require('mongodb')
|
|
|
|
|
|
|
|
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?'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientPromise = MongoClient.connect(
|
|
|
|
Settings.mongo.url,
|
|
|
|
Settings.mongo.options
|
|
|
|
)
|
|
|
|
|
|
|
|
let setupDbPromise
|
|
|
|
async function waitForDb() {
|
|
|
|
if (!setupDbPromise) {
|
|
|
|
setupDbPromise = setupDb()
|
|
|
|
}
|
|
|
|
await setupDbPromise
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = {}
|
|
|
|
async function setupDb() {
|
2020-10-05 04:46:34 -04:00
|
|
|
const internalDb = (await clientPromise).db()
|
2020-10-01 04:30:26 -04:00
|
|
|
|
|
|
|
db.contacts = internalDb.collection('contacts')
|
2021-02-17 06:40:56 -05:00
|
|
|
db.deletedFiles = internalDb.collection('deletedFiles')
|
2020-10-01 04:30:26 -04:00
|
|
|
db.deletedProjects = internalDb.collection('deletedProjects')
|
|
|
|
db.deletedSubscriptions = internalDb.collection('deletedSubscriptions')
|
|
|
|
db.deletedUsers = internalDb.collection('deletedUsers')
|
|
|
|
db.docHistory = internalDb.collection('docHistory')
|
|
|
|
db.docHistoryIndex = internalDb.collection('docHistoryIndex')
|
|
|
|
db.docOps = internalDb.collection('docOps')
|
|
|
|
db.docSnapshots = internalDb.collection('docSnapshots')
|
|
|
|
db.docs = internalDb.collection('docs')
|
|
|
|
db.githubSyncEntityVersions = internalDb.collection(
|
|
|
|
'githubSyncEntityVersions'
|
|
|
|
)
|
|
|
|
db.githubSyncProjectStates = internalDb.collection('githubSyncProjectStates')
|
|
|
|
db.githubSyncUserCredentials = internalDb.collection(
|
|
|
|
'githubSyncUserCredentials'
|
|
|
|
)
|
|
|
|
db.institutions = internalDb.collection('institutions')
|
|
|
|
db.messages = internalDb.collection('messages')
|
|
|
|
db.migrations = internalDb.collection('migrations')
|
|
|
|
db.notifications = internalDb.collection('notifications')
|
|
|
|
db.oauthAccessTokens = internalDb.collection('oauthAccessTokens')
|
|
|
|
db.oauthApplications = internalDb.collection('oauthApplications')
|
|
|
|
db.oauthAuthorizationCodes = internalDb.collection('oauthAuthorizationCodes')
|
|
|
|
db.projectHistoryFailures = internalDb.collection('projectHistoryFailures')
|
|
|
|
db.projectHistoryLabels = internalDb.collection('projectHistoryLabels')
|
|
|
|
db.projectHistoryMetaData = internalDb.collection('projectHistoryMetaData')
|
|
|
|
db.projectHistorySyncState = internalDb.collection('projectHistorySyncState')
|
|
|
|
db.projectInvites = internalDb.collection('projectInvites')
|
|
|
|
db.projects = internalDb.collection('projects')
|
|
|
|
db.publishers = internalDb.collection('publishers')
|
|
|
|
db.rooms = internalDb.collection('rooms')
|
|
|
|
db.samlCache = internalDb.collection('samlCache')
|
|
|
|
db.samlLogs = internalDb.collection('samlLogs')
|
|
|
|
db.spellingPreferences = internalDb.collection('spellingPreferences')
|
2022-01-24 05:59:30 -05:00
|
|
|
db.splittests = internalDb.collection('splittests')
|
2020-10-01 04:30:26 -04:00
|
|
|
db.subscriptions = internalDb.collection('subscriptions')
|
|
|
|
db.systemmessages = internalDb.collection('systemmessages')
|
|
|
|
db.tags = internalDb.collection('tags')
|
|
|
|
db.teamInvites = internalDb.collection('teamInvites')
|
|
|
|
db.templates = internalDb.collection('templates')
|
|
|
|
db.tokens = internalDb.collection('tokens')
|
|
|
|
db.users = internalDb.collection('users')
|
|
|
|
db.userstubs = internalDb.collection('userstubs')
|
|
|
|
}
|
2020-10-07 09:17:10 -04:00
|
|
|
|
|
|
|
async function getCollectionNames() {
|
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
|
|
|
|
const collections = await internalDb.collections()
|
|
|
|
return collections.map(collection => collection.collectionName)
|
|
|
|
}
|
2020-10-01 04:30:26 -04:00
|
|
|
|
2022-01-10 06:36:18 -05:00
|
|
|
/**
|
|
|
|
* WARNING: Consider using a pre-populated collection from `db` to avoid typos!
|
|
|
|
*/
|
|
|
|
async function getCollectionInternal(name) {
|
|
|
|
const internalDb = (await clientPromise).db()
|
|
|
|
return internalDb.collection(name)
|
|
|
|
}
|
|
|
|
|
2020-10-01 04:30:26 -04:00
|
|
|
module.exports = {
|
|
|
|
db,
|
|
|
|
ObjectId,
|
2020-10-07 09:17:10 -04:00
|
|
|
getCollectionNames,
|
2022-01-10 06:36:18 -05:00
|
|
|
getCollectionInternal,
|
2021-04-27 03:52:58 -04:00
|
|
|
waitForDb,
|
2020-10-01 04:30:26 -04:00
|
|
|
}
|