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 00:53:32 +00:00
|
|
|
const DATABASENAME = process.env.MONGO_DATABASE_NAME
|
2022-11-06 23:19:59 +00:00
|
|
|
|
|
|
|
const URI =
|
|
|
|
`mongodb://${USERNAME}:${PASSWORD}@${HOSTNAME ?? 'localhost'}:3001`;
|
|
|
|
|
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 00:53:32 +00:00
|
|
|
return client.db(DATABASENAME);
|
2022-11-06 23:19:59 +00:00
|
|
|
}
|