/engineering-practices:onion-architecturefrom engineering-practices
Onion Architecture enforcement for a Node.js backend stack such as Fastify 5, Drizzle ORM, Zod, TypeScript. Defines the four concentric layers (Domain, Application, Infrastructure, Presentation), the inward-only dependency rule, and how each tool maps to a layer. TRIGGER when: adding a new backend module, touching routes.ts / service.ts / repository.ts, "where does X go", "what layer", "can I import", adding new adapter, touching container.ts, service directly queries DB, route handler contains business logic, Drizzle schema imported in service. Does NOT cover: Fastify plugin API details (use fastify-best-practices), Drizzle query syntax (use drizzle-orm-patterns), Zod schema syntax (use zod), PostgreSQL schema design (use postgresql-table-design).
Ships in engineering-practices. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Dependencies point inward. The domain knows nothing about the outside world.
This skill enforces four concentric layers for every backend module in src/modules/. It answers "where does this code go?" and "can this file import from that file?" — not "how do I write this Drizzle query?" or "how does Fastify's plugin system work?".
modules/<name>/ directorythis.container.db.select() directlyservice.ts, repository.ts, or a helpersrc/platform/container.ts| Skill | What it covers (NOT this skill) |
|---|---|
fastify-best-practices | Plugin API, decorators, lifecycle hooks, serialization, SSE |
drizzle-orm-patterns | Query builder syntax, relations, migrations, transactions |
zod | Schema definition, safeParse, z.infer, coerce |
postgresql-table-design | Table design, indexes, constraints, pgvector |
Does it describe a business concept with invariants (entity, domain error)?
├── YES → domain-layer (vendor/shared/contracts/ or domain entities)
└── NO
Does it orchestrate a workflow — combining repo + adapter calls?
├── YES → application-layer (modules/*/service.ts)
└── NO
Does it talk to the DB, GitHub, LLM, Git, or any I/O?
├── YES → infrastructure-layer (modules/*/repository.ts or adapters/)
└── NO (HTTP shape, Fastify handler) → presentation-layer (modules/*/routes.ts)
I'm in... Can I import from...
──────────────────────────────────────────────────
domain/ → NOTHING outside domain
service.ts (application) → domain only (no DB, no Fastify)
repository.ts (infra) → domain + drizzle-orm + db/schema
routes.ts (presentation) → service.ts + Zod HTTP schemas only
container.ts → everything (composition root)
Does it validate HTTP request/response shape (params, body, reply)?
├── YES → top of routes.ts or _shared/schemas.ts (presentation layer)
└── NO
Does it check application-level preconditions in a service method?
├── YES → z.safeParse() inline in service.ts (application layer)
└── NO (domain invariant)
→ plain guard clause: if (!valid) throw new AppError(...)
(domain layer — NO Zod import)
modules/<name>/
├── routes.ts ← Fastify plugin: validate → service call → reply
├── service.ts ← Orchestration: no SQL, no adapter instantiation
├── repository.ts ← Drizzle queries: toDomain() + toDb() mappers
├── helpers.ts ← Pure transforms, DTO converters (optional)
└── constants.ts ← String/number literals (optional)
Inward-only dependencies — routes.ts can import service.ts; service.ts can NEVER import routes.ts. Violations break testability and create circular dependencies.
Domain knows nothing — vendor/shared/contracts/ and domain entities have zero imports from Fastify, Drizzle, Zod, or any adapter. If you need to add one, the code belongs in a different layer.
One composition root — all new ConcreteClass() calls live exclusively in src/platform/container.ts. Services receive a Container and pull what they need. Instantiating adapters in service constructors (e.g., new OpenAIProvider()) is forbidden.
Drizzle stays in infrastructure — $inferSelect and $inferInsert types never leave the repository file. Services and routes work with DTO types defined in vendor/shared/contracts/.
Thin routes — Fastify handlers do exactly three things: (1) validate input with Zod, (2) call one service method, (3) send the reply. Business rules, branching logic, and DB queries in routes are violations.
Validation is a stack — every layer validates what it owns. See validation-stack. Never duplicate validation across layers.
| File | What it covers |
|---|---|
| rules/layers.md | Four-layer model, project folder mapping, what belongs in each |
| rules/dependency-rule.md | Import allow-list per layer, violation examples |
| rules/domain-layer.md | Entities, domain errors, invariant guards, what NOT to import |
| rules/application-layer.md | Service pattern, orchestration rules, fire-and-forget |
| rules/infrastructure-layer.md | Repository pattern, data mappers, adapter placement |
| rules/presentation-layer.md | Fastify route rules, HTTP Zod schemas, error propagation |
| rules/validation-stack.md | Where each validation type lives across all four layers |
| rules/di-container.md | Container pattern, composition root, test doubles |
All 13 research URLs → references.md
references.md (opens in a new tab)rules/application-layer.md (opens in a new tab)rules/dependency-rule.md (opens in a new tab)rules/di-container.md (opens in a new tab)rules/domain-layer.md (opens in a new tab)rules/infrastructure-layer.md (opens in a new tab)rules/layers.md (opens in a new tab)rules/presentation-layer.md (opens in a new tab)rules/validation-stack.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ai-agentic-engineering-neo/dev-digest-ai-marketplace --plugin engineering-practices12 skills in this plugin
/drizzle-orm-patternsProvides comprehensive Drizzle ORM patterns for schema definition, CRUD operations, relations, queries, transactions, and migrations. Proactively use for any Drizzle ORM development including defining database schemas, writing type-safe queries, implementing relations, managing transactions, and setting up migrations with Drizzle Kit. Supports PostgreSQL, MySQL, SQLite, MSSQL, and CockroachDB./fastify-best-practicesGuides development of Fastify Node.js backend servers and REST APIs using TypeScript or JavaScript. Use when building, configuring, or debugging a Fastify application — including defining routes, implementing plugins, setting up JSON Schema validation, handling errors, optimising performance, managing authentication, configuring CORS and security headers, integrating databases, working with WebSockets, and deploying to production. Covers the full Fastify request lifecycle (hooks, serialization, logging with Pino) and TypeScript integration via strip types. Trigger terms: Fastify, Node.js server, REST API, API routes, backend framework, fastify.config, server.ts, app.ts./frontend-architectureCode organization and architecture decisions for React 19 + Next.js 15 App Router (TypeScript). WHERE code lives, not HOW to write it. Covers: project folder structure, feature-based organization, component splitting rules, constants/utils/helpers/services placement, business logic separation, naming conventions, state colocation, and Next.js Server/Client Component boundary decisions. TRIGGER when: "where to put", "folder structure", "project structure", "how to organize", "where does X live", "code organization", "feature folder", "colocation", "business logic", "utils vs helpers", "constants", "component splitting", "Server Component or Client Component", "Server Action or Route Handler", "where do I place". Does NOT cover: React hooks API, component implementation style, Next.js caching/metadata features, state management APIs (use react-best-practices, next-best-practices, or typescript-expert for those).
/mermaid-diagramCreate Mermaid diagrams in markdown. Use when the user wants to visualize workflows, architectures, API flows, data models, state machines, or system designs. Covers flowcharts, sequence diagrams, class diagrams, ER diagrams, state diagrams, and more./next-best-practicesNext.js best practices - file conventions, RSC boundaries, data patterns, async APIs, metadata, error handling, route handlers, image/font optimization, bundling/postgresql-table-designUse this skill when designing or reviewing a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features