Skip to content
Snippets Groups Projects
Select Git revision
  • f5c291cd84fb9f64083d391a0f8bf9684a26f811
  • master default
  • goodpaths
  • movie-page
  • front-bilel
  • vieille-branche
  • octofront
  • branche-TP-de-Tom
8 results

movie.js

Blame
  • movie.js 1.42 KiB
    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: Number },
        poster_path: { type: String },
        release_date: { type: Date },
        title: { type: String },
        video: { type: Boolean },
        vote_average: { type: Number },
        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;