<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matteo Tomičić</title>
    <description>The latest articles on DEV Community by Matteo Tomičić (@mtomicic).</description>
    <link>https://dev.to/mtomicic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3918652%2Fd5cd62c7-5286-42b0-af76-1a510e1acdce.jpg</url>
      <title>DEV Community: Matteo Tomičić</title>
      <link>https://dev.to/mtomicic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mtomicic"/>
    <language>en</language>
    <item>
      <title>How I extended NestJS's architecture for projects that outgrow the basics</title>
      <dc:creator>Matteo Tomičić</dc:creator>
      <pubDate>Thu, 07 May 2026 20:20:51 +0000</pubDate>
      <link>https://dev.to/mtomicic/how-i-extended-nestjss-architecture-for-projects-that-outgrow-the-basics-4nlj</link>
      <guid>https://dev.to/mtomicic/how-i-extended-nestjss-architecture-for-projects-that-outgrow-the-basics-4nlj</guid>
      <description>&lt;p&gt;NestJS ships with a great starting point. The module/controller/service pattern is clean, approachable, and gets you productive fast. For most projects, it's exactly what you need.&lt;/p&gt;

&lt;p&gt;But as a project grows - more features, more teammates, more complexity - that foundation benefits from a bit more structure. Not a full rewrite, not a completely different paradigm. Just a few extra layers with clearly defined responsibilities.&lt;/p&gt;

&lt;p&gt;This is what I came up with, and why.&lt;/p&gt;




&lt;h2&gt;
  
  
  The challenge with scaling the default structure
&lt;/h2&gt;

&lt;p&gt;The built-in structure doesn't tell you where shared technical concerns go, how to organize services as they multiply, or where external integrations like email or file storage belong. That's not a flaw - the framework intentionally leaves those decisions to you.&lt;/p&gt;

&lt;p&gt;Without deliberate decisions, though, you'll eventually run into familiar growing pains: services that accumulate too many responsibilities, shared utilities scattered without a clear home, and new developers spending their first days just figuring out where things live.&lt;/p&gt;

&lt;p&gt;Full Clean Architecture solves these problems - but it also introduces a significant amount of abstraction that can feel heavy for a small or mid-sized project. Interfaces for things that will never change. Layers for layers' sake.&lt;/p&gt;

&lt;p&gt;I wanted something in between: structured enough to scale, simple enough to onboard quickly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Four layers, clear boundaries
&lt;/h2&gt;

&lt;p&gt;I extended the default structure with four top-level groups, each with a clearly defined role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  common/         ← global technical infrastructure
  core/           ← stable infrastructure services
  integrations/   ← wrappers around external services
  modules/        ← business logic, by domain
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value isn't in the folders themselves - it's in the rules each one follows.&lt;/p&gt;




&lt;h2&gt;
  
  
  common/ - technical, global, domain-free
&lt;/h2&gt;

&lt;p&gt;Everything the framework needs to function, with zero knowledge of your business domain: global exception filter, response formatting interceptors, guards, custom decorators, global middleware, shared enums, TypeScript extensions for the request object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The rule:&lt;/strong&gt; if something in &lt;code&gt;common/&lt;/code&gt; needs to know what "business" means in your application - it doesn't belong in &lt;code&gt;common/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is also where the global &lt;code&gt;ValidationPipe&lt;/code&gt; lives, configured with &lt;code&gt;whitelist: true&lt;/code&gt;. That single option automatically strips any properties not declared in a DTO before they reach your business logic. Security as a default, not something you bolt on later.&lt;/p&gt;




&lt;h2&gt;
  
  
  core/ - set it up, then forget it
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;core/&lt;/code&gt; holds the application's internal technical infrastructure - things with no business logic, configured once and rarely touched:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;core/
  config/
    env/        ← env validation and typed access to variables
  database/     ← connection, setup
  queue/        ← BullMQ configuration and provider
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key distinction from &lt;code&gt;integrations/&lt;/code&gt;: everything in &lt;code&gt;core/&lt;/code&gt; is internal to the application. No API calls to the outside world, no third-party dependencies - just the setup of what the application runs itself.&lt;/p&gt;

&lt;p&gt;The env setup deserves a special mention. Rather than accessing &lt;code&gt;process.env.SOMETHING&lt;/code&gt; throughout the codebase and discovering at runtime that a variable is missing, env gets validated at startup. Wrong type or missing value - the app won't start. A small investment that saves hours of production debugging.&lt;/p&gt;




&lt;h2&gt;
  
  
  integrations/ - one place for every external service
&lt;/h2&gt;

