Best Practices
Best Practices
- Always use ModelRegistry.register() before initializing the database connection
- Use lifespan context manager for proper connection lifecycle
- Inherit from BaseDocument for all models to get auto-timestamps
- Create custom repository classes for business logic instead of mixing it with models
- Use transactions for operations that modify multiple documents
- Add indexes during model registration for frequently queried fields
- Implement pagination for list endpoints to avoid performance issues
- Use type hints for better IDE support and type checking