Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const mongoose = require('mongoose');
  4. const Schema = mongoose.Schema;
  5. const ObjectId = Schema.Types.ObjectId;
  6.  
  7. const ProductSchema = new Schema({
  8.     description: {
  9.         type: String,
  10.         required: true
  11.     },
  12.     category: {
  13.         type: ObjectId,
  14.         ref: 'Category',
  15.         required: true
  16.     },
  17.     price: {
  18.         type: ObjectId,
  19.         ref: 'Price',
  20.         required: true
  21.     },
  22.     creation_date: {
  23.         type: Date,
  24.         default: new Date()
  25.     }
  26. });
  27.  
  28. ProductSchema.post('validate', doc => {
  29.     if (!doc._id) {
  30.         doc.creation_date = Date.now();
  31.     }
  32. });
  33.  
  34. module.exports = mongoose.model('Product', ProductSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement