import { MongoClient, type Collection, type Db } from "mongodb"; import { env } from "../lib/env.js"; import type { AccountDocument, InviteDocument, SessionDocument, StrikeDocument, UserDocument } from "./types.js"; let client: MongoClient | null = null; let db: Db | null = null; function delay(ms: number): Promise { return new Promise((resolve) => { setTimeout(resolve, ms); }); } export async function connectMongo(): Promise { if (db) { return db; } let attempt = 0; for (;;) { try { client = new MongoClient(env.MONGODB); await client.connect(); db = client.db("revolt"); return db; } catch (error) { const waitMs = Math.min(1000 * 2 ** attempt, 30_000); attempt += 1; console.error( `MongoDB connection failed. Retrying in ${waitMs}ms.`, error ); await delay(waitMs); } } } export function getDb(): Db { if (!db) { throw new Error("MongoDB has not been connected yet"); } return db; } export function accounts(): Collection { return getDb().collection("accounts"); } export function users(): Collection { return getDb().collection("users"); } export function sessions(): Collection { return getDb().collection("sessions"); } export function invites(): Collection { return getDb().collection("invites"); } export function safetyStrikes(): Collection { return getDb().collection("safety_strikes"); } export async function closeMongo(): Promise { if (client) { await client.close(); client = null; db = null; } }