all repos — stealth-developers @ 71c8aa9aceea9650a5e4904940b0848c1516ece0

src/database/connection.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
import mongoose from "mongoose";
import cfg from "../config.ts";
import lily from "../utils/logging.ts";

const logger = lily.child("database");

export async function connectDatabase() {
	try {
		logger.info("connecting to mongodb...");
		await mongoose.connect(cfg.data.mongodb.uri, {
			dbName: cfg.data.mongodb.database,
		});
		logger.info("connected to mongodb");
	} catch (error) {
		logger.error("failed to connect to mongodb:", error);
		process.exit(1);
	}
}

export async function disconnectDatabase() {
	await mongoose.disconnect();
	logger.info("disconnected from mongodb");
}