&lt;p&gt;This is the layer that's most often missing in NestJS projects, and its absence is felt as soon as the project scales. Every external service the application talks to gets its own module here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;integrations/
  mailer/     ← email sending (SendGrid, SMTP, ...)
  storage/    ← file uploads (S3, local disk, ...)
  stripe/     ← payment processing
  sms/        ← SMS notifications (Bind, ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mailer/&lt;/code&gt; isn't just "send an email" - it's a wrapper that owns the SMTP configuration, templates, and retry logic. That logic doesn't belong inside the feature module that happens to use it, but it also doesn't belong in &lt;code&gt;core/&lt;/code&gt; which should stay independent of external services.&lt;/p&gt;

&lt;p&gt;The practical payoff: when you swap SendGrid for AWS SES, you change one directory. Every feature module that uses &lt;code&gt;mailer/&lt;/code&gt; is completely unaffected.&lt;/p&gt;




&lt;h2&gt;
  
  
  modules/ - where the actual work happens
&lt;/h2&gt;

&lt;p&gt;Every feature module follows the same internal structure, whether it's orders, users, products, or anything else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;modules/[feature]/
  dtos/                  ← input DTOs with validation
  repositories/          ← direct database access
  services/
    internal/            ← shared services within the module
    use-cases/           ← one service = one operation
  types/                 ← TypeScript types specific to this module
  [feature].controller.ts
  [feature].module.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  dtos/ - validation at the entry point
&lt;/h2&gt;

&lt;p&gt;DTOs are classes that define what the controller is allowed to receive, with validation rules declared via &lt;code&gt;class-validator&lt;/code&gt; decorators. One DTO per operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dtos/
  create-order.dto.ts
  update-order.dto.ts
  cancel-order.dto.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Combined with &lt;code&gt;whitelist: true&lt;/code&gt; on the global &lt;code&gt;ValidationPipe&lt;/code&gt;, anything not explicitly declared in a DTO gets automatically rejected - without writing that check in every controller.&lt;/p&gt;




&lt;h2&gt;
  
  
  use-cases/ - one service, one operation
&lt;/h2&gt;

&lt;p&gt;This is the most impactful decision in the whole architecture. Each file represents exactly one operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use-cases/
  create-order.service.ts
  cancel-order.service.ts
  confirm-order.service.ts
  get-order-summary.service.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a product manager reports a bug in the cancel order flow - you know exactly which file to open. When someone new joins the project and asks where the order creation logic lives - &lt;code&gt;create-order.service.ts&lt;/code&gt; answers the question before they finish asking.&lt;/p&gt;

&lt;p&gt;But readability isn't the only win here. This structure directly improves testability. Writing a unit test for a service with a single, clear responsibility is straightforward. Writing a unit test for a service with fifteen methods and a tangle of dependencies is the kind of work that always gets pushed to "later" - and later never comes.&lt;/p&gt;

&lt;p&gt;With NestJS's DI system, each use-case service can be tested in isolation, mocking only the dependencies it actually needs.&lt;/p&gt;




&lt;h2&gt;
  
  
  internal/ vs use-cases/ - a distinction worth making
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;use-cases/&lt;/code&gt; are the services a controller calls directly. One entry point, one responsibility.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;internal/&lt;/code&gt; are services used by other services within the module, but never directly by the controller. Example: a service that calculates order pricing with discounts - logic shared across multiple use cases, but not itself an operation the controller triggers. Pulling it into &lt;code&gt;internal/&lt;/code&gt; avoids duplication and gives you one focused place to test that logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deliberate trade-offs
&lt;/h2&gt;

&lt;p&gt;Repositories live inside their module, not in a shared Infrastructure layer. The reason is pragmatic: the orders repository is only ever used by the orders module. Abstracting something that isn't shared adds files and indirection without benefit. If two modules eventually need the same repository, that's the moment to refactor - not a reason to architect for it on day one.&lt;/p&gt;

&lt;p&gt;This structure also deliberately skips repository interfaces and ORM abstraction layers. For a smaller project, that flexibility is largely theoretical. The code required to support it is real, and it carries a real maintenance cost.&lt;/p&gt;




&lt;h2&gt;
  
  
  The test that matters
&lt;/h2&gt;

&lt;p&gt;Architecture should solve the problems a project has today, not imaginary problems it might face in two years. For a small team, this structure delivers predictability, built-in validation safety, and testability - without the overhead of a full Clean Architecture setup.&lt;/p&gt;

&lt;p&gt;The question I ask about any architectural decision isn't "is this theoretically correct?" It's "will a new developer understand where things go and where to find them within 30 minutes?"&lt;/p&gt;

&lt;p&gt;If the answer is yes - the architecture is doing its job.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;How do you approach NestJS architecture on smaller projects? Do you separate external services into a dedicated &lt;code&gt;integrations/&lt;/code&gt; layer, or pull them directly into modules? I'd love to hear what's worked - and what hasn't - from your experience in production.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>typescript</category>
      <category>architecture</category>
      <category>node</category>
    </item>
  </channel>
</rss>
