<?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: Geampiere Jaramillo</title>
    <description>The latest articles on DEV Community by Geampiere Jaramillo (@geampiere).</description>
    <link>https://dev.to/geampiere</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%2F1177947%2F1283d083-5b08-41da-8dfb-a45c696100ce.jpeg</url>
      <title>DEV Community: Geampiere Jaramillo</title>
      <link>https://dev.to/geampiere</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/geampiere"/>
    <language>en</language>
    <item>
      <title>SQL vs NoSQL: Which one to choose for your next project?</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Thu, 16 Apr 2026 14:56:25 +0000</pubDate>
      <link>https://dev.to/geampiere/sql-vs-nosql-which-one-to-choose-for-your-next-project-4jag</link>
      <guid>https://dev.to/geampiere/sql-vs-nosql-which-one-to-choose-for-your-next-project-4jag</guid>
      <description>&lt;p&gt;When it comes to databases, two of the most popular options are SQL (Structured Query Language) and NoSQL (Not Only SQL). Both have advantages and disadvantages, and the choice between them depends on the specific needs of your application. In this blog, we'll explore the key differences between SQL and NoSQL databases, their use cases, and how to choose the right one for your project.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is SQL?
&lt;/h2&gt;

&lt;p&gt;SQL (Structured Query Language) refers to relational databases that organize data into related tables. SQL databases are highly structured, meaning that each piece of data must follow a predefined schema with clear relationships between tables. SQL databases are known for their ability to handle complex transactions and advanced queries.&lt;/p&gt;

&lt;p&gt;Examples of SQL databases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MySQL&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;SQL Server&lt;/li&gt;
&lt;li&gt;SQLite&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Main features of SQL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Relational Model&lt;/strong&gt;: Data is organized into tables with rows and columns. Each table can have relationships with other tables, and data can be retrieved using SQL queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fixed Schema&lt;/strong&gt;: The database schema must be defined before data is inserted, and tables must follow a predictable structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ACID&lt;/strong&gt;: SQL offers ACID transactions (Atomicity, Consistency, Isolation, Durability), meaning that database operations are safe and reliable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex Queries&lt;/strong&gt;: SQL databases allow for complex queries using joins, subqueries, aggregations, and other operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  SQL Use Cases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Enterprise applications&lt;/strong&gt; requiring &lt;strong&gt;high consistency&lt;/strong&gt; and &lt;strong&gt;transactions&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Banking systems&lt;/strong&gt; where data integrity is crucial.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Management applications&lt;/strong&gt; that require complex relationships between data (e.g., inventory management systems, ERPs).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Applications that require regulatory compliance&lt;/strong&gt; (such as GDPR, HIPAA) and need to maintain data integrity and control.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  What is NoSQL?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;NoSQL&lt;/strong&gt; (Not Only SQL) refers to &lt;strong&gt;non-relational&lt;/strong&gt; databases that offer greater flexibility in data storage and querying. Unlike SQL databases, NoSQL does not use a rigid schema or relationships between tables. Instead, it focuses on storing data in &lt;strong&gt;documents&lt;/strong&gt;, &lt;strong&gt;key-value pairs&lt;/strong&gt;, &lt;strong&gt;columns&lt;/strong&gt;, or &lt;strong&gt;graphs&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of NoSQL databases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB&lt;/strong&gt; (document-oriented)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redis&lt;/strong&gt; (key-value)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cassandra&lt;/strong&gt; (column-based)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Neo4j&lt;/strong&gt; (graph-based)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Main features of NoSQL:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Flexible Data Model&lt;/strong&gt;: NoSQL allows for the storage of &lt;strong&gt;unstructured&lt;/strong&gt; or &lt;strong&gt;semi-structured&lt;/strong&gt; data. Documents can have different fields without adhering to a fixed schema. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Horizontal Scalability&lt;/strong&gt;: NoSQL databases are designed to &lt;strong&gt;scale horizontally&lt;/strong&gt;, meaning they can be distributed across multiple servers to handle large volumes of data. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High Availability&lt;/strong&gt;: NoSQL often employs &lt;strong&gt;replication&lt;/strong&gt; and &lt;strong&gt;partitioning&lt;/strong&gt; techniques to ensure data is available even when some nodes fail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eventual Consistency&lt;/strong&gt;: Instead of guaranteeing strong consistency like SQL databases, NoSQL typically opts for &lt;strong&gt;eventual consistency&lt;/strong&gt;, meaning that data may not be fully synchronized across all database nodes at any given time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  NoSQL Use Cases:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Real-time web applications&lt;/strong&gt;, such as &lt;strong&gt;instant messaging&lt;/strong&gt; or &lt;strong&gt;push notifications&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Big Data&lt;/strong&gt; and &lt;strong&gt;real-time analytics&lt;/strong&gt; of large volumes of unstructured data (e.g., logs, IoT sensors).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Social networks&lt;/strong&gt; and &lt;strong&gt;content platforms&lt;/strong&gt; that require &lt;strong&gt;scalability&lt;/strong&gt; and flexibility in data storage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distributed&lt;/strong&gt; and &lt;strong&gt;high-availability systems&lt;/strong&gt; that need rapid failure recovery.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Diferencias clave entre SQL y NoSQL
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc35h1ou9ynza2ojp8ip.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffc35h1ou9ynza2ojp8ip.png" alt=" " width="800" height="584"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  When to choose SQL?
&lt;/h3&gt;

