svrjs-nextjs-website/lib/db.ts

26 lines
607 B
TypeScript
Raw Normal View History

2024-06-20 19:46:48 +02:00
import { MongoClient } from "mongodb";
2024-06-22 10:30:51 +02:00
// Ensure the environment variable is set
2024-06-20 19:46:48 +02:00
if (!process.env.MONGODB_URI) {
2024-07-25 20:29:21 +02:00
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
2024-06-20 19:46:48 +02:00
}
const uri = process.env.MONGODB_URI;
2024-06-22 10:30:51 +02:00
const options = {
2024-07-25 20:29:21 +02:00
// Add any SSL/TLS options if needed, for example:
// ssl: true,
// tlsAllowInvalidCertificates: true,
2024-06-22 10:30:51 +02:00
};
2024-06-20 19:46:48 +02:00
2024-06-22 10:30:51 +02:00
let client: MongoClient;
2024-06-20 19:46:48 +02:00
let clientPromise: Promise<MongoClient>;
2024-06-22 10:30:51 +02:00
declare global {
2024-07-25 20:29:21 +02:00
var _mongoClientPromise: Promise<MongoClient> | undefined;
2024-06-20 19:46:48 +02:00
}
2024-06-22 10:30:51 +02:00
client = new MongoClient(uri, options);
clientPromise = client.connect();
2024-06-20 19:46:48 +02:00
export default clientPromise;