first commit
This commit is contained in:
33
lib/db/mongodb.ts
Normal file
33
lib/db/mongodb.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { MongoClient, Db } from 'mongodb';
|
||||
|
||||
if (!process.env.MONGODB_URI) {
|
||||
throw new Error('Please add your MongoDB URI to .env.local');
|
||||
}
|
||||
|
||||
const uri = process.env.MONGODB_URI;
|
||||
const options = {};
|
||||
|
||||
let client: MongoClient;
|
||||
let clientPromise: Promise<MongoClient>;
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const globalWithMongo = global as typeof globalThis & {
|
||||
_mongoClientPromise?: Promise<MongoClient>;
|
||||
};
|
||||
|
||||
if (!globalWithMongo._mongoClientPromise) {
|
||||
client = new MongoClient(uri, options);
|
||||
globalWithMongo._mongoClientPromise = client.connect();
|
||||
}
|
||||
clientPromise = globalWithMongo._mongoClientPromise;
|
||||
} else {
|
||||
client = new MongoClient(uri, options);
|
||||
clientPromise = client.connect();
|
||||
}
|
||||
|
||||
export async function getDatabase(): Promise<Db> {
|
||||
const client = await clientPromise;
|
||||
return client.db('librechat'); // Nom de votre base de données
|
||||
}
|
||||
|
||||
export default clientPromise;
|
||||
Reference in New Issue
Block a user