Database collection models

This commit is contained in:
Timothy Armes
2024-04-10 15:50:30 +02:00
parent 6a3b58f02d
commit 1da389a24a
12 changed files with 139 additions and 77 deletions
+41
View File
@@ -0,0 +1,41 @@
import mongoDB, { MongoClient } from "mongodb";
import { TournamentModel } from "@/server/models/tournamentModel";
import { UserModel } from "@/server/models/userModel";
import { ZoneModel } from "@/server/models/zoneModel";
import { ClubModel } from "./models/clubModel";
export const collections: {
tournaments?: mongoDB.Collection<TournamentModel>;
clubs?: mongoDB.Collection<ClubModel>;
users?: mongoDB.Collection<UserModel>;
} = {};
if (!process.env.MONGODB_URI) {
throw new Error('Invalid environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
const options = {};
let client: mongoDB.MongoClient;
export let clientPromise: Promise<MongoClient>;
if (!process.env.MONGODB_URI) {
throw new Error("Please add your Mongo URI to .env.local");
}
client = new MongoClient(uri, options);
clientPromise = client.connect();
export async function dbConnect() {
await clientPromise;
const userData: mongoDB.Db = client!.db("userData");
collections.users = userData.collection("userData");
const tournamentData: mongoDB.Db = client!.db("tournamentsFranceDB");
collections.tournaments = tournamentData.collection("tournaments");
collections.clubs = tournamentData.collection("clubs");
}