AgentSkillsCN

typegoose-modeling

按照项目规范,使用 Typegoose 创建 MongoDB 模型。适用于定义数据库 Schema、新建模型、处理嵌入式文档、为优化查询性能添加索引,或导出类型安全的模型实例时使用。

SKILL.md
--- frontmatter
name: typegoose-modeling
description: Create MongoDB models using Typegoose following project conventions. Use when defining database schemas, creating new models, working with embedded documents, adding indexes for query optimization, or exporting type-safe model instances.

Typegoose Modeling

Modular guides for creating type-safe MongoDB models with Typegoose and Mongoose, following project conventions.

Core Concepts

Models live in apps/server/src/db/models and use Typegoose to wrap Mongoose. Always use class-based schemas with @modelOptions and typed property decorators from apps/server/src/db/prop.ts.

CRITICAL: This project uses custom property decorators (objectIdProp, stringProp, etc.) instead of raw @prop decorators. These provide proper type inference automatically. See references/prop-decorators.md for complete details.

Quick Reference

GuideUse When
Prop DecoratorsUnderstanding available field decorators and type inference
Embedded DocumentsCreating nested objects and arrays of documents
IndexesOptimizing queries with single and compound indexes
Common PatternsEnums, timestamps, references, optional fields, collections
Best PracticesArchitecture patterns, keeping models pure, sharing schemas

Learning Path

Beginner: Start with Prop Decorators → Common Patterns → Basic model creation
Intermediate: Add Embedded Documents → Indexes for query optimization
Advanced: Best Practices → Architecture patterns

See Also

  • examples/basic-model.ts - Simple model structure
  • examples/model-with-indexes.ts - Single and compound indexes
  • examples/embedded-documents.ts - Nested documents