mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
refactor(db): replace connectDb import paths and introduce new connect module
- Updated import paths for connectDb across various files to use the new centralized connect module. - Removed the old connectDb file to streamline the database connection logic. - Ensured all tests and models reference the new connection method for consistency.
This commit is contained in:
parent
7cf3f98475
commit
eb368fcb70
17 changed files with 36 additions and 42 deletions
|
|
@ -1,57 +0,0 @@
|
|||
require('dotenv').config();
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
if (!process.env.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(mongoUri = process.env.MONGO_URI) {
|
||||
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
|
||||
};
|
||||
|
||||
mongoose.set('strictQuery', true);
|
||||
cached.promise = mongoose.connect(mongoUri, opts).then((mongoose) => {
|
||||
return mongoose;
|
||||
});
|
||||
}
|
||||
cached.conn = await cached.promise;
|
||||
|
||||
return cached.conn;
|
||||
}
|
||||
|
||||
function getModels() {
|
||||
return cached.models;
|
||||
}
|
||||
module.exports = {
|
||||
connectDb,
|
||||
getModels,
|
||||
get models() {
|
||||
if (!cached.models) {
|
||||
throw new Error('Models not registered. ');
|
||||
}
|
||||
return cached.models;
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue