/engineering-paved-path:onion-architecturefrom engineering-paved-path
Layering and dependency-rule conventions for TypeScript backends, with an optional pure-engine package. Use whenever editing or adding backend code; whenever adding a new module / use case / repository / adapter / outbound port / shared contract; whenever the question is *where should this live* on the backend (HTTP edge vs service vs repository vs adapter vs platform vs shared); whenever reviewing a backend PR for layer violations (ORM in a service, SDK import in a controller, `process.env` in a pure engine package, services importing other modules' services, SDK imports leaking past the adapter boundary). Trigger phrases: 'new endpoint', 'new module', 'where should this go', 'add a repository', 'add an adapter', 'wire up a port', 'consume X from a service', 'the engine needs to read', 'service is doing too much'. Layering and dependency placement ONLY — NOT NestJS runtime and wiring mechanics (use engineering-paved-path:nestjs-best-practices), NOT TypeORM query and migration mechanics (use engineering-paved-path:typeorm-patterns), NOT Zod schema mechanics (use engineering-paved-path:zod). For the client side use engineering-paved-path:frontend-architecture.
Trending skills
7-day visitors to each skill page
Ships in engineering-paved-path. 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.
Where code lives in a layered TypeScript backend, and which way dependencies are allowed to point. Layering decisions only — not framework runtime (engineering-paved-path:nestjs-best-practices), not ORM syntax (engineering-paved-path:typeorm-patterns), not Zod mechanics (engineering-paved-path:zod).
This is a pragmatic onion variant: feature-modular outside, onion-layered inside. It codifies a working pattern — it does not propose refactors to classical domain/application/infrastructure rings. The rules are stated in framework-neutral terms; the concrete examples in examples.md are written in NestJS + TypeORM, which is one instantiation of them, not a requirement. Where the project has one, a separate pure engine package holds domain computation with zero I/O. For sources and version history, see README.md.
This skill assumes nothing about your repository beyond what you tell it. When applying it, first locate (or ask for): the API package root, the module directory (src/modules/ or equivalent), the composition root, the shared contracts package, and whether a pure engine package exists. If the project documents its own layering rules, those take precedence.
Every rule below is written in the neutral column. Read it in whichever column your stack uses.
| Neutral term | NestJS + TypeORM | A framework-less / plain-HTTP setup |
|---|---|---|
| Feature module | a @Module() with its controller, service, repository and DTOs | a folder plus a route-registration function |
| HTTP edge | <name>.controller.ts | routes.ts |
| Application service | <name>.service.ts (@Injectable()) | service.ts |
| Repository | <name>.repository.ts holding an injected Repository<T> / DataSource | repository.ts holding the ORM client |
| Composition root | the module handed to NestFactory.create() — app.module.ts | a hand-written container module |
| DI mechanism | the framework container: providers, tokens, @Inject | a manually constructed container object |
| Port | an interface plus a DI token (Symbol) | an interface plus a container getter |
| Adapter | a provider bound to that token, usually through a factory | a class constructed in the container |
src/modules/Four rings, dependencies point inward.
| Ring | Lives in | Examples |
|---|---|---|
| Domain core — entities, value objects, pure invariants, shared contracts | the shared contracts package (shared/); the pure engine package (engine/src/) when present | domain types; port interfaces (LLMProvider, Embedder, PaymentGateway) |
| Application / use cases — orchestration, no I/O of its own | src/modules/<name>/service.ts; the engine's orchestration entry point | OrderService.placeOrder(), a pipeline that composes pure steps |
| Infrastructure adapters — DB, HTTP, LLM, third-party APIs, fs, secrets, time | src/adapters/<port>/*, src/modules/<name>/repository.ts, src/db/ | SDK-backed providers, ORM-backed repositories |
| Presentation — HTTP edge, request/response schemas, error envelope, streaming, cross-cutting enhancers | src/modules/<name>/controller.ts, src/platform/* | request/response schemas, the global exception filter, the event bus |
Dependency Rule (concrete, enforce in review):
_shared/. Never the ORM, never a third-party SDK, never db/, never an adapter directly.adapters/<port>/*.ts implements a port interface from the shared contracts package. May import third-party SDKs freely; never imports the HTTP framework, never imports db/, never imports a module's service or repository.node:fs, node:path, node:child_process or read process.env. Its only side effects go through injected ports.Every feature module is a folder under src/modules/<name>/ containing, at minimum:
modules/<name>/
<edge> # HTTP edge: request/response schemas + thin handlers
<service> # business logic
<repository> # ORM queries; module-internal
In NestJS that is <name>.controller.ts / <name>.service.ts / <name>.repository.ts, plus a <name>.module.ts that wires this module only.
Then register the module explicitly in one place — the composition root. Prefer static registration over filesystem autoload: the module list should be reviewable in one file. A feature module may import one other feature module it genuinely depends on; what the rule forbids is a second composition root — a feature module that gathers three or more others.
The handler does exactly three things, in this order:
Validation happens before the handler runs — a declared request schema, or a globally registered validation pipe. Never parse the raw body inside a handler; that bypasses the framework's error path and with it the whole error envelope.
The service receives its collaborators; it never constructs them.
export class OrderService {
constructor(
private readonly repo: OrderRepository,
@Inject(PAYMENTS) private readonly payments: PaymentGateway,
) {}
async placeOrder(workspaceId: string, input: PlaceOrderInput): Promise<Order> { /* … */ }
}
Never new SomeSdkProvider(...) in a service. When you need a side effect that does not yet have a port, add a port (§3) — do not import the SDK.
The only ORM importer in the module. Returns either module-internal row/entity types, or shared contract types when the data flows out to the service / API. Don't return raw ORM entity types from a service — map at the boundary (§5).
For multi-aggregate modules, split into repository/<aggregate>.repo.ts and compose them.
See the full skeleton in examples.md — Example 1: New module from scratch.
Add a port when any is true:
| Piece | Path | Notes |
|---|---|---|
| Port interface | the shared contracts package (e.g. shared/adapters.ts), or the module's ports/ when it is module-local | Shared contracts live in one place so both the API package and the engine can import them |
| DI token | <name>.tokens.ts — a Symbol or a const, never an inline string literal | An interface has no runtime identity; the token is what makes it injectable |
| Concrete adapter | src/adapters/<port>/<impl>.ts, or the module's adapters/ | E.g. adapters/embedder/openai.ts, adapters/payments/stripe.ts |
| Factory | <port>.factory.ts | Chooses the implementation and holds the safety checks |
| Mock for tests | src/adapters/mocks.ts | All port mocks live together |
| Binding | the composition root, or the owning module's provider list | Bind the token to the factory, not to the concrete class |
If a factory exists for an adapter, it exists for a reason — a credential check, an environment gate, a boot-time validation. A second module that binds the raw class to the same token silently loses that guard for everything routed through it. When another module needs a port that one module already provides through a factory, reuse the factory or import the exporting module. Never re-bind the concrete class.
A resolver behind a feature flag must throw a typed configuration error before constructing the SDK client. Match this pattern for any new adapter behind a flag — the gate fires before any third-party constructor runs, so a disabled integration cannot fail on a missing credential.
Know the operational cost: a fail-closed factory that requires a new variable means the process does not boot without it, not that the feature is off. That is the right default, and it makes the config change and the code change a single deploy.
The engine's entry point declares its inputs as a typed input struct whose port fields (e.g. llm: LLMProvider) come from the shared contracts package. The engine never constructs a provider — the host application does, and passes it in. If the engine ever needs a new outbound (e.g. a search index), add a port to the shared contracts, declare it on the input struct, and let the host pass a concrete adapter.
openai, stripe, @octokit/rest) directly → push behind an existing or new port.useClass in one module and by a factory in another → one binding, through the factory.See examples.md — Example 2: Adding a new outbound port.
The single most-asked question. Look it up here before writing.
| Kind of logic | Goes in | Anti-pattern |
|---|---|---|
| HTTP schema, request parsing, response shaping | the module's HTTP edge (DTOs / route schemas) | Business branching inside the handler body |
| Tenant/workspace scoping, identity pulled off the request | a shared context helper or parameter decorator at the top of the handler, then pass workspaceId to the service | Reading the request object inside the service |
| Orchestration of multiple adapters / repos / engine calls | the service | A repository calling another repository |
| Single-aggregate read/write, SQL | the repository (or repository/<aggregate>.repo.ts) | A query built outside a repository file |
| Reading configuration or the environment | a factory, or the configuration service consumed by a service | A repository reading process.env — pass the resolved value in as a parameter |
| Cross-cutting infrastructure: pricing, streaming, model routing, error taxonomy, resilience, run logging | src/platform/* | Re-implementing one of these inside a module |
| Pure domain computation (assembly, validation pipelines, map-reduce, output shaping) | the engine package when present, else a pure module in src/ | Burying pure computation inside an I/O-bound service (kills reuse from CLI/CI entry points) |
| Outbound HTTP / LLM / third-party / fs call | src/adapters/<port>/<impl>.ts (behind a port interface) | Importing the SDK directly in a service |
| Shared contract or port interface (cross-module / cross-package) | the shared contracts package | Redefining the same entity type in two packages |
| Module-internal helpers (DTO mappers, small pure functions) | modules/<name>/<name>.mappers.ts or a sibling file | Promoting one-shot helpers to platform/ |
| Mocks for tests | src/adapters/mocks.ts | A per-module __mocks__/ directory |
Schemas serve three distinct purposes — do not merge them.
| Purpose | Where it lives | Why separate |
|---|---|---|
| Transport DTO (HTTP request / response) | the module's HTTP edge (a DTO class, or a schema declared next to the route), or _shared/ for cross-module ids | Untrusted input; tolerant parsing; errors become a 4xx via the framework's validation path and the API error envelope |
| Domain invariant (the canonical shape of a core entity) | the shared contracts package | Single source of truth consumed by every package |
| Persistence shape (ORM entity / row type) | the entity definition, private to the repository | DB and domain shapes drift; keep ORM types from leaking up the rings |
Share atoms (enums, branded ids) by composition, not by reusing one whole schema across two purposes "to save lines". When the schemas have drifted enough that you're tempted to fork them, fork them.
For a /:id route where the id is a uuid, reuse a shared id schema. Only define a fresh one when the id is not a uuid (e.g. /providers/:id where the id is a provider name).
Where a database enum and a contract's literal union deliberately differ, convert through an exhaustive lookup map — and audit the read path as carefully as the write path; the write conversion is the visible one and creates a false sense of coverage.
If the project has a pure engine package, run this checklist before writing any code in it:
process.env reads — all inputs are function arguments.node:fs, node:path, node:child_process. (Pure node:crypto, node:util is fine.)src/index.ts (the entire public surface). Anything not re-exported there is package-internal.If you need to read a file, call an API, or look at an env var — that work belongs in the host application, which then plumbs the result into the engine as an argument. See examples.md — Example 3: Adding capability to the engine.
This invariant is what lets the same engine code run unchanged from other entry points (CLI, CI runner). Don't break it for ergonomics.
NestFactory.create(), services declare their collaborators as constructor parameters, and adapters are bound to tokens in a provider list. Working around that with a hand-rolled container is not "more onion" — it is a second graph nobody can see. The mechanics live in engineering-paved-path:nestjs-best-practices.new ConcreteAdapter() outside the composition root (or its factories) is the violation to look for in review.paths or workspace packages) — never relative ../../other-package/src/.....js suffixes on relative imports, apply them consistently within a package — half-migrated imports break at runtime, not compile time.A filename convention that splits hermetic tests from infrastructure-backed tests maps directly to the rings:
| Ring | Test type | Filename |
|---|---|---|
| Domain (engine, shared contracts) | Unit, hermetic, fast | *.spec.ts |
Application (src/modules/<name>/service) | Unit with fake ports | *.spec.ts |
Infrastructure (src/adapters/**, src/modules/<name>/repository) | Integration: real DB / recorded transport | *.integration-spec.ts |
| Presentation (the HTTP edge) | Smoke / contract against a booted app | *.e2e-spec.ts |
Rule of thumb: if a "unit" test needs a real DB or network, the dependency points outward — fix the layering, not the test. Push the dependency behind a port.
Reach for the shared src/adapters/mocks.ts rather than ad-hoc stubs.
One caveat the rings do not tell you: a hermetic test that constructs a service directly proves the logic and nothing about the wiring. Keep at least one test per module that compiles the real module graph — that is the only lane where a broken DI edge shows up.
Escape hatch for true CRUD with no branching:
Don't apply this escape hatch when:
One-line smell → one-line fix.
process.env read inside the pure engine → take the value as a function argument; the host reads env and plumbs it in.process.env read inside a repository → pass the resolved value as a method parameter; the repository owns DB I/O only.platform/, or factor a port that both depend on.context → call service → return → move the logic into the service.running rows leak otherwise.NEW BACKEND FILE → WHAT IS IT?
├── HTTP endpoint
│ └── modules/<name>/ ← the module's HTTP edge
│ (handler = context → service.call → return)
│
├── Business logic / orchestration
│ └── modules/<name>/ ← the service
│
├── DB query
│ ├── single aggregate → modules/<name>/ ← the repository
│ └── multi-aggregate → modules/<name>/repository/<aggregate>.repo.ts
│
├── Outbound SDK call (LLM / HTTP / third-party / fs)
│ ├── port interface → shared contracts package (adapters.ts)
│ ├── DI token → modules/<name>/<name>.tokens.ts
│ ├── concrete impl → src/adapters/<port>/<impl>.ts
│ ├── factory → <port>.factory.ts (safety checks live here)
│ ├── binding → the composition root / owning module
│ └── test mock → src/adapters/mocks.ts
│
├── Cross-cutting (pricing, streaming, model routing, error taxonomy, resilience)
│ └── src/platform/<topic>.ts
│
├── Shared contract / type / port interface
│ └── shared contracts package ({contracts,adapters}.ts)
│
├── Pure domain computation (assembly / validation / map-reduce / shaping)
│ └── engine package src/… (export from src/index.ts if a consumer should see it)
│
├── Module-internal helper / DTO mapper
│ └── modules/<name>/<name>.mappers.ts
│
└── Test
├── DB-backed → *.integration-spec.ts
├── Booted app → *.e2e-spec.ts
└── Anything else → *.spec.ts
A repository file belongs exclusively to its own module. No file outside modules/<name>/ may import it. This is the hard boundary that keeps ORM queries contained and prevents service-layer coupling from silently spreading across the tree.
Violation example:
// modules/webhooks/webhooks.service.ts
import { AgentsRepository } from '../agents/agents.repository'; // ❌ CRITICAL
import { SkillsRepository } from '../skills/skills.repository'; // ❌ CRITICAL
import { WebhooksRepository } from './webhooks.repository'; // ✅ own module — correct
Why it matters: when a service reaches directly into a sibling module's repository, it bypasses any caching, telemetry, error translation and invariant checks that the sibling service owns. It also makes schema migrations silently breaking across modules.
Correct alternatives (pick one):
| Need | Correct approach |
|---|---|
| Read data from another module | Call that module's service (no direct repo access) |
| Shared lookup at construction time | Define a port interface in the shared contracts package, bind a concrete impl in the composition root, inject it |
| Validation that requires data from two modules | Lift the validation into platform/ as a cross-cutting helper, or wire both services through the composition root |
How to detect: search for an import of …/modules/<name>/…repository in any file whose own path is outside modules/<name>/ — every hit is a violation.
Services must be callable from a unit test with zero HTTP machinery. If a service method signature includes the framework's request, response, or execution-context type — or any import from the framework's HTTP package — the service is coupled to the presentation layer. That is a hard ring violation: it makes unit testing impossible without a live server, and it breaks the invariant that the application layer has no knowledge of the delivery mechanism.
Violation shape:
// ❌ the edge passes framework objects through
async (req, res) => {
await service.process(req); // passes the request wholesale
await service.notify(res, id); // passes the response
}
Correct shape:
// ✓ the edge extracts domain values and passes those
async (req, res) => {
const { workspaceId, userId } = await getContext(req);
return service.process(workspaceId, req.body.input);
}
// ✓ the service knows nothing about the delivery mechanism
async process(workspaceId: string, input: ProcessInput): Promise<ProcessResult> { … }
If the service needs something from the request beyond what the context helper returns, extract it at the edge and pass the domain value explicitly.
For concrete code skeletons of each common task, see examples.md.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub yamchinsky/dev-digest-ai-marketplace --plugin engineering-paved-path12 skills in this plugin
/frontend-architectureFolder structure and code organization for React + Next.js (App Router) projects. Use this skill whenever the user asks where files should live (components, hooks, utils, helpers, lib, services, constants, types), how to break a component into smaller pieces, how to organize a feature folder, where to put business logic, when to use a barrel index, or how to lay out app/ in Next.js (route groups, private folders). Trigger on phrases like 'folder structure', 'project structure', 'where should I put', 'how to organize', 'where do constants go', 'utils vs helpers', 'app router structure'. Covers organization decisions only — NOT React hook rules or render patterns (use engineering-paved-path:react-best-practices) and NOT Next-specific runtime behavior (use engineering-paved-path:next-best-practices)./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./nestjs-best-practicesNestJS application patterns for TypeScript backends — modules and providers, dependency injection and injection tokens, the composition root, thin controllers, guards/interceptors/pipes/filters and their execution order, validation at the edge, ConfigModule and typed configuration, lifecycle hooks, exception filters, and testing with Test.createTestingModule. Use when building, wiring, reviewing or debugging a NestJS application: adding a module or a provider, resolving 'Nest can't resolve dependencies of X', choosing an injection scope, registering a global guard, shaping an error response, writing a testing module, or upgrading a NestJS major. Trigger terms: NestJS, Nest, @nestjs/common, @nestjs/core, app.module.ts, @Module, @Injectable, @Controller, provider, injection token, forwardRef, ValidationPipe, exception filter, APP_GUARD, Test.createTestingModule, nest build, nest start. NestJS runtime and wiring ONLY — NOT which layer a file belongs to (use engineering-paved-path:onion-architecture), NOT TypeORM query, migration or entity mechanics (use engineering-paved-path:typeorm-patterns), NOT PostgreSQL schema design (use engineering-paved-path:postgresql-table-design), NOT Zod schema mechanics (use engineering-paved-path:zod)./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/react-best-practicesModern React best practices and anti-pattern catalog (2025-26). Use when writing, reviewing, or refactoring React components, hooks, and state management. Covers component design, state patterns, hooks misuse, performance, data fetching, and code organization.Other plugins' skills for the same job