svrjs-nextjs-website/lib/db.ts
2024-06-22 14:53:31 +05:30

25 lines
612 B
TypeScript

import { MongoClient } from "mongodb";
// Ensure the environment variable is set
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
const options = {
// Add any SSL/TLS options if needed, for example:
// ssl: true,
// tlsAllowInvalidCertificates: true,
};
let client: MongoClient;
let clientPromise: Promise<MongoClient>;
declare global {
var _mongoClientPromise: Promise<MongoClient> | undefined;
}
client = new MongoClient(uri, options);
clientPromise = client.connect();
export default clientPromise;