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 classSchemaType- Helper class for type definitionsDiscriminator- Schema discriminator for inheritanceSchemaDefinition- TypeScript type for schema definitionsSchemaFieldDefinition- TypeScript type for field definitionsIndexDefinition- TypeScript type for index definitionsVirtualFieldDefinition- TypeScript type for virtual fieldsDiscriminatorOptions- TypeScript type for discriminator options
Schema Methods
new Schema(definition: SchemaDefinition, options?: { strict?: boolean })- Create schemaschema.validate(data: any): Promise<ValidationResult>- Validate dataschema.validateSync(data: any): ValidationResult- Synchronous validationschema.applyDefaults(data: any): any- Apply default valuesschema.applyGetters(data: any): any- Apply getter functionsschema.applySetters(data: any): any- Apply setter functionsschema.index(definition: IndexDefinition): this- Add an indexschema.virtual(name: string, definition?: VirtualFieldDefinition): this- Add virtual fieldschema.pre(hook: string, callback: Function): this- Add pre-hookschema.post(hook: string, callback: Function): this- Add post-hook
SchemaType Static Methods
SchemaType.String(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.Number(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.Boolean(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.Date(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.Array(of?: SchemaFieldDefinition | string, options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.Object(options?: Partial<SchemaFieldDefinition>): SchemaFieldDefinitionSchemaType.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;
}