mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-12 11:25:13 +00:00
39 lines
751 B
JavaScript
39 lines
751 B
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const bookingSchema = new mongoose.Schema({
|
|
tour: {
|
|
type: mongoose.Schema.ObjectId,
|
|
ref: 'Tour',
|
|
required: [true, 'Booking must belong to a Tour!']
|
|
},
|
|
user: {
|
|
type: mongoose.Schema.ObjectId,
|
|
ref: 'User',
|
|
required: [true, 'Booking must belong to a User!']
|
|
},
|
|
price: {
|
|
type: Number,
|
|
require: [true, 'Booking must have a price.']
|
|
},
|
|
createdAt: {
|
|
type: Date,
|
|
default: Date.now()
|
|
},
|
|
paid: {
|
|
type: Boolean,
|
|
default: true
|
|
}
|
|
});
|
|
|
|
bookingSchema.pre(/^find/, function(next) {
|
|
this.populate('user').populate({
|
|
path: 'tour',
|
|
select: 'name'
|
|
});
|
|
next();
|
|
});
|
|
|
|
const Booking = mongoose.model('Booking', bookingSchema);
|
|
|
|
module.exports = Booking;
|