<?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: Sasith Warnaka</title>
    <description>The latest articles on DEV Community by Sasith Warnaka (@sasithwarnakafonseka).</description>
    <link>https://dev.to/sasithwarnakafonseka</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%2F1066654%2F114ff34d-86b4-48f3-8789-979032e3d7d5.jpeg</url>
      <title>DEV Community: Sasith Warnaka</title>
      <link>https://dev.to/sasithwarnakafonseka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sasithwarnakafonseka"/>
    <language>en</language>
    <item>
      <title>🚀 Best ORM for NestJS in 2025: Drizzle ORM vs TypeORM vs Prisma</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Sat, 13 Sep 2025 19:21:26 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/best-orm-for-nestjs-in-2025-drizzle-orm-vs-typeorm-vs-prisma-229c</link>
      <guid>https://dev.to/sasithwarnakafonseka/best-orm-for-nestjs-in-2025-drizzle-orm-vs-typeorm-vs-prisma-229c</guid>
      <description>&lt;p&gt;When building a &lt;strong&gt;NestJS&lt;/strong&gt; backend, one of the first architectural decisions is:&lt;br&gt;&lt;br&gt;
👉 &lt;em&gt;Which ORM (Object-Relational Mapper) should I use?&lt;/em&gt;  &lt;/p&gt;

&lt;p&gt;The ORM defines &lt;strong&gt;how your application talks to the database&lt;/strong&gt; — directly impacting &lt;strong&gt;performance, scalability, and developer experience (DX)&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;This article compares the &lt;strong&gt;three most popular ORMs for NestJS in 2025&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeORM&lt;/strong&gt; – classic, decorator-based ORM
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prisma&lt;/strong&gt; – modern, schema-first ORM with tooling
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drizzle ORM&lt;/strong&gt; – lightweight, SQL-first, TypeScript-native
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally, we’ll decide which one is the &lt;strong&gt;best choice for high-performance NestJS projects in 2025&lt;/strong&gt;.  &lt;/p&gt;


