| | require('dotenv').config(); |
| | const { isEnabled } = require('@librechat/api'); |
| | const { logger } = require('@librechat/data-schemas'); |
| |
|
| | const mongoose = require('mongoose'); |
| | const MONGO_URI = process.env.MONGO_URI; |
| |
|
| | if (!MONGO_URI) { |
| | throw new Error('Please define the MONGO_URI environment variable'); |
| | } |
| | |
| | const maxPoolSize = parseInt(process.env.MONGO_MAX_POOL_SIZE) || undefined; |
| | |
| | const minPoolSize = parseInt(process.env.MONGO_MIN_POOL_SIZE) || undefined; |
| | |
| | const maxConnecting = parseInt(process.env.MONGO_MAX_CONNECTING) || undefined; |
| | |
| | const maxIdleTimeMS = parseInt(process.env.MONGO_MAX_IDLE_TIME_MS) || undefined; |
| | |
| | const waitQueueTimeoutMS = parseInt(process.env.MONGO_WAIT_QUEUE_TIMEOUT_MS) || undefined; |
| | |
| | const autoIndex = |
| | process.env.MONGO_AUTO_INDEX != undefined |
| | ? isEnabled(process.env.MONGO_AUTO_INDEX) || false |
| | : undefined; |
| |
|
| | |
| | const autoCreate = |
| | process.env.MONGO_AUTO_CREATE != undefined |
| | ? isEnabled(process.env.MONGO_AUTO_CREATE) || false |
| | : undefined; |
| | |
| | |
| | |
| | |
| | |
| | let cached = global.mongoose; |
| |
|
| | if (!cached) { |
| | cached = global.mongoose = { conn: null, promise: null }; |
| | } |
| |
|
| | async function connectDb() { |
| | if (cached.conn && cached.conn?._readyState === 1) { |
| | return cached.conn; |
| | } |
| |
|
| | const disconnected = cached.conn && cached.conn?._readyState !== 1; |
| | if (!cached.promise || disconnected) { |
| | const opts = { |
| | bufferCommands: false, |
| | ...(maxPoolSize ? { maxPoolSize } : {}), |
| | ...(minPoolSize ? { minPoolSize } : {}), |
| | ...(maxConnecting ? { maxConnecting } : {}), |
| | ...(maxIdleTimeMS ? { maxIdleTimeMS } : {}), |
| | ...(waitQueueTimeoutMS ? { waitQueueTimeoutMS } : {}), |
| | ...(autoIndex != undefined ? { autoIndex } : {}), |
| | ...(autoCreate != undefined ? { autoCreate } : {}), |
| | |
| | |
| | |
| | |
| | |
| | }; |
| | logger.info('Mongo Connection options'); |
| | logger.info(JSON.stringify(opts, null, 2)); |
| | mongoose.set('strictQuery', true); |
| | cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => { |
| | return mongoose; |
| | }); |
| | } |
| | cached.conn = await cached.promise; |
| |
|
| | return cached.conn; |
| | } |
| |
|
| | module.exports = { |
| | connectDb, |
| | }; |
| |
|