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