Schema Module

The Schema module defines the structure, validation rules, indexes, and behavior of your data models. It provides a Mongoose-like API for defining schemas with TypeScript support.

What is it?

Schema is a class that defines the structure and validation rules for documents in a collection. It supports Mongoose-like shorthand syntax, field validation, indexes, virtual fields, getters/setters, and hooks.

Why do you need it?

  • Data Structure: Define the shape and types of your documents
  • Validation: Enforce data integrity with built-in and custom validators
  • Indexes: Define indexes for query performance
  • Defaults: Set default values for fields
  • Virtual Fields: Create computed properties
  • Hooks: Execute code before/after operations

How it works

When you create a Schema, it parses the definition and builds an internal structure with paths (fields), indexes, virtuals, and hooks. The schema is then used by Models to validate documents, apply defaults, and manage indexes. The schema supports both Mongoose-like shorthand (e.g., name: String) and full definitions (e.g., name: { type: String, required: true }).

Exports

  • Schema - Main Schema class
  • SchemaType - Helper class for type definitions
  • Discriminator - Schema discriminator for inheritance
  • SchemaDefinition - TypeScript type for schema definitions
  • SchemaFieldDefinition - TypeScript type for field definitions
  • IndexDefinition - TypeScript type for index definitions
  • VirtualFieldDefinition - TypeScript type for virtual fields
  • DiscriminatorOptions - TypeScript type for discriminator options

Schema Methods

  • new Schema(definition: SchemaDefinition, options?: { strict?: boolean }) - Create schema
  • schema.validate(data: any): Promise<ValidationResult> - Validate data
  • schema.validateSync(data: any): ValidationResult - Synchronous validation
  • schema.applyDefaults(data: any): any - Apply default values
  • schema.applyGetters(data: any): any - Apply getter functions
  • schema.applySetters(data: any): any - Apply setter functions
  • schema.index(definition: IndexDefinition): this - Add an index
  • schema.virtual(name: string, definition?: VirtualFieldDefinition): this - Add virtual field
  • schema.pre(hook: string, callback: Function): this - Add pre-hook
  • schema.post(hook: string, callback: Function): this - Add post-hook

SchemaType Static Methods

  • SchemaType.String(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Number(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Boolean(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Date(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Array(of?: SchemaFieldDefinition | string, options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Object(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition
  • SchemaType.Mixed(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinition

TypeScript Types

interface SchemaDefinition {
  [key: string]: SchemaFieldDefinition | string | Function;
}

interface SchemaFieldDefinition {
  type: string;
  required?: boolean;
  default?: any | (() => any);
  unique?: boolean;
  index?: boolean | string | IndexDefinition;
  validate?: { validator: Function; message?: string };
  min?: number;
  max?: number;
  enum?: any[];
  get?: (value: any) => any;
  set?: (value: any) => any;
}

interface IndexDefinition {
  type: 'persistent' | 'geo' | 'fulltext' | 'ttl';
  fields: string[];
  unique?: boolean;
  sparse?: boolean;
  expireAfterSeconds?: number;
}
📖 For usage examples and best practices: See Models & Schemas Guide for detailed schema documentation, examples, and best practices.