‫Let's now, really quickly, create the model
‫for our bookings so that then, in the next video,
‫we can actually start creating some real bookings.
‫So, let's close up some of these
‫files here, also these folders.
‫Here is the models folder and so booking model.js.
‫And so this is, of course, gonna be very similar
‫to what we already did before,
‫so I will do this pretty quickly.
‫So as always, we need mongoose,
‫and we create our booking schema mongoose.Schema,
‫and now, remember how we said before
‫that we were going to use paragraph referencing
‫here on the bookings, so basically keeping a reference
‫to the tour and also to the user who booked the tour.
‫So that is very easy.
‫We did this many times before.
‫So mongoose.Schema.ObjectId, right?
‫And then the reference, and this one
‫is going to point to the tour model.
‫Then let's also say these are required,
‫Booking must belong to a Tour.
‫Now let's just grab this
‫and create the same thing for a user, okay?
‫So that's what we just said we were gonna do
‫keeping a reference of both the tour
‫that's being purchased and the user who does the purchase.
‫Then we also want to know the price
‫at which the purchase actually happened,
‫and so that's because the price might change
‫in the future, and so then we would no longer know
‫how much a certain user paid for a tour.
‫And so it's important to also have this here
‫in the booking, so this is going to be a number,
‫and we also require this,
‫Booking must have a price.
‫Next up, let's also create our time stamp createdAt,
‫which is a date,
‫and then simply add default
‫so that we don't have to do anything.
‫Finally, I also want to create a paid property,
‫and this one will be automatically set to true,
‫but this is just in case that, for example,
‫an administrator wants to create a booking
‫outside of Stripe.
‫So, for example, if a customer doesn't have a credit card
‫and wants to pay directly, like in a store with cash,
‫or something like that.
‫And in this case, an administrator might then use
‫our bookings API in order to basically
‫manually create a tour, and so that might then be paid
‫or not yet paid.
‫So just a very small detail here,
‫by the thought of that, and thought that it might be
‫interesting to include that here as well.
‫But of course, by default, it will be true
‫so that we don't have to do anything.
‫Now finally, let's then create a model out of this,
‫so booking is equal to mongoose.model,
‫and then, of course, our bookingSchema
‫and then exporting the whole thing,
‫is equal to Booking.
‫Now what we also want to do here
‫is to populate the tour and the user automatically
‫whenever there is a query, all right?
‫So remember how we used to do that
‫using query middleware.
‫So, right on the Schema.pre,
‫and then here or as usual regular expression
‫and whatever starts with find.
‫So here next,
‫and then this.populate,
‫and let's do it actually automatically
‫for both the user and the tours.
‫And in this case that's absolutely no problem
‫for performance, because there won't be
‫many calls to the bookings, because only guides and admins
‫will actually be allowed to do them, all right?
‫So basically, for a guide to check
‫was actually booked their tours.
‫So that's one of the use cases that I see
‫for this part of the API, okay?
‫So, again, this query will not happen that often
‫so we can easily populate all of this
‫without any problem.
‫So let's populate also the tour,
‫but here I actually want to only select the tour name.
‫So let's say path, tour, and select just the name, okay?
‫Now here we have some kind of error, all right,
‫so that should of course be mongoose not moongoose.
‫So, one more time, ESLint saved us here.
‫Oh, and actually there are more.
‫Save it one more time, and now we're good.
‫So that's actually it.
‫This is our bookings model
‫that we can now start using in the next video.