Advertisement
SejimFU

Untitled

Dec 5th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. var mongoose = require('mongoose');
  4. var Schema = mongoose.Schema;
  5.  
  6. var Comment = require('./comment_model');
  7. var User = require('./user_model');
  8.  
  9. var eventSchema = new Schema({
  10.   picture: {
  11.     type: String,
  12.     default: null
  13.   },
  14.   eventDate: {
  15.     type: Date
  16.   },
  17.   location: {
  18.     type: String,
  19.     required: true
  20.   },
  21.   distance: {
  22.     type: String,
  23.     required: true
  24.   },
  25.   pace: {
  26.     type: String,
  27.     required: true
  28.   },
  29.   author: {
  30.     type: Schema.Types.ObjectId,
  31.     ref: 'User'
  32.   },
  33.   likes: [{
  34.     type: Schema.Types.ObjectId,
  35.     ref: 'User'
  36.   }],
  37.   runners: [{
  38.     type: Schema.Types.ObjectId,
  39.     ref: 'User'
  40.   }],
  41.   comments: [{
  42.     type: Schema.Types.ObjectId,
  43.     ref: 'Comment'
  44.   }]
  45. }, {
  46.   timestamps: true //auto generation time of creation and last update
  47. });
  48.  
  49. //*********************************************************** */
  50. //            MIDDLEWARE REMOVE COMMENTS REF TO THIS EVENT
  51. // ************************************************************
  52.  
  53. // eventSchema.pre('remove', async function (next) {
  54. //   try {
  55. //     await Comment.remove({
  56. //       "_id": {
  57. //         $in: this.comments
  58. //       }
  59. //     });
  60.  
  61. //     next();
  62. //   } catch (err) {
  63. //     next(err)
  64. //   }
  65. // });
  66.  
  67. //*********************************************************** */
  68. //            MIDDLEWARE REMOVE REF IN USER TO THIS EVENT
  69. // ************************************************************
  70.  
  71. // eventSchema.pre('remove', async function (next) {
  72. //   try {
  73. //     var event = this;
  74. //     await User.update({
  75. //       createdEvent: event._id
  76. //     }, {
  77. //       $pull: {
  78. //         createdEvent: event._id
  79. //       }
  80. //     }, {
  81. //       multi: true
  82. //     });
  83. //     next();
  84. //   } catch (err) {
  85. //     next(err)
  86. //   }
  87. // });
  88.  
  89. module.exports = mongoose.model('Event', eventSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement