svrjs-nextjs-website/lib/db.ts

26 lines
612 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) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
2024-06-22 10:30:51 +02:00
const options = {
// Add any SSL/TLS options if needed, for example:
// ssl: true,
// tlsAllowInvalidCertificates: true,
};
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 {
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;