2025-01-21 18:41:27 +08:00
|
|
|
// lib/mongodb.ts
|
|
|
|
import { MongoClient, Db } from 'mongodb';
|
|
|
|
|
|
|
|
|
|
|
|
const uri = process.env.MONGODB_URI;
|
|
|
|
let client: MongoClient;
|
|
|
|
let clientPromise: Promise<MongoClient>;
|
|
|
|
|
|
|
|
if (!uri) {
|
|
|
|
throw new Error('Please add your Mongo URI to.env.local');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-10 19:20:12 +08:00
|
|
|
// client = new MongoClient(uri,{
|
|
|
|
// maxPoolSize: 5,
|
|
|
|
// maxConnecting:5,
|
|
|
|
// maxIdleTimeMS: 3000,
|
|
|
|
// waitQueueTimeoutMS: 1000
|
|
|
|
// });
|
|
|
|
// clientPromise = client.connect();
|
|
|
|
|
|
|
|
|
|
|
|
// export const getDb = async () => {
|
|
|
|
// const client = await clientPromise;
|
|
|
|
// return client.db('ai-bot')
|
|
|
|
// };
|
|
|
|
// export const getCollection = async (collection: string) => {
|
|
|
|
// const client = await clientPromise;
|
|
|
|
// return client.db('ai-bot').collection(collection);
|
|
|
|
// };
|
|
|
|
|
|
|
|
const ins = new MongoClient(uri,{
|
|
|
|
maxPoolSize: 5,
|
|
|
|
maxConnecting:5,
|
|
|
|
maxIdleTimeMS: 3000,
|
|
|
|
waitQueueTimeoutMS: 1000
|
|
|
|
}).db('ai-bot')
|
|
|
|
export const getDb = async () => {
|
|
|
|
return ins
|
2025-01-21 18:41:27 +08:00
|
|
|
};
|
|
|
|
export const getCollection = async (collection: string) => {
|
2025-02-10 19:20:12 +08:00
|
|
|
return ins.collection(collection);
|
2025-01-21 18:41:27 +08:00
|
|
|
};
|