Database Schema
Overview
This document provides comprehensive documentation of the MongoDB schema definitions, relationships, indexes, and data integrity rules for the Order Management module. The schemas are designed to support a multi-tenant SaaS architecture with proper data isolation and performance optimization.
Technology Stack
Database: MongoDB with Mongoose ODM
Schema Definition: Mongoose Schema with TypeScript typing
Indexing Strategy: Compound indexes for optimal query performance
Data Validation: Schema-level validation with business rules
Multi-tenancy: Tenant-based data isolation
Audit Trail: State history tracking for all entities
Core Schemas
Order Schema
Collection Name: order
Purpose: Main order entity containing order metadata, status, and relationships to services and products.
Schema Definition:
Indexes:
Business Rules:
Required Fields: Status is mandatory for all orders
Status Validation: Only valid enum values are accepted
Service Limits: Maximum 10 services per order (configurable)
Product Limits: Maximum 50 products per order (configurable)
Note Length: Business notes limited to 1000 characters
State History: All status changes are automatically tracked
Order Service Schema
Collection Name: orderservice
Purpose: Individual service within an order with appointment details, specialist assignments, and status tracking.
Schema Definition:
Indexes:
Business Rules:
Valid Order Reference: orderId must be a valid ObjectId
Active Service: Service snapshot must be from an active service
Future Appointments: Appointment start time must be in the future
Valid Time Range: End time must be after start time
Specialist Assignment: At least one specialist must be assigned
Note Limits: Customer notes limited to 500 characters
Order Product Schema
Collection Name: orderproduct
Purpose: Products included in an order, either standalone or associated with specific services.
Schema Definition:
Indexes:
Business Rules:
Active Product: Product snapshot must be from an active product
Valid Quantity: Quantity must be positive integer between 1-100
Valid References: Order and service IDs must be valid ObjectIds when provided
Sale Date: Sale date can only be set when product is delivered
Payment Consistency: Payment status must align with order payment status
Sub-Schemas
Order Service Details Schema
Purpose: Detailed appointment information including specialists, attendees, location, and attachments.
Schema Definition:
Notification Settings Schema
Purpose: Customer notification preferences for order updates and reminders.
Schema Definition:
Supporting Sub-Schemas
Specialist Schema
Attendant Schema
Location Schema
Attachment Schema
Index Strategy
Performance Optimization
Compound Indexes
Sparse Indexes
Text Indexes
Index Monitoring
Data Integrity Rules
Referential Integrity
Order Relationships
Cascade Operations
Data Validation
Business Rule Validation
Multi-field Validation
Multi-tenancy Support
Tenant Isolation
Schema Design
Query Middleware
Data Partitioning
Audit Trail and State History
State History Schema
Automatic History Tracking
Performance Optimization
Connection Management
Query Optimization
Memory Optimization
Backup and Recovery
Backup Strategy
Recovery Procedures
Monitoring and Maintenance
Health Checks
Maintenance Tasks
Last updated
Was this helpful?