Files
ChessHub/backend/models/user.js
T
Moon Patel 5786795e58 Changes in this commit:
- New model added for games played
- Game model ref added to User model
- New APIs created to create and get games
2023-07-06 23:47:20 +05:30

67 lines
1.6 KiB
JavaScript

const { Schema, default: mongoose } = require("mongoose");
const { String, ObjectId } = Schema.Types;
const userSchema = new Schema(
{
username: {
type: String,
unique: true,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
password_hash: {
type: String,
required: true,
},
fname: String,
lname: String,
location: String,
country: String,
friends: [
{
type: ObjectId,
ref: "User",
},
],
games: [
{
type: ObjectId,
ref: "Game",
},
],
},
{
virtuals: {
fullName: {
get() {
return this.fname + " " + this.lname;
},
},
},
methods: {
async getFriends() {
await this.populate("friends", "username email");
// console.log(this.friends);
return this.friends.map((friend) => {
return {
username: friend.username,
email: friend.email,
id: friend.id,
};
});
},
async getGames() {
await this.populate("games");
return this.games;
},
},
}
);
const User = mongoose.model("User", userSchema);
module.exports = { User };