monkeytype/backend/init/db.ts
Rizwan Mustafa 020ac5cb24
Add logging support from Winston with colored output (#2774)
* Converted initial outputs to color outputs

* Some more coloring

* Colored all error outputs

* Completed coloring of outputs

* Created basic logger instance

* Moved over to Winston for logging

* Remove unnnecessary stuff

* Added max file size

* Renamed log to logToDb

* minor refactor and added tab separation with timestamps

* Some changes. Thanks Bruception

* Created wrapper for logger

* Tiny refactor

* Some fixes

* Some fixes

* Update example env

* Remove general

* using default yellow, making colors bold

* removed square brackets

* removed square brackets

* using logger instead of console log

* updated timestamp format

* moved comment up

* Fixed typo

Co-authored-by: Miodec <bartnikjack@gmail.com>
2022-03-27 11:29:11 -04:00

80 lines
1.8 KiB
TypeScript

import {
AuthMechanism,
Collection,
Db,
MongoClient,
MongoClientOptions,
} from "mongodb";
import Logger from "../utils/logger";
class DatabaseClient {
static mongoClient: MongoClient;
static db: Db;
static collections: Record<string, Collection<any>> = {};
static connected = false;
static async connect(): Promise<void> {
const {
DB_USERNAME,
DB_PASSWORD,
DB_AUTH_MECHANISM,
DB_AUTH_SOURCE,
DB_URI,
DB_NAME,
} = process.env;
if (!DB_URI || !DB_NAME) {
throw new Error("No database configuration provided");
}
const connectionOptions: MongoClientOptions = {
connectTimeoutMS: 2000,
serverSelectionTimeoutMS: 2000,
};
if (DB_USERNAME && DB_PASSWORD) {
connectionOptions.auth = {
username: DB_USERNAME,
password: DB_PASSWORD,
};
}
if (DB_AUTH_MECHANISM) {
connectionOptions.authMechanism = DB_AUTH_MECHANISM as AuthMechanism;
}
if (DB_AUTH_SOURCE) {
connectionOptions.authSource = DB_AUTH_SOURCE;
}
this.mongoClient = new MongoClient(DB_URI, connectionOptions);
try {
await this.mongoClient.connect();
this.db = this.mongoClient.db(DB_NAME);
this.connected = true;
} catch (error) {
Logger.error(error.message);
Logger.error(
"Failed to connect to database. Exiting with exit status code 1."
);
process.exit(1);
}
}
static async close(): Promise<void> {
if (this.connected) {
await this.mongoClient.close();
}
}
static collection<T>(collectionName: string): Collection<T> {
if (!(collectionName in this.collections)) {
this.collections[collectionName] = this.db.collection<T>(collectionName);
}
return this.collections[collectionName];
}
}
export default DatabaseClient;