<?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: Asif</title>
    <description>The latest articles on DEV Community by Asif (@asifroyal).</description>
    <link>https://dev.to/asifroyal</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%2F100355%2Fb13442b4-7ff2-42b4-933a-11bf1931618b.jpg</url>
      <title>DEV Community: Asif</title>
      <link>https://dev.to/asifroyal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asifroyal"/>
    <language>en</language>
    <item>
      <title>Enhancing Backend Security: Safeguarding Firebase App Check for Flutter Apps</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Fri, 13 Oct 2023 08:07:05 +0000</pubDate>
      <link>https://dev.to/asifroyal/enhancing-backend-security-safeguarding-firebase-app-check-for-flutter-apps-14p9</link>
      <guid>https://dev.to/asifroyal/enhancing-backend-security-safeguarding-firebase-app-check-for-flutter-apps-14p9</guid>
      <description>&lt;p&gt;In the realm of mobile app development, ensuring robust security for backend resources is paramount. Firebase App Check stands as a pivotal security feature that ensures exclusive access to resources within your Firebase project. While its primary role is to fortify Firebase-related services, there exists a compelling need to extend its protective mantle over non-Firebase backends and APIs utilized by your Flutter app. This article delves into the intriguing concept of employing Firebase App Check to shield custom backends and REST APIs that underlie your Flutter application.&lt;/p&gt;

&lt;h2&gt;
  
  
  App Check: A Bird's-Eye View:
&lt;/h2&gt;

&lt;p&gt;Firebase App Check operates by generating tokens that are app-specific and undergo verification by Firebase services to establish the authenticity of incoming requests. However, when it comes to safeguarding custom APIs, a more intricate choreography is required, encompassing both the client-side and server-side realms.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Intricate Dance: Protecting Custom APIs
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Client-Side Configuration&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To initiate the protective ballet, your Flutter app must first be outfitted to conjure App Check tokens and enfold them within API requests. This necessitates the integration of the app_check plugin and invocation of &lt;code&gt;initializeAppCheck()&lt;/code&gt; on your FirebaseApp instance.&lt;/p&gt;

&lt;p&gt;The culmination of this setup allows for the generation of App Check tokens, subsequently woven into the fabric of your backend API requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;   &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;FirebaseAppCheck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token then adorns your API request as a header or parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;   &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="s"&gt;"/api/user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="s"&gt;"X-Firebase-AppCheck"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;token&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;ol&gt;
&lt;li&gt;
&lt;strong&gt;Server-Side Verification&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As the performance unfolds on the server stage, App Check tokens demand validation, a task deftly performed by the Firebase Admin SDK. For the Node.js domain, the rhapsody goes as follows:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;admin&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;firebase-admin&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;verifyToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&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;claims&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;admin&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appCheck&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;verifyToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

     &lt;span class="c1"&gt;// Token attains its zenith of validity sans any error&lt;/span&gt;
     &lt;span class="c1"&gt;// Employ claims.uid for user identification&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For other arenas, a gaze upon the Admin SDK documentation will illuminate the path toward token verification. This validation operetta orchestrates trust in the app's origin, with a rhythmic Express middleware weaving the narrative:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;checkAppToken&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;res&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="p"&gt;{&lt;/span&gt;

     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&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-firebase-appcheck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

     &lt;span class="k"&gt;try&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;claims&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appCheck&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;verifyToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;token&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid token&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="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Curtain Call: The Grand Finale:
&lt;/h2&gt;

&lt;p&gt;In the denouement, the harmonious convergence of Firebase App Check within Flutter applications and its synchronization with token validation within the backend harmoniously culminates in an impervious shield for any custom API or resource. This masterstroke fends off potential vulnerabilities like CSRF attacks and guards against the machinations of unauthorized interlopers.&lt;/p&gt;

&lt;p&gt;With succinctly scripted verses of code harmonizing the dance between the app and the server, Firebase App Check emerges as a powerful sentinel, casting its protective aura over the entire stack. The Firebase Admin SDK, akin to a conductor of this symphony, offers an effortless means to verify tokens across a spectrum of platforms and environments, lending an air of serenity to the intricate interplay of security.&lt;/p&gt;

</description>
      <category>node</category>
      <category>flutter</category>
      <category>firebase</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Macros to the Rescue! Add Pagination to Non-DB Collections in Laravel</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Thu, 27 Jul 2023 07:26:17 +0000</pubDate>
      <link>https://dev.to/asifroyal/macros-to-the-rescue-add-pagination-to-non-db-collections-in-laravel-h94</link>
      <guid>https://dev.to/asifroyal/macros-to-the-rescue-add-pagination-to-non-db-collections-in-laravel-h94</guid>
      <description>&lt;p&gt;Laravel's powerful Collection class provides many helpful methods for working with arrays of data. However, the built-in &lt;code&gt;paginate&lt;/code&gt; method only works on Eloquent query builder and database results. &lt;/p&gt;

&lt;p&gt;If you want to paginate a standard Laravel Collection instance, such as from an array or other custom data source, you're out of luck. Or are you?&lt;/p&gt;

&lt;p&gt;By defining a custom macro, we can add pagination capabilities to &lt;strong&gt;any&lt;/strong&gt; Collection instance in Laravel. Here's how it works...&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining the Pagination Macro
&lt;/h2&gt;

&lt;p&gt;Laravel allows you to define custom macros on existing classes to augment them with new functionality. We can utilize this to create a reusable &lt;code&gt;paginate&lt;/code&gt; macro on the Collection class.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;boot&lt;/code&gt; method of a service provider, like &lt;code&gt;AppServiceProvider&lt;/code&gt;, we'll define the macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Collection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Pagination\LengthAwarePaginator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;macro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'paginate'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$perPage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$pageName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Resolve current page from request  &lt;/span&gt;
  &lt;span class="nv"&gt;$page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$page&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;LengthAwarePaginator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;resolveCurrentPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$pageName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Paginate the Collection  &lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LengthAwarePaginator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;forPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$perPage&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
      &lt;span class="nv"&gt;$perPage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;$page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nv"&gt;$options&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;The macro creates a new &lt;code&gt;LengthAwarePaginator&lt;/code&gt; instance which will handle the pagination render logic and links. We just need to pass the specific page's chunk of data from the collection using &lt;code&gt;forPage&lt;/code&gt;, as well as the total collection count.&lt;/p&gt;

&lt;h2&gt;
  
  
  Paginating a Collection
&lt;/h2&gt;

&lt;p&gt;With the macro defined, we can now paginate non-DB collections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// a Collection instance&lt;/span&gt;

&lt;span class="nv"&gt;$paginated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$collection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10 items per page&lt;/span&gt;

&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$paginated&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] on page 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The macro handles all the complexity of slicing the collection into pages and generating the paginator instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Pagination Super Powers!
&lt;/h2&gt;

&lt;p&gt;Defining custom macros provides tremendous flexibility to expand Laravel's base classes. In this case, we've given &lt;strong&gt;all&lt;/strong&gt; collections the ability to be paginated with a simple, expressive syntax.&lt;/p&gt;

&lt;p&gt;While less efficient for giant data sets, this approach is great for smaller arrays or collections not coming directly from a database query.&lt;/p&gt;

&lt;p&gt;So don't limit yourself to just paginating Eloquent results. With a bit of macro magic, you can bring pagination powers to any Laravel collection!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Validating Base64 Images by Size in Laravel: A How-to Guide</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Wed, 12 Jul 2023 10:09:20 +0000</pubDate>
      <link>https://dev.to/asifroyal/validating-base64-images-by-size-in-laravel-a-how-to-guide-20na</link>
      <guid>https://dev.to/asifroyal/validating-base64-images-by-size-in-laravel-a-how-to-guide-20na</guid>
      <description>&lt;p&gt;As Laravel developers, we often need to validate uploaded images to ensure they meet certain size requirements. Out of the box, Laravel's validation rules like &lt;code&gt;max&lt;/code&gt; only work on numeric values, not base64-encoded images. &lt;/p&gt;

&lt;p&gt;In this post, we'll walk through extending Laravel's validation to handle custom base64 image size validation. By writing a custom validation rule, we can reuse this logic anywhere image uploads are required in our app.&lt;/p&gt;

&lt;h1&gt;
  
  
  Custom Validation Rule
&lt;/h1&gt;

&lt;p&gt;All custom validation rules are registered by extending the &lt;code&gt;Validator&lt;/code&gt; in the &lt;code&gt;boot&lt;/code&gt; method of our &lt;code&gt;AppServiceProvider&lt;/code&gt;. First, we'll use Laravel's &lt;code&gt;Validator::extend&lt;/code&gt; method to define the logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Validator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64_image_size'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$parameters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$validator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Decode the image&lt;/span&gt;
  &lt;span class="nv"&gt;$decodedImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;base64_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Get image size in kilobytes &lt;/span&gt;
  &lt;span class="nv"&gt;$imageSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$decodedImage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// Check if image is below max size&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$imageSize&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nv"&gt;$parameters&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are decoding the base64-encoded image to get its binary size in bytes. Then we convert to kilobytes to compare against the maximum size defined in the parameters. If the image is below the max size, the validation passes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Custom Error Messages
&lt;/h1&gt;

&lt;p&gt;Next, we'll customize the error message using the &lt;code&gt;Validator::replacer&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Validator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;replacer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'base64_image_size'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$rule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$parameters&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="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;':attribute'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;':max'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$attribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$parameters&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="nv"&gt;$message&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;This replaces &lt;code&gt;:attribute&lt;/code&gt; with the field name and &lt;code&gt;:max&lt;/code&gt; with the defined max size. The message itself is defined in the &lt;code&gt;resources/lang/xx/validation.php&lt;/code&gt; language file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'base64_image_size'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The :attribute must not be larger than :max KB.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Usage
&lt;/h1&gt;

&lt;p&gt;Finally, we can use this custom rule during validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$validator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Validator&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="s1"&gt;'image'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required|base64_image_size:500'&lt;/span&gt;&lt;span class="p"&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="nv"&gt;$validator&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fails&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Image too large&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By extending Laravel's validation capabilities, we've built a reusable way to validate base64 image sizes across our application. The same approach can be used for any validation logic that doesn't fit neatly into the built-in rules.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>webdev</category>
      <category>programming</category>
      <category>php</category>
    </item>
    <item>
      <title>Uncovering Hidden Gems: PostgreSQL Window Functions You May Not Know About</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Wed, 22 Mar 2023 09:30:03 +0000</pubDate>
      <link>https://dev.to/asifroyal/uncovering-hidden-gems-postgresql-window-functions-you-may-not-know-about-1fbn</link>
      <guid>https://dev.to/asifroyal/uncovering-hidden-gems-postgresql-window-functions-you-may-not-know-about-1fbn</guid>
      <description>&lt;p&gt;Welcome, fellow database fans! Today, we'll delve into the world of PostgreSQL window functions and look at some lesser-known truths that can help you improve your database querying skills. So strap in and let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Window Functions?
&lt;/h2&gt;

&lt;p&gt;Window functions are a powerful feature of SQL that allow you to perform calculations across a set of rows related to the current row. This allows you to carry out advanced calculations, such as running totals, moving averages, and rank calculations.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, window functions are used in conjunction with the &lt;code&gt;OVER()&lt;/code&gt; clause, which defines the range or "window" of rows to be used for the calculation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesser-Known Facts about PostgreSQL Window Functions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Flexible Window Frames
&lt;/h3&gt;

&lt;p&gt;You might be familiar with the default sliding window frame, which is defined as &lt;code&gt;ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW&lt;/code&gt;. However, PostgreSQL offers several other options for defining window frames, such as &lt;code&gt;RANGE&lt;/code&gt; and &lt;code&gt;GROUPS&lt;/code&gt;. This allows you to create custom window frames that suit your specific needs.&lt;/p&gt;

&lt;p&gt;For example, you can create a moving average over a specific range of rows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;FOLLOWING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;your_table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Multiple Window Functions in a Single Query
&lt;/h3&gt;

&lt;p&gt;PostgreSQL allows you to use multiple window functions within a single query. This enables you to perform complex calculations and aggregations efficiently, without the need for multiple subqueries.&lt;/p&gt;

&lt;p&gt;Consider the following example, which calculates both the running total and running average of sales:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sale_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sale_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;running_total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sale_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;running_average&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Partitioning with Window Functions
&lt;/h3&gt;

&lt;p&gt;Partitioning is a powerful feature that allows you to divide your dataset into smaller groups based on one or more columns. By using the &lt;code&gt;PARTITION BY&lt;/code&gt; clause with window functions, you can perform calculations on each group independently.&lt;/p&gt;

&lt;p&gt;For instance, you might want to calculate the cumulative sum of sales for each product:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sale_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sale_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;product_running_total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Custom Aggregates in Window Functions
&lt;/h3&gt;

&lt;p&gt;PostgreSQL allows you to use custom aggregate functions within window functions. This means you can create your own aggregate functions and use them in your window calculations.&lt;/p&gt;

&lt;p&gt;For example, you might want to calculate the moving median of a dataset:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;AGGREGATE&lt;/span&gt; &lt;span class="n"&gt;median&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anyelement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;SFUNC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;array_append&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;STYPE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;anyarray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;FINALFUNC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;array_median&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;median&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;FOLLOWING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;your_table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Use Window Functions with DISTINCT
&lt;/h3&gt;

&lt;p&gt;You can use window functions in combination with the &lt;code&gt;DISTINCT&lt;/code&gt; keyword to calculate unique values within a window frame. This can be helpful when you want to perform calculations on a distinct set of values.&lt;/p&gt;

&lt;p&gt;For example, you might want to calculate the number of unique customers per day:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;unique_customers&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&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;PostgreSQL window functions are a very strong part of your SQL toolkit. They include several features that aren't as well-known but can help you conduct complex computations and groupings quickly. By learning these hidden jewels, you can improve your database querying skills and become a real Database Ninja.&lt;/p&gt;

&lt;p&gt;Happy querying!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>Get the count of affected rows with ease: The power of the RETURNING clause in Postgres</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Wed, 11 Jan 2023 13:19:29 +0000</pubDate>
      <link>https://dev.to/asifroyal/get-the-count-of-affected-rows-with-ease-the-power-of-the-returning-clause-3g2j</link>
      <guid>https://dev.to/asifroyal/get-the-count-of-affected-rows-with-ease-the-power-of-the-returning-clause-3g2j</guid>
      <description>&lt;p&gt;When working with databases, it is frequently vital to know how many rows have been changed by a query, and this is especially true when conducting operations such as updates or removals. Through its &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause, Postgres offers a method that is both powerful and versatile for doing this task.&lt;/p&gt;

&lt;p&gt;Postgres's &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause makes it possible to retrieve both the data of the &lt;em&gt;impacted rows&lt;/em&gt; and the &lt;em&gt;total number of rows&lt;/em&gt; that were &lt;em&gt;modified&lt;/em&gt; or &lt;em&gt;deleted&lt;/em&gt;. The &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause is used to get certain columns from a database. It is appended at the end of a query.&lt;/p&gt;

&lt;p&gt;For example, to update all rows in a table where a certain condition is true and return the number of affected rows, you would use the following query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE mytable SET mycolumn = 'new value' WHERE mycondition = true RETURNING *;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query would update the data of all rows that fit the condition "mycondition = true" and it would return the data of the rows that were changed.&lt;/p&gt;

&lt;p&gt;In the same way that you would use the following query if you wanted to use the &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause with the DELETE statement, you also have the option to do so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM mytable WHERE mycondition = true RETURNING *;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's important to remember that the &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause only works with update and delete statements, not with select statements. It's also only available in &lt;strong&gt;Postgres&lt;/strong&gt;, not in other &lt;strong&gt;RDBMS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In conclusion, the &lt;strong&gt;&lt;em&gt;RETURNING&lt;/em&gt;&lt;/strong&gt; clause in &lt;strong&gt;Postgres&lt;/strong&gt; is a powerful tool that makes it easy to find out how many rows are affected by update and delete queries. it can make it much easier to write robust and reliable code that works with databases.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Effortlessly manage configuration data in your Node.js microservices with nconf</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Mon, 09 Jan 2023 12:27:55 +0000</pubDate>
      <link>https://dev.to/asifroyal/effortlessly-manage-configuration-data-in-your-nodejs-microservices-with-nconf-1hl6</link>
      <guid>https://dev.to/asifroyal/effortlessly-manage-configuration-data-in-your-nodejs-microservices-with-nconf-1hl6</guid>
      <description>&lt;p&gt;Building microservices requires careful attention to detail in the area of configuration management. It will be much simpler for you to launch and maintain your services as a result of this ability to segregate configuration data from code. In this post, we'll take a look at how to manage configuration data in Node.js microservices by using the &lt;strong&gt;&lt;em&gt;nconf&lt;/em&gt;&lt;/strong&gt; package.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Should You Use &lt;strong&gt;&lt;em&gt;nconf&lt;/em&gt;&lt;/strong&gt;?
&lt;/h3&gt;

&lt;p&gt;There are several good reasons why you should consider using nconf as the configuration management tool for your Node.js microservices, including the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Flexibility&lt;/em&gt;&lt;/strong&gt;: nconf offers you the ability to load configuration data from a wide number of sources, including command-line arguments, environment variables, a configuration file, and default settings. Because of this, it is simple to modify the behaviour of your microservices in accordance with the environment they are running in&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Ease of use&lt;/em&gt;&lt;/strong&gt;: nconf's application programming interface (API) is quite straightforward, which makes retrieving and setting configuration data very simple. Additionally, it supports hierarchical data structures, which enables you to store and retrieve configuration settings that are nested within one another&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Well documented&lt;/em&gt;&lt;/strong&gt;: because it is so well documented, both learning it and putting it to use won't be difficult at all, thanks to nconf's extensive documentation and several working examples.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Here is an example of how you might use &lt;strong&gt;&lt;em&gt;nconf&lt;/em&gt;&lt;/strong&gt; in a Node.js microservice:&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 nconf = require('nconf');

// Initialize nconf to use (in-order):
//   1. Command-line arguments
//   2. Environment variables
//   3. A configuration file
//   4. Default values
nconf.argv().env().file({ file: 'config.json' }).defaults({
  port: 3000,
  host: 'localhost'
});

const app = express();

// Get the value of the 'port' key from the configuration data
const port = nconf.get('port');

app.listen(port, () =&amp;gt; {
  console.log(`Microservice listening on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code generates an Express app and makes use of &lt;strong&gt;&lt;em&gt;nconf&lt;/em&gt;&lt;/strong&gt; to load configuration data from a configuration file, command-line arguments, environment variables, and other sources, in addition to using default values. After that, the &lt;strong&gt;&lt;em&gt;get()&lt;/em&gt;&lt;/strong&gt; method is used in order to acquire the value of the port key from the configuration data, and it then begins the server process on the port that was given.&lt;/p&gt;

&lt;p&gt;After that, you will be able to add routes to the application, which will enable customers to interact with the microservice as required. As an illustration, you can decide to build a route in order to obtain the currently active configuration data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/config', (req, res) =&amp;gt; {
  res.json(nconf.get());
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This route retrieves all of the configuration data by utilizing the &lt;strong&gt;&lt;em&gt;get()&lt;/em&gt;&lt;/strong&gt; method, and then gives it back to the client in the form of a &lt;strong&gt;&lt;em&gt;JSON&lt;/em&gt;&lt;/strong&gt; object.&lt;/p&gt;

</description>
      <category>node</category>
      <category>microservices</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Boost Your Query Performance with Function-Based Indexes in PostgreSQL</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Wed, 04 Jan 2023 12:19:29 +0000</pubDate>
      <link>https://dev.to/asifroyal/boost-your-query-performance-with-function-based-indexes-in-postgresql-185h</link>
      <guid>https://dev.to/asifroyal/boost-your-query-performance-with-function-based-indexes-in-postgresql-185h</guid>
      <description>&lt;p&gt;Are you fed up with slow PostgreSQL queries that search for case-insensitive matches? Look no farther than indexes based on functions!&lt;/p&gt;

&lt;p&gt;An index that is constructed based on the result of a function or expression is known as a function-based index. This means that it can be used to accelerate searches that repeatedly evaluate the same function or expression.&lt;/p&gt;

&lt;p&gt;For example, let's say you have a table called &lt;strong&gt;&lt;em&gt;employees&lt;/em&gt;&lt;/strong&gt; that contains a column called &lt;strong&gt;&lt;em&gt;first_name&lt;/em&gt;&lt;/strong&gt;. You frequently run queries that search for employees with a given first name, but you want the search to be case-insensitive. One way to achieve this is to create a function-based index on the lower() function of the &lt;strong&gt;&lt;em&gt;first_name&lt;/em&gt;&lt;/strong&gt; column:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX lower_first_name_idx ON employees (lower(first_name));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when you run a query like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM employees WHERE lower(first_name) = 'john';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL will use the &lt;strong&gt;&lt;em&gt;lower_first_name_idx&lt;/em&gt;&lt;/strong&gt; index to search for rows that match the condition, which should be faster than a full table scan.&lt;/p&gt;

&lt;p&gt;It's important to remember that function-based indexes can only be used if the indexed expression is in the WHERE clause of a query and is used in an exact match (i.e., = or IN) or a range condition (i.e., BETWEEN). If the expression is used in a &lt;strong&gt;&lt;em&gt;SELECT&lt;/em&gt;&lt;/strong&gt; list or an &lt;strong&gt;&lt;em&gt;ORDER BY&lt;/em&gt;&lt;/strong&gt; clause, they can't be used.&lt;/p&gt;

&lt;p&gt;So, if you want to make your queries run faster, think about using function-based indexes in PostgreSQL the next time you want to do that. Your database will thank you, as will your users.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>sql</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>How to Save Unmatched Text in a jQuery Select2 Dropdown with an AJAX Call</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Tue, 03 Jan 2023 12:30:25 +0000</pubDate>
      <link>https://dev.to/asifroyal/how-to-save-unmatched-text-in-a-jquery-select2-dropdown-with-an-ajax-call-34j2</link>
      <guid>https://dev.to/asifroyal/how-to-save-unmatched-text-in-a-jquery-select2-dropdown-with-an-ajax-call-34j2</guid>
      <description>&lt;p&gt;jQuery Select2 is a powerful dropdown component that allows users to search for and select items from a list. It's easy to use and can be changed in many ways, making it a popular choice for web developers.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll learn how to use a jQuery Select2 dropdown to search for text inside a database using an AJAX call. We'll also learn how to deal with the Enter key and save the text typed; into the database if there are no search results.&lt;/p&gt;

&lt;p&gt;First, let's initialize the Select2 dropdown and set the AJAX call parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$(document).ready(function() {
  // Initialize the Select2 dropdown
  $('#mySelect2').select2({
    // Set the AJAX call parameters
    ajax: {
      url: '/search',
      dataType: 'json',
      delay: 250,
      data: function (params) {
        return {
          q: params.term, // search term
          page: params.page
        };
      },
      processResults: function (data, params) {
        // parse the results into the format expected by Select2
        // since we are using custom formatting functions we do not need to
        // alter the remote JSON data, except to indicate that infinite
        // scrolling can be used
        params.page = params.page || 1;

        return {
          results: data.items,
          pagination: {
            more: (params.page * 30) &amp;lt; data.total_count
          }
        };
      },
      cache: true
    },
    minimumInputLength: 1,
    templateResult: formatRepo, // template for the results
    templateSelection: formatRepoSelection // template for the selected item
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we are using an AJAX call to search for text in a database. The &lt;strong&gt;&lt;em&gt;minimumInputLength&lt;/em&gt;&lt;/strong&gt; parameter is set to 1, so the AJAX call is only triggered when the user enters at least one character in the dropdown. We are also using &lt;strong&gt;&lt;em&gt;templateResult&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;templateSelection&lt;/em&gt;&lt;/strong&gt; to customize the display of the results and the selected item.&lt;/p&gt;

&lt;p&gt;Next, let's figure out what to do when the Enter key is pressed and save the entered text to the database if no search results are found:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Handle the Enter key press on the Select2 dropdown
$('#mySelect2').on('keypress', function(e) {
  var keyCode = e.keyCode || e.which;

  if (keyCode === 13) {
    // Get the entered text
    var text = $(this).val();

    // Check if there are no search results
    if ($('.select2-results__option--highlighted').length === 0) {
      // Save the text to the database via an AJAX call
      saveTextToDatabase(text);

      // Reset the Select2 dropdown
      $(this).val(null).trigger('change');
    }
  }
});

// Function to save the text to the database via an AJAX call
function saveTextToDatabase(text) {
  $.ajax({
    url: '/save',
    type: 'POST',
    data: {text: text},
    success: function(response) {
      console.log('Text saved to database: ' + text);
    },
    error: function(error) {
      console.log(error);
    }
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may come in handy for adding new records to the database and ensuring that the list is always up to date. This functionality may be effortlessly added to any of our online applications with just a few lines of code.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>fullstack</category>
      <category>frontend</category>
      <category>braziliandevs</category>
    </item>
    <item>
      <title>Dynamically Change the Content-Disposition of an S3 Object URL</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Thu, 29 Dec 2022 12:10:29 +0000</pubDate>
      <link>https://dev.to/asifroyal/dynamically-change-the-content-disposition-of-an-s3-object-url-4mb0</link>
      <guid>https://dev.to/asifroyal/dynamically-change-the-content-disposition-of-an-s3-object-url-4mb0</guid>
      <description>&lt;p&gt;The &lt;strong&gt;&lt;em&gt;Content-Disposition&lt;/em&gt;&lt;/strong&gt; response header of an S3 object can be changed dynamically by modifying the header value in the HTTP response that is returned when the object's URL is accessed.&lt;/p&gt;

&lt;p&gt;One way to do this is to use an AWS Lambda function to intercept the request for the object's URL and modify the &lt;strong&gt;&lt;em&gt;Content-Disposition&lt;/em&gt;&lt;/strong&gt; response header before returning the response to the browser. This allows you to change the &lt;strong&gt;&lt;em&gt;Content-Disposition&lt;/em&gt;&lt;/strong&gt; value dynamically based on the request or other  conditions.&lt;/p&gt;

&lt;p&gt;Here's an example of a Lambda function that changes the &lt;strong&gt;&lt;em&gt;Content-Disposition&lt;/em&gt;&lt;/strong&gt; value based on the value of a query parameter in the request URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.handler = async (event) =&amp;gt; {
  // Get the object key and bucket from the event
  const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
  const bucket = event.Records[0].s3.bucket.name;

  // Get the request query parameters
  const params = event.Records[0].cf.request.querystring;

  // Set the Content-Disposition value based on the "download" query parameter
  let contentDisposition;
  if (params.includes('download=1')) {
    contentDisposition = 'attachment';
  } else {
    contentDisposition = 'inline';
  }

  // Get the object from S3
  const s3 = new AWS.S3();
  const s3Object = await s3.getObject({
    Bucket: bucket,
    Key: key
  }).promise();

  // Create the response
  const response = {
    status: '200',
    statusDescription: 'OK',
    headers: {
      'Content-Type': [{
        key: 'Content-Type',
        value: s3Object.ContentType
      }],
      'Content-Disposition': [{
        key: 'Content-Disposition',
        value: contentDisposition
      }]
    },
    body: s3Object.Body
  };

  // Return the response
  return response;
};

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

&lt;/div&gt;



&lt;p&gt;To use this Lambda function, you would need to set up an Amazon CloudFront distribution that points to your S3 bucket and associates the Lambda function with the distribution as an origin request trigger. Then, you can access the object's URL through the CloudFront distribution and use the &lt;strong&gt;&lt;em&gt;download&lt;/em&gt;&lt;/strong&gt; query parameter to control whether the object is rendered in the browser window or downloaded.&lt;/p&gt;

&lt;p&gt;For example, the following URL would cause the object to be downloaded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://my-cloudfront-distribution.com/path/to/object.ext?download=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the following URL would cause the object to be rendered in the browser window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://my-cloudfront-distribution.com/path/to/object.ext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, you can easily switch between rendering the object in the browser window and forcing a download without having to manually change the &lt;strong&gt;&lt;em&gt;Content-Disposition&lt;/em&gt;&lt;/strong&gt; value.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>cloud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Displaying PDF Files in an Iframe: A Simple Solution</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Wed, 21 Dec 2022 13:24:35 +0000</pubDate>
      <link>https://dev.to/asifroyal/displaying-pdf-files-in-an-iframe-a-simple-solution-2anj</link>
      <guid>https://dev.to/asifroyal/displaying-pdf-files-in-an-iframe-a-simple-solution-2anj</guid>
      <description>&lt;p&gt;Are you looking for a way to embed a PDF file on your website without relying on external services or plugins? If so, then you're in luck! In this tutorial, we'll show you how to use PHP and cURL to download the content of a PDF file from a URL and display it in an iframe.&lt;/p&gt;

&lt;p&gt;First, let's start by creating a function that uses cURL to download the PDF file:&lt;/p&gt;

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

function displayPdfFromUrl($url) {
  // Initialize cURL session
  $ch = curl_init();

  // Set cURL options
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  // Execute cURL request
  $data = curl_exec($ch);

  // Close cURL session
  curl_close($ch);

  // Return PDF file content
  return $data;
}


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

&lt;/div&gt;

&lt;p&gt;This function takes a URL as an argument and uses &lt;strong&gt;&lt;em&gt;cURL&lt;/em&gt;&lt;/strong&gt; to download the content of the PDF file at that URL. The content is then returned as a string.&lt;/p&gt;

&lt;p&gt;Next, we can use the &lt;strong&gt;&lt;em&gt;base64_encode&lt;/em&gt;&lt;/strong&gt; function to encode the PDF data as a base64 string, which we can then use as the &lt;strong&gt;&lt;em&gt;src&lt;/em&gt;&lt;/strong&gt; attribute for an iframe element:&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%2Fjmh5orm2vopl3nxwlfrt.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%2Fjmh5orm2vopl3nxwlfrt.png" alt="code sample"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all there is to it! With just a few lines of code, you can easily display PDF files on your website without relying on external services or plugins.&lt;/p&gt;

</description>
      <category>php</category>
      <category>curl</category>
      <category>iframe</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Set Up a Cron Job to Update Your Firebase Collection</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Tue, 20 Dec 2022 07:04:00 +0000</pubDate>
      <link>https://dev.to/asifroyal/how-to-set-up-a-cron-job-to-update-your-firebase-collection-55ka</link>
      <guid>https://dev.to/asifroyal/how-to-set-up-a-cron-job-to-update-your-firebase-collection-55ka</guid>
      <description>&lt;p&gt;Are you tired of manually updating your Firebase collection? Do you want a way to automate the process and save yourself time? Then a cron job is the solution you need!&lt;/p&gt;

&lt;p&gt;A cron job is a task that is scheduled to run at a specific time or interval. In this tutorial, we'll show you how to set up a cron job using NodeJS, Express, and TypeScript to update your Firebase collection every 5 minutes.&lt;/p&gt;

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

&lt;p&gt;Before you get started, make sure you have the following tools installed on your machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NodeJS&lt;/li&gt;
&lt;li&gt;Express&lt;/li&gt;
&lt;li&gt;TypeScript&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Install the Firebase SDK
&lt;/h2&gt;

&lt;p&gt;To access and update your Firebase database from your NodeJS code, you'll need to install the Firebase SDK for NodeJS. You can do this by running 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;npm install firebase-admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Initialize the Firebase Admin SDK
&lt;/h2&gt;

&lt;p&gt;Once you have the Firebase SDK installed, you'll need to initialize the Firebase Admin SDK by providing it with the appropriate credentials. You can find the credentials in the Firebase console under the "Project Settings" section.&lt;/p&gt;

&lt;p&gt;To initialize the Firebase Admin SDK in your code, use the following lines:&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 admin from 'firebase-admin';

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://&amp;lt;DATABASE_NAME&amp;gt;.firebaseio.com"
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Update the Firebase Collection
&lt;/h2&gt;

&lt;p&gt;Now that you have initialized the Firebase Admin SDK, you can use it to access and update your Firebase database. For example, you can update a specific collection in your database using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = admin.firestore();
const collectionRef = db.collection('&amp;lt;COLLECTION_NAME&amp;gt;');

collectionRef.update({
  field1: 'new value1',
  field2: 'new value2'
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Set Up a Cron Job
&lt;/h2&gt;

&lt;p&gt;To set up a cron job that runs every 5 minutes, you'll need to use a package called "node-cron". You can install this package by running 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;npm install node-cron

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

&lt;/div&gt;



&lt;p&gt;Once you have node-cron installed, you can use it to schedule a task to run every 5 minutes by using the following code:&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 cron from 'node-cron';

cron.schedule('*/5 * * * *', () =&amp;gt; {
  // your code to update the Firebase collection goes here
});

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Set Up an HTTP Server
&lt;/h2&gt;

&lt;p&gt;Finally, you'll need to set up an HTTP server using Express in order to run your cron job. You can do this by using the following code:&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 express from 'express';

const app = express();

app.listen(3000, () =&amp;gt; {
  console.log('Cron job listening on port 3000');
});

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

&lt;/div&gt;



&lt;p&gt;With these steps, you should have a cron job that runs every 5 minutes, updates a Firebase collection, and is set up using NodeJS, Express, and TypeScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>firebase</category>
      <category>cronjob</category>
    </item>
    <item>
      <title>A practical method for managing environment variables in microservices running on AWS ECS</title>
      <dc:creator>Asif</dc:creator>
      <pubDate>Tue, 13 Dec 2022 10:19:49 +0000</pubDate>
      <link>https://dev.to/asifroyal/a-practical-method-for-managing-environment-variables-in-microservices-running-on-aws-ecs-4ad</link>
      <guid>https://dev.to/asifroyal/a-practical-method-for-managing-environment-variables-in-microservices-running-on-aws-ecs-4ad</guid>
      <description>&lt;p&gt;There are several methods for managing environment variables in AWS ECS microservices. To store and manage your environment variables, one common method is to use &lt;strong&gt;AWS Systems Manager Parameter Store&lt;/strong&gt;. This service allows you to store sensitive information, such as database passwords, in a secure and scalable way. The parameters can then be referenced in your ECS tasks or services by using the &lt;strong&gt;&lt;em&gt;ssm&lt;/em&gt;&lt;/strong&gt; parameter provider in the ECS task definition. This allows you to easily update and manage your environment variables without having to redeploy your services or update your task definitions.&lt;/p&gt;

&lt;p&gt;Another option is to store and manage your secrets using AWS Secrets Manager. This service allows you to store and encrypt your secrets, and then use the &lt;strong&gt;&lt;em&gt;secretsmanager&lt;/em&gt;&lt;/strong&gt; parameter provider in the task definition to reference them in your ECS tasks. This can be a convenient way to manage your secrets and automatically rotate them if necessary. &lt;/p&gt;

&lt;p&gt;Whatever method you use, it's critical to follow best practices for security and secret management when working with environment variables in ECS. This includes encrypting your secrets at all times and using IAM policies to limit access to your secrets.&lt;/p&gt;

&lt;p&gt;Here's an illustration:&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 express from 'express';
import { SSM } from '@aws-sdk/client-ssm';

const app = express();

// Set up AWS Systems Manager client
const ssm = new SSM({
  region: 'us-east-1'
});

// Function to get a secret from the parameter store
async function getSecret(name: string) {
  const result = await ssm.getParameter({
    Name: name,
    WithDecryption: true
  }).promise();

  return result.Parameter.Value;
}

// Function to retrieve the database password from the parameter store
// and use it to connect to the database
async function connectToDatabase() {
  // Get the database password from the parameter store
  const password = await getSecret('/database/password');

  // Connect to the database using the password
  const db = new Prisma({
    database: 'mydb',
    user: 'myuser',
    password: password,
  });
}

app.get('/', (req, res) =&amp;gt; {
  res.send('Hello World!');
});

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

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;strong&gt;&lt;em&gt;getSecret&lt;/em&gt;&lt;/strong&gt; function is used to retrieve a secret from the parameter store by its name. The &lt;strong&gt;&lt;em&gt;connectToDatabase&lt;/em&gt;&lt;/strong&gt; function uses the &lt;strong&gt;&lt;em&gt;getSecret&lt;/em&gt;&lt;/strong&gt; function to retrieve the database password from the parameter store, and then uses it to connect to the database.&lt;/p&gt;

&lt;p&gt;You can use this approach to retrieve any secrets that you need for your application, and use them in whatever way is appropriate for your application.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>aws</category>
      <category>node</category>
      <category>ssm</category>
    </item>
  </channel>
</rss>
