<?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: Afshin Tavakolian</title>
    <description>The latest articles on DEV Community by Afshin Tavakolian (@afsh7n).</description>
    <link>https://dev.to/afsh7n</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%2F571061%2F44c7df38-c4cc-4927-8ab8-4b27ea42427d.jpg</url>
      <title>DEV Community: Afshin Tavakolian</title>
      <link>https://dev.to/afsh7n</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afsh7n"/>
    <language>en</language>
    <item>
      <title>🚀 Building a Modular and Scalable Backend with NestJS</title>
      <dc:creator>Afshin Tavakolian</dc:creator>
      <pubDate>Sat, 23 Nov 2024 00:06:34 +0000</pubDate>
      <link>https://dev.to/afsh7n/building-a-modular-and-scalable-backend-with-nestjs-17dk</link>
      <guid>https://dev.to/afsh7n/building-a-modular-and-scalable-backend-with-nestjs-17dk</guid>
      <description>&lt;p&gt;As developers, we often face challenges in building maintainable and scalable backend systems. Questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do I manage multiple modules efficiently?&lt;/li&gt;
&lt;li&gt;What’s the best way to integrate Docker for database switching?&lt;/li&gt;
&lt;li&gt;How can I auto-generate API documentation while ensuring security?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To address these challenges, I’ve created a &lt;strong&gt;NestJS Starter Template&lt;/strong&gt; that streamlines backend development. It’s packed with features like modular design, Docker integration, JWT authentication, and Swagger documentation.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is This Template About?
&lt;/h1&gt;

&lt;p&gt;This starter template is designed for developers who want to hit the ground running with &lt;strong&gt;NestJS&lt;/strong&gt;, a progressive Node.js framework. It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalable Modular Architecture&lt;/strong&gt;: Each feature is encapsulated, making the codebase clean and maintainable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pre-configured Docker Setup&lt;/strong&gt;: Switch between &lt;strong&gt;MySQL&lt;/strong&gt; and &lt;strong&gt;PostgreSQL&lt;/strong&gt; with ease.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Swagger Documentation&lt;/strong&gt;: Automatically generated and interactive API docs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JWT Authentication&lt;/strong&gt;: Secure authentication out of the box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment-based Configuration&lt;/strong&gt;: Centralized management for development, staging, and production.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Key Features
&lt;/h1&gt;

&lt;h1&gt;
  
  
  1️⃣ Modular Design
&lt;/h1&gt;

&lt;p&gt;The template uses a feature-based folder structure:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/&lt;br&gt;
├── modules/&lt;br&gt;
│   ├── auth/  # Authentication module&lt;br&gt;
│   ├── user/  # User module&lt;br&gt;
├── common/    # Shared utilities (e.g., decorators, pipes)&lt;br&gt;
├── configs/   # Environment configurations&lt;br&gt;
├── core/      # Core functionalities (e.g., database, logger)&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  2️⃣ Dockerized Development&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;Whether you prefer &lt;strong&gt;MySQL&lt;/strong&gt; or &lt;strong&gt;PostgreSQL&lt;/strong&gt;, the setup is pre-configured:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Run with MySQL&lt;br&gt;
docker-compose -f docker-compose.mysql.yml up --build
&lt;h1&gt;
  
  
  Run with PostgreSQL
&lt;/h1&gt;

&lt;p&gt;docker-compose -f docker-compose.postgres.yml up --build&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  3️⃣ JWT Authentication&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;The starter comes with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Register Route&lt;/strong&gt;: Sign up new users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Login Route&lt;/strong&gt;: Authenticate and receive a JWT.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protected Routes&lt;/strong&gt;: Access only with a valid token.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  4️⃣ Swagger Integration
&lt;/h1&gt;

&lt;p&gt;API documentation is auto-generated with Swagger, accessible at &lt;code&gt;/api/docs&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to Get Started
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Step 1: Clone the Repository
&lt;/h1&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone &lt;br&gt;
cd nestStarthttps://github.com/your-repo/nestStart.git&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Step 2: Configure Environment Variables&lt;br&gt;
&lt;/h1&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# General Configuration&lt;br&gt;
PORT=3000&lt;br&gt;
NODE_ENV=development
&lt;h1&gt;
  
  
  Database Configuration
&lt;/h1&gt;

&lt;p&gt;DB_TYPE=postgres&lt;br&gt;
DB_HOST=postgres&lt;br&gt;
DB_PORT=5432&lt;br&gt;
DB_USERNAME=admin&lt;br&gt;
DB_PASSWORD=password&lt;br&gt;
DB_NAME=my_database&lt;/p&gt;
&lt;h1&gt;
  
  
  JWT Configuration
&lt;/h1&gt;

&lt;p&gt;JWT_SECRET=my_super_secret_key&lt;br&gt;
JWT_EXPIRES_IN=1h&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Step 3: Run the Application&lt;br&gt;
&lt;/h1&gt;
&lt;br&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Using Docker&lt;br&gt;
docker-compose -f docker-compose.postgres.yml up --build
&lt;h1&gt;
  
  
  Or run locally
&lt;/h1&gt;

&lt;p&gt;npm install&lt;br&gt;
npm run start:dev&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Endpoints&lt;br&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;POST /auth/register&lt;/strong&gt;: Register a new user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST /auth/login&lt;/strong&gt;: Authenticate and get a JWT.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GET /users&lt;/strong&gt;: View all users (requires authentication).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What’s Next?
&lt;/h1&gt;

&lt;p&gt;I plan to enhance the template with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Advanced logging and analytics&lt;/li&gt;
&lt;li&gt;GraphQL support&lt;/li&gt;
&lt;li&gt;Unit and integration tests&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This starter template is my contribution to the developer community. It’s open-source, flexible, and ready for production. Whether you're building an API for a startup or managing an enterprise-grade application, this template will save you hours of setup time.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/afsh7n/nestStart" rel="noopener noreferrer"&gt;Check it out on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’d love to hear your feedback. What features would you like to see added? Let’s connect and build something amazing together. 💻✨&lt;/p&gt;

&lt;p&gt;Feel free to use and customize this article for your LinkedIn post. Let me know if you need further adjustments! 🚀&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>typescript</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Building an Extensible Notification System</title>
      <dc:creator>Afshin Tavakolian</dc:creator>
      <pubDate>Sat, 05 Oct 2024 09:01:10 +0000</pubDate>
      <link>https://dev.to/afsh7n/building-an-extensible-notification-system-39jl</link>
      <guid>https://dev.to/afsh7n/building-an-extensible-notification-system-39jl</guid>
      <description>&lt;h3&gt;
  
  
  Building an Extensible Notification System
&lt;/h3&gt;

&lt;p&gt;In today’s digital world, notification systems have become an essential part of various platforms and applications. These systems are responsible for delivering important alerts to users via different channels such as email, SMS, or in-app notifications. The design of such a system should be flexible enough to allow for the easy addition of new notification channels in the future without requiring major structural changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Challenge
&lt;/h3&gt;

&lt;p&gt;Imagine you are developing a notification system for a platform. This system must be able to send notifications through multiple channels (such as email, SMS, in-app notifications, and even push messages). Additionally, some notifications may need to be sent through multiple channels simultaneously. The system must be designed in such a way that it can easily be extended to support new channels in the future without rewriting much of the existing code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exploring Suitable Design Patterns
&lt;/h3&gt;

&lt;p&gt;To tackle this challenge, there are several design patterns that can be implemented. Let's explore three design patterns: &lt;strong&gt;Strategy&lt;/strong&gt;, &lt;strong&gt;Observer&lt;/strong&gt;, and &lt;strong&gt;Decorator&lt;/strong&gt;, and see how they fit into building an extensible notification system.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Strategy Pattern
&lt;/h4&gt;

&lt;p&gt;The Strategy pattern allows you to switch between different algorithms dynamically. In this case, each notification channel (like email, SMS, etc.) can be treated as a separate strategy, with each channel implemented in its own class, following a common interface.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility:&lt;/strong&gt; You can easily swap out the notification channel by changing the strategy object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean Code:&lt;/strong&gt; Each notification channel is implemented separately, making it easier to manage and maintain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complexity with Multiple Channels:&lt;/strong&gt; If you want to send notifications through multiple channels simultaneously, managing them can become more complex.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Observer Pattern
&lt;/h4&gt;

&lt;p&gt;The Observer pattern enables you to notify multiple observers (in this case, notification channels) at the same time. Whenever a notification is triggered, all subscribed channels are notified and can send the message accordingly.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simultaneous Notifications:&lt;/strong&gt; You can send notifications through multiple channels simultaneously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy to Extend:&lt;/strong&gt; Adding new channels is easy without changing the core structure of the system.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complex Management:&lt;/strong&gt; Managing and coordinating between different channels can become complex and requires careful design.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The Decorator pattern allows you to dynamically add new behavior to objects without altering their structure. In the case of notifications, you can chain different behaviors (such as sending via SMS and email) together.&lt;/p&gt;

&lt;h5&gt;
  
  
  Pros:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;High Flexibility:&lt;/strong&gt; You can add new behaviors (channels) dynamically without changing the core system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chaining:&lt;/strong&gt; Allows you to send notifications through multiple channels by chaining behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Cons:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complexity in Management:&lt;/strong&gt; As more channels are added, managing the chain of decorators can become complex.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Choosing the Best Design Pattern
&lt;/h3&gt;

&lt;p&gt;After reviewing the patterns, &lt;strong&gt;Observer Pattern&lt;/strong&gt; stands out as the best choice for this challenge. The ability to manage multiple notification channels simultaneously and easily extend the system by adding new channels makes it the most suitable solution. With this pattern, you can scale the system as new notification channels are introduced, without having to rewrite significant parts of the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the Observer Pattern
&lt;/h3&gt;

&lt;p&gt;Here’s how you can implement the Observer pattern for your notification system. You would have a core &lt;code&gt;NotificationSubject&lt;/code&gt; class that handles notifying all the attached channels. Each channel (like email, SMS, etc.) would be an observer attached to this subject.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationSubject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$observers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;NotificationObserver&lt;/span&gt; &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;observers&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$observer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;NotificationObserver&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailObserver&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NotificationObserver&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Send email&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsObserver&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NotificationObserver&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Send SMS&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever a message is passed to the &lt;code&gt;notify&lt;/code&gt; method, all attached observers (channels) will send the notification through their respective medium.&lt;/p&gt;

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

&lt;p&gt;An extensible notification system allows you to easily add new notification channels while managing multiple channels simultaneously. The &lt;strong&gt;Observer Pattern&lt;/strong&gt; is chosen as the best solution because of its ability to handle multiple notification channels and its flexibility to expand the system as new channels are added.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keywords:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Notification System&lt;/li&gt;
&lt;li&gt;Extensible Design&lt;/li&gt;
&lt;li&gt;Design Patterns&lt;/li&gt;
&lt;li&gt;Observer Pattern&lt;/li&gt;
&lt;li&gt;Managing Notifications&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This article explored the challenge of implementing an extensible notification system and reviewed potential design patterns that could be used to manage and expand notification channels in the future.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
