Pattern: How should shared schemas be consumed by NestJS modules?
Documentation for Sessions
🎨 Pattern: How should shared schemas be consumed by NestJS modules?
Date: 2026-01-31
Time: 18:32:10 UTC
Session: editor2-75.21
Agent: backend
Confidence: 95%
Status: ✅ Documented
📋 Decision Context
Question:
How should shared schemas be consumed by NestJS modules?
Task: Session 75.15: Two-Layer Agent Intelligence & Preference Override System
🎯 Decision Made
Chosen:
Export schema class and SchemaFactory from backend-core, consumer registers with MongooseModule.forFeature() and adds service to providers
Options Considered:
- Create a BackendCoreModule that registers everything
- Export only types, keep services in each app
- Export schemas and services separately, consumer registers (chosen)
💭 Reasoning
This matches the existing pattern used by User schema and PlatformUserService. The key insight is that services using @InjectModel cannot be provided by a shared module - the schema must be registered in the consuming module's MongooseModule.forFeature(). This is because Mongoose binds models to specific connections, and each app has its own connection. The pattern ensures proper DI while keeping business logic in the shared service.
📁 Files Involved
libs/libs-api/backend-core/src/index.tsapis/backend-api/src/app/users/users.module.ts
🔗 Related
- Decision ID:
dev_1769884330376_2dc1t4m - Session: 75
- Workspace: editor2
Auto-generated by MCP Smart Consolidation