Mongoose and MongoDB patterns — schema design, validation, indexes, virtuals,
99
99%
Does it follow best practices?
Impact
100%
1.11xAverage score across 5 eval scenarios
Passed
No known issues
A startup is building an event ticketing platform where users can browse events, purchase tickets, and manage their bookings. The engineering team has been asked to implement the core Mongoose data layer before the first sprint review. The platform needs to handle concurrent ticket purchases (with potential for duplicate email registrations), enforce valid ticket statuses throughout the purchase lifecycle, and make it easy to query upcoming events by category and date.
The team lead has asked you to write the schema definitions for three core models: Event, Ticket, and Session. The Event model holds event metadata and must support efficient queries that filter by category and sort by date. The Ticket model tracks individual ticket purchases tied to an event and must enforce valid status values. The Session model stores short-lived authentication sessions that should expire automatically after 7 days to keep the database clean.
Write a TypeScript file at schemas.ts that exports the three Mongoose schemas and models: Event, Ticket, and Session.
The schemas should reflect realistic field requirements for a ticketing platform. The Event model should have at minimum: a title, a category (from a fixed set of genres), a date, a venue, and a price. The Ticket model should have at minimum: a reference to the event, a buyer email, and a status field with a small set of valid values. The Session model should store a userId and a creation timestamp, and sessions should auto-expire after 7 days.
Include any indexes that would make common queries efficient, and enforce data integrity at the schema level.