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:

  1. Required Fields: Status is mandatory for all orders

  2. Status Validation: Only valid enum values are accepted

  3. Service Limits: Maximum 10 services per order (configurable)

  4. Product Limits: Maximum 50 products per order (configurable)

  5. Note Length: Business notes limited to 1000 characters

  6. 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:

  1. Valid Order Reference: orderId must be a valid ObjectId

  2. Active Service: Service snapshot must be from an active service

  3. Future Appointments: Appointment start time must be in the future

  4. Valid Time Range: End time must be after start time

  5. Specialist Assignment: At least one specialist must be assigned

  6. 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:

  1. Active Product: Product snapshot must be from an active product

  2. Valid Quantity: Quantity must be positive integer between 1-100

  3. Valid References: Order and service IDs must be valid ObjectIds when provided

  4. Sale Date: Sale date can only be set when product is delivered

  5. 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?