<?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: Gracious Obeagu</title>
    <description>The latest articles on DEV Community by Gracious Obeagu (@greegman).</description>
    <link>https://dev.to/greegman</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%2F1696618%2Fac8300f7-464e-4c5a-9ea4-11420e5a8702.jpg</url>
      <title>DEV Community: Gracious Obeagu</title>
      <link>https://dev.to/greegman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/greegman"/>
    <language>en</language>
    <item>
      <title>Overcoming a Challenging Backend Problem with TypeScript: My Journey and Aspiration</title>
      <dc:creator>Gracious Obeagu</dc:creator>
      <pubDate>Fri, 28 Jun 2024 02:32:25 +0000</pubDate>
      <link>https://dev.to/greegman/overcoming-a-challenging-backend-problem-with-typescript-my-journey-and-aspiration-5gda</link>
      <guid>https://dev.to/greegman/overcoming-a-challenging-backend-problem-with-typescript-my-journey-and-aspiration-5gda</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a backend developer, you must solve complex challenges on a regular basis, but every now and then, a particularly difficult problem emerges that tests your expertise and inventiveness. I recently experienced such a situation while working on a project, and I'd want to share my experience with it, particularly my first use of TypeScript to address it. This experience is very relevant to me as I prepare to embark on a new adventure through the HNG Internship, which I am really enthusiastic about. By the way you can read about HNG &lt;a href="https://hng.tech/internship"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;I was required to build an API for an app that requires, Two-Factor Authentication (2FA) system. This system will also store images and send activation links upon registration to the user's email and so on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full breakdown of the features:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Upon registration, a user receives a confirmation email and proceeds to log in. &lt;/li&gt;
&lt;li&gt;The login process involves 2FA, where  a user  enters their email address, a six-digit One-Time Password (OTP) is generated and sent to the provided email address. &lt;/li&gt;
&lt;li&gt;Using in-memory cache, Redis, or a database to store the OTP, which expires after 5 minutes, &lt;/li&gt;
&lt;li&gt;All Users using the App must have API keys.&lt;/li&gt;
&lt;li&gt;Files can be uploaded using the API key.&lt;/li&gt;
&lt;li&gt;If not already done, users must generate an API key to upload files.&lt;/li&gt;
&lt;li&gt;Uploaded files must be associated with the user who owns the API key.&lt;/li&gt;
&lt;li&gt;Only image files are allowed currently and should be stored as Base64 strings in the database.&lt;/li&gt;
&lt;li&gt;Files should be deleted from the system/app folder after being stored.&lt;/li&gt;
&lt;li&gt;And finally writing a README that is up to date.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Initial Approach
&lt;/h2&gt;

&lt;p&gt;The first step was to have a quick crash course on how to use Typescript in Node/Express JS. Armed with this information, I also looked up how to use in-memory databases like redis for caching things like OTP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diving Deeper with TypeScript (My Thought Process)
&lt;/h2&gt;

&lt;p&gt;Using TypeScript for the first time, having heard about its benefits in terms of type safety and improved developer experience, I thought it would be a good opportunity to learn and apply it.&lt;/p&gt;

&lt;p&gt;I Wrote the Authentication and Authorization for the APP, used a Package called Nodemailer for sending activation link for registration and OTP (2FA) to the user when the user logs in.&lt;/p&gt;

&lt;p&gt;Generating API-Keys, this was another torn in the flesh as I have never done something like this before, but after careful consideration, I decided I was going to use Middleware to handle it. So when the user creates an account, activates the and logs in an API-KEY is generated. This API-KEY can now be used by the user to make any file upload, The user can also delete and existing API-KEY and create another one&lt;/p&gt;

&lt;p&gt;Database Relationship was crucial as it helped me to know which user uploaded an image so I used One-to-many relationship to achieve this feature, so I have an image schema with image-filename, base64 string and user-id properties&lt;/p&gt;

&lt;p&gt;I also wrote a logic that makes it impossible for a user to upload a file that is not an image and also store this file in base64 for better optimization as saving image directly to the database isn't a good approach and can make the system lag.&lt;/p&gt;

&lt;p&gt;The Image file after been converted to base64 string is deleted from the system/app folder&lt;/p&gt;

&lt;p&gt;A well documented Readme file that can help anyone using the app to read and understand the flow&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up TypeScript
&lt;/h2&gt;

&lt;p&gt;First, I set up a TypeScript environment in my project. This involved installing TypeScript and configuring the &lt;code&gt;tsconfig.json&lt;/code&gt; file to enable strict type-checking and other beneficial options.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;json&lt;br&gt;
   {&lt;br&gt;
     "compilerOptions": {&lt;br&gt;
       "target": "ES6",&lt;br&gt;
       "module": "commonjs",&lt;br&gt;
       "strict": true,&lt;br&gt;
       "esModuleInterop": true,&lt;br&gt;
       "skipLibCheck": true&lt;br&gt;
     }&lt;br&gt;
   }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TypeScript Interfaces
&lt;/h2&gt;

&lt;p&gt;Using TypeScript’s type definitions helped ensure that the data structures were well-defined and that the query results matched the expected types.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;typescript&lt;br&gt;
   interface Product {&lt;br&gt;
    base64: string;&lt;br&gt;
     user_id: ObjectID;&lt;br&gt;
   }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Indexing&lt;/strong&gt;: I added appropriate indexes to the columns frequently used TypeScript helped manage these database schema changes more reliably by ensuring the consistency of column names and types across the codebase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching with TypeScript&lt;/strong&gt;: I implemented caching for frequently accessed data. By storing the results of these queries in a Redis cache, I especially used this in sending OTP to help reduce the load on the database and speed up response times for repeat requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing and Validation
&lt;/h2&gt;

&lt;p&gt;After making these changes, I thoroughly tested the system to make sure there is no loose ends and properly captured all the operational errors that might occurs by sending a meaningful response to the user of any error that might occur&lt;/p&gt;

&lt;h2&gt;
  
  
  Reflection
&lt;/h2&gt;

&lt;p&gt;This experience reinforced several key principles for me: the importance of detailed diagnostics, the power of optimization, and the value of perseverance in problem-solving. It also highlighted the need for continuous learning and adaptation in the ever-evolving field of backend development. Using TypeScript for the first time was a game-changer, providing a robust framework for writing reliable and maintainable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looking Ahead: The HNG Internship
&lt;/h2&gt;

&lt;p&gt;As I look forward to starting my journey with the HNG Internship, I am filled with excitement and anticipation. The HNG Internship represents an opportunity to further hone my skills, collaborate with talented individuals, and contribute to impactful projects. I am eager to bring my problem-solving mindset and technical expertise to the table, while also learning from the diverse experiences and insights of my peers and mentors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why HNG?
&lt;/h2&gt;

&lt;p&gt;I believe that the HNG Internship will provide a unique platform to grow both professionally and personally. The program’s emphasis on real-world projects, mentorship, and community aligns perfectly with my aspirations. I am particularly drawn to the collaborative environment and the chance to work on innovative solutions that can make a difference. Have you heard about HNG hire? You can read about it &lt;a href="https://hng.tech/hire"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Solving difficult backend problems is not just about technical skills; it’s about persistence, creativity, and a willingness to learn. As I prepare for the HNG Internship, I am excited to tackle new challenges, build meaningful connections, and continue my journey of growth and discovery in the world of backend development.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
