Files
overleaf-cep/tools/migrations/lib/mongodb.mjs
Brian Gough 729e0f5ac9 move migrations to shared location (#28306)
* fix: correct typedef for Document in helpers.mjs

* add move-migrations codemod

* update migration paths to use shared migrations directory

* move migrations to shared location

* fix: update Dockerfile and docker-compose.ci.yml to include migrations directory

* feat: add migrations tool to workspaces in package.json

* [monorepo] Fix order of docker ignore rules

* [web] remove unused docker ignore file

* [monorepo] replace old references to migrations folder

* [server-ce] copy migrations from new place

* [migrations] Inline web scripts

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] move three web scripts over

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] add missing collection

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] remove lodash dependency

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] avoid mongodb-legacy dependency

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [monorepo] run migrations from tools/migrations

Co-authored-by: Brian Gough <brian.gough@overleaf.com>

* [migrations] simplify migration for adding gitBridge feature to users

* [monorepo] run migrations from tests in all the services

* [migrations] add Jenkins pipeline for linting/formatting

* [monorepo] fixup running web migrations everywhere

* [monorepo] trigger Jenkins builds on changes to mongo migrations

* [migrations] add Jenkins pipeline for linting/formatting

* [monorepo] build scripts: update devDependencies before deps scanning

* [monorepo] build scripts: formerly depend on tools/migrations

* [monorepo] run eslint on .mjs files

* [migrations] enable more eslint rules and fix all the errors

* [rake] fix migrations:list task

---------

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>
GitOrigin-RevId: 14cf69cc1b9405bbc75adbb9a000e555500e0614
2025-10-16 08:07:37 +00:00

100 lines
4.1 KiB
JavaScript

import { ObjectId, ReadPreference, MongoClient } from 'mongodb'
import Settings from '@overleaf/settings'
export { ObjectId } from 'mongodb'
export const READ_PREFERENCE_PRIMARY = ReadPreference.primary.mode
export const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries
? ReadPreference.secondary.mode
: ReadPreference.secondaryPreferred.mode
const mongoClient = new MongoClient(Settings.mongo.url, Settings.mongo.options)
const internalDb = mongoClient.db()
export const db = {
contacts: internalDb.collection('contacts'),
deletedProjects: internalDb.collection('deletedProjects'),
deletedSubscriptions: internalDb.collection('deletedSubscriptions'),
deletedUsers: internalDb.collection('deletedUsers'),
dropboxEntities: internalDb.collection('dropboxEntities'),
dropboxProjects: internalDb.collection('dropboxProjects'),
docHistory: internalDb.collection('docHistory'),
docHistoryIndex: internalDb.collection('docHistoryIndex'),
docSnapshots: internalDb.collection('docSnapshots'),
docs: internalDb.collection('docs'),
feedbacks: internalDb.collection('feedbacks'),
githubSyncEntityVersions: internalDb.collection('githubSyncEntityVersions'),
githubSyncProjectStates: internalDb.collection('githubSyncProjectStates'),
githubSyncUserCredentials: internalDb.collection('githubSyncUserCredentials'),
globalMetrics: internalDb.collection('globalMetrics'),
grouppolicies: internalDb.collection('grouppolicies'),
groupAuditLogEntries: internalDb.collection('groupAuditLogEntries'),
institutions: internalDb.collection('institutions'),
messages: internalDb.collection('messages'),
migrations: internalDb.collection('migrations'),
notifications: internalDb.collection('notifications'),
oauthAccessTokens: internalDb.collection('oauthAccessTokens'),
oauthApplications: internalDb.collection('oauthApplications'),
oauthAuthorizationCodes: internalDb.collection('oauthAuthorizationCodes'),
projectAuditLogEntries: internalDb.collection('projectAuditLogEntries'),
projectHistoryChunks: internalDb.collection('projectHistoryChunks'),
projectHistoryFailures: internalDb.collection('projectHistoryFailures'),
projectHistoryGlobalBlobs: internalDb.collection('projectHistoryGlobalBlobs'),
projectHistoryLabels: internalDb.collection('projectHistoryLabels'),
projectHistorySizes: internalDb.collection('projectHistorySizes'),
projectHistorySyncState: internalDb.collection('projectHistorySyncState'),
projectInvites: internalDb.collection('projectInvites'),
projects: internalDb.collection('projects'),
publishers: internalDb.collection('publishers'),
rooms: internalDb.collection('rooms'),
samlCache: internalDb.collection('samlCache'),
samlLogs: internalDb.collection('samlLogs'),
spellingPreferences: internalDb.collection('spellingPreferences'),
splittests: internalDb.collection('splittests'),
ssoConfigs: internalDb.collection('ssoConfigs'),
subscriptions: internalDb.collection('subscriptions'),
surveys: internalDb.collection('surveys'),
systemmessages: internalDb.collection('systemmessages'),
tags: internalDb.collection('tags'),
teamInvites: internalDb.collection('teamInvites'),
tokens: internalDb.collection('tokens'),
userAuditLogEntries: internalDb.collection('userAuditLogEntries'),
users: internalDb.collection('users'),
onboardingDataCollection: internalDb.collection('onboardingDataCollection'),
scriptLogs: internalDb.collection('scriptLogs'),
}
export const connectionPromise = mongoClient.connect()
export async function getCollectionNames() {
const internalDb = mongoClient.db()
const collections = await internalDb.collections()
return collections.map(collection => collection.collectionName)
}
/**
* WARNING: Consider using a pre-populated collection from `db` to avoid typos!
*/
export async function getCollectionInternal(name) {
const internalDb = mongoClient.db()
return internalDb.collection(name)
}
export async function waitForDb() {
await connectionPromise
}
const mongodb = {
db,
ObjectId,
connectionPromise,
waitForDb,
getCollectionNames,
getCollectionInternal,
READ_PREFERENCE_PRIMARY,
READ_PREFERENCE_SECONDARY,
}
export default mongodb