From 1029a67b5a46eadb9cce2ec2debf50ff51cffa36 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Thu, 8 Feb 2024 20:04:37 -0500 Subject: [PATCH] update models --- nextjs/models/party.ts | 46 ++++++++++++++++++++++++++++++------------ nextjs/models/user.ts | 9 +++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/nextjs/models/party.ts b/nextjs/models/party.ts index fdb5ad1..64f3204 100644 --- a/nextjs/models/party.ts +++ b/nextjs/models/party.ts @@ -1,29 +1,49 @@ -import mongoose from "mongoose"; -import { User } from "./user"; +import mongoose, { Types, Schema, Document } from "mongoose"; +import { User } from "./User"; -export interface Party extends mongoose.Document { +export interface Party extends Document { code: string; - players: User[]; + players: Types.DocumentArray; isStarted: boolean; gameMode: string; showPlayers: boolean; - winner: User; + winner?: User; } -const PartySchema = new mongoose.Schema( +const PartySchema = new Schema( { - code: { type: String, required: true, unique: true }, - players: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }], - isStarted: { type: Boolean, default: false }, - gameMode: { type: String, default: "Classic" }, - showPlayers: { type: Boolean, default: true }, + code: { + type: String, + required: true, + unique: true, + }, + players: [ + { + type: Types.ObjectId, + ref: "User", + }, + ], + isStarted: { + type: Boolean, + default: false, + }, + gameMode: { + type: String, + default: "Classic", + }, + showPlayers: { + type: Boolean, + default: true, + }, winner: { - type: mongoose.Schema.Types.ObjectId, + type: Types.ObjectId, ref: "User", default: null, }, }, - { timestamps: true }, + { + timestamps: true, + }, ); export default mongoose.models.Party || diff --git a/nextjs/models/user.ts b/nextjs/models/user.ts index 740b386..4774aaf 100644 --- a/nextjs/models/user.ts +++ b/nextjs/models/user.ts @@ -1,6 +1,5 @@ -import mongoose from "mongoose"; -import { Document, Schema, Types } from "mongoose"; -import { Party } from "./party"; +import mongoose, { Document, Schema, Types } from "mongoose"; +import { Party } from "./Party"; export interface User extends Document { email: string; @@ -65,7 +64,9 @@ const UserSchema = new Schema( }, }, }, - { timestamps: true }, + { + timestamps: true, + }, ); export default mongoose.models.User || mongoose.model("User", UserSchema);