33 lines
912 B
TypeScript
33 lines
912 B
TypeScript
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; |