Skip to content
Snippets Groups Projects
Commit fc112b68 authored by Bilel El Yaagoubi's avatar Bilel El Yaagoubi
Browse files

db_structure

parent 9de50d69
No related branches found
No related tags found
1 merge request!7Populate and db structure
const mongoose = require("mongoose");
const MovieSchema = new mongoose.Schema(
{
adult: { type: Boolean },
backdrop_path: { type: String },
genre_ids: [Number],
id: { type: Number, required: true, unique: true },
original_language: { type: String },
original_title: { type: String },
overview: { type: String },
popularity: { type: String },
poster_path: { type: String },
release_date: { type: String },
title: { type: String },
video: { type: Boolean },
vote_average: { type: String },
vote_count: { type: Number },
},
{
toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals
}
);
MovieSchema.virtual("liking_users", {
ref: "UserModel",
localField: "_id", // The user _id should match the viewers field in movies
foreignField: "liked_movies",
});
MovieSchema.virtual("later_watchers", {
ref: "UserModel",
localField: "_id", // The user _id should match the viewers field in movies
foreignField: "to_see_later",
});
MovieSchema.virtual("masking_users", {
ref: "UserModel",
localField: "_id", // The user _id should match the viewers field in movies
foreignField: "masked_movies",
});
const MovieModel = mongoose.model(
"MovieModel",
MovieSchema,
"movies_populated"
);
module.exports = MovieModel;
const mongoose = require("mongoose");
const { Schema } = mongoose;
const UserSchema = new mongoose.Schema({
const UserSchema = new mongoose.Schema(
{
email: { type: String, required: true, unique: true },
firstName: { type: String },
lastName: { type: String },
});
is_admin: { type: Boolean },
liked_movies: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }],
to_see_later: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }],
masked_movies: [{ type: Schema.Types.ObjectId, ref: "MovieModel" }],
},
{
toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals
}
);
const UserModel = mongoose.model("UserModel", UserSchema, "users");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment