Files
fjla-home/src/lib/db.ts
Fred Boniface eccc7e5a07 First commit
2025-01-24 16:01:59 +00:00

35 lines
1.0 KiB
TypeScript

import { MongoClient, ServerApiVersion } from "mongodb";
if (!process.env.MONGODB_URI) {
console.log(process.env.MONGODB_URI)
console.log("MONGODB_URI Not valid, auth will not work");
}
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017";
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
};
let client: MongoClient
if (process.env.NODE_ENV !== "production") {
const globalWithMongo = global as typeof globalThis & {
_mongoClient?: MongoClient
}
if (!globalWithMongo._mongoClient) {
globalWithMongo._mongoClient = new MongoClient(uri, options)
}
client = globalWithMongo._mongoClient
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options)
}
// Export a module-scoped MongoClient. By doing this in a
// separate module, the client can be shared across functions.
export default client