plan.model.js 977 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import mongoose from 'mongoose';
  2. import Meal from "./meal.model.js";
  3. import autopopulate from 'mongoose-autopopulate';
  4. const planSchema = new mongoose.Schema({
  5. userId: String,
  6. title: String,
  7. hasDate: Boolean,
  8. date: {
  9. type: Date,
  10. default: null
  11. },
  12. gotEverything: Boolean,
  13. missingIngredients: [{
  14. name: String,
  15. checked: Boolean,
  16. }],
  17. connectedMealId: String,
  18. createdAt: {
  19. type: Date,
  20. default: new Date()
  21. }
  22. }, {
  23. toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
  24. toObject: { virtuals: true } // So `toObject()` output includes virtuals
  25. });
  26. /** This virtual populates the entire Meal from its ID and adds it to the plan */
  27. planSchema.virtual('connectedMeal', {
  28. ref: 'Meal',
  29. localField: 'connectedMealId',
  30. foreignField: '_id',
  31. justOne: true,
  32. autopopulate: true,
  33. });
  34. planSchema.plugin(autopopulate);
  35. const Plan = mongoose.model('Plan', planSchema);
  36. export default Plan;