&lt;h2&gt;
  
  
  ⚡ TypeORM – The Classic Choice
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mature and widely used in the NestJS ecosystem
&lt;/li&gt;
&lt;li&gt;Supports PostgreSQL, MySQL, MariaDB, SQLite, MongoDB
&lt;/li&gt;
&lt;li&gt;Familiar OOP-style with decorators (&lt;code&gt;@Entity&lt;/code&gt;, &lt;code&gt;@Column&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;Built-in &lt;strong&gt;relations&lt;/strong&gt;, eager/lazy loading, cascading
&lt;/li&gt;
&lt;li&gt;Strong &lt;strong&gt;NestJS integration&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance overhead due to heavy abstraction
&lt;/li&gt;
&lt;li&gt;Migrations can be buggy (schema drift issues)
&lt;/li&gt;
&lt;li&gt;TypeScript support is weak for relations
&lt;/li&gt;
&lt;li&gt;Lots of “magic” → harder debugging
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Slower in complex queries due to reflection and metadata.&lt;br&gt;&lt;br&gt;
Lazy loading often causes the &lt;strong&gt;N+1 query problem&lt;/strong&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ Best for teams from &lt;strong&gt;Hibernate/Entity Framework&lt;/strong&gt; background or those who want auto-managed relations.  &lt;/p&gt;


&lt;h2&gt;
  
  
  ⚡ Prisma – The DX Darling
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Schema-first&lt;/strong&gt; approach (&lt;code&gt;schema.prisma&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;Auto-generates fully typed &lt;strong&gt;client&lt;/strong&gt; → incredible DX
&lt;/li&gt;
&lt;li&gt;Rich ecosystem: Prisma Studio, Migrate, Data Browser
&lt;/li&gt;
&lt;li&gt;Great documentation &amp;amp; community
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance overhead from Rust query engine
&lt;/li&gt;
&lt;li&gt;Transactions are limited (interactive mode)
&lt;/li&gt;
&lt;li&gt;Limited flexibility for raw SQL
&lt;/li&gt;
&lt;li&gt;Can be harder to tune for high-performance queries
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
⚠️ Faster than TypeORM, but still slower than SQL-focused builders like Drizzle.  &lt;/p&gt;

&lt;p&gt;✅ Best for teams who value &lt;strong&gt;developer experience&lt;/strong&gt; and &lt;strong&gt;fast prototyping&lt;/strong&gt;.  &lt;/p&gt;


&lt;h2&gt;
  
  
  ⚡ Drizzle ORM – The Performance Leader
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeScript-first, SQL-first&lt;/strong&gt; – schema and queries are fully typed
&lt;/li&gt;
&lt;li&gt;Lightweight – no runtime reflection
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Near raw SQL performance&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Safe migrations with &lt;code&gt;drizzle-kit&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Strong inference → safer refactors
&lt;/li&gt;
&lt;li&gt;Great with &lt;strong&gt;serverless DBs&lt;/strong&gt; (Turso, Neon, Planetscale)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Newer, smaller ecosystem than Prisma/TypeORM
&lt;/li&gt;
&lt;li&gt;Requires developers to know SQL concepts
&lt;/li&gt;
&lt;li&gt;No lazy-loading → explicit joins only
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Drizzle is currently the &lt;strong&gt;fastest ORM for NestJS apps in 2025&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
It compiles queries down to SQL with &lt;strong&gt;minimal overhead&lt;/strong&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;✅ Best for &lt;strong&gt;performance-sensitive apps&lt;/strong&gt; (real-time systems, analytics, fintech).  &lt;/p&gt;


&lt;h2&gt;
  
  
  ⚡ Benchmark &amp;amp; Feature Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature / ORM&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;TypeORM&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Prisma&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Drizzle ORM&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ Slowest&lt;/td&gt;
&lt;td&gt;⚠️ Medium&lt;/td&gt;
&lt;td&gt;✅ Fastest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Type Safety&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;⚠️ Partial&lt;/td&gt;
&lt;td&gt;✅ Excellent&lt;/td&gt;
&lt;td&gt;✅ Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Migrations&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;⚠️ Buggy&lt;/td&gt;
&lt;td&gt;✅ Good&lt;/td&gt;
&lt;td&gt;✅ Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Learning Curve&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Easy (OOP devs)&lt;/td&gt;
&lt;td&gt;✅ Easy&lt;/td&gt;
&lt;td&gt;⚠️ Needs SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Relations&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Built-in&lt;/td&gt;
&lt;td&gt;✅ Declarative&lt;/td&gt;
&lt;td&gt;⚠️ Manual&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Ecosystem&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;✅ Mature&lt;/td&gt;
&lt;td&gt;✅ Huge&lt;/td&gt;
&lt;td&gt;⚠️ Growing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best For&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Legacy/OOP&lt;/td&gt;
&lt;td&gt;DX-first teams&lt;/td&gt;
&lt;td&gt;Performance &amp;amp; Type Safety&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  ⚡ NestJS Integration Examples
&lt;/h2&gt;
&lt;h3&gt;
  
  
  🟢 TypeORM
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;TypeOrmModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;postgres&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;entities&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;synchronize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="nx"&gt;TypeOrmModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forFeature&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  🟣 Prisma
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrismaService&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt; 
  &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnModuleInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;onModuleInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;$connect&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="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrismaService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;PrismaService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrismaModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  🔵 Drizzle ORM
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;drizzle&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;drizzle-orm/node-postgres&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pool&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;connectionString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_URL&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;drizzle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;useValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DatabaseModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ⚡ Final Verdict – Best ORM for NestJS in 2025
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;TypeORM&lt;/strong&gt; → Best if you need &lt;strong&gt;decorator-style entities &amp;amp; auto-relations&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prisma&lt;/strong&gt; → Best if you want &lt;strong&gt;fantastic DX &amp;amp; tooling&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drizzle ORM&lt;/strong&gt; → Best if you care about &lt;strong&gt;performance, type safety, and migrations&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;🏆 For most &lt;strong&gt;new NestJS projects in 2025&lt;/strong&gt;, the winner is &lt;strong&gt;Drizzle ORM&lt;/strong&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It combines &lt;strong&gt;raw SQL speed&lt;/strong&gt;, &lt;strong&gt;strict TypeScript safety&lt;/strong&gt;, and &lt;strong&gt;robust migrations&lt;/strong&gt; → making it the &lt;strong&gt;future-proof choice&lt;/strong&gt; for serious applications.  &lt;/p&gt;


&lt;h2&gt;
  
  
  🎬 Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeORM&lt;/strong&gt; is the legacy powerhouse, but struggles with performance.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prisma&lt;/strong&gt; is the most beginner-friendly with top DX.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drizzle ORM&lt;/strong&gt; is the &lt;strong&gt;best for modern, high-performance NestJS apps&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 If you’re starting a new project in 2025, choose &lt;strong&gt;Drizzle ORM&lt;/strong&gt; for scalability and performance.  &lt;/p&gt;




&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/drizzle-team" rel="noopener noreferrer"&gt;
        drizzle-team
      &lt;/a&gt; / &lt;a href="https://github.com/drizzle-team/drizzle-orm" rel="noopener noreferrer"&gt;
        drizzle-orm
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      ORM
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div&gt;
  &lt;a rel="noopener noreferrer" href="https://github.com/drizzle-team/drizzle-orm/./misc/readme/logo-github-sq-dark.svg#gh-dark-mode-only"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fdrizzle-team%2Fdrizzle-orm%2F.%2Fmisc%2Freadme%2Flogo-github-sq-dark.svg%23gh-dark-mode-only"&gt;&lt;/a&gt;
  &lt;a rel="noopener noreferrer" href="https://github.com/drizzle-team/drizzle-orm/./misc/readme/logo-github-sq-light.svg#gh-light-mode-only"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fdrizzle-team%2Fdrizzle-orm%2F.%2Fmisc%2Freadme%2Flogo-github-sq-light.svg%23gh-light-mode-only"&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div&gt;
  &lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Headless ORM for NodeJS, TypeScript and JavaScript 🚀&lt;/h3&gt;
&lt;/div&gt;
  &lt;a href="https://orm.drizzle.team" rel="nofollow noopener noreferrer"&gt;Website&lt;/a&gt; •
  &lt;a href="https://orm.drizzle.team/docs/overview" rel="nofollow noopener noreferrer"&gt;Documentation&lt;/a&gt; •
  &lt;a href="https://x.com/drizzleorm" rel="nofollow noopener noreferrer"&gt;Twitter&lt;/a&gt; •
  &lt;a href="https://driz.link/discord" rel="nofollow noopener noreferrer"&gt;Discord&lt;/a&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;What's Drizzle?&lt;/h3&gt;
&lt;/div&gt;
&lt;p&gt;Drizzle is a modern TypeScript ORM developers &lt;a href="https://stateofdb.com/tools/drizzle" rel="nofollow noopener noreferrer"&gt;wanna use in their next project&lt;/a&gt;
It is &lt;a href="https://bundlephobia.com/package/drizzle-orm" rel="nofollow noopener noreferrer"&gt;lightweight&lt;/a&gt; at only ~7.4kb minified+gzipped, and it's tree shakeable with exactly 0 dependencies.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Drizzle supports every PostgreSQL, MySQL and SQLite database&lt;/strong&gt;, including serverless ones like &lt;a href="https://orm.drizzle.team/docs/get-started-sqlite#turso" rel="nofollow noopener noreferrer"&gt;Turso&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/get-started-postgresql#neon" rel="nofollow noopener noreferrer"&gt;Neon&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/connect-xata" rel="nofollow noopener noreferrer"&gt;Xata&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/get-started-mysql#planetscale" rel="nofollow noopener noreferrer"&gt;PlanetScale&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/get-started-sqlite#cloudflare-d1" rel="nofollow noopener noreferrer"&gt;Cloudflare D1&lt;/a&gt;, &lt;a href="https://fly.io/docs/litefs/" rel="nofollow noopener noreferrer"&gt;FlyIO LiteFS&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/get-started-postgresql#vercel-postgres" rel="nofollow noopener noreferrer"&gt;Vercel Postgres&lt;/a&gt;, &lt;a href="https://orm.drizzle.team/docs/get-started-postgresql#supabase" rel="nofollow noopener noreferrer"&gt;Supabase&lt;/a&gt; and &lt;a href="https://orm.drizzle.team/docs/get-started-postgresql#aws-data-api" rel="nofollow noopener noreferrer"&gt;AWS Data API&lt;/a&gt;. No bells and whistles, no Rust binaries, no serverless adapters, everything just works out of the box.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Drizzle is serverless-ready by design&lt;/strong&gt;. It works in every major JavaScript runtime like NodeJS, Bun, Deno, Cloudflare Workers, Supabase functions, any Edge runtime, and even in browsers.&lt;br&gt;
With Drizzle you can be &lt;a href="https://orm.drizzle.team/benchmarks" rel="nofollow noopener noreferrer"&gt;&lt;strong&gt;fast out of the box&lt;/strong&gt;&lt;/a&gt; and save time and costs while never introducing any data proxies into your…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/drizzle-team/drizzle-orm" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


</description>
      <category>drizzle</category>
      <category>typeorm</category>
      <category>prisma</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>Introduction to NestJS and TensorFlow for Machine Learning</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Mon, 27 Jan 2025 21:51:26 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/introduction-to-nestjs-and-tensorflow-for-machine-learning-2gmi</link>
      <guid>https://dev.to/sasithwarnakafonseka/introduction-to-nestjs-and-tensorflow-for-machine-learning-2gmi</guid>
      <description>&lt;h3&gt;
  
  
  What is NestJS?
&lt;/h3&gt;

&lt;p&gt;NestJS is a modern Node.js framework Layouted to help you Construct efficient and scalable Host-side Uses. it  stacked with typescript and follows principles from object-oriented and practical scheduling devising it light to form and run your cipher. With its Angular-inspired Structure it comes packed with helpful Characteristics like Requirement injection middleware and decorators making it a solid pick for Constructing reliable and powerful APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why TensorFlow.js for ML?
&lt;/h3&gt;

&lt;p&gt;TensorFlow.js is a JavaScript library that allows developers to train and Use Calculater learning Representations directly in the browser or Node.js. it provides the power to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;charge pre-trained Representations or school green ones&lt;/li&gt;
&lt;li&gt;do real-time predictions&lt;/li&gt;
&lt;li&gt;Check cc computations exploitation webgl for acceleration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;by combine tensorflowjs with nestjs you get form smart genus apis that incorporate cc Rolealities with amp light and ascendable backend frame&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases and Goals of the Series
&lt;/h3&gt;

&lt;p&gt;This series will take you from a beginner to a proficient developer capable of integrating ML with NestJS. the name goals include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;scope leading amp nestjs diligence with tensorflowjs&lt;/li&gt;
&lt;li&gt;burden pre-trained Representations and education bespoke Representations&lt;/li&gt;
&lt;li&gt;construction ascendable genus apis to break cc capabilities&lt;/li&gt;
&lt;li&gt;exploring real-world cc Uses such as arsenic Checkimonial systems or see credit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up the Environment
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Installing NestJS
&lt;/h4&gt;

&lt;p&gt;Install the NestJS CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g @nestjs/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a new NestJS project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest new ml-backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to the project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ml-backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Adding TensorFlow.js to Your Project
&lt;/h4&gt;

&lt;p&gt;Install TensorFlow.js:&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 @tensorflow/tfjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Overview of the Project Structure
&lt;/h4&gt;

&lt;p&gt;After setting up, your project structure should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ml-backend/
├── src/
│   ├── app.controller.ts
│   ├── app.service.ts
│   ├── ml/
│   │   ├── ml.service.ts
│   │   └── ml.controller.ts
├── main.ts
├── package.json
└── tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this structure: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;app.controller.ts: Entry point for handling basic routes.&lt;/li&gt;
&lt;li&gt;ml/: Folder dedicated to your machine learning logic and APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Building Your First ML Model
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. How to Load and Use a Pre-Trained Model
&lt;/h4&gt;

&lt;p&gt;Create an ML service:&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 service ml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the logic to load a pre-trained model in ml.service.ts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as tf from '@tensorflow/tfjs';
import { Injectable } from '@nestjs/common';

@Injectable()
export class MlService {
  private model: tf.LayersModel;

  async loadModel(): Promise&amp;lt;void&amp;gt; {
    this.model = await tf.loadLayersModel('https://path-to-your-model/model.json');
  }

  async predict(inputData: number[]): Promise&amp;lt;number[]&amp;gt; {
    const tensor = tf.tensor2d([inputData], [1, inputData.length]);
    const prediction = this.model.predict(tensor) as tf.Tensor;
    return prediction.dataSync();
  }
}

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

&lt;/div&gt;



&lt;p&gt;Call loadModel() during the application bootstrap process to ensure the model is ready.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Making Predictions Using TensorFlow.js
&lt;/h4&gt;

&lt;p&gt;TensorFlow.js allows you to work with tensors, which are multi-dimensional arrays optimized for ML computations.&lt;/p&gt;

&lt;p&gt;Example prediction flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const input = [5.1, 3.5, 1.4, 0.2];
const result = await this.mlService.predict(input);
console.log('Prediction:', result);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Hands-On Example with a Simple Dataset
&lt;/h4&gt;

&lt;p&gt;To keep it simple, let’s use a pre-trained Iris flower classification model. The input would be an array of four features (sepal length, sepal width, petal length, petal width), and the output will predict the class of the flower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating ML into a NestJS API
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Creating a NestJS Service for ML Logic
&lt;/h4&gt;

&lt;p&gt;The MlService already handles loading the model and making predictions. We’ll now expose this logic through a controller.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Building REST APIs to Expose Predictions
&lt;/h4&gt;

&lt;p&gt;Create a controller:&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 controller ml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add an endpoint to ml.controller.ts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Post, Body } from '@nestjs/common';
import { MlService } from './ml.service';

@Controller('ml')
export class MlController {
  constructor(private readonly mlService: MlService) {}

  @Post('predict')
  async predict(@Body('input') input: number[]): Promise&amp;lt;number[]&amp;gt; {
    return this.mlService.predict(input);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Testing APIs with Postman
&lt;/h3&gt;

&lt;p&gt;Start the application:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Open Postman and send a POST request to:
&lt;/h4&gt;

&lt;p&gt;URL: &lt;a href="http://localhost:3000/ml/predict" rel="noopener noreferrer"&gt;http://localhost:3000/ml/predict&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "input": [5.1, 3.5, 1.4, 0.2]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response will return predictions for the given input.&lt;/p&gt;

&lt;p&gt;Next Steps&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the upcoming articles, we’ll explore:&lt;/li&gt;
&lt;li&gt;Training and deploying custom ML models.&lt;/li&gt;
&lt;li&gt;Optimizing API performance for real-world applications.&lt;/li&gt;
&lt;li&gt;Building and deploying a full ML-powered application.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nestjs</category>
      <category>tensorflow</category>
      <category>machinelearning</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Mastering PM2: Optimizing Node.js and Next.js Applications for Performance and Scalability</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Wed, 09 Oct 2024 20:33:23 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/mastering-pm2-optimizing-nodejs-and-nextjs-applications-for-performance-and-scalability-4552</link>
      <guid>https://dev.to/sasithwarnakafonseka/mastering-pm2-optimizing-nodejs-and-nextjs-applications-for-performance-and-scalability-4552</guid>
      <description>&lt;p&gt;In today's high-traffic web applications, ensuring smooth performance for projects like Next.js is essential. From my experience, PM2 has been a reliable tool for efficiently scaling, optimizing resources, and maintaining uptime. This article explores advanced PM2 techniques such as clustering, scaling, zero-downtime deployments, and monitoring—practical strategies I’ve successfully implemented in production environments to maintain high performance and reduce downtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why PM2?
&lt;/h3&gt;

&lt;p&gt;Before diving into advanced features, let’s clarify why PM2 is the go-to process manager for Node.js projects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Process Management: PM2 automatically keeps your app running. If a process crashes, PM2 restarts it instantly, ensuring uptime.&lt;/li&gt;
&lt;li&gt;Clustering: It allows you to spawn multiple instances of your Node.js app to take full advantage of multi-core CPUs, boosting your app’s performance.&lt;/li&gt;
&lt;li&gt;Zero-Downtime Deployment: PM2 enables zero-downtime reloads, meaning that your application remains online even when it’s updated.&lt;/li&gt;
&lt;li&gt;Real-Time Monitoring: With built-in monitoring tools, PM2 helps track CPU and memory usage for each process.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Advanced Features of PM2 for Node.js Projects
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Cluster Mode: Unlocking Full CPU Potential
&lt;/h4&gt;

&lt;p&gt;By default, Node.js applications run on a single thread, meaning they only use one CPU core. For CPU-bound tasks or high-concurrency applications, this can be a significant bottleneck. PM2’s cluster mode solves this by spawning multiple instances of your app, each mapped to a core.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 start app.js -I max
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command tells PM2 to launch your app in cluster mode, with max instances based on the available CPU cores. PM2 automatically balances incoming requests across these instances using a built-in load balancer, improving response times and overall throughput.&lt;/p&gt;

&lt;p&gt;Benefits:&lt;br&gt;
Optimized CPU usage: Instead of one core, you utilize all available cores.&lt;br&gt;
Fault tolerance: If one instance crashes, others remain unaffected.&lt;br&gt;
Scalability: You can dynamically increase or decrease the number of instances based on real-time traffic.&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Zero-Downtime Deployment
&lt;/h4&gt;

&lt;p&gt;PM2 enables zero-downtime deployment using its reload mechanism, which restarts your application instances one at a time. This means the server continues to serve users while new code is deployed.&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 reload app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is crucial for production environments, ensuring users do not experience any interruptions even during updates or patches. For Next.js apps using SSR (server-side rendering), this feature is invaluable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scaling and Optimizing Node.js Projects with PM2
&lt;/h3&gt;

&lt;p&gt;For applications handling heavy loads or requiring high availability, PM2’s scaling options are key to success. Here are some advanced strategies:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Horizontal Scaling
&lt;/h4&gt;

&lt;p&gt;To scale your Node.js applications horizontally, PM2’s cluster mode allows you to dynamically add or remove instances based on server load.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 scale app +3  # Add 3 more instances
pm2 scale app 2   # Scale down to 2 instances
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Horizontal scaling is ideal for high-traffic web apps, APIs, or any service where concurrency is crucial. For Next.js, where SSR adds to server load, PM2’s clustering optimizes the rendering process by distributing it across multiple instances.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Memory Management and Auto-Restart
&lt;/h4&gt;

&lt;p&gt;Memory leaks are a common issue in Node.js applications, particularly in long-running processes. PM2 can monitor your app’s memory usage and automatically restart instances when they exceed a set memory limit. This helps maintain performance without manual intervention.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 start app.js --max-memory-restart 500M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, if any instance consumes more than 500MB of memory, PM2 restarts it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advanced Ecosystem Configuration
&lt;/h3&gt;

&lt;p&gt;For complex projects, managing multiple applications or microservices becomes essential. PM2 allows you to manage these apps using an ecosystem file where you define different services, environments, and configurations in one place.&lt;/p&gt;

&lt;p&gt;Example ecosystem.config.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  apps: [
    {
      name: 'api',
      script: './api.js',
      instances: 4,
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'production',
      }
    },
    {
      name: 'next-app',
      script: 'npm',
      args: 'start',
      instances: 'max',
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'production',
      }
    }
  ]
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup allows you to run both a Node.js API and a Next.js app, each with multiple instances optimized for production.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating PM2 with Docker
&lt;/h3&gt;

&lt;p&gt;If you're running your Node.js or Next.js application inside Docker, PM2 can still manage your processes effectively. However, when using Docker, you should use pm2-runtime instead of the standard PM2 command, as Docker requires non-daemonized processes.&lt;/p&gt;

&lt;p&gt;Example Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:latest
WORKDIR /app
COPY . .
RUN npm install
CMD ["pm2-runtime", "ecosystem.config.js"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using pm2-runtime, your application runs inside the container without issues related to daemonizing processes, making Docker and PM2 a powerful combination for containerized deployments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Monitoring and Logs: Keeping Track of Your App’s Health
&lt;/h3&gt;

&lt;p&gt;PM2 provides extensive monitoring tools for tracking your applications. The command pm2 monit shows real-time stats like CPU and memory usage, while pm2 logs lets you view logs from all instances in real-time.&lt;/p&gt;

&lt;p&gt;For even more advanced monitoring, PM2 offers PM2 Plus, a web-based dashboard that gives you a comprehensive view of your application’s performance, including alerts, memory profiling, and more.&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 monit  # Real-time monitoring
pm2 logs   # Log viewing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion: Why PM2 is Essential for Node.js Projects
&lt;/h3&gt;

&lt;p&gt;PM2 is a must-have tool for any Node.js developer looking to manage production-grade applications. Whether you're working with a simple API or a complex SSR framework like Next.js, PM2 helps you achieve:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Optimized performance through multi-core utilization.&lt;/li&gt;
&lt;li&gt;Scalability with dynamic process scaling.&lt;/li&gt;
&lt;li&gt;Reliability through auto-restarts and memory management.&lt;/li&gt;
&lt;li&gt;Continuous availability with zero-downtime deployments.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By leveraging PM2’s advanced features, you can ensure your Node.js applications run smoothly, even under heavy load, and are ready to handle any production challenge.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further Reading
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://pm2.keymetrics.io/" rel="noopener noreferrer"&gt;PM2 Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pm2.keymetrics.io/docs/usage/cluster-mode/" rel="noopener noreferrer"&gt;Scaling Node.js Applications with PM2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>pm2</category>
      <category>node</category>
      <category>nextjs</category>
      <category>performanceoptimization</category>
    </item>
    <item>
      <title>Redux with Persistent State Rehydration in Next.js (App Router)</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Sun, 22 Sep 2024 20:03:04 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/implementing-redux-with-redux-persist-in-a-nextjs-app-router-application-3d7c</link>
      <guid>https://dev.to/sasithwarnakafonseka/implementing-redux-with-redux-persist-in-a-nextjs-app-router-application-3d7c</guid>
      <description>&lt;p&gt;It can be overwhelming when it comes to application state management on the different pages when working with Next.js. Redux comes in handy in this case which is a robust and effective state management solution and it is made better when implemented with Redux Persist which makes the state to survive even after a page refresh. This tutorial is aimed a getting started with Redux and Redux Persist in Next.js, in particular with the App Router (/app) structure.&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;p&gt;Before starting, ensure that you have Node.js and npm installed. If not, download and install them from the official Node.js website.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Setting up a Next.js App Router Project
&lt;/h3&gt;

&lt;p&gt;To create a new Next.js project with the App Router structure, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest my-next-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to enable the experimental App Router feature when prompted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Installing Redux and Redux Persist
&lt;/h3&gt;

&lt;p&gt;Next, install the required packages for Redux and Redux Persist:&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 redux react-redux redux-persist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These dependencies will help you manage state within your app and persist it across sessions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Setting Up Redux Store and Persist Configuration
&lt;/h3&gt;

&lt;p&gt;In your project, create a redux folder in the src directory to organize Redux files. Inside this folder, create a store.js file to configure the Redux store and Redux Persist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/redux/store.js

import { configureStore } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; 
import rootReducer from './reducers';

const persistConfig = {
  key: 'root',
  storage,
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

export const store = configureStore({
  reducer: persistedReducer,
});

export const persistor = persistStore(store);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Creating Reducers
&lt;/h3&gt;

&lt;p&gt;In the same redux folder, create a reducers folder to define your reducers. For this example, create a simple counter reducer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// src/redux/reducers/counterReducer.js

const initialState = {
  count: 0,
};

const counterReducer = (state = initialState, action) =&amp;gt; {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    case 'DECREMENT':
      return { count: state.count - 1 };
    default:
      return state;
  }
};

export default counterReducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create an index.js file in the reducers folder to combine reducers (if you have multiple):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// src/redux/reducers/index.js

import { combineReducers } from 'redux';
import counterReducer from './counterReducer';

export default combineReducers({
  counter: counterReducer,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Creating Actions
&lt;/h3&gt;

&lt;p&gt;In the redux folder, create an actions folder to define action creators. Create a counterActions.js file to handle the counter actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/redux/actions/counterActions.js

export const increment = () =&amp;gt; ({
  type: 'INCREMENT',
});

export const decrement = () =&amp;gt; ({
  type: 'DECREMENT',
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Integrating Redux and Redux Persist with App Router
&lt;/h3&gt;

&lt;p&gt;To integrate Redux and Redux Persist with the Next.js App Router, modify the layout.js file within the app directory. This file wraps your application’s UI and ensures that Redux and Redux Persist are accessible across all routes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// app/layout.js

import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { store, persistor } from '../src/redux/store';
import './globals.css';

export default function RootLayout({ children }) {
  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;Provider store={store}&amp;gt;
          &amp;lt;PersistGate loading={null} persistor={persistor}&amp;gt;
            {children}
          &amp;lt;/PersistGate&amp;gt;
        &amp;lt;/Provider&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup ensures that Redux and Redux Persist are initialized when the app loads.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Using Redux in a Component
&lt;/h3&gt;

&lt;p&gt;Now, let's create a page that uses the Redux state. Inside the app directory, create a new folder counter and add a page.js file to create a simple counter component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// app/counter/page.js

'use client'; // Enable client-side rendering for this page

import { useDispatch, useSelector } from 'react-redux';
import { increment, decrement } from '../../src/redux/actions/counterActions';

export default function CounterPage() {
  const count = useSelector((state) =&amp;gt; state.counter.count);
  const dispatch = useDispatch();

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Counter: {count}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch(increment())}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; dispatch(decrement())}&amp;gt;Decrement&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useSelector hook retrieves the current counter state, and useDispatch dispatches the increment and decrement actions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 8: Running Your Application
&lt;/h3&gt;

&lt;p&gt;Finally, start your Next.js application by running:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Visit &lt;a href="http://localhost:3000/counter" rel="noopener noreferrer"&gt;http://localhost:3000/counter&lt;/a&gt; to see your counter page in action. The state will persist even after page reloads, thanks to Redux Persist.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;You have successfully integrated Redux and Redux Persist into a Next.js project using the App Router. This setup allows for seamless state management with persistence across sessions. From here, you can expand this approach to manage more complex states and interactions in your application. Happy coding!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
    </item>
    <item>
      <title>GraphQL vs. REST: Choosing the Right API Strategy for your Project 🤔</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Sat, 07 Sep 2024 15:11:13 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/graphql-vs-rest-choosing-the-right-api-strategy-for-my-project-2hig</link>
      <guid>https://dev.to/sasithwarnakafonseka/graphql-vs-rest-choosing-the-right-api-strategy-for-my-project-2hig</guid>
      <description>&lt;p&gt;APIs are the backbone of modern web and mobile applications; they enable the transfer of data between servers and clients easily. In recent times, two of the most popular ways in which APIs are designed are REST-Representational State Transfer-and GraphQL-Graph Query Language. While powerful, both come with their advantages and limitations. Choosing the right one for your project may affect your application's performance, speed of development, and long-term maintainability. This article provides a detailed comparison between REST and GraphQL based on their main features, advantages, and disadvantages. It also gives recommendations as to in which cases one should be used instead of another.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;REST&lt;/strong&gt; is a traditional architecture style that bases web services on a set of constraints, relying on standard HTTP methods like GET, POST, PUT, DELETE, and PATCH to operate on resources. Each resource shall be identifiable by a unique URL (endpoint), and the server shall determine how the response shall be formatted.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of REST:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;REST is lightweight-it's rather intuitive, easy to pick up, and makes use of the usual HTTP methods. It's much easier to get people up to speed and work together as long as most developers know how it works.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching Support: REST intrinsically supports HTTP caching because it's a process of storing responses locally for repeated requests to attain performance boosts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stateless: RESTful APIs are stateless; that is, everything a server needs to understand a client's request is part of that request itself. This allows scaling quite easily because it then becomes quite easy to distribute the requests across many servers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mature Ecosystem and Tooling: REST has been around for decades; thus, it comes with huge ecosystem, and solid tooling is out there to perform debugging and monitoring testing that will help developers make sure reliability and performance are guaranteed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons of REST:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data Over-fetching or Under-fetching: REST APIs might result in over-fetching, fetching more data than what was needed, or under-fetching, where too many requests need to be fired in order to receive all the data since the response structure resides with the server-side.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This will be painful with a number of endpoints dealing with multiple and maintaining them: while scaling up the application, it becomes complex and might turn out to be a headache for any changes at the backend, meaning updating lots of these endpoints, thereby again creating some overhead about their maintenance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Limited flexibility in changing data structure: REST is not that flexible when it comes to change in data structure. Any client, though dependent upon the server for making changes, has to change the API response format, which usually makes the development and iteration quite slow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;GraphQL is both a language for APIs and an execution environment that carries out those queries by relying on types provided by your data. On the other hand, GraphQL uses one endpoint for any type of request. The clients say what they want to get as a response - just what they need.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of GraphQL:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Flexible Data Retrieval: GraphQL has the facility for clients to fetch only the data they really need; hence, no over-fetching and under-fetching problems. This will then imply efficient data usage and probably faster response times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Single endpoint for all requests: GraphQL uses a single endpoint for all requests. Consequently, it reduces complexity and hence minimizes the headaches associated with API versioning. In this respect, it turns out to be pretty helpful for dynamic and fast-changing applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strongly typed: GraphQL schemes are strongly typed, offering a clear contract between the server and the client. This therefore reduces errors, and one could more correctly guess API responses. It enhances collaboration between the front-end and the back-end teams.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-Time Data with Subscriptions: GraphQL offers subscriptions that enable fetching of data in real time. Thus, it is better suited for applications that demand this data be alive-something like chat applications, social feeds, or updates about ongoing sports events.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons of GraphQL:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Steeper Learning Curve: GraphQL demands a new query language and needs to be set up with a schema; therefore, it hugely increases the complexity and time needed at the beginning of development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching Challenges: GraphQL does not quite perform well with traditional HTTP caching; hence, more advanced caching solutions need to be implemented, such as client-side caching, or with the use of specific tools like Apollo Client.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Overhead for Simple Use Cases: For applications whose data requirements are simple, the flexibility afforded by GraphQL is overkill and brings a certain degree of unnecessary complexity into the project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Possible expensive queries: Since the client is in a position to request any combination of data from the server, there is a possibility that a client can request more data than expected. That is, the costly or inefficiency-guaranteeing queries that could degrade the performance of the server. Mitigating this calls for careful monitoring and analysis of query complexity.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Choose REST?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simple applications: REST would go for projects with simple data requirements and clearly defined resources. It's rather lightweight, considerably documented, and highly compatible with most web standards.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching to optimize performance: In case your application heavily relies on HTTP caching to manage performance with reduced server load, then REST is ideal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well-Established Projects: REST works best when the system is legacy or based on, or when a team is accustomed to working with the philosophy of REST.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Choose GraphQL?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Complex Data Requirements: GraphQL comes into its own when an application needs to fetch data from multiple sources or when clients require only subsets of a dataset.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-Time Updates and Dynamic Data: Applications that make use of updates to live dashboards or any kind of collaboration tools rely very much on GraphQL subscriptions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Agile Development and Iteration: GraphQL allows rapid development and iteration because the client can define their data needs independently of the actual results they are interested in, hence reducing changes required in the backend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mobile and Low Bandwidth Environments: GraphQL plays an important role in applications where the minimization of data transfer is of essence, such as mobile applications and low-bandwidth environments, by reducing data payload.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;GraphQL and REST have different strong points, which predominate in various situations. REST would be a good choice when you operate simple, clearly defined APIs with high caching demands, while GraphQL could act perfectly within complex applications that require flexibility and real-time features along with data fetching efficiency. Anyway, this or that could fit depending on the actual needs of your project and the level of proficiency of your team.&lt;/p&gt;

&lt;p&gt;Which API strategy should be employed depends on data complexity, the performance needs of an application, and the type of client applications. If you can weigh the pros and cons of each approach well enough, then you're on your merry way to making a rather appropriate decision that sets your project up for success.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>restapi</category>
      <category>graphql</category>
      <category>webapi</category>
    </item>
    <item>
      <title>Redux with Persistent State Rehydration in Next.js</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Tue, 31 Oct 2023 20:06:18 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/setting-up-redux-with-persistent-state-rehydration-in-nextjs-h3o</link>
      <guid>https://dev.to/sasithwarnakafonseka/setting-up-redux-with-persistent-state-rehydration-in-nextjs-h3o</guid>
      <description>&lt;p&gt;In a Next.js application, incorporating Redux for state management and persisting and rehydrating the state is crucial for ensuring a seamless user experience. This tutorial will guide you through setting up Redux and using Redux Persist for state persistence and rehydration in a Next.js application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before getting started, make sure you have Node.js and npm installed on your machine. If not, you can download and install them from the official website.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Creating a Next.js Application
&lt;/h2&gt;

&lt;p&gt;Let's begin by creating a new Next.js application. Open your terminal and execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-next-app my-next-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;my-next-app&lt;/code&gt; with the desired name for your project. This command sets up a new Next.js application for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Installing Redux and Redux Persist
&lt;/h2&gt;

&lt;p&gt;To add Redux and Redux Persist to your project, run the following command in your project's root directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;redux react-redux redux-persist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will install the necessary dependencies for Redux and Redux Persist.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Setting Up Redux
&lt;/h2&gt;

&lt;p&gt;In your Next.js project, create a folder named &lt;code&gt;redux&lt;/code&gt; inside the &lt;code&gt;src&lt;/code&gt; directory. This folder will house your Redux configuration.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;redux&lt;/code&gt; folder, create a file named &lt;code&gt;store.js&lt;/code&gt; to set up your Redux store and include Redux Persist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/redux/store.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;persistStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;persistReducer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;storage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist/lib/storage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;rootReducer&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./reducers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistedReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;persistReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rootReducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistedReducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;persistStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Create Reducers
&lt;/h2&gt;

&lt;p&gt;Inside the &lt;code&gt;redux&lt;/code&gt; folder, create a folder named &lt;code&gt;reducers&lt;/code&gt;. This is where you'll define your reducers. For this example, let's create a simple counter reducer. Inside the &lt;code&gt;reducers&lt;/code&gt; folder, create a file called &lt;code&gt;counterReducer.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/redux/reducers/counterReducer.js&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counterReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DECREMENT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;counterReducer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Creating Actions
&lt;/h2&gt;

&lt;p&gt;In your &lt;code&gt;redux&lt;/code&gt; folder, create a folder named &lt;code&gt;actions&lt;/code&gt;. Create a file named &lt;code&gt;counterActions.js&lt;/code&gt; to define your actions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/redux/actions/counterActions.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decrement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DECREMENT&lt;/span&gt;&lt;span class="dl"&gt;'&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;h2&gt;
  
  
  Step 6: Integrating Redux into Next.js
&lt;/h2&gt;

&lt;p&gt;In your Next.js application, you need to integrate Redux. Create a file named &lt;code&gt;_app.js&lt;/code&gt; in the &lt;code&gt;pages&lt;/code&gt; directory. This file is special in Next.js and wraps your entire application with global components. Import the &lt;code&gt;Provider&lt;/code&gt; component from &lt;code&gt;react-redux&lt;/code&gt;, your Redux store, and the &lt;code&gt;PersistGate&lt;/code&gt; component from &lt;code&gt;redux-persist/integration/react&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/_app.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PersistGate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist/integration/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;persistor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../src/redux/store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pageProps&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PersistGate&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;persistor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;persistor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;pageProps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/PersistGate&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;MyApp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 7: Using Redux in a Component
&lt;/h2&gt;

&lt;p&gt;You can now use Redux in your components. Let's create a simple counter component to demonstrate how Redux works. In the &lt;code&gt;pages&lt;/code&gt; directory, create a file named &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/index.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useSelector&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decrement&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../src/redux/actions/counterActions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Redux&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;())}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;())}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Decrement&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have successfully set up Redux with state persistence and rehydration in your Next.js application. With Redux Persist, your application's state will be saved and restored even after page refreshes or navigation.&lt;/p&gt;

&lt;p&gt;Feel free to expand upon this basic example and implement more advanced features as your project requires. Redux and Redux Persist offer a robust solution for state management in Next.js applications. Happy coding!&lt;/p&gt;

&lt;p&gt;Update : &lt;a href="https://dev.to/sasithwarnakafonseka/implementing-redux-with-redux-persist-in-a-nextjs-app-router-application-3d7c"&gt;Redux with Persistent State Rehydration in Next.js (App Router)&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>redux</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Node.js Architectural Patterns with Examples</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Tue, 05 Sep 2023 04:25:17 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/nodejs-architectural-patterns-with-examples-1335</link>
      <guid>https://dev.to/sasithwarnakafonseka/nodejs-architectural-patterns-with-examples-1335</guid>
      <description>&lt;p&gt;Exploring Node.js Architectural Patterns with Examples&lt;br&gt;
Node.js, with its non-blocking, event-driven architecture, has become a popular choice for building a wide range of applications. When developing with Node.js, it's essential to choose the right architectural pattern to match your project's requirements. In this article, we'll explore several Node.js architectural patterns and provide examples to illustrate their use.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. MVC (Model-View-Controller)
&lt;/h2&gt;

&lt;p&gt;The Model-View-Controller (MVC) pattern is a widely-used architectural pattern for web applications. It separates an application into three components:&lt;/p&gt;

&lt;p&gt;Model: Handles data and business logic.&lt;br&gt;
View: Handles presentation and user interface.&lt;br&gt;
Controller: Manages the interaction between Model and View.&lt;/p&gt;

&lt;p&gt;Here's a simple Node.js MVC example using Express.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();

// Model
const items = [];

// View
app.get('/items', (req, res) =&amp;gt; {
  res.json(items);
});

// Controller
app.post('/items', (req, res) =&amp;gt; {
  const newItem = req.body;
  items.push(newItem);
  res.status(201).json(newItem);
});

app.listen(3000, () =&amp;gt; {
  console.log('Server is running on port 3000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. RESTful API
&lt;/h2&gt;

&lt;p&gt;Node.js is a popular choice for building RESTful APIs. RESTful architecture follows principles such as statelessness and uniform interfaces. &lt;/p&gt;

&lt;p&gt;Here's a simple REST API example using Express.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();

app.get('/api/books', (req, res) =&amp;gt; {
  // Return a list of books
});

app.get('/api/books/:id', (req, res) =&amp;gt; {
  // Return details of a specific book
});

app.post('/api/books', (req, res) =&amp;gt; {
  // Create a new book
});

app.put('/api/books/:id', (req, res) =&amp;gt; {
  // Update a book
});

app.delete('/api/books/:id', (req, res) =&amp;gt; {
  // Delete a book
});

app.listen(3000, () =&amp;gt; {
  console.log('RESTful API server is running on port 3000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Microservices
&lt;/h2&gt;

&lt;p&gt;Microservices architecture involves breaking down a complex application into small, independent services. Each service has its own functionality and communicates with others via APIs. Node.js is well-suited for building microservices due to its lightweight nature and scalability. &lt;/p&gt;

&lt;p&gt;Here's a simplified example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Service 1
const express = require('express');
const app = express();
// Define service 1 routes and functionality

// Service 2
const express2 = require('express');
const app2 = express2();
// Define service 2 routes and functionality

// ...

app.listen(3001, () =&amp;gt; {
  console.log('Service 1 is running on port 3001');
});

app2.listen(3002, () =&amp;gt; {
  console.log('Service 2 is running on port 3002');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Real-time Applications
&lt;/h2&gt;

&lt;p&gt;Node.js is an excellent choice for real-time applications that require low-latency communication between the server and clients. Libraries like Socket.io make it easy to implement real-time features. &lt;/p&gt;

&lt;p&gt;Here's a basic chat application example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const http = require('http');
const socketIo = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIo(server);

io.on('connection', (socket) =&amp;gt; {
  console.log('A user connected');

  socket.on('chat message', (message) =&amp;gt; {
    io.emit('chat message', message);
  });

  socket.on('disconnect', () =&amp;gt; {
    console.log('A user disconnected');
  });
});

server.listen(3000, () =&amp;gt; {
  console.log('Chat server is running on port 3000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Event-Driven Architecture
&lt;/h2&gt;

&lt;p&gt;Node.js's event-driven nature makes it suitable for an Event-Driven Architecture. You can use the EventEmitter module to build systems that respond to events and asynchronous actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', () =&amp;gt; {
  console.log('An event occurred!');
});

myEmitter.emit('event');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. GraphQL
&lt;/h2&gt;

&lt;p&gt;GraphQL is a query language for APIs that allows clients to request exactly the data they need. Node.js can be used to build GraphQL servers, making it suitable for situations where clients have diverse data requirements. &lt;/p&gt;

&lt;p&gt;Here's a simplified example using the Apollo Server library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () =&amp;gt; 'Hello, world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) =&amp;gt; {
  console.log(`GraphQL server ready at ${url}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Layered Architecture
&lt;/h2&gt;

&lt;p&gt;Similar to MVC, you can organize your Node.js application into layers such as presentation, business logic, and data access. This promotes separation of concerns and maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. CQRS (Command Query Responsibility Segregation)
&lt;/h2&gt;

&lt;p&gt;In the CQRS (Command Query Responsibility Segregation) pattern, you separate the reading and writing parts of your application. Node.js can be used to build the APIs for both the command and query sides of your system.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Hexagonal Architecture
&lt;/h2&gt;

&lt;p&gt;Hexagonal Architecture emphasizes the separation of concerns and the use of ports and adapters to isolate the core application from external dependencies. Node.js can be used effectively in this pattern.&lt;/p&gt;

&lt;p&gt;The choice of architectural pattern depends on your project's specific requirements, scalability needs, and your team's familiarity with the pattern. Often, a combination of these patterns is used within a single application to address different concerns effectively.&lt;/p&gt;

&lt;p&gt;Explore these architectural patterns and choose the one that best fits your Node.js project to ensure scalability, maintainability, and performance.&lt;/p&gt;

</description>
      <category>node</category>
      <category>architectural</category>
      <category>patterns</category>
      <category>architecturalpatterns</category>
    </item>
    <item>
      <title>Next.js Image vs. HTML &lt;img&gt;: Optimizing Website Images for Performance</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Sat, 10 Jun 2023 17:06:23 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/nextjs-image-vs-html-optimizing-website-images-for-performance-30p4</link>
      <guid>https://dev.to/sasithwarnakafonseka/nextjs-image-vs-html-optimizing-website-images-for-performance-30p4</guid>
      <description>&lt;p&gt;Images are an integral part of web design, but they can significantly impact website performance if not optimized properly. In this article, we will explore the benefits of using Next.js Image compared to the standard HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element and provide examples of how you can implement similar optimizations using HTML.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Size Optimization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js Image:&lt;/p&gt;

&lt;p&gt;Next.js Image excels in size optimization by automatically serving correctly sized images for each device. It utilizes modern image formats like WebP and AVIF to reduce file sizes while maintaining quality. The component determines the width and height of the image based on the imported file, preventing layout shifts during image loading.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Image from 'next/image';
import profilePic from '../public/profile.jpg';

const MyComponent = () =&amp;gt; (
  &amp;lt;Image
    src={profilePic}
    alt="Profile Picture"
    // Width and height are automatically provided based on the imported image file
  /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;To optimize image sizes using HTML, you can resize and compress images before uploading them to your website. Additionally, specify the width and height attributes in the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag to reserve space for the image, ensuring proper layout and preventing layout shifts.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="image.jpg" alt="Description of the image" width="500" height="300" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Visual Stability&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js Image:&lt;/p&gt;

&lt;p&gt;Next.js Image addresses visual stability concerns by automatically preventing layout shifts during image loading. It reserves space for the image and applies techniques like blur-up placeholders and native browser lazy loading.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Image from 'next/image';
import profilePic from '../public/profile.jpg';

const MyComponent = () =&amp;gt; (
  &amp;lt;Image
    src={profilePic}
    alt="Profile Picture"
    // Width and height are automatically provided based on the imported image file
    // Additional props for visual stability can be added
    // e.g., placeholder="blur" for blur-up placeholders
  /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;To maintain visual stability, reserve space for the image using CSS. Set the width and height of the parent container and use CSS properties like object-fit to control how the image is displayed within its container.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div style="width: 500px; height: 300px;"&amp;gt;
  &amp;lt;img src="image.jpg" alt="Description of the image" style="object-fit: cover; width: 100%; height: 100%;" /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Lazy Loading&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js Image:&lt;/p&gt;

&lt;p&gt;Next.js Image leverages native browser lazy loading, ensuring that images are loaded only when they enter the viewport. This improves page load times, particularly for websites with numerous images.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Image from 'next/image';
import profilePic from '../public/profile.jpg';

const MyComponent = () =&amp;gt; (
  &amp;lt;Image
    src={profilePic}
    alt="Profile Picture"
    // Width and height are automatically provided based on the imported image file
    // Lazy loading is enabled by default
  /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;To implement lazy loading using HTML, use the loading="lazy" attribute in the &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag. This attribute instructs the browser to lazily load the image when it enters the viewport.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="image.jpg" alt="Description of the image" loading="lazy" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Image Optimization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next.js Image:&lt;/p&gt;

&lt;p&gt;Next.js Image provides automatic image optimization, including size reduction through modern image formats and on-demand image resizing for images stored on remote servers. It also integrates with content delivery networks (CDNs) for optimized image delivery.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Image from 'next/image';
import profilePic from '../public/profile.jpg';

const MyComponent = () =&amp;gt; (
  &amp;lt;Image
    src={profilePic}
    alt="Profile Picture"
    // Width and height are automatically provided based on the imported image file
    // Image optimization is handled by Next.js
  /&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;To optimize images using HTML, consider compressing images before uploading them to your website. Use image compression tools or services to reduce file sizes while maintaining acceptable image quality. Additionally, leverage CDNs to serve optimized versions of your images, benefiting from caching, compression, and content delivery.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While the standard HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; element offers some image optimization possibilities, Next.js Image provides a more comprehensive and streamlined solution for optimizing images within the Next.js framework. Next.js Image offers size optimization, visual stability, lazy loading, and automatic image optimization features, allowing developers to deliver high-performance websites with ease.&lt;/p&gt;

&lt;p&gt;However, if you're working with HTML, you can still achieve image optimization by resizing and compressing images, specifying width and height attributes, implementing lazy loading, and utilizing CDNs for optimized image delivery. Consider your specific requirements and the level of optimization needed when deciding between Next.js Image and HTML &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building a Laravel and Next.js Project with Docker: A Step-by-Step Guide</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Thu, 08 Jun 2023 15:29:50 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/building-a-laravel-and-nextjs-project-with-docker-a-step-by-step-guide-eih</link>
      <guid>https://dev.to/sasithwarnakafonseka/building-a-laravel-and-nextjs-project-with-docker-a-step-by-step-guide-eih</guid>
      <description>&lt;p&gt;In modern web development, combining the power of Laravel, a robust PHP framework, with Next.js, a popular React framework, can deliver an exceptional full-stack web application experience. To enhance the development process and facilitate deployment, Docker provides an efficient containerization solution. In this article, we'll explore a step-by-step guide on how to build a Laravel and Next.js project using Docker.&lt;/p&gt;

&lt;p&gt;Table of Contents:&lt;/p&gt;

&lt;p&gt;1.Prerequisites&lt;br&gt;
2.Setting up Laravel Backend with Docker&lt;br&gt;
 2.1 Dockerizing the Laravel Backend&lt;br&gt;
 2.2 Defining the Docker Compose File&lt;br&gt;
 2.3 Establishing Communication with the Next.js Frontend&lt;br&gt;
3.Building the Next.js Frontend with Docker&lt;br&gt;
 3.1 Dockerizing the Next.js Frontend&lt;br&gt;
 3.2 Defining the Docker Compose File&lt;br&gt;
 3.3 Connecting to the Laravel Backend&lt;br&gt;
4.Deploying the Docker Containers&lt;br&gt;
5.Conclusion&lt;/p&gt;

&lt;p&gt;Section 1: Prerequisites&lt;/p&gt;

&lt;p&gt;Before diving into the implementation, it's essential to ensure you have the necessary tools installed, including Docker, Composer, and Node.js.&lt;/p&gt;

&lt;p&gt;Section 2: Setting up Laravel Backend with Docker&lt;/p&gt;

&lt;p&gt;2.1 Dockerizing the Laravel Backend:&lt;/p&gt;

&lt;p&gt;Create a Dockerfile in the root directory of your Laravel project with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM php:8.0-fpm

WORKDIR /var/www/html

RUN apt-get update &amp;amp;&amp;amp; apt-get install -y \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip \
    git \
    nano

RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

COPY . /var/www/html

RUN chown -R www-data:www-data /var/www/html/storage

CMD ["php-fpm"]


EXPOSE 9000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.2 Defining the Docker Compose File:&lt;/p&gt;

&lt;p&gt;Create a docker-compose.yml file in the project's root directory with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:9000"
    volumes:
      - .:/var/www/html
    depends_on:
      - db

  db:
    image: mariadb:10.6
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: newsDb
      MYSQL_USER: root
      MYSQL_PASSWORD: secretnewsDb
      MYSQL_ROOT_PASSWORD: secretnewsDb
    volumes:
      - ./mysql:/var/lib/mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.3 Establishing Communication with the Next.js Frontend:&lt;/p&gt;

&lt;p&gt;Update your Laravel application's API endpoints to point to the Docker network and the correct Laravel container URL. Make sure to use the appropriate container names or aliases defined in the Docker Compose file (e.g., &lt;a href="http://laravel-container:9000/api" rel="noopener noreferrer"&gt;http://laravel-container:9000/api&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Section 3: Building the Next.js Frontend with Docker&lt;/p&gt;

&lt;p&gt;3.1 Dockerizing the Next.js Frontend:&lt;/p&gt;

&lt;p&gt;Create a Dockerfile in the root directory of your Next.js project with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:16-alpine
RUN mkdir -p /app
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.2 Defining the Docker Compose File:&lt;/p&gt;

&lt;p&gt;Update the docker-compose.yml file in the project's root directory with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - .:/app
    depends_on:
      - laravel

  laravel:
    # Configuration for Laravel backend service

  db:
    # Configuration for the database service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.3 Connecting to the Laravel Backend:&lt;/p&gt;

&lt;p&gt;Update your Next.js application's API endpoints to point to the Docker network and the correct Laravel container URL. Make sure to use the appropriate container names or aliases defined in the Docker Compose file (e.g., &lt;a href="http://laravel-container:9000/api" rel="noopener noreferrer"&gt;http://laravel-container:9000/api&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Section 4: Deploying the Docker Containers&lt;/p&gt;

&lt;p&gt;To deploy the Docker containers, open a terminal in the project's root directory and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will build the Docker images for both the Laravel backend and Next.js frontend and start the containers. Once the containers are up and running, open another terminal and follow the steps below to set up the database for the Laravel backend.&lt;/p&gt;

&lt;p&gt;4.1 Accessing the Laravel Backend Container:&lt;/p&gt;

&lt;p&gt;Open a new terminal window and run the following command to access the running Laravel backend container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose exec app bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will open a bash session inside the Laravel container.&lt;/p&gt;

&lt;p&gt;4.2 Running Database Migrations:&lt;/p&gt;

&lt;p&gt;Once you are inside the Laravel container, run the following command to execute the database migrations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create the necessary database tables based on the migration files defined in your Laravel project.&lt;/p&gt;

&lt;p&gt;4.3 Verifying the Database Setup:&lt;/p&gt;

&lt;p&gt;After running the migrations, you can verify the database setup by accessing the MySQL container. Open another terminal window and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose exec db bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will open a bash session inside the MySQL container. Once inside the container, you can use the MySQL command-line client to connect to the database and perform any necessary checks or queries.&lt;/p&gt;

&lt;p&gt;Section 5: Conclusion&lt;/p&gt;

&lt;p&gt;In this article, we explored a step-by-step guide on building a Laravel and Next.js project with Docker. By Dockerizing both the Laravel backend and Next.js frontend, establishing communication between the containers, and deploying the application, you can streamline your development and deployment processes. Leveraging Docker's containerization capabilities allows for efficient collaboration and scalability.&lt;/p&gt;

&lt;p&gt;Note: This article provides a high-level overview of building a Laravel and Next.js project with Docker. For detailed instructions, code examples, and further exploration, it's recommended to refer to the official documentation of Laravel, Next.js, and Docker.&lt;/p&gt;

&lt;p&gt;GitHub Repositories:&lt;/p&gt;

&lt;p&gt;Laravel Backend Example: &lt;a href="https://github.com/sasithwarnakafonseka/backend-laravel-api" rel="noopener noreferrer"&gt;https://github.com/sasithwarnakafonseka/backend-laravel-api&lt;/a&gt;&lt;br&gt;
Next.js Frontend Example: &lt;a href="https://github.com/sasithwarnakafonseka/react-news-app" rel="noopener noreferrer"&gt;https://github.com/sasithwarnakafonseka/react-news-app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>laravel</category>
      <category>docker</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Building Maintainable Next.js Applications with SOLID Principles</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Thu, 08 Jun 2023 06:13:18 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/building-maintainable-nextjs-applications-with-solid-principles-16g3</link>
      <guid>https://dev.to/sasithwarnakafonseka/building-maintainable-nextjs-applications-with-solid-principles-16g3</guid>
      <description>&lt;p&gt;Next.js is a powerful framework for building React applications, providing server-side rendering, static site generation, and a wealth of features. When combined with SOLID principles, Next.js enables the development of highly maintainable, extensible, and robust applications. In this article, we will explore how to apply SOLID principles to Next.js development, with practical examples to demonstrate their effectiveness.&lt;/p&gt;

&lt;p&gt;Single Responsibility Principle (SRP):&lt;br&gt;
The Single Responsibility Principle states that a component or module should have a single responsibility. In Next.js, we can apply this principle by breaking down our code into smaller, focused components and pages. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UserProfile.js
const UserProfile = ({ user }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{user.name}&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;{user.bio}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the UserProfile component is responsible for rendering the user profile information, focusing solely on its presentation without handling any data fetching or state management.&lt;/p&gt;

&lt;p&gt;Open-Closed Principle (OCP):&lt;br&gt;
The Open-Closed Principle suggests that code should be open for extension but closed for modification. In Next.js, we can achieve this by using abstractions and interfaces. Let's consider an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Button.js
const Button = ({ onClick, children }) =&amp;gt; {
  return &amp;lt;button onClick={onClick}&amp;gt;{children}&amp;lt;/button&amp;gt;;
};
&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;// PrimaryButton.js
const PrimaryButton = ({ onClick, children }) =&amp;gt; {
  return (
    &amp;lt;Button onClick={onClick} className="primary-button"&amp;gt;
      {children}
    &amp;lt;/Button&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the PrimaryButton component extends the behavior of the base Button component by adding a specific class name and style. By leveraging the OCP, we can create reusable components that can be easily extended without modifying the existing code.&lt;/p&gt;

&lt;p&gt;Liskov Substitution Principle (LSP):&lt;br&gt;
The Liskov Substitution Principle ensures that derived classes can be used interchangeably with their base classes without affecting the correctness of the program. In Next.js, we can demonstrate this principle using component inheritance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// BaseLayout.js
const BaseLayout = ({ children }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;header&amp;gt;Header&amp;lt;/header&amp;gt;
      {children}
      &amp;lt;footer&amp;gt;Footer&amp;lt;/footer&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

&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;// ExtendedLayout.js
const ExtendedLayout = ({ children }) =&amp;gt; {
  return (
    &amp;lt;BaseLayout&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Extended Layout&amp;lt;/h1&amp;gt;
        {children}
      &amp;lt;/div&amp;gt;
    &amp;lt;/BaseLayout&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the ExtendedLayout component extends the behavior of the BaseLayout component by adding additional content. The ExtendedLayout can be used interchangeably with the BaseLayout component without breaking the expected layout structure or functionality.&lt;/p&gt;

&lt;p&gt;Interface Segregation Principle (ISP):&lt;br&gt;
The Interface Segregation Principle suggests that client-specific interfaces should be preferred over general-purpose interfaces. In Next.js, we can follow this principle by creating focused and specific interfaces for our components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UserCard.js
const UserCard = ({ user }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;{user.name}&amp;lt;/h2&amp;gt;
      &amp;lt;p&amp;gt;{user.email}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the UserCard component has a specific interface tailored to the user-related information it needs to display. By adhering to the ISP, we ensure that components have precise interfaces, reducing unnecessary dependencies and potential coupling.&lt;/p&gt;

&lt;p&gt;Dependency Inversion Principle (DIP):&lt;br&gt;
The Dependency Inversion Principle states that high-level modules should not depend on low-level modules. Both should depend on abstractions. In Next.js, we can implement the DIP through dependency injection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UserDataProvider.js
import { useEffect, useState } from 'react';

const UserDataProvider = ({ children }) =&amp;gt; {
  const [users, setUsers] = useState([]);

  useEffect(() =&amp;gt; {
    fetchUsers().then((data) =&amp;gt; setUsers(data));
  }, []);

  return &amp;lt;&amp;gt;{children(users)}&amp;lt;/&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the UserDataProvider component fetches user data and provides it to its children as a render prop. By separating the data fetching logic from the components that rely on it, we adhere to the DIP, allowing for easier testing and flexibility.&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
By applying SOLID principles to Next.js development, we can create maintainable, flexible, and scalable applications. The Single Responsibility Principle helps us create focused components and pages. The Open-Closed Principle promotes extensibility through abstractions. The Liskov Substitution Principle ensures interchangeability of derived and base components. The Interface Segregation Principle allows for specific and client-focused interfaces. Finally, the Dependency Inversion Principle encourages dependency injection for better modularity and testability. By embracing SOLID principles in Next.js development, we can build robust and maintainable applications that can easily evolve and scale over time.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>SOLID Principles</title>
      <dc:creator>Sasith Warnaka</dc:creator>
      <pubDate>Mon, 17 Apr 2023 15:09:32 +0000</pubDate>
      <link>https://dev.to/sasithwarnakafonseka/solid-principals-2l0o</link>
      <guid>https://dev.to/sasithwarnakafonseka/solid-principals-2l0o</guid>
      <description>&lt;p&gt;Turning ideas into reality through software is a logical art form that must be done with great care. Building sophisticated software is fun and challenging, but maintaining and updating poorly written software is even more difficult and frustrating. The methodology for writing good, well-structured code is to follow and adhere to a set of principles known as the SOLID Principles.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;SOLID&lt;/strong&gt; principle was developed by computer scientist Robert J. Martin in an article published in 2000. Since then, SOLID principles have been widely adopted and are considered the basis for good software design. Consider the abbreviation SOLID. &lt;/p&gt;

&lt;h3&gt;
  
  
  (E) Principle of Sole Liability
&lt;/h3&gt;

&lt;p&gt;A class can serve only one purpose. That is, a class can perform only one type of operation, such as database logic, logging logic, and so on. This principle ensures that each class focuses on a single responsibility and does not burden the code with extra work that makes it difficult to modify or maintain. For example, consider a class responsible for recording and processing data. This violates the single responsibility principle because the class has two responsibilities. I recommend creating two separate classes, one for logging and one for handling data, each with its own responsibilities.&lt;/p&gt;

&lt;h3&gt;
  
  
  (O) pen/closing principle
&lt;/h3&gt;

&lt;p&gt;Classes should be open for extension and closed for modification. That is, existing classes must be extensible without having to be rewritten to implement new functionality. This principle promotes the use of abstractions that enable flexible, modular code that can be extended without changing existing code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example&lt;/strong&gt;, let's take a class that generates a report. Instead of modifying the existing class whenever a new report type is added, it is recommended that you create a new class that inherits from the original report class and adds new functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  (L)iskov's substitution principle
&lt;/h3&gt;

&lt;p&gt;It should be easy to replace parent classes with child classes. That is, if the Rectangle class is a subtype of the Shape class, Shape can be replaced by Rectangle without breaking the functional flow. This principle promotes the use of inheritance in a way that preserves system functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example&lt;/strong&gt;, consider a class hierarchy where the base class is Image and the derived classes are Rectangle and Circle. The Liskow substitution principle should allow code to use any expected shape, such as a rectangle or a circle, without causing problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  (I) Principle of interfacial separation
&lt;/h3&gt;

&lt;p&gt;Interfaces should not force classes to implement functionality they do not support. This means that larger interfaces must be split into smaller interfaces to provide support. This principle promotes the use of small, focused interfaces specific to the requirements of each class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example&lt;/strong&gt;, take an interface with 10 methods. If a class needs to implement only two of these methods, consider splitting the interface into two sub-interfaces, each containing only the necessary methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  (D) Principle of inversion of dependence
&lt;/h3&gt;

&lt;p&gt;Classes should depend on abstractions, but not specifications. That is, higher-level modules should not depend on lower-level modules. Both must be based on abstraction. This principle uses interfaces to separate classes and promotes modular design. For example, consider a class that depends on a database. Instead of using the database classes directly, it is recommended to create an interface with the database and make the classes depend on the interface. This promotes loose coupling and enables flexible code.&lt;/p&gt;

&lt;p&gt;Finally, the SOLID principles provide guidelines for writing maintainable and extensible software. Following these principles helps software engineers create code that is easier to understand, adapt, and maintain over time. By applying these principles, software engineers can create high-quality software.&lt;/p&gt;

</description>
      <category>solid</category>
      <category>development</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
