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
+4 -9
View File
@@ -1,7 +1,7 @@
import { unstable_cache } from "next/cache";
import clientPromise from "@/lib/mongodb";
import { Club, ClubData } from "@/types";
import { collections, dbConnect } from "@/server/mongodb";
import { Club } from "@/types";
import { errorLog } from "@/utils/logger";
import ClubsDisplay from "./ClubsDisplay";
@@ -10,13 +10,8 @@ export const revalidate = 3600; // Revalidate cache every 6 hours
const getClubs = async () => {
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB");
const data = await db
.collection("clubs")
.find<ClubData>({})
.sort({ name: 1 })
.toArray();
await dbConnect();
const data = await collections.clubs!.find({}).sort({ name: 1 }).toArray();
return data
.filter((c) => !!c.coordinates)
+8 -9
View File
@@ -9,9 +9,9 @@ import { fr } from "date-fns/locale";
import { groupBy } from "lodash";
import { unstable_cache } from "next/cache";
import clientPromise from "@/lib/mongodb";
import { TournamentData } from "@/types";
import { TimeControl, Tournament, tcMap, tournamentDataSchema } from "@/types";
import { tournamentModelSchema } from "@/server/models/tournamentModel";
import { collections, dbConnect } from "@/server/mongodb";
import { TimeControl, Tournament, tcMap } from "@/types";
import { errorLog } from "@/utils/logger";
import TournamentsDisplay from "./TournamentsDisplay";
@@ -20,11 +20,10 @@ setDefaultOptions({ locale: fr });
const getTournaments = async () => {
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB");
const data = await db
.collection("tournaments")
.aggregate<TournamentData>([
await dbConnect();
const data = await collections
.tournaments!.aggregate([
{
$addFields: {
dateParts: {
@@ -52,7 +51,7 @@ const getTournaments = async () => {
.toArray();
const bad = data.filter((t) => {
const result = tournamentDataSchema.safeParse(t);
const result = tournamentModelSchema.safeParse(t);
if (result.success === false) {
console.log(JSON.stringify(result, null, 2));
console.log(JSON.stringify(t, null, 2));