Skip to content
Snippets Groups Projects
Select Git revision
  • 283c79a88a0a6bdad6a9827f5d1a339e00077e84
  • main default
2 results

README.md

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;