mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 03:10:15 +01:00
🔧 refactor: Move MongoDB connection logic to a separate module and make database name optional
This commit is contained in:
parent
cffd66054e
commit
6a811c708b
2 changed files with 53 additions and 5 deletions
53
api/lib/db/connect.js
Normal file
53
api/lib/db/connect.js
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
require('dotenv').config();
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
const MONGO_URI = process.env.MONGO_URI;
|
||||||
|
const MONGO_DB_NAME = process.env.MONGO_DB_NAME;
|
||||||
|
|
||||||
|
if (!MONGO_URI) {
|
||||||
|
throw new Error('Please define the MONGO_URI environment variable');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global is used here to maintain a cached connection across hot reloads
|
||||||
|
* in development. This prevents connections growing exponentially
|
||||||
|
* during API Route usage.
|
||||||
|
*/
|
||||||
|
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,
|
||||||
|
// useNewUrlParser: true,
|
||||||
|
// useUnifiedTopology: true,
|
||||||
|
// bufferMaxEntries: 0,
|
||||||
|
// useFindAndModify: true,
|
||||||
|
// useCreateIndex: true
|
||||||
|
};
|
||||||
|
|
||||||
|
if (MONGO_DB_NAME) {
|
||||||
|
opts.dbName = MONGO_DB_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const MONGO_URI = process.env.MONGO_URI;
|
const MONGO_URI = process.env.MONGO_URI;
|
||||||
const MONGO_DB_NAME = process.env.MONGO_DB_NAME;
|
|
||||||
|
|
||||||
if (!MONGO_URI) {
|
if (!MONGO_URI) {
|
||||||
throw new Error('Please define the MONGO_URI environment variable');
|
throw new Error('Please define the MONGO_URI environment variable');
|
||||||
|
|
@ -34,10 +33,6 @@ async function connectDb() {
|
||||||
// useCreateIndex: true
|
// useCreateIndex: true
|
||||||
};
|
};
|
||||||
|
|
||||||
if (MONGO_DB_NAME) {
|
|
||||||
opts.dbName = MONGO_DB_NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
mongoose.set('strictQuery', true);
|
mongoose.set('strictQuery', true);
|
||||||
cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
|
cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
|
||||||
return mongoose;
|
return mongoose;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue