wisesplit/lib/getMongoDBClient.ts

26 lines
608 B
TypeScript
Raw Normal View History

2022-11-07 01:33:24 +00:00
import { Db, MongoClient } from "mongodb";
2022-11-07 01:33:24 +00:00
const USERNAME = process.env.MONGO_USERNAME;
const PASSWORD = process.env.MONGO_PASSWORD;
const HOSTNAME = process.env.MONGO_HOSTNAME;
const DATABASE_NAME = process.env.MONGO_DATABASE_NAME;
const DATABASE_PORT = process.env.MONGO_DATABASE_PORT;
2022-11-07 01:33:24 +00:00
const URI = `mongodb://${USERNAME}:${PASSWORD}@${
HOSTNAME ?? "localhost"
}:${DATABASE_PORT}`;
let db: Db | null = null;
export const getMongoDBClient = async () => {
if (db) {
return db;
}
const client = new MongoClient(URI);
await client.connect();
db = client.db(DATABASE_NAME);
2022-11-07 01:33:24 +00:00
2022-11-07 01:10:15 +00:00
return db;
2022-11-07 01:33:24 +00:00
};