Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema;
  3.  
  4. mongoose.Promise = global.Promise;
  5.  
  6. const Address = new Schema({
  7.     addressLine1: String,
  8.     addressLine2: String,
  9.     city: String,
  10.     state: {
  11.         type: String,
  12.         enum: ['CA', 'FL', 'GA', 'NC', 'NV', 'NY', 'PR'],
  13.         default: 'GA'
  14.     },
  15.     zipcode: String,
  16.     latitude: 0,
  17.     longitude: 0
  18. });
  19.  
  20. const User = new Schema({
  21.     email: String,
  22.     password: String,
  23.     username: String,
  24.     img: String,  
  25.     isOwner: {
  26.         type: Boolean,
  27.         default: true
  28.     },
  29. });
  30.  
  31. const Home = new Schema({
  32.     owner: User,
  33.     img: String,
  34.     description: String,
  35.     address: Address,
  36.     rooms: 0,
  37.     guests: 0,
  38.     smoking: {
  39.         type: Boolean,
  40.         default: false
  41.     },
  42.     kids: {
  43.         type: Boolean,
  44.         default: false
  45.     },
  46.     pets: {
  47.         type: Boolean,
  48.         default: false
  49.     }
  50. });
  51.  
  52. const Availability = new Schema({
  53.     home: Home,
  54.     zipcode: String,
  55.     startDate: {
  56.         type: Date,
  57.         default: Date.now()
  58.     },
  59.     endDate: {
  60.         type: Date,
  61.         default: Date.now()
  62.     }
  63. });
  64.  
  65. Availability.pre('save', function(next) {
  66.    if (this.startDate > this.endDate) {
  67.        return next(new Error('Start date is after end date!'));
  68.    }
  69.     next();
  70. });
  71.  
  72. const AvailabilityModel = mongoose.model('Availability', Availability);
  73. const AddressModel = mongoose.model('Address', Address);
  74. const HomeModel = mongoose.model('Home', Home);
  75. const UserModel = mongoose.model('User', User);
  76.  
  77. module.exports = {
  78.     User: UserModel,
  79.     Home: HomeModel,
  80.     Address: AddressModel,
  81.     Availability: AvailabilityModel
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement