<?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>Forem: João Paulo</title>
    <description>The latest articles on Forem by João Paulo (@joaoreider).</description>
    <link>https://forem.com/joaoreider</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%2F1016146%2F446af906-3109-4ee9-aa75-8884d1704ce6.jpeg</url>
      <title>Forem: João Paulo</title>
      <link>https://forem.com/joaoreider</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/joaoreider"/>
    <language>en</language>
    <item>
      <title>CAP Theorem: The Choice Between Consistency and Availability in Distributed Systems</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Sun, 22 Sep 2024 14:32:29 +0000</pubDate>
      <link>https://forem.com/joaoreider/cap-theorem-the-choice-between-consistency-and-availability-in-distributed-systems-30pk</link>
      <guid>https://forem.com/joaoreider/cap-theorem-the-choice-between-consistency-and-availability-in-distributed-systems-30pk</guid>
      <description>&lt;p&gt;The CAP Theorem is a concept in distributed systems that helps us understand the trade-offs between Consistency, Availability and Partition Tolerance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consistency&lt;/strong&gt; means that all clients see the same data, regardless of which instance of the application they are connected to. In other words, the information is the same across all points in the system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Availability&lt;/strong&gt; refers to the system's ability to respond to user requests at all times. This means that even if something goes wrong, the system should remain operational and accessible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partition Tolerance&lt;/strong&gt; is the ability of the system to continue functioning even when network failures occur that prevent communication between nodes.&lt;/p&gt;

&lt;p&gt;When a partition problem occurs in the network, the nodes in the distributed system may become unable to communicate. In these cases, we have to make a choice: if we prioritize consistency, the system may lose availability; if we prioritize availability, the nodes may operate inconsistently until the partition is resolved.&lt;/p&gt;

&lt;p&gt;The CAP Theorem helps us understand, at a high level, the limitations of distributed systems, especially regarding consistency, availability, and partition tolerance. It's important to remember that the theorem simplifies the real complexity of systems and the difficulty of achieving an ideal balance among these three properties in practical scenarios.&lt;/p&gt;

&lt;p&gt;Understanding the CAP Theorem is crucial for software architects and developers, as it forces us to make informed decisions about how to design our systems. Ultimately, each choice will have a direct impact on the user experience and the overall effectiveness of the system.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>captheorem</category>
      <category>softwareengineering</category>
      <category>backend</category>
    </item>
    <item>
      <title>Idempotency Explained: Ensuring Reliable API Calls. A practical example in Nestjs</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Sat, 14 Sep 2024 22:42:29 +0000</pubDate>
      <link>https://forem.com/joaoreider/idempotency-explained-ensuring-reliable-and-repeated-api-calls-in-nestjs-5emc</link>
      <guid>https://forem.com/joaoreider/idempotency-explained-ensuring-reliable-and-repeated-api-calls-in-nestjs-5emc</guid>
      <description>&lt;p&gt;Idempotency refers to an operation that can be performed multiple times without causing additional effects. In other words, no matter how many times the same action is repeated, the result will always be the same as if it had been executed only once. &lt;/p&gt;

&lt;p&gt;Idempotency ensures that the operation is only processed once, even if it's executed multiple times, preventing duplicate payments or unintended outcomes. This is particularly useful in scenarios where operations, such as processing a credit transaction, might be retried due to network failures or system crashes. &lt;/p&gt;

&lt;p&gt;In the context of web development, certain HTTP methods are idempotent and others no.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt;: Retrieving a resource using a GET request should not change the state of the resource on the server. Whether you request the same resource once or multiple times, the result should be the same.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt;: Updating a resource using a PUT request should result in the same resource state regardless of how many times the request is made with the same data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some HTTP methods are &lt;strong&gt;not idempotent&lt;/strong&gt;, meaning that repeated applications of the same request can lead to different results. Two common examples are POST and PATCH:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt;: The POST method is typically used to create new resources or submit data. Each POST request can create a new resource or initiate a new action, so sending the same POST request multiple times can result in multiple new resources or actions being created.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PATCH&lt;/strong&gt;: The PATCH method is used to apply partial modifications to a resource. Since the PATCH request updates only certain fields, the outcome can change with each request, depending on the state of the resource before the update.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Idempotency Matters
&lt;/h2&gt;

&lt;p&gt;Idempotency is crucial in systems where reliability and consistency are important. It helps in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fault Tolerance&lt;/strong&gt;: Idempotent operations ensure that retrying operations won’t lead to inconsistent states or unintended effects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: Designing systems with idempotent operations can simplify error handling and recovery processes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt;: Idempotency helps in managing concurrent operations, reducing the risk of conflicting changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementing an idempotent endpoint in Nestjs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Create the Idempotency Middleware&lt;/strong&gt;&lt;/p&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;Injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;NestMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;BadRequestException&lt;/span&gt;&lt;span class="p"&gt;,&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;@nestjs/common&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;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NextFunction&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;express&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;IdempotencyService&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;../services/idempotency.service&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="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;IdempotencyMiddleware&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;NestMiddleware&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;idempotencyService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IdempotencyService&lt;/span&gt;&lt;span class="p"&gt;)&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NextFunction&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;idempotencyKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-idempotency-key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BadRequestException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Idempotency key is missing&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cachedKey&lt;/span&gt; &lt;span class="o"&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="nx"&gt;idempotencyService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cachedKey&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="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;400&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="nx"&gt;idempotencyService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;saveKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idempotencyKey&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="nf"&gt;next&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. Create the Idempotency Service&lt;/strong&gt;&lt;/p&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;Inject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Injectable&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;@nestjs/common&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;iDistributedCacheService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;IDistributedCacheService&lt;/span&gt;&lt;span class="p"&gt;,&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;./distributed-cache.service&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;TEN_MINUTES&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;../constants/app.constants&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="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;IdempotencyService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iDistributedCacheService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;distributedCache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IDistributedCacheService&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;async&lt;/span&gt; &lt;span class="nf"&gt;getKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&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;return&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="nx"&gt;distributedCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&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;saveKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;distributedCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TEN_MINUTES&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Note, in this case i used a general cache service, you can store it in a simple HashMap local or something more robust like redis&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;3. Register the middleware in your app module&lt;/strong&gt;&lt;/p&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;MiddlewareConsumer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RequestMethod&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;@nestjs/common&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;TransactionsModule&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;./modules/transactions/transactions.module&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;PrismaModule&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;./modules/prisma/prisma.module&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;PrismaService&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;./modules/prisma/prisma.service&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;ClientsModule&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;./modules/clients/clients.module&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;ConfigModule&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;@nestjs/config&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;IdempotencyMiddleware&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;./libs/commons/middlewares/idempotency.middleware&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;RedisDistributedCacheService&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;./libs/commons/services/redis-distributed-cache.service&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;iDistributedCacheService&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;./libs/commons/services/distributed-cache.service&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;IdempotencyService&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;./libs/commons/services/idempotency.service&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="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="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;IdempotencyService&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;span class="nf"&gt;configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MiddlewareConsumer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;consumer&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;IdempotencyMiddleware&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoutes&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transactions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RequestMethod&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Yow can now test the implemetation. Make a POST request to the route that you registered with the &lt;em&gt;x-idempotency-key&lt;/em&gt; in header. The server should not proccess duplicate requests.&lt;/p&gt;

&lt;p&gt;Thank you for reaching this point. If you need to contact me, here is my email: &lt;a href="mailto:joaopauloj405@gmail.com"&gt;joaopauloj405@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Must know concept for Backend Developers: Database transactions</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Mon, 02 Sep 2024 13:23:53 +0000</pubDate>
      <link>https://forem.com/joaoreider/must-know-concept-for-backend-developers-database-transactions-1pkc</link>
      <guid>https://forem.com/joaoreider/must-know-concept-for-backend-developers-database-transactions-1pkc</guid>
      <description>&lt;p&gt;Imagine you want to transfer money to your mother. You will need to withdraw money from your account and send it to hers. &lt;/p&gt;

&lt;p&gt;You withdraw the money from your account, but when you go to make the transfer, the operation fails. You check your account and the money is no longer there, but it also hasn't reached your mother's account. &lt;/p&gt;

&lt;p&gt;How can you prevent this from happening? &lt;strong&gt;Database transactions&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;A database transaction is a sequence of operations performed within a database management system as a single logical unit of work.&lt;/p&gt;

&lt;p&gt;Transactions are closely related to database integrity and ensure the consistent state of a database even in the event of failure. &lt;/p&gt;

&lt;h2&gt;
  
  
  How it works?
&lt;/h2&gt;

&lt;p&gt;The flow is simple and usually adopts an all or nothing policy: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Begin the transaction&lt;/li&gt;
&lt;li&gt;Execute the operations (data manipulations, queries, etc)&lt;/li&gt;
&lt;li&gt;If no errors, commit the transaction, if an error occurs, rollback.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;h2&gt;
  
  
  Typescript Example:
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transaction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Payable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Transaction&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;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;$transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prisma&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createdTransaction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;data&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="nx"&gt;transaction&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;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;data&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="nx"&gt;payable&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;return&lt;/span&gt; &lt;span class="nx"&gt;createdTransaction&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>typescript</category>
      <category>database</category>
      <category>backend</category>
      <category>prisma</category>
    </item>
    <item>
      <title>How the event loop enables asynchronous programming</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Sun, 14 Jul 2024 19:48:03 +0000</pubDate>
      <link>https://forem.com/joaoreider/how-the-event-loop-enables-asynchronous-programming-488p</link>
      <guid>https://forem.com/joaoreider/how-the-event-loop-enables-asynchronous-programming-488p</guid>
      <description>&lt;p&gt;Javascript is a single-threaded programming language (only runs one thread at a time), and runs its code &lt;strong&gt;sequentially&lt;/strong&gt;. With the &lt;strong&gt;event loop&lt;/strong&gt; we can execute tasks in more threads. Understanding this component allows us to create more efficient and performant programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does event loop work?
&lt;/h2&gt;

&lt;p&gt;Before talking about the event loop, it is necessary to talk about the &lt;strong&gt;call stack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The call stack is the structure that allows the ordering and execution of our program. It is also the one that registers function calls.&lt;/p&gt;

&lt;p&gt;As said, Javascript runs the code step by step sequentially. First, it allocates memory for variables and then executes the functions at the top of the stack. This flow has a problem: if a function takes a long time to respond, it will &lt;strong&gt;block other functions&lt;/strong&gt; from performing their tasks until it responds.&lt;/p&gt;

&lt;p&gt;In real applications, these functions are typically linked to api calls.&lt;/p&gt;

&lt;p&gt;To solve this problem we use &lt;strong&gt;Javascript Web Apis&lt;/strong&gt;. With this, we have access to non-blocking functions that run on their own threads and not on the main one and thus do not block others from executing their tasks.&lt;/p&gt;

&lt;p&gt;The event loop is what allows JavaScript to perform asynchronous operations. It monitors the call stack and the task queue. The event loop continually checks whether the call stack is empty. If so, it takes the first task from the task queue and moves it to the call stack, where it is executed&lt;/p&gt;

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

&lt;p&gt;Therefore, thanks to the event loop, we can perform operations efficiently and, most importantly, without blocking the execution of the main code.&lt;/p&gt;

&lt;p&gt;Thank you for reaching this point. If you need to contact me, here is my email: &lt;a href="mailto:joaopauloj405@gmail.com"&gt;joaopauloj405@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A little about promises in Javascript</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Sun, 14 Jul 2024 00:53:51 +0000</pubDate>
      <link>https://forem.com/joaoreider/a-little-about-promises-in-javascript-4oh7</link>
      <guid>https://forem.com/joaoreider/a-little-about-promises-in-javascript-4oh7</guid>
      <description>&lt;p&gt;JavaScript is a &lt;strong&gt;synchronous&lt;/strong&gt; programming language; however, due to the callback functions, we can make it work as an asynchronous programming language.&lt;/p&gt;

&lt;p&gt;The concept of promises are very similar to promises we make in real life, we guarantee that something will be done. A promise can be &lt;strong&gt;kept&lt;/strong&gt; or &lt;strong&gt;broken&lt;/strong&gt; and you hope to know what the promise ended up being.&lt;/p&gt;

&lt;p&gt;In terms of code, this is precisely what occurs. Promises can assume the statuses of pending, resolved, or rejected.&lt;/p&gt;

&lt;p&gt;The promise object possesses both &lt;strong&gt;prototype methods&lt;/strong&gt; and &lt;strong&gt;static methods&lt;/strong&gt;. The prototypes are those that can only be applied to instances of the promise class. There are three prototypes: then, catch, finally. They all return a promise and handle the states of onFulfilled, onRejected, onFinally, respectively.&lt;/p&gt;

&lt;p&gt;There are 4 static methods: &lt;strong&gt;reject&lt;/strong&gt; and &lt;strong&gt;resolve&lt;/strong&gt;, which help you create resolved or rejected promises and &lt;strong&gt;all&lt;/strong&gt; and &lt;strong&gt;race&lt;/strong&gt;. “all” receives an array of promises and returns a promise that resolves when all past promises are resolved or a promise that rejects with the reason for the first promise that was rejected. “race” also receives an array of promises and returns a promise that is resolved or rejected as soon as one of the passed promises is resolved or rejected. All promises run in parallel. This is an interesting feature that allows you to improve the performance of your code.&lt;/p&gt;

&lt;p&gt;In modern JavaScript code, you will see a lot of use of &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt;, which were introduced to simplify promise management with much more readable code. Async declares a function as asynchronous and await waits for promises to be resolved, pausing execution until the promise is resolved or rejected. This new nomenclature is more modern and avoids the famous “callback hell” (deep nesting of callbacks) of javascript.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Callback hell Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function main() {
  getUserData(1, (err, userData) =&amp;gt; {
    if (err) {
      console.error(err);
      return;
    }
    getUserOrders(userData.userId, (err, orders) =&amp;gt; {
      if (err) {
        console.error(err);
        return;
      }
      getOrderProducts(orders[0].orderId, (err, products) =&amp;gt; {
        if (err) {
          console.error(err);
          return;
        }
        processProducts(products, (err, result) =&amp;gt; {
          if (err) {
            console.error(err);
            return;
          }
          console.log(result);
        });
      });
    });
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;async/await&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function main() {
  try {
    const userData = await getUserData(1);
    const orders = await getUserOrders(userData.userId);
    const products = await getOrderProducts(orders[0].orderId);
    const result = await processProducts(products);
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, Promises are a powerful tool in JavaScript for handling &lt;strong&gt;asynchronous operations&lt;/strong&gt;, providing a clear and structured way to manage the flow of asynchronous code execution. With the introduction of async/await, promise management has become even more intuitive, allowing developers to write asynchronous code that resembles synchronous code in terms of readability and maintainability. Understanding and using promises effectively is essential for developing modern and efficient JavaScript applications.&lt;/p&gt;

&lt;p&gt;Thank you for reaching this point. If you need to contact me, here is my email: &lt;a href="mailto:joaopauloj405@gmail.com"&gt;joaopauloj405@gmail.com&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The interesting regex for Identifying Prime Numbers</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Fri, 28 Jun 2024 13:33:34 +0000</pubDate>
      <link>https://forem.com/joaoreider/the-interesting-regex-for-identifying-prime-numbers-405h</link>
      <guid>https://forem.com/joaoreider/the-interesting-regex-for-identifying-prime-numbers-405h</guid>
      <description>&lt;p&gt;Prime numbers are numbers greater than 1 that have no positive divisors other than 1 and themselves.&lt;/p&gt;

&lt;p&gt;The regex is &lt;code&gt;^1?$|^(11+?)\1+$&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's breaking Down...&lt;/p&gt;

&lt;p&gt;First you need to know the meaning of these 3 symbols:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;^&lt;/strong&gt;: &lt;em&gt;start of the string&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;$&lt;/strong&gt;: &lt;em&gt;end of the string&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;|&lt;/strong&gt;: &lt;em&gt;OR operator&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we have two components in the main regex:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1?&lt;/code&gt;: The literal “1” and the question mark "?" for match the previous char zero or one times. So if we have zero characters or “1” it will match. Soon we will know the reason for this pattern here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(11+?)\1+&lt;/code&gt;: The second pattern. &lt;code&gt;(11+?)&lt;/code&gt; is a group and matches any string that starts with “11” by one or more “1”s. The “+?” makes it non-greedy, meaning it matches as few characters as possible. &lt;br&gt;
&lt;code&gt;\1+&lt;/code&gt; capture the same text again with as many characters as possible. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;E.g. So for the number  '111111', the pattern '11' is repeated three times. For the number 5 ('11111'), there is no way to split it into repeated sub-patterns.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the interesting thing is that we found how to evenly divide repeated sub patterns. &lt;br&gt;
In the same number '111111', the pattern  '111' is also repeated twice and this is captured by regex.&lt;br&gt;
For prime numbers, the string cannot be divided evenly into repeating sub patterns.&lt;/p&gt;

&lt;p&gt;Ah, and  first pattern (1?) handles the non-prime cases of 0 and 1.&lt;/p&gt;

&lt;p&gt;Thank you for reaching this point. If you need to contact me, here is my email: &lt;a href="mailto:joaopauloj405@gmail.com"&gt;joaopauloj405@gmail.com&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sapere aude&lt;/em&gt;&lt;/p&gt;

</description>
      <category>regex</category>
      <category>math</category>
    </item>
    <item>
      <title>Understand Dependency Injection in NestJs in a simple way</title>
      <dc:creator>João Paulo</dc:creator>
      <pubDate>Fri, 21 Jun 2024 14:01:44 +0000</pubDate>
      <link>https://forem.com/joaoreider/understand-dependency-injection-in-nestjs-in-a-simple-way-fd9</link>
      <guid>https://forem.com/joaoreider/understand-dependency-injection-in-nestjs-in-a-simple-way-fd9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Dependency injection&lt;/strong&gt; is when an object provides the dependency of another object. This means that a class relies on methods from another class. It is a well-known design pattern, and Nest is built strongly based on this pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inversion of control&lt;/strong&gt; is a concept that aligns with dependency injection. It is basically delegating to the framework the construction of the project's dependencies.&lt;/p&gt;

&lt;p&gt;The purpose of injection is to invert control, providing the dependencies of the object instead of allowing it to create them itself. For this, it is necessary to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Explicitly define the dependencies and where the injection occurs. In Nest, for example, this point is in the constructor. This is where the framework inverts control. 
&lt;code&gt;constructor(private catsService: CatsService) {}
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Have an inversion of control container for dependency injection to occur. In Nest, it is possible to access this container through the module and retrieve the instance of the class we want.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In short, what is done is transferring the task of instantiating the object to someone else (IoC) and directly using that instance as a dependency (Injection).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt; of using this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ease of testing the application (we can inject mocked services)&lt;/li&gt;
&lt;li&gt;Improved code maintenance (reduction of code and decoupling)&lt;/li&gt;
&lt;li&gt;Reusability (just inject the dependency to use it)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;: There are few, but generally:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increased complexity in the project&lt;/li&gt;
&lt;li&gt;Service construction logic can be in inaccessible places&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reaching this point. If you need to contact me, here is my email: &lt;a href="mailto:joaopauloj405@gmail.com"&gt;joaopauloj405@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://www.digitalocean.com/community/tutorials/a-guide-on-dependency-injection-in-nestjs" rel="noopener noreferrer"&gt;https://www.digitalocean.com/community/tutorials/a-guide-on-dependency-injection-in-nestjs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://martinfowler.com/articles/injection.html" rel="noopener noreferrer"&gt;https://martinfowler.com/articles/injection.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=GX8YC21qQQk&amp;amp;t=1010s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=GX8YC21qQQk&amp;amp;t=1010s&lt;/a&gt;&lt;/p&gt;



</description>
      <category>nestjs</category>
      <category>designpatterns</category>
      <category>dependencyinjection</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
