@everytravel/shared
Version:
A comprehensive shared package for Everytravel containing Mongoose models and CRUD operations for hotel booking, user management, and transaction handling. Updated with improved model syntax and enhanced error handling.
53 lines (48 loc) • 2.19 kB
JavaScript
import mongoose from 'mongoose';
const { Schema } = mongoose;
import { isEmail, trimIfString } from '../validation/index.js';
/**
* @module Host.model
* @description Mongoose schema for the Host collection.
*
* Fields:
* - firstName: Host's first name (String)
* - lastName: Host's last name (String)
* - email: Host's email address (String)
* - phoneNumber: Object containing countryCode and number (String)
* - password: Host's hashed password (String)
* - dateOfBirth: Host's date of birth (Date)
* - gender: Host's gender (String)
* - verifiedEmail: Whether the host's email is verified (Boolean)
* - country: Host's country (String)
* - address: Host's address (String)
* - profilePicUrl: URL to host's profile picture (String)
* - googleId: Google OAuth host ID (String)
* - facebookId: Facebook OAuth host ID (String)
* - appleId: Apple OAuth host ID (String)
* - propertiesCreated: Array of Property references (ObjectId[])
*/
const HostSchema = new Schema({
firstName: { type: String, set: trimIfString },
lastName: { type: String, set: trimIfString },
email: { type: String, set: (v) => (typeof v === 'string' ? v.trim().toLowerCase() : v), validate: { validator: (v) => !v || isEmail(v), message: 'Invalid email' }, index: true },
phoneNumber: {
countryCode: { type: String, set: trimIfString },
number: { type: String, set: trimIfString },
},
password: { type: String },
verifiedEmail: { type: Boolean, default: false },
verified: { type: Boolean, default: false },
country: { type: String, set: trimIfString },
address: { type: String, set: trimIfString },
profilePicUrl: { type: String, set: trimIfString },
// OAuth provider IDs
googleId: { type: String, index: true },
facebookId: { type: String, index: true },
appleId: { type: String, index: true },
propertiesCreated: [{ type: Schema.Types.ObjectId, ref: 'Property' }],
});
// Indexes to optimize lookups
// Note: field-level indexes already defined on email, googleId, facebookId, appleId
const Host = mongoose.model('Host', HostSchema);
export default Host;