&lt;p&gt;You should choose an &lt;strong&gt;SQL&lt;/strong&gt; database when your application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Requires data integrity and ACID-compliant transactions&lt;/strong&gt; (e.g., financial or banking systems).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Has a well-defined data structure&lt;/strong&gt; with clear relationships between entities (users, products, orders).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requires complex queries&lt;/strong&gt; involving multiple tables with relationships (joins, subqueries, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Doesn't need massive horizontal scalability&lt;/strong&gt;, but rather vertical scalability (increasing the resources of a single server).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to choose NoSQL?
&lt;/h3&gt;

&lt;p&gt;You should choose a &lt;strong&gt;NoSQL&lt;/strong&gt; database when your application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Handles large volumes&lt;/strong&gt; of unstructured or semi-structured data, such as logs, IoT sensor data, or multimedia content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requires high horizontal scalability&lt;/strong&gt;, as NoSQL databases are designed to be distributed and handle large amounts of traffic and data.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Requires high availability&lt;/strong&gt; and fast recovery from failures (ideal for distributed applications with large numbers of concurrent users). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handle changing data&lt;/strong&gt; without requiring a rigid schema or predefined structure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The choice between &lt;strong&gt;SQL&lt;/strong&gt; and &lt;strong&gt;NoSQL&lt;/strong&gt; largely depends on the specific needs of your application. If your project requires complex transactions, data integrity, and well-defined relationships, &lt;strong&gt;SQL&lt;/strong&gt; is the best option. On the other hand, if your application needs to handle large volumes of data with a flexible schema and requires horizontal scalability, &lt;strong&gt;NoSQL&lt;/strong&gt; may be more suitable.&lt;/p&gt;

&lt;p&gt;There is no one-size-fits-all answer; some applications can even benefit from a hybrid approach, using both technologies depending on the requirements of each part of the system. For example, you could use an SQL database to handle financial transactions and a NoSQL database to store logs or real-time data. &lt;/p&gt;

&lt;p&gt;By understanding the strengths and limitations of both options, you can make a more informed decision about which type of database to use for your project.&lt;/p&gt;

</description>
      <category>database</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>SOLID Principles in NestJS</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Wed, 28 May 2025 15:12:09 +0000</pubDate>
      <link>https://dev.to/geampiere/solid-principles-in-nestjs-4ola</link>
      <guid>https://dev.to/geampiere/solid-principles-in-nestjs-4ola</guid>
      <description>&lt;p&gt;When building software, keeping your code organized and scalable is key. &lt;strong&gt;SOLID&lt;/strong&gt; is a set of principles that help improve code quality. In this blog, we’ll explore how to apply &lt;strong&gt;SOLID&lt;/strong&gt; in &lt;strong&gt;NestJS&lt;/strong&gt; to write clean, maintainable, and extendable code.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Single Responsibility Principle (SRP)
&lt;/h3&gt;

&lt;p&gt;Every class should have &lt;strong&gt;one responsibility&lt;/strong&gt;. This makes your code easier to understand and modify.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example in NestJS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class UserService {
  constructor(private readonly userRepository: UserRepository) {}

  createUser(data: CreateUserDto) {
    this.userRepository.save(data); // Responsible only for user logic
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;UserService&lt;/code&gt; only handles user logic. Validation and other concerns should be separated.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Open/Closed Principle (OCP)
&lt;/h3&gt;

&lt;p&gt;Classes should be open for &lt;strong&gt;extension&lt;/strong&gt;, but closed for &lt;strong&gt;modification&lt;/strong&gt;. You can add new features without changing existing code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example in NestJS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PaymentStrategy {
  processPayment(amount: number): void;
}

class CreditCardPayment implements PaymentStrategy {
  processPayment(amount: number) {
    console.log(`Processing $${amount} via Credit Card`);
  }
}

@Injectable()
export class PaymentService {
  constructor(private paymentStrategy: PaymentStrategy) {}

  executePayment(amount: number) {
    this.paymentStrategy.processPayment(amount);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add more payment methods by extending PaymentStrategy without changing PaymentService.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Liskov Substitution Principle (LSP)
&lt;/h3&gt;

&lt;p&gt;Derived classes should be &lt;strong&gt;substitutable&lt;/strong&gt; for their base classes without breaking the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example in NestJS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
  makeSound() {}
}

class Dog extends Animal {
  makeSound() {
    console.log('Bark');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use a &lt;strong&gt;Dog&lt;/strong&gt; wherever an &lt;strong&gt;Animal&lt;/strong&gt; is expected, without issues.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Interface Segregation Principle (ISP)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Clients should not be forced to depend on interfaces they don’t use. Split large interfaces into smaller, specific ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example in NestJS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PaymentProcessor {
  processPayment(amount: number): void;
}

class CreditCardProcessor implements PaymentProcessor {
  processPayment(amount: number) {
    console.log(`Processing $${amount} via Credit Card`);
  }
}

class PaypalProcessor implements PaymentProcessor {
  processPayment(amount: number) {
    console.log(`Processing $${amount} via PayPal`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each class only implements what it needs, making the code cleaner and easier to manage.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Dependency Inversion Principle (DIP)
&lt;/h3&gt;

&lt;p&gt;High-level modules should not depend on low-level modules. Both should depend on &lt;strong&gt;abstractions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example in NestJS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Logger {
  log(message: string): void;
}

@Injectable()
export class MyService {
  constructor(private readonly logger: Logger) {}

  executeAction() {
    this.logger.log('Action executed');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;MyService&lt;/code&gt; depends on the &lt;code&gt;Logger&lt;/code&gt; interface, not a specific implementation. This improves flexibility and testability.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Applying &lt;strong&gt;SOLID&lt;/strong&gt; principles in &lt;strong&gt;NestJS&lt;/strong&gt; helps you write clean, flexible, and maintainable code. These principles make it easier to test, scale, and evolve your applications without introducing bugs.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>backend</category>
      <category>node</category>
      <category>solidprinciples</category>
    </item>
    <item>
      <title>Design Patterns in NestJS</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Tue, 27 May 2025 15:41:55 +0000</pubDate>
      <link>https://dev.to/geampiere/design-patterns-in-nestjs-9h0</link>
      <guid>https://dev.to/geampiere/design-patterns-in-nestjs-9h0</guid>
      <description>&lt;p&gt;When developing backend applications with NestJS, one of the biggest challenges is creating an architecture that is scalable, maintainable, and extensible. One effective way to achieve this is by using design patterns, which allow us to build more flexible, decoupled, and reusable solutions.&lt;/p&gt;

&lt;p&gt;We will explore the most common design patterns you can implement in your NestJS applications, such as Dependency Injection, Singleton, Decorators, Strategy, and others. You will learn how to apply these patterns to improve code quality and long-term maintainability.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Dependency Injection (DI)
&lt;/h3&gt;

&lt;p&gt;Dependency Injection is one of the core principles of NestJS. This pattern allows classes to depend on other objects without creating them explicitly, which facilitates decoupling and component reusability.&lt;/p&gt;

&lt;p&gt;How it works in NestJS:&lt;br&gt;
NestJS uses TypeScript's dependency injection container. Whenever you inject a service into another via the constructor, NestJS handles the instantiation and passes the dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class MyService {
  getHello(): string {
    return 'Hello, World!';
  }
}

@Controller()
export class MyController {
  constructor(private readonly myService: MyService) {}

  @Get()
  getHello(): string {
    return this.myService.getHello();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling&lt;/strong&gt;: Classes do not need to know the implementation of their dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Easy to scale and maintain when adding new dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: Enables mocking services during unit testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Singleton
&lt;/h3&gt;

&lt;p&gt;The Singleton pattern ensures a class has only one instance and provides a global point of access. NestJS uses this by default for services.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class MySingletonService {
  private counter = 0;

  increment() {
    this.counter++;
    return this.counter;
  }

  getCounter() {
    return this.counter;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: Maintains shared state throughout the application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource Management:&lt;/strong&gt; Ideal for managing resources like database connections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Decorator Pattern
&lt;/h3&gt;

&lt;p&gt;Decorators are fundamental in NestJS. They allow you to add functionality to classes and methods without modifying their structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Controller('users')
export class UserController {
  @Get(':id')
  getUser(@Param('id') id: string): string {
    return `User ID: ${id}`;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: Decorators make code more readable and organized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Adds functionality in a modular way.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Strategy Pattern
&lt;/h3&gt;

&lt;p&gt;The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It is useful when you need different behaviors for a service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PaymentStrategy {
  pay(amount: number): string;
}

@Injectable()
export class CreditCardPayment implements PaymentStrategy {
  pay(amount: number): string {
    return `Paid ${amount} using Credit Card`;
  }
}

@Injectable()
export class PayPalPayment implements PaymentStrategy {
  pay(amount: number): string {
    return `Paid ${amount} using PayPal`;
  }
}

@Injectable()
export class PaymentService {
  constructor(private readonly paymentStrategy: PaymentStrategy) {}

  processPayment(amount: number): string {
    return this.paymentStrategy.pay(amount);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: Easily change system behavior without altering the structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extensibility&lt;/strong&gt;: New strategies can be added without affecting existing code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Observer Pattern
&lt;/h3&gt;

&lt;p&gt;The Observer pattern is useful when multiple objects need to be notified of changes in another object's state. In NestJS, this can be implemented using events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class MyService {
  private eventEmitter = new EventEmitter();

  constructor() {
    this.eventEmitter.on('userCreated', (user) =&amp;gt; {
      console.log('User Created:', user);
    });
  }

  createUser(user: any) {
    this.eventEmitter.emit('userCreated', user);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling&lt;/strong&gt;: Observers react to events without needing to know the subject's internal logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Easy to add new functionalities by subscribing to events.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Command Pattern
&lt;/h3&gt;

&lt;p&gt;The Command pattern encapsulates a request as an object, allowing you to parameterize clients with different requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateUserCommand {
  constructor(public readonly username: string, public readonly email: string) {}
}

@Injectable()
export class UserService {
  executeCreateUserCommand(command: CreateUserCommand): void {
    console.log(`Creating user with username: ${command.username} and email: ${command.email}`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling&lt;/strong&gt;: Separates the request from its execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability&lt;/strong&gt;: Commands can be reused in different parts of the app.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Design patterns are powerful tools for building scalable and maintainable backend applications. In NestJS, you can take full advantage of these patterns to structure your code in a modular, decoupled, and efficient way.&lt;/p&gt;

&lt;p&gt;The key is to understand when and how to apply each pattern based on your project’s requirements. Start experimenting with them and see how your NestJS architecture improves.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>programming</category>
      <category>backend</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>Operators and Methods to Manipulate Arrays in JavaScript</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Tue, 13 May 2025 15:29:44 +0000</pubDate>
      <link>https://dev.to/geampiere/operators-and-methods-to-manipulate-arrays-in-javascript-3f7h</link>
      <guid>https://dev.to/geampiere/operators-and-methods-to-manipulate-arrays-in-javascript-3f7h</guid>
      <description>&lt;p&gt;When working with JavaScript—whether on the frontend with frameworks like React or on the backend with NestJS—one of the most common data structures is the array. Arrays allow us to store collections of data and offer a wide range of operators and methods to manipulate them efficiently.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore the main operators and methods you can use with arrays to transform, filter, combine, and analyze information.&lt;/p&gt;




&lt;h3&gt;
  
  
  📌 What Is an Array?
&lt;/h3&gt;

&lt;p&gt;An array in JavaScript is an ordered collection of items. It can contain any type of data: numbers, strings, objects, or even other arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];
const mixed = [1, 'text', true, null, [6, 7]];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🧹 Useful Operators for Arrays
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Spread Operator (...)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Allows cloning or combining arrays in a very readable way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b]; // [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Destructuring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Extracts individual elements from an array easily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [first, second] = [10, 20, 30]; // first = 10, second = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🔄 Common Methods to Manipulate Arrays
&lt;/h3&gt;

&lt;h3&gt;
  
  
  🔹 Mutation Methods (modify the original array)
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;push()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds elements to the end.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pop()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes the last element.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;shift()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes the first element.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;unshift()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds elements to the beginning.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;splice()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds/removes elements at a position.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sort()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Sorts the array.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;reverse()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reverses the array order.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ['apple', 'banana'];
fruits.push('orange'); // ['apple', 'banana', 'orange']
fruits.splice(1, 1, 'pear'); // ['apple', 'pear', 'orange']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔹 Iteration &amp;amp; Transformation Methods
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Primary Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;forEach()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Executes a function for each element.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Transforms elements and returns a new array.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;filter()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns elements that meet a condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;reduce()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reduces array to a single value.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;find()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns the first matching element.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;some()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Checks if some elements match a condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;every()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Checks if all elements match a condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(n =&amp;gt; n * 2); // [2, 4, 6, 8, 10]
const evens = numbers.filter(n =&amp;gt; n % 2 === 0); // [2, 4]
const total = numbers.reduce((acc, n) =&amp;gt; acc + n, 0); // 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔹 Search Methods
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const letters = ['a', 'b', 'c'];
letters.includes('b');  // true
letters.indexOf('c');   // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔹 Conversion Methods
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const words = ['hello', 'world'];
words.join(' ');  // 'hello world'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🧠 Tip: Non-Destructive vs Destructive Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-destructive:&lt;/strong&gt; &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;slice()&lt;/code&gt;, &lt;code&gt;concat()&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Destructive&lt;/strong&gt;: &lt;code&gt;push()&lt;/code&gt;, &lt;code&gt;pop()&lt;/code&gt;, &lt;code&gt;splice()&lt;/code&gt;, &lt;code&gt;sort()&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prefer non-destructive methods when aiming for immutability—a key principle in functional programming and modern frameworks.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔪 Bonus: Advanced Methods
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;flat()&lt;/code&gt; and &lt;code&gt;flatMap()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nested = [1, [2, 3], [4, 5]];
nested.flat(); // [1, 2, 3, 4, 5]

const duplicated = [1, 2, 3].flatMap(n =&amp;gt; [n, n]); // [1, 1, 2, 2, 3, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🚀 Conclusion
&lt;/h3&gt;

&lt;p&gt;Mastering array manipulation is essential for any JavaScript developer. Whether you're building APIs with NestJS or transforming data in the frontend, understanding these operators and methods will help you write cleaner, more efficient code.&lt;/p&gt;

&lt;p&gt;Do you have a favorite array method or a cool tip? Let me know in the comments!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Method Binding in NestJS: What It Is and When to Use It</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Thu, 08 May 2025 15:03:34 +0000</pubDate>
      <link>https://dev.to/geampiere/method-binding-in-nestjs-what-it-is-and-when-to-use-it-3f8j</link>
      <guid>https://dev.to/geampiere/method-binding-in-nestjs-what-it-is-and-when-to-use-it-3f8j</guid>
      <description>&lt;p&gt;In &lt;strong&gt;NestJS&lt;/strong&gt; development—a Node.js framework focused on modular architecture and class-based design—one concept that can confuse developers is &lt;strong&gt;method binding&lt;/strong&gt;. This blog post explains what it is, when it matters, and how to handle it properly.&lt;/p&gt;




&lt;h3&gt;
  
  
  What Is Method Binding?
&lt;/h3&gt;

&lt;p&gt;In JavaScript and TypeScript, the value of &lt;code&gt;this&lt;/code&gt; inside a method can change depending on how and where the method is called. &lt;strong&gt;Method binding&lt;/strong&gt; refers to ensuring that &lt;code&gt;this&lt;/code&gt; inside the method always points to the correct class instance, especially when the method is passed as a callback.&lt;/p&gt;




&lt;h3&gt;
  
  
  Classic Problem: Callback with &lt;code&gt;setInterval&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
@Injectable()
export class MyService {
  private count = 0;

  increment() {
    this.count++;
    console.log('Count:', this.count);
  }

  start() {
    setInterval(this.increment, 1000); // 'this' is undefined here
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;this.increment&lt;/code&gt; loses its class context when passed as a callback to &lt;code&gt;setInterval&lt;/code&gt;, resulting in an error or unexpected behavior.&lt;/p&gt;




&lt;h3&gt;
  
  
  Common Solutions in NestJS
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Use bind(this)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;start() {
  setInterval(this.increment.bind(this), 1000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;bind&lt;/code&gt; ensures that &lt;code&gt;this&lt;/code&gt; inside &lt;code&gt;increment&lt;/code&gt; continues to refer to the &lt;code&gt;MyService&lt;/code&gt; instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Use Arrow Functions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;increment = () =&amp;gt; {
  this.count++;
  console.log('Count:', this.count);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrow functions do not have their own &lt;code&gt;this&lt;/code&gt;; they inherit it from their creation context, which avoids the problem without needing &lt;code&gt;bind&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  When You Need to Bind Methods
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When passing methods as callbacks to async functions like &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;, or event handlers.&lt;/li&gt;
&lt;li&gt;When using methods in &lt;strong&gt;custom listeners&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;When using methods as &lt;strong&gt;middleware or hooks&lt;/strong&gt; where context is lost.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Best Practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In services or controllers, consider using &lt;strong&gt;arrow functions&lt;/strong&gt; to preserve &lt;code&gt;this&lt;/code&gt; context.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;bind(this)&lt;/code&gt; if you need to pass a method as a callback and prefer not to redefine it.&lt;/li&gt;
&lt;li&gt;Avoid overusing arrow functions in classes if you rely on inheritance or unit testing that depends on traditional methods.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;NestJS makes dependency and state management easy, but the behavior of &lt;code&gt;this&lt;/code&gt; in JavaScript still needs attention. Understanding and properly applying method binding helps avoid subtle bugs and leads to cleaner, more maintainable code. Whether through &lt;code&gt;bind(this)&lt;/code&gt; or arrow functions, choosing the right strategy depends on your application's context and design.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
      <category>backend</category>
    </item>
    <item>
      <title>Mastering Functional Programming: Immutability, Composition, Pure and Arrow Functions</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Wed, 07 May 2025 16:02:48 +0000</pubDate>
      <link>https://dev.to/geampiere/mastering-functional-programming-immutability-composition-pure-and-arrow-functions-3co3</link>
      <guid>https://dev.to/geampiere/mastering-functional-programming-immutability-composition-pure-and-arrow-functions-3co3</guid>
      <description>&lt;p&gt;Functional programming has gained traction in modern development due to its ability to produce more &lt;strong&gt;predictable&lt;/strong&gt;, &lt;strong&gt;modular&lt;/strong&gt;, and &lt;strong&gt;testable&lt;/strong&gt; code. In this post, we explore four fundamental principles: &lt;strong&gt;immutability&lt;/strong&gt;, &lt;strong&gt;function composition&lt;/strong&gt;, &lt;strong&gt;pure functions&lt;/strong&gt;, and &lt;strong&gt;arrow functions&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧊 Immutability: Protect Your State
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; means that once a value is created, it cannot be changed. Instead of mutating data, we return new versions with the desired updates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Why is it useful?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevents unpredictable side effects&lt;/li&gt;
&lt;li&gt;Easier debugging and testing&lt;/li&gt;
&lt;li&gt;Ideal for concurrent environments (like React or Redux)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;❌ Mutable example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = { name: 'Alice', age: 25 };
user.age = 26; // Mutation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;✅ Immutable example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const user = { name: 'Alice', age: 25 };
const updatedUser = { ...user, age: 26 }; // New copy with changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🔁 Function Composition: Chain Your Logic
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Function composition&lt;/strong&gt; means combining small, reusable functions to build more complex logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const trim = str =&amp;gt; str.trim();
const toUpper = str =&amp;gt; str.toUpperCase();

const cleanAndCapitalize = str =&amp;gt; toUpper(trim(str));

cleanAndCapitalize("  hello  "); // "HELLO"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or dynamically compose using a helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const compose = (...fns) =&amp;gt; x =&amp;gt; fns.reduce((acc, fn) =&amp;gt; fn(acc), x);

const cleanAndCapitalize = compose(trim, toUpper);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 &lt;strong&gt;Benefit&lt;/strong&gt;: cleaner, modular, and easily testable code.&lt;/p&gt;




&lt;h3&gt;
  
  
  🦼 Pure Functions: No Surprises
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;pure function&lt;/strong&gt; follows two rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Always returns the same output for the same inputs&lt;/li&gt;
&lt;li&gt;Has no side effects (doesn't modify external variables or interact with I/O)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;✅ Pure function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (a, b) =&amp;gt; a + b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;❌ Impure function:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let counter = 0;
const increment = () =&amp;gt; ++counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easier to test&lt;/li&gt;
&lt;li&gt;Fewer hidden dependencies&lt;/li&gt;
&lt;li&gt;Can be memoized or cached&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🌽 Arrow Functions: Elegant and Contextual
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Arrow functions&lt;/strong&gt; (&lt;code&gt;=&amp;gt;&lt;/code&gt;) offer a concise way to declare functions in JavaScript and TypeScript. They also &lt;strong&gt;don’t have their own&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt;, making them perfect for callbacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Simple example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const double = n =&amp;gt; n * 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🧠 Implicit return:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sum = (a, b) =&amp;gt; a + b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🎯 &lt;code&gt;this&lt;/code&gt; behavior:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Timer() {
  this.seconds = 0;

  setInterval(() =&amp;gt; {
    this.seconds++;
    console.log(this.seconds);
  }, 1000); // `this` works correctly
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🧹 Conclusion
&lt;/h3&gt;

&lt;p&gt;Adopting functional principles like &lt;strong&gt;immutability&lt;/strong&gt;, &lt;strong&gt;composition&lt;/strong&gt;, and &lt;strong&gt;pure functions&lt;/strong&gt; (along with arrow functions) helps you write more &lt;strong&gt;declarative&lt;/strong&gt;, &lt;strong&gt;maintainable&lt;/strong&gt;, and &lt;strong&gt;robust&lt;/strong&gt; code. These ideas are especially powerful in modern frameworks like &lt;strong&gt;React&lt;/strong&gt;, &lt;strong&gt;NestJS&lt;/strong&gt;, or &lt;strong&gt;Redux&lt;/strong&gt;, where predictable data flow is crucial.&lt;/p&gt;

</description>
      <category>cleancode</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Test-Driven Development (TDD) and Domain-Driven Design (DDD): The Perfect Duo for Robust NestJS Applications</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Tue, 06 May 2025 14:37:53 +0000</pubDate>
      <link>https://dev.to/geampiere/test-driven-development-tdd-and-domain-driven-design-ddd-the-perfect-duo-for-robust-nestjs-44g0</link>
      <guid>https://dev.to/geampiere/test-driven-development-tdd-and-domain-driven-design-ddd-the-perfect-duo-for-robust-nestjs-44g0</guid>
      <description>&lt;p&gt;When working on complex applications, especially in the backend world, keeping domain clarity and code quality is a constant challenge. That's where &lt;strong&gt;DDD (Domain-Driven Design)&lt;/strong&gt; and &lt;strong&gt;TDD (Test-Driven Development)&lt;/strong&gt; shine as complementary approaches. In this article, I'll show you how to combine them in a project using &lt;strong&gt;NestJS&lt;/strong&gt; with modern best practices.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧠 What is DDD?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Domain-Driven Design&lt;/strong&gt; is a software development philosophy that focuses on the business domain. Instead of forcing the domain to fit the code, we adapt the code to represent the domain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key components of DDD:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entities&lt;/strong&gt;: Objects with identity (e.g., a &lt;code&gt;User&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value Objects&lt;/strong&gt;: Objects without identity, defined by their attributes (&lt;code&gt;Email&lt;/code&gt;, &lt;code&gt;Money&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Aggregates&lt;/strong&gt;: Consistency boundaries grouping related entities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repositories&lt;/strong&gt;: Abstractions for accessing aggregates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Domain/Application Services.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🔀 What is TDD?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Test-Driven Development&lt;/strong&gt; is a software development practice following this cycle:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Write a failing test&lt;/strong&gt; (Red).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the minimum code to pass it&lt;/strong&gt; (Green).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refactor the code&lt;/strong&gt; (Refactor).&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;TDD not only ensures your code works, but it also forces clean API and logic design from the beginning.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  🤝 How Do DDD and TDD Complement Each Other?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DDD&lt;/strong&gt; helps define what needs to be built and how to model it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TDD&lt;/strong&gt; ensures what is built is correct, testable, and maintainable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With DDD, you design a rich domain model; with TDD, you continuously validate its expected behavior.&lt;/p&gt;




&lt;h3&gt;
  
  
  🛠️ Example: Changing a User's Name in NestJS using DDD + TDD
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Write the Test First (TDD)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// change-user-name.use-case.spec.ts

describe('ChangeUserNameUseCase', () =&amp;gt; {
  it('should change the user name', async () =&amp;gt; {
    const user = new User('1', 'John', 'john@example.com');
    const userRepository = {
      findById: jest.fn().mockResolvedValue(user),
      save: jest.fn(),
    };

    const useCase = new ChangeUserNameUseCase(userRepository);
    await useCase.execute('1', 'Jane');

    expect(user.name).toBe('Jane');
    expect(userRepository.save).toHaveBeenCalledWith(user);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Domain Logic with DDD&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// user.entity.ts
export class User {
  constructor(
    public readonly id: string,
    public name: string,
    public readonly email: string
  ) {}

  changeName(newName: string) {
    if (!newName) throw new Error('Name cannot be empty');
    this.name = newName;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Application Layer Use Case&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// change-user-name.use-case.ts

export class ChangeUserNameUseCase {
  constructor(private readonly userRepository: UserRepository) {}

  async execute(userId: string, newName: string) {
    const user = await this.userRepository.findById(userId);
    user.changeName(newName);
    await this.userRepository.save(user);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🧹 Benefits of Combining DDD + TDD
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Testable domain models&lt;/strong&gt; from day one.&lt;/li&gt;
&lt;li&gt;Better interface design thanks to the "test-first" approach.&lt;/li&gt;
&lt;li&gt;High cohesion in business logic.&lt;/li&gt;
&lt;li&gt;Low coupling between layers (presentation, application, domain, infrastructure).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧼 Best Practices
&lt;/h3&gt;

&lt;p&gt;✅ Use interfaces in repositories to decouple infrastructure from domain.&lt;br&gt;
✅ Keep Value Objects immutable.&lt;br&gt;
✅ Use case tests are more valuable than controller tests.&lt;br&gt;
✅ Avoid domain logic in controllers: delegate to use cases.&lt;br&gt;
✅ If a business rule changes, only the aggregate should be updated.&lt;/p&gt;




&lt;h3&gt;
  
  
  🏁 Conclusion
&lt;/h3&gt;

&lt;p&gt;Adopting &lt;strong&gt;DDD&lt;/strong&gt; to model your domain properly and using &lt;strong&gt;TDD&lt;/strong&gt; to continuously validate its behavior not only improves code quality but also makes your app more scalable and maintainable. In NestJS, this combination is natural thanks to its modular architecture and built-in testing support.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Building a complex or critical product? Then DDD + TDD isn't optional it's essential.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tdd</category>
      <category>programming</category>
      <category>microservices</category>
      <category>node</category>
    </item>
    <item>
      <title>Mastering Microservices Patterns: Circuit Breaker, Fallback, Bulkhead, Saga, and CQRS</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Mon, 05 May 2025 15:20:51 +0000</pubDate>
      <link>https://dev.to/geampiere/mastering-microservices-patterns-circuit-breaker-fallback-bulkhead-saga-and-cqrs-4h55</link>
      <guid>https://dev.to/geampiere/mastering-microservices-patterns-circuit-breaker-fallback-bulkhead-saga-and-cqrs-4h55</guid>
      <description>&lt;p&gt;In the world of microservices, we often face challenges like availability, resilience, scalability, and data consistency. To tackle these issues, several architectural patterns help us build more reliable and robust systems. In this article, we’ll explore five essential microservices patterns: &lt;strong&gt;Circuit Breaker, Fallback, Bulkhead, Saga,&lt;/strong&gt; and &lt;strong&gt;CQRS&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Circuit Breaker: Protecting the system from failure
&lt;/h3&gt;

&lt;p&gt;When a microservice starts failing repeatedly—due to high latency or downtime—the &lt;strong&gt;Circuit Breaker&lt;/strong&gt; pattern acts like an electrical fuse.&lt;br&gt;
Instead of continuing to make failing calls, the circuit "opens" and temporarily blocks new requests to the failing service. This protects the rest of the system from cascading failures and gives the faulty service time to recover.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-world use case:&lt;/strong&gt;&lt;br&gt;
If a payment service becomes unresponsive, the order service can open the circuit and avoid waiting on it, improving system stability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Popular tools:&lt;/strong&gt; Resilience4j, Hystrix, NestJS with resiliency libraries.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fallback: Always have a plan B
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Fallback&lt;/strong&gt; is the natural companion to Circuit Breaker. It defines an alternative response when a service call fails.&lt;br&gt;
Instead of showing an error to the user, you provide a safe, degraded but usable experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
If a product recommendation service is down, show trending or popular products instead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefit&lt;/strong&gt;:&lt;br&gt;
Improves resilience and ensures a smoother user experience during failures.&lt;/p&gt;




&lt;h3&gt;
  
  
  Bulkhead: Isolate to survive
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Bulkhead&lt;/strong&gt; pattern, inspired by ship compartments, isolates parts of the system to contain failures.&lt;br&gt;
In microservices, it means isolating resources (like threads, memory, or connections) so that a failure in one component doesn't bring down the entire system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practical example:&lt;/strong&gt;&lt;br&gt;
Separate the HTTP connection pool for critical services (like payments) and non-critical ones (like email notifications).&lt;br&gt;
This way, if notifications are overloaded, they won’t impact core functionality.&lt;/p&gt;




&lt;h3&gt;
  
  
  Saga: Coordinating distributed transactions
&lt;/h3&gt;

&lt;p&gt;When a business process spans multiple microservices, the Saga pattern helps coordinate actions in a &lt;strong&gt;decentralized&lt;/strong&gt; and &lt;strong&gt;asynchronous&lt;/strong&gt; way.&lt;/p&gt;

&lt;p&gt;Instead of relying on expensive distributed transactions, each service performs its action and, in case of failure, executes &lt;strong&gt;compensating actions&lt;/strong&gt; to undo previous steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical use case:&lt;/strong&gt;&lt;br&gt;
During a purchase process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reserve product stock.&lt;/li&gt;
&lt;li&gt;Charge the customer.&lt;/li&gt;
&lt;li&gt;Confirm the order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the payment fails, the system triggers a &lt;strong&gt;compensation&lt;/strong&gt; to release the reserved stock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two styles of Sagas:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Orchestrated&lt;/strong&gt;: A central service directs the flow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choreographed&lt;/strong&gt;: Services react to events in a distributed manner.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  CQRS: Divide and conquer
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;CQRS&lt;/strong&gt; (Command Query Responsibility Segregation) promotes the &lt;strong&gt;separation of read and write responsibilities&lt;/strong&gt; within an application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this helpful?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Reads and writes often have different performance and scaling requirements.&lt;/li&gt;
&lt;li&gt;You can optimize and scale them independently.&lt;/li&gt;
&lt;li&gt;It offers more flexibility for high-load systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example scenario:&lt;/strong&gt;&lt;br&gt;
In an e-commerce platform:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Product listings and order history are handled by a &lt;strong&gt;read model&lt;/strong&gt; (possibly using a cache like Redis).&lt;/li&gt;
&lt;li&gt;Actions like placing an order or updating stock are handled by a &lt;strong&gt;write model&lt;/strong&gt; (using a transactional DB like PostgreSQL).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better performance and user experience.&lt;/li&gt;
&lt;li&gt;Easier scaling for high-traffic endpoints.&lt;/li&gt;
&lt;li&gt;More flexible architecture.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Caveat&lt;/strong&gt;:&lt;br&gt;
CQRS adds complexity, so it's most useful in systems with &lt;strong&gt;high query loads&lt;/strong&gt; or &lt;strong&gt;complex business logic&lt;/strong&gt; on writes.&lt;/p&gt;




&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;By using patterns like &lt;strong&gt;Circuit Breaker, Fallback, Bulkhead, Saga,&lt;/strong&gt; and &lt;strong&gt;CQRS&lt;/strong&gt;, we can build microservices that are more resilient, fault-tolerant, and prepared for the real-world challenges of distributed systems.&lt;/p&gt;

&lt;p&gt;Each pattern serves a specific purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Circuit Breaker prevents cascading failures.&lt;/li&gt;
&lt;li&gt;Fallback ensures graceful degradation.&lt;/li&gt;
&lt;li&gt;Bulkhead isolates failures.&lt;/li&gt;
&lt;li&gt;Saga coordinates long-running processes.&lt;/li&gt;
&lt;li&gt;CQRS separates concerns for better optimization.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding and applying these patterns will help you create microservice architectures that don’t just work—but thrive in production.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>backend</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>Queues vs Topics</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Fri, 02 May 2025 16:02:21 +0000</pubDate>
      <link>https://dev.to/geampiere/queues-vs-topics-3e67</link>
      <guid>https://dev.to/geampiere/queues-vs-topics-3e67</guid>
      <description>&lt;p&gt;In the world of software development—especially with distributed systems and microservices—&lt;strong&gt;asynchronous messaging&lt;/strong&gt; has become essential for building scalable and resilient applications. Two of the most common messaging patterns are queues and topics. While they might sound similar, their behavior and use cases are quite different.&lt;/p&gt;

&lt;p&gt;In this post, I’ll explain what they are, how they differ, and when to use each one.&lt;/p&gt;




&lt;h3&gt;
  
  
  What Is a Queue?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;queue&lt;/strong&gt; follows a &lt;strong&gt;point-to-point&lt;/strong&gt; communication model: messages are sent by a producer and consumed by &lt;strong&gt;only one consumer&lt;/strong&gt;. Once a message is processed, it is removed from the queue.&lt;/p&gt;

&lt;p&gt;Key characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each message is processed &lt;strong&gt;by one and only one consumer&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Guarantees &lt;strong&gt;single delivery&lt;/strong&gt; of each message.&lt;/li&gt;
&lt;li&gt;Useful for &lt;strong&gt;load balancing&lt;/strong&gt; between multiple consumers.&lt;/li&gt;
&lt;li&gt;Maintains message order (FIFO – First In, First Out).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to use queues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Background processing.&lt;/li&gt;
&lt;li&gt;Order or job handling systems.&lt;/li&gt;
&lt;li&gt;Distributing workload across workers.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  What Is a Topic?
&lt;/h3&gt;

&lt;p&gt;A topic uses a publish-subscribe (pub/sub) model. In this case, a message published to a topic is received by all subscribers.&lt;/p&gt;

&lt;p&gt;Key characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multiple consumers&lt;/strong&gt; receive the &lt;strong&gt;same message&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Full decoupling between producers and consumers.&lt;/li&gt;
&lt;li&gt;Enables more flexible, event-driven architectures.&lt;/li&gt;
&lt;li&gt;Subscribers can process messages at their own pace.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to use topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time notifications or broadcasting.&lt;/li&gt;
&lt;li&gt;Event-driven systems.&lt;/li&gt;
&lt;li&gt;Integrations where multiple services must react to the same event.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Differences Between Queues and Topics
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Feature&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Queue&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Topic&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Communication model&lt;/td&gt;
&lt;td&gt;Point-to-Point&lt;/td&gt;
&lt;td&gt;Publish-Subscribe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Message distribution&lt;/td&gt;
&lt;td&gt;One consumer per message&lt;/td&gt;
&lt;td&gt;All subscribers receive the message&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delivery guarantee&lt;/td&gt;
&lt;td&gt;Single delivery&lt;/td&gt;
&lt;td&gt;One copy per subscriber&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main purpose&lt;/td&gt;
&lt;td&gt;Load balancing&lt;/td&gt;
&lt;td&gt;Broadcasting messages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use cases&lt;/td&gt;
&lt;td&gt;Job processing, task queues&lt;/td&gt;
&lt;td&gt;Notifications, event sourcing, integration&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  RabbitMQ and Kafka: How They Implement It
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;RabbitMQ&lt;/strong&gt;, queues are used for point-to-point messaging, and &lt;strong&gt;topic exchanges&lt;/strong&gt; are used for pub/sub.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Kafka&lt;/strong&gt;, everything is based on &lt;strong&gt;topics&lt;/strong&gt;, but partitions can help simulate queue-like behavior.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Both queues and topics are fundamental in modern systems. Choosing between them depends on your business and technical needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;queues&lt;/strong&gt; when each task should be processed &lt;strong&gt;once&lt;/strong&gt;, by &lt;strong&gt;only one&lt;/strong&gt; worker.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;topics&lt;/strong&gt; when a message must be delivered to &lt;strong&gt;multiple subscribers&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mastering both models will help you design systems that are &lt;strong&gt;scalable&lt;/strong&gt;, &lt;strong&gt;resilient&lt;/strong&gt;, and &lt;strong&gt;easy to maintain&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>backend</category>
      <category>programming</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Building Flexible Applications with Hexagonal and Event-Driven Architecture in NestJS</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Thu, 01 May 2025 14:48:07 +0000</pubDate>
      <link>https://dev.to/geampiere/building-flexible-applications-with-hexagonal-and-event-driven-architecture-in-nestjs-578i</link>
      <guid>https://dev.to/geampiere/building-flexible-applications-with-hexagonal-and-event-driven-architecture-in-nestjs-578i</guid>
      <description>&lt;p&gt;In modern software development, one of the biggest challenges is keeping code clean, scalable, and loosely coupled.&lt;br&gt;
NestJS, thanks to its modular design and pattern-friendly structure, is a powerful framework for building robust systems using Hexagonal Architecture and Event-Driven Architecture.&lt;br&gt;
I’ll explain what these architectures are, why they matter, and how you can apply them using NestJS.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧩 What Is Hexagonal Architecture?
&lt;/h3&gt;

&lt;p&gt;Also known as &lt;strong&gt;Ports and Adapters, Hexagonal Architecture&lt;/strong&gt; organizes code to separate core business logic from external concerns like databases, APIs, or messaging systems.&lt;/p&gt;

&lt;h4&gt;
  
  
  🔧 Key Concepts:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain&lt;/strong&gt;: The heart of your business logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ports&lt;/strong&gt;: Interfaces that connect the domain to the outside world.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adapters&lt;/strong&gt;: Implementations of the ports (e.g., HTTP controllers, database services, or message brokers).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  🎯 Benefits:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling&lt;/strong&gt;: Infrastructure changes (like switching databases) don’t affect the business logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testability&lt;/strong&gt;: Core logic can be tested without involving real infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Independent evolution:&lt;/strong&gt; You can update adapters without altering the domain code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In &lt;strong&gt;NestJS&lt;/strong&gt;, Hexagonal Architecture fits naturally with modules. The domain is implemented as pure services, ports as TypeScript interfaces, and adapters as infrastructure-specific providers.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔄 What Is Event-Driven Architecture?
&lt;/h3&gt;

&lt;p&gt;Event-Driven Architecture (EDA) enables components of a system to communicate by emitting and listening to events, rather than calling each other directly.&lt;/p&gt;

&lt;p&gt;In this model:&lt;br&gt;
Components emit events when something important happens.&lt;/p&gt;

&lt;p&gt;Other components listen and react to those events asynchronously.&lt;/p&gt;

&lt;p&gt;A simple example:&lt;br&gt;
When a user signs up, a user.created event is emitted. Services like “send welcome email” or “log metrics” can react without the main service knowing about them.&lt;/p&gt;

&lt;h4&gt;
  
  
  🎯 Benefits:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loose coupling:&lt;/strong&gt; Services are not tightly bound to each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: You can add new consumers of an event without touching the producers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resilience&lt;/strong&gt;: Using queues (Kafka, RabbitMQ), events can be persisted and processed even if services are temporarily down.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In &lt;strong&gt;NestJS&lt;/strong&gt;, EDA is implemented using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;EventEmitterModule&lt;/code&gt; for in-memory internal events.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@nestjs/microservices&lt;/code&gt; module to work with external brokers like Kafka or RabbitMQ.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🚀 Combining Hexagonal and Event-Driven Architecture in NestJS
&lt;/h3&gt;

&lt;p&gt;Using both &lt;strong&gt;Hexagonal Architecture&lt;/strong&gt; and Event-Driven Architecture gives you systems that are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highly &lt;strong&gt;modular&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decoupled&lt;/strong&gt; both from infrastructure and from internal processes&lt;/li&gt;
&lt;li&gt;Easier to &lt;strong&gt;maintain&lt;/strong&gt;, evolve, and scale&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  A Typical Strategy in NestJS:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Your &lt;strong&gt;domain layer&lt;/strong&gt; triggers domain events like &lt;code&gt;UserCreatedEvent&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;infrastructure adapter&lt;/strong&gt; (e.g., a Kafka publisher) publishes that event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other services&lt;/strong&gt; consume the event and act independently.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This decouples logic not only between modules, but also across your entire system.&lt;/p&gt;




&lt;h3&gt;
  
  
  🛠️ Best Practices and Tools
&lt;/h3&gt;

&lt;p&gt;While using these architectures in NestJS, keep these practices in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;DTOs&lt;/strong&gt; to validate input.&lt;/li&gt;
&lt;li&gt;Consider &lt;strong&gt;Event Sourcing&lt;/strong&gt; or &lt;strong&gt;Outbox Pattern&lt;/strong&gt; for consistent event publishing.&lt;/li&gt;
&lt;li&gt;Implement &lt;strong&gt;global error handling&lt;/strong&gt; using NestJS filters and custom exceptions.&lt;/li&gt;
&lt;li&gt;Focus tests on the domain using &lt;strong&gt;mocked ports&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Design for &lt;strong&gt;idempotency&lt;/strong&gt; when processing events to avoid duplicate side effects.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;observability tools&lt;/strong&gt; (logs, tracing) for debugging event flows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For infrastructure-based EDA, tools like &lt;strong&gt;Kafka&lt;/strong&gt;, &lt;strong&gt;RabbitMQ&lt;/strong&gt;, and &lt;strong&gt;NATS&lt;/strong&gt; are great options, and NestJS integrates well with all of them.&lt;/p&gt;




&lt;h3&gt;
  
  
  📚 Conclusion
&lt;/h3&gt;

&lt;p&gt;By combining &lt;strong&gt;Hexagonal Architecture&lt;/strong&gt; and &lt;strong&gt;Event-Driven Architecture&lt;/strong&gt; in NestJS, you can build applications that are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean&lt;/li&gt;
&lt;li&gt;Loosely coupled&lt;/li&gt;
&lt;li&gt;Scalable&lt;/li&gt;
&lt;li&gt;Resilient&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These patterns allow your system to grow and adapt over time without becoming unmanageable.&lt;/p&gt;

&lt;p&gt;Are you already applying these architectures in your projects? 🚀&lt;/p&gt;

</description>
      <category>node</category>
      <category>backend</category>
      <category>microservices</category>
      <category>eventdriven</category>
    </item>
    <item>
      <title>How to Manage Memory and Avoid Leaks in NestJS Applications</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Mon, 28 Apr 2025 20:32:14 +0000</pubDate>
      <link>https://dev.to/geampiere/how-to-manage-memory-and-avoid-leaks-in-nestjs-applications-3geh</link>
      <guid>https://dev.to/geampiere/how-to-manage-memory-and-avoid-leaks-in-nestjs-applications-3geh</guid>
      <description>&lt;p&gt;Efficient memory management is crucial for building scalable and reliable applications, especially when working with &lt;strong&gt;Node.js&lt;/strong&gt; and &lt;strong&gt;NestJS&lt;/strong&gt;. Poor resource management can lead to &lt;strong&gt;memory leaks&lt;/strong&gt;, degraded performance, and even system crashes.&lt;/p&gt;

&lt;p&gt;We'll explore &lt;strong&gt;best practices&lt;/strong&gt; to avoid memory leaks and the &lt;strong&gt;tools&lt;/strong&gt; you can use to monitor and optimize resource usage in NestJS applications.&lt;/p&gt;




&lt;h3&gt;
  
  
  📚 What is a Memory Leak?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;memory leak&lt;/strong&gt; occurs when an application keeps references to objects that are no longer needed, preventing JavaScript’s &lt;strong&gt;garbage collector&lt;/strong&gt; from freeing them. Over time, this leads to growing memory usage, performance degradation, and potential crashes.&lt;/p&gt;




&lt;h3&gt;
  
  
  🛠️ Best Practices to Manage Memory in NestJS
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Avoid Circular References
&lt;/h4&gt;

&lt;p&gt;When two or more objects refer to each other, &lt;strong&gt;circular references&lt;/strong&gt; are created, making it harder for the garbage collector to clean them up.&lt;br&gt;
✅ Use patterns like &lt;code&gt;WeakMap&lt;/code&gt; for weak references when needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wm = new WeakMap();
let obj = { name: 'Test' };
wm.set(obj, 'some value');
obj = null; // Now it can be garbage collected

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  2. Manually Release Resources
&lt;/h4&gt;

&lt;p&gt;Always close database connections, sockets, and streams when they are no longer needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;await this.databaseConnection.close();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In NestJS, use lifecycle hooks like &lt;code&gt;OnModuleDestroy&lt;/code&gt; and &lt;code&gt;OnApplicationShutdown&lt;/code&gt; to automatically release resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class AppService implements OnApplicationShutdown {
  async onApplicationShutdown(signal: string) {
    await this.databaseConnection.close();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  3. Properly Manage Event Listeners
&lt;/h4&gt;

&lt;p&gt;Unremoved event listeners can cause memory leaks.&lt;/p&gt;

&lt;p&gt;✅ Always &lt;code&gt;removeListener&lt;/code&gt; or &lt;code&gt;off&lt;/code&gt; listeners you no longer need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.eventEmitter.off('orderCreated', this.handleOrderCreated);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4. Control the Growth of Collections
&lt;/h4&gt;

&lt;p&gt;Avoid storing unnecessary data in arrays or maps.&lt;br&gt;
Clear them when they are no longer needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.largeArray = [];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  5. Use Streams for Large Files
&lt;/h4&gt;

&lt;p&gt;When handling large files, use &lt;strong&gt;streams&lt;/strong&gt; instead of loading the entire content into memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const stream = fs.createReadStream('bigfile.txt');
stream.pipe(process.stdout);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Monitor and Optimize Dependencies
&lt;/h4&gt;

&lt;p&gt;Some third-party libraries can introduce memory leaks.&lt;br&gt;
✅ Regularly review, audit, and update your project dependencies.&lt;/p&gt;


&lt;h3&gt;
  
  
  🔥 Tools to Detect and Prevent Memory Leaks
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Node.js Inspector + Chrome DevTools
&lt;/h4&gt;

&lt;p&gt;Start your app with &lt;code&gt;--inspect&lt;/code&gt; to debug in Chrome:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node --inspect dist/main.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From &lt;code&gt;chrome://inspect&lt;/code&gt;, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take heap snapshots.&lt;/li&gt;
&lt;li&gt;Monitor object growth.&lt;/li&gt;
&lt;li&gt;Analyze memory usage.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Clinic.js
&lt;/h4&gt;

&lt;p&gt;A powerful performance analysis suite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g clinic
clinic doctor -- node dist/main.js

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates visual reports about memory, CPU, and event loop behavior.&lt;/p&gt;




&lt;h4&gt;
  
  
  Heapdump
&lt;/h4&gt;

&lt;p&gt;Capture memory snapshots (.heapsnapshot) for deeper analysis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install heapdump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as heapdump from 'heapdump';
heapdump.writeSnapshot('/path/to/dump.heapsnapshot');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can open these snapshots in &lt;strong&gt;Chrome DevTools&lt;/strong&gt; to find memory leaks.&lt;/p&gt;




&lt;h4&gt;
  
  
  Memwatch-Next
&lt;/h4&gt;

&lt;p&gt;Real-time memory leak detection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install memwatch-next
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as memwatch from 'memwatch-next';
memwatch.on('leak', (info) =&amp;gt; {
  console.error('Leak detected:', info);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Prometheus + Grafana
&lt;/h4&gt;

&lt;p&gt;For production-grade monitoring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;prom-client&lt;/code&gt; to expose memory metrics.&lt;/li&gt;
&lt;li&gt;Visualize and set up alerts with &lt;strong&gt;Grafana&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install prom-client

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const client = require('prom-client');
const collectDefaultMetrics = client.collectDefaultMetrics;

collectDefaultMetrics();

const http = require('http');
const server = http.createServer((req, res) =&amp;gt; {
  res.setHeader('Content-Type', client.register.contentType);
  res.end(client.register.metrics());
});

server.listen(8080);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;🧹 Extra Best Practices for NestJS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use DTOs and Pipes to validate and sanitize input data.&lt;/li&gt;
&lt;li&gt;Apply clean architecture principles like &lt;strong&gt;CQRS&lt;/strong&gt; or &lt;strong&gt;Hexagonal Architecture&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Properly manage Singleton instances.&lt;/li&gt;
&lt;li&gt;Avoid heavy decorators or stateful interceptors unless necessary.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✨ Conclusion
&lt;/h3&gt;

&lt;p&gt;Proper memory management in a &lt;strong&gt;NestJS&lt;/strong&gt; application is &lt;strong&gt;not optional&lt;/strong&gt; — it's essential for building stable and scalable services.&lt;br&gt;
By following best practices and leveraging the right tools, you can detect and fix memory leaks before they escalate into major problems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember: In production, a memory leak doesn't just hurt your app — it can hurt your business!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>node</category>
      <category>softwaredevelopment</category>
      <category>performance</category>
      <category>backend</category>
    </item>
    <item>
      <title>Authentication using NestJS and JWT</title>
      <dc:creator>Geampiere Jaramillo</dc:creator>
      <pubDate>Mon, 28 Apr 2025 15:04:32 +0000</pubDate>
      <link>https://dev.to/geampiere/authentication-using-nestjs-and-jwt-3lj7</link>
      <guid>https://dev.to/geampiere/authentication-using-nestjs-and-jwt-3lj7</guid>
      <description>&lt;p&gt;Authentication is a fundamental pillar in web application development. I’ll explain how to handle authentication in an API built with NestJS, using JWT (JSON Web Tokens) to securely and efficiently protect our routes.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is Authentication?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt; is the process of verifying that someone is who they claim to be. In web applications, this typically involves providing a &lt;strong&gt;username&lt;/strong&gt; and &lt;strong&gt;password&lt;/strong&gt;. If the credentials are correct, access to protected resources is granted.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why use JWT?
&lt;/h3&gt;

&lt;p&gt;JWT (JSON Web Token) is an open standard that allows information to be securely transmitted between two parties. In an API, it’s commonly used to issue a "pass" (token) to the user upon login, which can then be used to authorize future requests.&lt;/p&gt;

&lt;p&gt;Main advantages of using JWT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupled Authentication:&lt;/strong&gt; The server doesn’t need to store sessions in memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Perfect for distributed systems or microservices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Tokens are signed and can carry securely encoded data.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  How to Implement Authentication in NestJS?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Install Necessary Dependencies
&lt;/h4&gt;

&lt;p&gt;First, install the required packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @nestjs/jwt @nestjs/passport passport passport-local bcryptjs
npm install --save-dev @types/passport-local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  2. Create the Authentication Module
&lt;/h4&gt;

&lt;p&gt;Generate an &lt;code&gt;AuthModule&lt;/code&gt;, along with its service &lt;code&gt;AuthService&lt;/code&gt; and controller &lt;code&gt;AuthController&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest generate module auth
nest generate service auth
nest generate controller auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  3. Authentication Logic
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;AuthService&lt;/strong&gt; will validate users and generate tokens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class AuthService {
  constructor(
    private usersService: UsersService,
    private jwtService: JwtService,
  ) {}

  async validateUser(username: string, password: string): Promise&amp;lt;any&amp;gt; {
    const user = await this.usersService.findOneByUsername(username);
    if (user &amp;amp;&amp;amp; bcrypt.compareSync(password, user.password)) {
      const { password, ...result } = user;
      return result;
    }
    return null;
  }

  async login(user: any) {
    const payload = { username: user.username, sub: user.id };
    return {
      access_token: this.jwtService.sign(payload),
    };
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4. Create the Login DTO
&lt;/h4&gt;

&lt;p&gt;We create a DTO (Data Transfer Object) to structure the login request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class LoginDto {
  username: string;
  password: string;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures the API always receives well-formed data during login.&lt;/p&gt;




&lt;h4&gt;
  
  
  5. Implement the JWT Strategy
&lt;/h4&gt;

&lt;p&gt;Passport requires a strategy to validate tokens. Here’s the &lt;code&gt;JwtStrategy&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.JWT_SECRET,
    });
  }

  async validate(payload: any) {
    return { userId: payload.sub, username: payload.username };
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  6. Protect Routes
&lt;/h4&gt;

&lt;p&gt;We protect routes using guards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@UseGuards(JwtAuthGuard)
@Get('profile')
getProfile(@Request() req) {
  return req.user;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;JwtAuthGuard&lt;/code&gt; automatically checks if a valid token is present before granting access.&lt;/p&gt;




&lt;h3&gt;
  
  
  Full Authentication Flow
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The user sends a username and password to &lt;code&gt;/auth/login&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The server validates the credentials.&lt;/li&gt;
&lt;li&gt;If valid, the server returns a JWT.&lt;/li&gt;
&lt;li&gt;The client stores the token (e.g., in LocalStorage).&lt;/li&gt;
&lt;li&gt;For every protected request, the client sends the token in the Authorization header.&lt;/li&gt;
&lt;li&gt;The backend validates the token before processing the request.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Best Practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Encrypt passwords using strong hashing algorithms like &lt;strong&gt;bcrypt&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protect your secrets&lt;/strong&gt; used for signing JWTs (use environment variables).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set short expiration times&lt;/strong&gt; for access tokens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement refresh tokens&lt;/strong&gt; if you need longer user sessions.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Authentication with NestJS and JWT provides a modern, secure, and scalable way to protect your APIs. By combining Passport.js with NestJS, we can handle users, sessions, and permissions in a clean and efficient manner.&lt;/p&gt;

</description>
      <category>node</category>
      <category>authjs</category>
      <category>security</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
