What problem does it solve?
This Skill solves inconsistent and risky MongoDB usage in Node.js codebases by enforcing a clean Mongoose architecture that prevents connection explosions and slow, unindexed queries.
Core Features & Use Cases
- Single connection pattern: Centralizes
mongoose.connect() in src/lib/db.ts and reuses the cached connection to avoid exhausting MongoDB connection limits.
- Layered structure: Keeps schema/model definitions in
src/models/ and moves query logic into src/repositories/, with services validating input before repository calls.
- Performance and safety rules: Uses
.lean() for read queries where document methods aren’t needed, requires appropriate schema indexes for frequently queried fields, and mitigates NoSQL injection by validating inputs (e.g., ObjectId format).
- Use Case: Imagine you inherited a codebase where each route handler calls
mongoose.connect() and queries are scattered across models. Use this Skill to reorganize into src/lib, src/models, src/repositories, and src/services while standardizing .lean(), .populate(), and indexed schema fields.
Quick Start
Tell your agent: “Apply the Mongoose architecture rules to my repository: enforce a single cached MongoDB connection in src/lib/db.ts, move schema/model code into src/models, move queries into src/repositories using .lean() and .populate() appropriately, and ensure inputs are validated at the service boundary to prevent NoSQL injection.”