<?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: Amanpreet Singh</title>
    <description>The latest articles on DEV Community by Amanpreet Singh (@amanpreet-dev).</description>
    <link>https://dev.to/amanpreet-dev</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%2F263780%2Fd8d3d26d-0f19-41ec-9da4-4f4f0a2ce569.png</url>
      <title>DEV Community: Amanpreet Singh</title>
      <link>https://dev.to/amanpreet-dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amanpreet-dev"/>
    <language>en</language>
    <item>
      <title>MongoDB 8.0: An Overview of the Latest Features and Upgrades</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Tue, 26 Nov 2024 13:07:50 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/mongodb-80-an-overview-of-the-latest-features-and-upgrades-a6i</link>
      <guid>https://dev.to/amanpreet-dev/mongodb-80-an-overview-of-the-latest-features-and-upgrades-a6i</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fcr4xocxua89lbed2c49n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fcr4xocxua89lbed2c49n.png" alt="MongoDB 8.0: An Overview of the Latest Features and Upgrades" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The recent release of MongoDB 8.0 continues its tradition of innovation, introducing enhancements that promise to revolutionize how developers interact with data. Whether you're a seasoned MongoDB user or exploring it for the first time, this latest version offers exciting opportunities to elevate your development.&lt;/p&gt;

&lt;p&gt;MongoDB is used across various industries, from large-scale e-commerce platforms and stream processing to transactional systems and complex Internet of Things (IoT) networks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whats new in MongoDB 8.0?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Performance Updates
&lt;/h3&gt;

&lt;p&gt;The most popular document database is now faster than ever,&lt;br&gt;&lt;br&gt;
With &lt;strong&gt;36%&lt;/strong&gt;  &lt;strong&gt;quicker reads&lt;/strong&gt; ,&lt;br&gt;&lt;br&gt;
&lt;strong&gt;59%&lt;/strong&gt;  &lt;strong&gt;higher throughput for updates&lt;/strong&gt; ,&lt;br&gt;&lt;br&gt;
&lt;strong&gt;200%&lt;/strong&gt;  &lt;strong&gt;faster time-series aggregations&lt;/strong&gt; ,&lt;br&gt;&lt;br&gt;
and up to &lt;strong&gt;50&lt;/strong&gt; times &lt;strong&gt;faster resharding&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fnwbu1acs3l3ldrbronv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnwbu1acs3l3ldrbronv1.png" alt="Image depicting the performance improvements of MongoDB 8.0. In order going down the image, YCSB Bulk Load is 56% faster, YCSB 100% Read is 36% faster, TCSV 95/5 is 32% faster, Linkbench is 24% faster, and TSBS is 60% faster." width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Logging
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can configure the DB Profiler to log slow operations based on the time that MongoDB takes to process an operation, rather than the total latency of the operation. This allows for more accurate logging of slow queries, as it focuses specifically on the time MongoDB spends processing them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Slow query logs feature a metric called &lt;code&gt;queues.execution.totalTimeQueuedMicros&lt;/code&gt;. This metric helps determine if an operation is slow due to the time it takes to complete or the time it spends waiting to start.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Query analysis tools like the Query Profiler, Performance Advisor, and Search Query Telemetry report slow operations based on &lt;code&gt;workingMillis&lt;/code&gt; rather than &lt;code&gt;durationMillis&lt;/code&gt;. This shift offers a more accurate view of problematic queries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Aggregation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can use &lt;code&gt;$convert&lt;/code&gt; operator to convert string values to binData values and vice versa.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New helper expression, &lt;code&gt;$toUUID&lt;/code&gt; which provides simplified conversions of strings to UUID values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Behavior of&lt;/strong&gt; &lt;code&gt;$rank&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;$denseRank&lt;/code&gt;: In MongoDB 8.0, &lt;code&gt;null&lt;/code&gt; and missing field values in &lt;code&gt;$denseRank&lt;/code&gt; and &lt;code&gt;$rank&lt;/code&gt; for &lt;code&gt;sortBy&lt;/code&gt; operations are treated the same when calculating rankings, thus making the behavior of &lt;code&gt;$denseRank&lt;/code&gt; and &lt;code&gt;$rank&lt;/code&gt; consistent with &lt;code&gt;$sort&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Starting in MongoDB 8.0, &lt;strong&gt;Queryable Encryption&lt;/strong&gt; supports the range queries on encrypted fields. You can use &lt;code&gt;$lt&lt;/code&gt;, &lt;code&gt;$gt&lt;/code&gt;, &lt;code&gt;$lte&lt;/code&gt;, and &lt;code&gt;$gte&lt;/code&gt; operators on encrypted fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Cybersecurity Schema Framework&lt;/strong&gt; : In MongoDB 8.0, you can specify the OCSF schema for audit log messages.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
     "activity_id" : &amp;lt;int&amp;gt;,
     "category_uid" : &amp;lt;int&amp;gt;,
     "class_uid" : &amp;lt;int&amp;gt;,
     "time" : &amp;lt;int&amp;gt;,
     "severity_id" : &amp;lt;int&amp;gt;,
     "type_uid" : &amp;lt;int&amp;gt;,
     "metadata" : &amp;lt;document&amp;gt;
     "actor" : {
        "user" : {
           "type_id" : &amp;lt;int&amp;gt;,
           "name" : &amp;lt;string&amp;gt;,
           "groups" : &amp;lt;array of documents&amp;gt;
        }
     }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ingress Queue&lt;/strong&gt; : MongoDb 8.0 introduces a new queue for ingress admission control. Operations waiting to enter the database from the network are placed in the ingress queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, this queue is unrestricted, allowing all operations to proceed without any queuing. However, by setting a maximum limit for the queue, you can manage operations at this stage, queuing them if the number of concurrent operations reaches the specified limit.&lt;/p&gt;
&lt;h3&gt;
  
  
  Replication &amp;amp; Sharding
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Majority Write Concern&lt;/strong&gt; : In previous releases, write operations that use the &lt;em&gt;majority&lt;/em&gt; write concerns would wait and return an acknowledgement after the majority of replicate set members applied the change.&lt;br&gt;&lt;br&gt;
Starting in MongoDB 8.0, these operations return an acknowledgement when the majority of replica set members have written the oplog entry for the change.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Moving a Collection&lt;/strong&gt; : Starting in MongoDB 8.0, you can unshard collections and move unsharded collections between shards on sharded clusters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use of&lt;/strong&gt; &lt;code&gt;$lookup&lt;/code&gt; &lt;strong&gt;stage in Transactions&lt;/strong&gt; : Starting with MongoDB 8.0, you can use the $lookup stage within a transaction when targeting a sharded collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Store Application Data on Config Shards&lt;/strong&gt; : Starting in MongoDB 8.0, you can configure a config server to store application data in addition to the usual sharded cluster metadata. The config server is then known as a &lt;em&gt;config shard&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  General Changes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bulk Write&lt;/strong&gt; command: The existing &lt;code&gt;bulkWrite()&lt;/code&gt; method only allows to modification of one collection in one request but from MongoDB 8.0, you can use the new &lt;code&gt;bulkWrite&lt;/code&gt; command to perform many insert, update and delete operations on multiple collections in one request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;New Query Shape and Query Settings&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read Concern on Capped Collections&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parameter Filtering&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shutdown Performance&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sort Support for&lt;/strong&gt; &lt;code&gt;updateOne&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Query Planner Optimization Time&lt;/strong&gt; : The method &lt;code&gt;explain()&lt;/code&gt; returns information on query planner optimization time. The &lt;code&gt;queryPlanner.optimizationTimeMillis&lt;/code&gt; status shows the time in milliseconds that the query planner spent on optimizations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Upgrading to MongoDB 8.0
&lt;/h2&gt;

&lt;p&gt;To upgrade to MongoDB 8.0 from a 7.x deployment, the 7.x deployment must have &lt;code&gt;featureCompatibilityVersion&lt;/code&gt; set to &lt;code&gt;7.x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To check the version use the below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To download MongoDB 8.0, go to the &lt;a href="https://www.mongodb.com/try/download/community-edition" rel="noopener noreferrer"&gt;MongoDB Download Center.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're interested in the latest features of MongoDB 7.0 instead of the newest version, you can find the information &lt;a href="https://dev.to/amanpreet-dev/unpacking-the-latest-features-and-enhancements-of-mongodb-70-2m8g"&gt;here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;MongoDB 8.0 has certainly raised the bar. With a full set of new features, security improvements, and other performance enhancements, MongoDB 8.0 is the perfect choice for organizations looking to take their development to the next level.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new, as I did. If so, kindly like and share the article, and &lt;strong&gt;follow me&lt;/strong&gt; to read more exciting articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mongodb.com/docs/" rel="noopener noreferrer"&gt;&lt;strong&gt;MongoDB Docs&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/docs/manual/release-notes/8.0/" rel="noopener noreferrer"&gt;&lt;strong&gt;Release Notes for MongoDB&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>Essential Guide to Mastering Git Tags</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Tue, 13 Aug 2024 08:30:21 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/essential-guide-to-mastering-git-tags-4fj0</link>
      <guid>https://dev.to/amanpreet-dev/essential-guide-to-mastering-git-tags-4fj0</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fucnocsxteu5tnj5tm9g2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fucnocsxteu5tnj5tm9g2.png" alt="Essential Guide to Mastering Git Tags" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In software development, version control is essential for managing changes, tracking progress, and ensuring the integrity of your codebase. Git, the most popular and widely used is a distributed version control system, that offers various tools to help developers maintain a clean and organized project history. One such tool is Git &lt;em&gt;tags&lt;/em&gt;. In this article, we'll explore what Git &lt;em&gt;tags&lt;/em&gt; are, why they are important, and how you can use them to enhance your development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Git Tags?
&lt;/h2&gt;

&lt;p&gt;Git &lt;em&gt;tags&lt;/em&gt; are references to specific points in a Git repository's history. Git &lt;em&gt;tags&lt;/em&gt; are fixed and immutable, unlike branches, which move as you continue to commit. Tags are typically used to mark important milestones, such as releases, breaking changes (e.g., &lt;code&gt;v1.0.0&lt;/code&gt;), making it easier to refer back to them later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference between Git tags and Git branches
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Git Branches&lt;/em&gt; are &lt;em&gt;mutable&lt;/em&gt; references whereas &lt;em&gt;Git Tags&lt;/em&gt; are &lt;em&gt;immutable&lt;/em&gt; references. In other words, a &lt;code&gt;git tag&lt;/code&gt; is created to point to a specific commit and after that does not change, whereas a new commit will automatically update the branch to that commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Git tags
&lt;/h2&gt;

&lt;p&gt;There are two types of tags in Git - &lt;strong&gt;lightweight&lt;/strong&gt; and &lt;strong&gt;annotated&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Lightweight Tags
&lt;/h3&gt;

&lt;p&gt;Lightweight tags as the name suggests are simple references to a commit. They act like a branch that doesn’t change, effectively serving as a quick pointer to a specific commit.&lt;/p&gt;

&lt;p&gt;Lightweight tags are also considered as temporary tags as they are not stored in the Git database.&lt;/p&gt;

&lt;p&gt;Lightweight tags can be created by using the following command &lt;code&gt;git tag &amp;lt;tagname&amp;gt;&lt;/code&gt; as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git tag v1.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Annotated Tags
&lt;/h3&gt;

&lt;p&gt;Annotated tags are more detailed. They store additional information, including the tagger’s name, email, date, and a tagging message. They are stored as full objects in the Git database. They can also be signed and verified with GPG (GNU Privacy Guard).&lt;/p&gt;

&lt;p&gt;Since annotated tags are detailed, they include extra information hence they are created while adding a message such as &lt;code&gt;git tag -a &amp;lt;tagname&amp;gt; -m "message"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git tag &lt;span class="nt"&gt;-a&lt;/span&gt; v1.0.0 &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Release version 1.0.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create an annotated tag, use the &lt;code&gt;-a&lt;/code&gt; flag and include a message with the &lt;code&gt;-m&lt;/code&gt; flag together with &lt;code&gt;tagname&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  List and View Tags
&lt;/h2&gt;

&lt;p&gt;To list all tags in the repository use &lt;code&gt;git tag&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git tag

&lt;span class="c"&gt;# The above command will output something like this.&lt;/span&gt;
v1.0
v1.1
v1.2
v1.3
v1.4
v1.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get detailed information on a particular tag use &lt;code&gt;git show &amp;lt;tagname&amp;gt;&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git show v1.0

&lt;span class="c"&gt;# The above command will output something like this.&lt;/span&gt;
commit 0e9c504d7ec2ee4cef250dff136826065fd8d9a6 &lt;span class="o"&gt;(&lt;/span&gt;tag: v1.4&lt;span class="o"&gt;)&lt;/span&gt;
Author: Aman &amp;lt;aman@example.com&amp;gt;
Date:   Fri Jul 12 19:12:40 2024 &lt;span class="nt"&gt;-0400&lt;/span&gt;
    feat: Add refresh token storage and validation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is an example of a lightweight tag displaying the information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sharing Tags
&lt;/h2&gt;

&lt;p&gt;By default, tags are not pushed to the remote repository when you push commits. To push tags you need to explicitly use the command &lt;code&gt;git push &amp;lt;remote&amp;gt; &amp;lt;tagname&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin v1.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case there are a lot of tags and you want to push it one go, you can use a &lt;code&gt;--tags&lt;/code&gt; flag, here is the command for that. Using a &lt;code&gt;--tags&lt;/code&gt; flag pushes all types of tags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin &lt;span class="nt"&gt;--tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To push only annotated tags to the remote use the flag &lt;code&gt;--follow-tags&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin &lt;span class="nt"&gt;--follow-tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting Tags
&lt;/h2&gt;

&lt;p&gt;To delete a local tag simply use &lt;code&gt;git tag -d &amp;lt;tagname&amp;gt;&lt;/code&gt; as shown in the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git tag &lt;span class="nt"&gt;-d&lt;/span&gt; v1.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To delete a remote tag use &lt;code&gt;git push &amp;lt;remote&amp;gt; -d &amp;lt;tagname&amp;gt;&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin &lt;span class="nt"&gt;-d&lt;/span&gt; v1.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Git Tags Cheat Sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Command&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Lists all tags in the repository.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a lightweight tag at the current commit.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag -a &amp;lt;tagname&amp;gt; -m "&amp;lt;message&amp;gt;"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates an annotated tag with a message.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag -a v1.0.0 -m "Initial release version 1.0.0"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git show &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Displays details of an annotated tag, including the message.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git show v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag -d &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Deletes a local tag.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag -d v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git push &amp;lt;remote&amp;gt; &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pushes a specific tag to the remote repository.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git push origin v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git push --tags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pushes all local tags to the remote repository.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git push --tags&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git push &amp;lt;remote&amp;gt; --follow-tags&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pushes only annotated tags to the remote repository.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git push origin --follow-tags&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git push origin -d &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Deletes a tag from the remote repository.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git push origin -d v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag &amp;lt;tagname&amp;gt; &amp;lt;commit&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tags a specific commit instead of the latest one.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag v1.0.0 9fceb02&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git checkout &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Check out a tag (detached HEAD state).&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git checkout v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git tag -s &amp;lt;tagname&amp;gt; -m "&amp;lt;message&amp;gt;"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a signed tag using GPG.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git tag -s v1.0.0 -m "Signed release"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;git verify-tag &amp;lt;tagname&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Verifies a signed tag.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;git verify-tag v1.0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Best Practices for Git Tags
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Semantic Versioning:&lt;/strong&gt; Adopt a versioning strategy like semantic versioning (e.g., &lt;code&gt;v2.1.0&lt;/code&gt;) to keep your tags meaningful. You can learn about semantic versioning &lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tag Releases:&lt;/strong&gt; Always tag a commit when you release a new version of your software. This can be automated via CI/CD pipelines based on your branching strategy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep Tags Descriptive:&lt;/strong&gt; Use annotated tags for significant commits to provide context and information about the tag. Annotated tags bring more information as compared to lightweight tags.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;em&gt;Git&lt;/em&gt; &lt;em&gt;tags&lt;/em&gt; are a powerful tool in your version control toolkit, offering a simple yet effective way to mark important points in your repository's history. Whether you're managing releases, tracking milestones, or preparing for deployment, &lt;em&gt;Git&lt;/em&gt; &lt;em&gt;tags&lt;/em&gt; help you maintain a clear and organized workflow.&lt;/p&gt;

&lt;p&gt;That's it for today, and congratulations to everyone who has followed this article! You've successfully learned about Git tags. Go and implement them in your projects and make your life as a developer easy! 🎉&lt;/p&gt;

&lt;p&gt;I hope you have learned something new, just as I did. If you enjoyed this article, please like and share it. Also, &lt;strong&gt;follow me to read&lt;/strong&gt;] more exciting articles. You can check out my social links &lt;a href="https://github.com/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Basics-Tagging" rel="noopener noreferrer"&gt;Git Documentation: Tagging&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Top 9 Essential Utility Types in TypeScript</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Mon, 12 Aug 2024 17:29:42 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/top-9-essential-utility-types-in-typescript-jm3</link>
      <guid>https://dev.to/amanpreet-dev/top-9-essential-utility-types-in-typescript-jm3</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fy9p39en0utpoccijcd2s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fy9p39en0utpoccijcd2s.png" alt="Top 9 Essential Utility Types in TypeScript" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use TypeScript?
&lt;/h2&gt;

&lt;p&gt;Typescript is a strongly typed superset of Javascript that gives you better tooling at any scale, enabling developers to catch errors at compile time rather than runtime.&lt;/p&gt;

&lt;p&gt;TypeScript brings&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Safety&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced IDE support&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are Utility Types?
&lt;/h2&gt;

&lt;p&gt;Utility types are predefined types that help manipulate and transform other types. They help in simplifying complex type definitions.&lt;/p&gt;

&lt;p&gt;By understanding and effectively using these utility types, you can write more concise, readable, and maintainable code, making your development process smoother and more efficient. These utilities are available globally.&lt;/p&gt;

&lt;p&gt;Let's explore the top 9 utility types in TypeScript, with examples and explanations of how they can streamline your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partial&amp;lt;Type&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type with all properties of &lt;code&gt;Type&lt;/code&gt; set to optional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;updatedUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the interface &lt;code&gt;User&lt;/code&gt; has two required properties. However, by using the &lt;code&gt;Partial&amp;lt;User&amp;gt;&lt;/code&gt; type, we can make these properties optional.&lt;/p&gt;

&lt;p&gt;This can be useful in update operations where all the fields are not provided.&lt;/p&gt;

&lt;h3&gt;
  
  
  Required&amp;lt;Type&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type containing all properties of &lt;code&gt;Type&lt;/code&gt; set to required. You can consider this the opposite of &lt;code&gt;Partial&amp;lt;Type&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&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;newuser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// error as `age` is missing in the newuser&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code will give you the following error -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Property 'age' is missing in type '{ name: string; }' but required in type 'Required&amp;lt;User&amp;gt;'.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This utility is beneficial where all the properties are required.&lt;/p&gt;

&lt;h3&gt;
  
  
  Readonly&amp;lt;Type&amp;gt;
&lt;/h3&gt;

&lt;p&gt;This utility constructs a type with all the properties of the &lt;code&gt;Type&lt;/code&gt; sets to &lt;code&gt;readonly&lt;/code&gt;, meaning the properties of the newly constructed type cannot be reassigned.&lt;br&gt;&lt;br&gt;
In other words, all the properties of a type &lt;code&gt;Type&lt;/code&gt; are immutable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Readonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Naman&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Error: Cannot assign to 'name' because it is a read-only property.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This utility type can be used to avoid and protect objects from accidental mutations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Record&amp;lt;Keys, Type&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs an object type whose property keys are &lt;code&gt;Keys&lt;/code&gt; and whose property values are &lt;code&gt;Type&lt;/code&gt;. This utility helps map the properties of a type to another type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;moderator&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Permission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;write&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;roles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Permission&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;write&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;delete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;moderator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;write&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This utility helps map the properties of a type to another type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pick&amp;lt;Type, Keys&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type by picking the set of properties &lt;code&gt;Keys&lt;/code&gt; from &lt;code&gt;Type&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserContactInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&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;contact&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserContactInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aman@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The utility type &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; is very useful when you only need a subset of properties from a larger type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Omit&amp;lt;Type, Keys&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type by picking all the properties of &lt;code&gt;Type&lt;/code&gt; and then removing &lt;code&gt;Keys&lt;/code&gt;. This type is an opposite version of &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&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="nl"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&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="nl"&gt;password&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PublicUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&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;publicUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PublicUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aman@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The utility type &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; is useful when you need to exclude sensitive or unnecessary properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  NonNullable&amp;lt;Type&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type by excluding &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; from the defined &lt;code&gt;Type&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;NonNullable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aman&lt;/span&gt;&lt;span class="dl"&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 &lt;code&gt;NonNullable&amp;lt;T&amp;gt;&lt;/code&gt; type is useful for making sure a value is always present, which helps avoid potential runtime errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exclude&amp;lt;Type, Union&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type by excluding from &lt;code&gt;Type&lt;/code&gt; those types that are assignable to &lt;code&gt;Union&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ErrorStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pending&lt;/span&gt;&lt;span class="dl"&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;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ErrorStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&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 constant &lt;code&gt;error&lt;/code&gt; will have excluded value from the type &lt;code&gt;ErrorStatus&lt;/code&gt;. This type is useful for refining types by excluding or removing specific values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extract&amp;lt;Type, Union&amp;gt;
&lt;/h3&gt;

&lt;p&gt;Constructs a type by extracting from &lt;code&gt;Type&lt;/code&gt; those types that are assignable to &lt;code&gt;Union&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;square&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;triangle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Design&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Extract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;circle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the type &lt;code&gt;Design&lt;/code&gt; will be the type &lt;code&gt;{ kind: "circle"; radius: number; }&lt;/code&gt;. The &lt;code&gt;Extract&amp;lt;T, U&amp;gt;&lt;/code&gt; utility type can be used to isolate specific types or values from a union.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;These utility types are powerful tools in TypeScript. They let you manipulate and transform types in a way that is both clear and concise.&lt;/p&gt;

&lt;p&gt;The utility types are not limited to those mentioned above; there are a few more utility types you can use in your projects such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ReturnType&amp;lt;Type&amp;gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parameters&amp;lt;Type&amp;gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ConstructorParameters&amp;lt;Type&amp;gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;and a few more&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;That's it for today, and congratulations to everyone who has followed this article! You've successfully learned about the top 9 utility types in Typescript. Go and implement them in your projects! 🎉&lt;/p&gt;

&lt;p&gt;I hope you have learned something new, just as I did. If you enjoyed this article, please like and share it. Also, follow me to read more exciting articles. You can check out my social links &lt;a href="https://github.com/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>types</category>
    </item>
    <item>
      <title>How to Easily Import Data from Word Documents into Your App: A Complete Guide</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Mon, 15 Jul 2024 00:30:06 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/how-to-easily-import-data-from-word-documents-into-your-app-a-complete-guide-1kmi</link>
      <guid>https://dev.to/amanpreet-dev/how-to-easily-import-data-from-word-documents-into-your-app-a-complete-guide-1kmi</guid>
      <description>&lt;h2&gt;
  
  
  &lt;img src="https://media2.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%2F7cytfy3go49mvlildr0g.png" alt="How to Easily Import Data from Word Documents into Your App: A Complete Guide" width="800" height="420"&gt;
&lt;/h2&gt;

&lt;p&gt;Learn how to import data from Word documents into your app using Python with this comprehensive guide&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Recently, I was involved in data migration for a client. The data mainly consists of exam questions and their explanations. The data was structured in (.xlsx) format but there was one problem with the content of the data.&lt;/p&gt;

&lt;p&gt;Some of the questions included mathematical equations which was a problem for us as it could not be saved as a text format in the cell of the Excel document. Usually, the equations are added in shape format which was difficult to read programmatically.&lt;/p&gt;

&lt;p&gt;Some of the equations were very complex e.g.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1wua22xa0spnqykhgm3c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1wua22xa0spnqykhgm3c.png" alt="Blackbody Equation, Power per wavelength." width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, instead of saving the mathematical questions in Excel format, they used DOC format which was way easy as compared to adding equations in an Excel format.&lt;/p&gt;

&lt;p&gt;There were around 1000 mathematical questions that included equations in it. One way was to copy/paste manually and the other way was to do it programmatically.&lt;/p&gt;

&lt;p&gt;Being a software developer I preferred the second way and you will find that in the next few minutes how I was able to import the data from a Word document, but before that, we should understand the importance of importing and exporting data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Import/export data?
&lt;/h2&gt;

&lt;p&gt;Import and export of data play a significant role in today's world. Almost all businesses require a set of data to formulate growth strategies and enhance operations, analysis, and decision-making.&lt;/p&gt;

&lt;p&gt;The key benefits of importing and exporting data are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Enhanced decision making&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data sharing and Collaboration&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data Visualization and Reporting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accuracy and Reliability&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;and many more...&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most common formats to import data are CSV, XML, or JSON as they ensure compatibility across different systems and platforms.&lt;/p&gt;

&lt;p&gt;However today we will discuss a different format i.e. Doc files or Word Documents which is mainly a word processing document format. DOC files are used to store data such as formatted text, images, tables, and charts. The most common DOC formats widely used are &lt;code&gt;.doc&lt;/code&gt; or &lt;code&gt;.docx&lt;/code&gt; and they are merely not just a text file but larger than that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the environment
&lt;/h2&gt;

&lt;p&gt;We will be using a Python library &lt;code&gt;python-docx&lt;/code&gt; to read and write DOC files. This library works well with the &lt;code&gt;.docx&lt;/code&gt; format. If you are having &lt;code&gt;.doc&lt;/code&gt; format, you might first need to convert them to &lt;code&gt;.docx&lt;/code&gt; formats either by using Microsoft Word or some conversion tool.&lt;/p&gt;

&lt;p&gt;One good thing that was done was the Doc file was formatted properly since there were 1000 questions to be imported, and each question with its details was added to a separate table. This not only helped organize the document but also did help in importing the document.&lt;/p&gt;

&lt;p&gt;Below is the sample image of the Word document which was used to be imported.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F5rjj9w5tldj4rw3iql3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5rjj9w5tldj4rw3iql3v.png" alt="A table with two columns labeled " width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install python-docx
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;python-docx&lt;/em&gt; is a library for reading, creating, and updating (.docx) files. Let's first install the library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;docx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole task of extracting data from a Word document was to&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Read the Document i.e. use the library &lt;code&gt;python-docx&lt;/code&gt; to extract text from the document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parse the extracted text into structured JSON&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generate JSON object.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Extracting Data
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Read the Document
&lt;/h3&gt;

&lt;p&gt;Below is an example of how to open a document file and read its content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;docx&lt;/span&gt;

&lt;span class="c1"&gt;# Load the document
&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;docx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sample.docx&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Structure of the Document
&lt;/h3&gt;

&lt;p&gt;The basic structure of the document was in a tabular format, and it looks something like below.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;No&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Question&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;cos -640° =?&lt;/td&gt;
&lt;td&gt;cos 80°&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;=&lt;/strong&gt; ?&lt;/td&gt;
&lt;td&gt;½ log 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;If  tan&lt;sup&gt;2&lt;/sup&gt; x + (1 - ) tan x -  = 0 then x is&lt;/td&gt;
&lt;td&gt;n - /4  or n + /3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;The distance between the foci of the ellipse 4*x*&lt;sup&gt;2&lt;/sup&gt;+5*y*&lt;sup&gt;2&lt;/sup&gt;=20 is&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;If the two circles &lt;em&gt;x&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;+&lt;em&gt;y&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;+2*gx*+2f*y*=0 and &lt;em&gt;x&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt; + &lt;em&gt;y&lt;/em&gt;&lt;sup&gt;2&lt;/sup&gt;+2*g*1&lt;em&gt;x&lt;/em&gt;+2f1&lt;em&gt;y&lt;/em&gt;=0&lt;/td&gt;
&lt;td&gt;f/f1=&lt;em&gt;g&lt;/em&gt;/&lt;em&gt;g&lt;/em&gt;1
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now based on the above structure we can easily identify that the first row is the header row and the rest of the rows are the data rows. In the next part, we will parse the data based on the tabular format.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Keep in mind that if your mathematical equations are saved as plain text, they will appear as a regular string. However, if they are stored as LaTeX content, they will display exactly as they were saved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Parse the Document
&lt;/h3&gt;

&lt;p&gt;Let's parse the data based on the structure. Since our data is stored in tabular format, it is good to make sure that the count of tables in the document is 1 and not more than that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Count the No of Tables in the document.
&lt;/span&gt;&lt;span class="n"&gt;table_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tables&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Number of tables in the document:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;table_count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case it is more than 1 then you need to make sure about the structure of the table and whether it is to be extracted or not.&lt;/p&gt;

&lt;p&gt;Let's extract the questions from the table, assuming we have multiple tables and the table to be used is at the index 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Extract questions from the document
&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extract_questions_from_table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_questions_from_table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cells&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="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cells&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="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;solution&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cells&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tables&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="n"&gt;rows&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="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we are using &lt;em&gt;nested list comprehension&lt;/em&gt;. We can extract the data from the table. The outer loop for the table in &lt;code&gt;doc.tables&lt;/code&gt; iterates over each table in the document, and the inner loop for row in &lt;code&gt;table.rows[1:]&lt;/code&gt; iterates over each row in the current table, starting from the second row.&lt;/p&gt;

&lt;p&gt;For each row, a dictionary is created with the text from the first three cells and added to the list.&lt;/p&gt;

&lt;p&gt;The output should be something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'question'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'cos -640° = ?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'solution'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'cos 80°'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'question'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'= ?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'solution'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'½ log 2'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'question'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'If  tan2 x + (1 - ) tan x -  = 0 then x is'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'solution'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'n - /4  or n + /3'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'question'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'The distance between the foci of the ellipse 4x2+5y2=20 is'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'solution'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'2'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'5'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'question'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'If the two circles x2+y2+2gx+2fy=0 and x2 + y2+2g1x+2f1y=0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'solution'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'f/f1=g/g1'&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generate the JSON from extracted data
&lt;/h3&gt;

&lt;p&gt;Next, we can create the JSON format from the output we received earlier, by using the below code. Make sure the native module &lt;code&gt;json&lt;/code&gt; is imported initially.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Generate JSON based on the output response.
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;questionnaires&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;questionnaire&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;index&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;explanation&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;solution&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;questionnaires&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questionnaire&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;w&lt;/span&gt;&lt;span class="sh"&gt;"&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;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questionnaires&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Generate JSON File
&lt;/span&gt;&lt;span class="n"&gt;jsonfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sample.json&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voilà! We have successfully extracted the data from a Word document into JSON format. It is important to note that the more structured the data is, the easier it will be to extract.&lt;/p&gt;

&lt;p&gt;The above data can be easily stored in any type of persistent storage, such as RDBMS or NoSQL databases.&lt;/p&gt;

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

&lt;p&gt;That's it for today, and congratulations to everyone who has followed this blog! You've successfully imported data from a structured Word document into JSON format. Awesome job! 🎉&lt;/p&gt;

&lt;p&gt;I hope you have learned something new, just as I did. If you enjoyed this article, please like and share it. Also, &lt;a href="https://dev.to/amanpreet-dev"&gt;&lt;strong&gt;follow me to read&lt;/strong&gt;&lt;/a&gt; more exciting articles. You can check out my social links &lt;a href="https://github.com/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>documentation</category>
      <category>dataextraction</category>
      <category>word</category>
    </item>
    <item>
      <title>Why Every Web Developer Should Know HTMX?</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Fri, 22 Sep 2023 06:03:27 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/why-every-web-developer-should-know-htmx-2ipd</link>
      <guid>https://dev.to/amanpreet-dev/why-every-web-developer-should-know-htmx-2ipd</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fayhg8efz3uvvoh9mdifu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fayhg8efz3uvvoh9mdifu.png" alt="Why every Web Developer should know HTMX?" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In an era dominated by front-end frameworks like React, Angular, Vue, and Svelte simplicity can often feel like a lost art. Enters HTMX - a lightweight, yet powerful, javascript library that dares to reimagine the way we think about modern web development.&lt;/p&gt;

&lt;p&gt;So what is HTMX?, lets deep dive into the world of HTMX for the next few minutes and explore everything about its key features and breathing new life into the increasingly complex landscapes of front-end development.&lt;/p&gt;

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

&lt;p&gt;What if you could supercharge your HTML with AJAX requests, CSS transitions, web socket, and server-side events capabilities directly from HTML attributes? That's what HTMX can do for you.&lt;/p&gt;

&lt;p&gt;HTMX is small (approx 14k min.gz in size), dependency-free, extendable, and IE11 compatible. Yes you heard me right it is compatible with IE11&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fqx8oi81gioxq1m7rs6ty.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fqx8oi81gioxq1m7rs6ty.jpg" alt="No more IE11 meme" width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Choose HTMX?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simplicity and Ease of Learning&lt;/strong&gt;: HTMX allows you to add dynamic and interactive content directly within HTML without any need to write custom JavaScript. Making it easier for developers who are more comfortable with HTML and CSS.&lt;br&gt;&lt;br&gt;
It's especially helpful for newcomers to web development or server-side developers wanting to add a little front-end magic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-Side Rendering Friendly&lt;/strong&gt;: HTMX is designed to work well with server-renders HTML, making it a good fit for projects that rely on server-side frameworks like Django, and PHP.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rapid Prototyping&lt;/strong&gt;: The simplicity of HTMX makes it well-suited for quickly prototyping dynamic behavior without having to set up a full front-end development environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Core Concepts and Terminology
&lt;/h2&gt;

&lt;p&gt;HTMX makes HTML extensible, more interactive and thus turning static HTML into "hypertext", which is HTML with behaviors and dynamic capabilities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AJAX&lt;/strong&gt;: HTMX makes it easier to use AJAX right within HTML, without writing JavaScript code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WebSockets&lt;/strong&gt;: Support of Websockets in HTML via HTMX enables real-time data exchange with the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-Sent Events&lt;/strong&gt;: HTMX supports the protocol for receiving updates from the server over a single HTTP connection, making it easier to build real-time updates into your web app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;hx-* Attributes&lt;/strong&gt;: The &lt;code&gt;hx-*&lt;/code&gt; attributes are custom attributes introduced by HTMX to extend HTML tags with dynamic behaviors making them more interactive and extensible. Some of them are &lt;code&gt;hx-get&lt;/code&gt;, &lt;code&gt;hx-post&lt;/code&gt;, &lt;code&gt;hx-target&lt;/code&gt; etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Handling:&lt;/strong&gt; Event handling in HTMX allows you to manage the lifecycle of HTTP requests, from the moment the event is triggered to the time when the new content is swapped into the DOM, thus adding more interactivity and dynamic behavior to the applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enough with the theoretical part, let's now deep dive into some practical work with some code examples. Before that let's set up HTMX to use further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up HTMX
&lt;/h2&gt;

&lt;p&gt;HTMX is a dependency-free, browser-oriented javascript library, thus no need for complicated build steps or systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Via a CDN&lt;/strong&gt;: The fastest and simplest way is to add the below code to the &lt;code&gt;head&lt;/code&gt; tag of your web page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Download a copy&lt;/strong&gt;: The next simplest way is to download &lt;code&gt;htmx.min.js&lt;/code&gt; from &lt;a href="https://unpkg.com/htmx.org/dist/htmx.min.js" rel="noopener noreferrer"&gt;unpkg.com&lt;/a&gt;, add it to the appropriate directory in your project, and include it using &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Via NPM&lt;/strong&gt;: For NPM-based build systems, you can install HTMX via a simple command&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Basic Examples and Use Cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Button Click to Fetch Data
&lt;/h3&gt;

&lt;p&gt;The below example shows how to use HTMX to fetch data from a URL when a button is clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button hx-get="/fetch/data" hx-trigger="click" &amp;gt;Fetch Data&amp;lt;/button&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;When the button is clicked, HTMX sends a GET request &lt;code&gt;/fetch/data&lt;/code&gt; and replaces the content of the button with the response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating a ToDo List
&lt;/h3&gt;

&lt;p&gt;Suppose you have a ToDo list and you want to mark an item as completed when it's clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul id="todo-list"&amp;gt;
  &amp;lt;li hx-post="/mark-complete" hx-trigger="click" hx-swap="outerHTML"&amp;gt;Buy Milk&amp;lt;/li&amp;gt;
  &amp;lt;li hx-post="/mark-complete" hx-trigger="click" hx-swap="outerHTML"&amp;gt;Walk the Dog&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;When you click an item, HTMX sends a POST request to &lt;code&gt;/mark-complete&lt;/code&gt; and replaces the clicked &lt;code&gt;li&lt;/code&gt; element with the new HTML received from the server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Form Submission
&lt;/h3&gt;

&lt;p&gt;The below example shows how to submit a form using HTMX&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form hx-post="/submit/form" hx-trigger="submit"&amp;gt;
  &amp;lt;input type="text" name="username" placeholder="Enter your username"&amp;gt;
  &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;When the form is submitted, HTMX sends a POST request to &lt;code&gt;/submit/form&lt;/code&gt; with the form data. The form content will be replaced by the server's response.&lt;/p&gt;

&lt;p&gt;Some of the other &lt;strong&gt;Use Cases&lt;/strong&gt; can be like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Live Search&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Infinite Scroll&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Form Validation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bulk update&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time updates&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Advanced Features
&lt;/h2&gt;

&lt;p&gt;HTMX offers several advanced features that make it a powerful tool for developing interactive and dynamic web applications. Below are some of the features.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partial Updates with &lt;code&gt;hx-select&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;the &lt;code&gt;hx-select&lt;/code&gt; attribute allows you to specify which part of the server's response should be used to update the DOM. The enables you to perform partial page updates and reduce the amount of data transferred over the network.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div hx-get="/fetch/data" hx-select="#some-element"&amp;gt;
  Click to Fetch Partial Data
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Request Indicators
&lt;/h3&gt;

&lt;p&gt;HTMX allows you to add visual indicators that show when a request is in progress. The &lt;code&gt;hx-indicator&lt;/code&gt; attribute lets you specify an element that will be displayed during the AJAX call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button hx-get="/data" hx-indicator="#loadingSpinner"&amp;gt;
    Fetch Data
&amp;lt;/button&amp;gt;
&amp;lt;div id="loadingSpinner" style="display:none;"&amp;gt;Loading...&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Trigger Modifiers
&lt;/h3&gt;

&lt;p&gt;HTMX supports various trigger modifiers like &lt;code&gt;once&lt;/code&gt;, &lt;code&gt;delay&lt;/code&gt;, and &lt;code&gt;throttle&lt;/code&gt; to fine-tune when and how often should be triggered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button hx-get="/data" hx-trigger="click delay:500ms"&amp;gt;Fetch Data&amp;lt;/button&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lazy Loading
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;hx-trigger&lt;/code&gt; attribute set to &lt;code&gt;revealed&lt;/code&gt;, HTMX enables lazy loading of content as elements come into view, this can improve performance by reducing initial load times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div hx-get="/lazy-load" hx-trigger="revealed"&amp;gt;
  &amp;lt;!-- Content will be loaded when this element comes into view --&amp;gt;
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  History Management
&lt;/h3&gt;

&lt;p&gt;HTMX provides a way to manipulate browser history. With attributes like &lt;code&gt;hx-push-url&lt;/code&gt;, you can change the URL displayed in the browser's address bar after a request, facilitating navigation and bookmarking.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div hx-get="/new/page" hx-push-url="true"&amp;gt;
  Go to New Page
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Based on the above code, when a user clicks on the &lt;code&gt;div&lt;/code&gt;, HTMX will snapshot the current DOM and store it before it requests the&lt;code&gt;/new/page&lt;/code&gt;. It then does the swap and pushes a new location onto the history stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  WebSockets
&lt;/h3&gt;

&lt;p&gt;If you wish to establish a &lt;code&gt;WebSocket&lt;/code&gt; connection in HTMX, you use the &lt;code&gt;ht-ws&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div hx-ws="connect:wss:/chatroom"&amp;gt;
    &amp;lt;div id="chat_room"&amp;gt;
        ...
    &amp;lt;/div&amp;gt;
    &amp;lt;form hx-ws="send:submit"&amp;gt;
        &amp;lt;input name="chat_message"&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;connect&lt;/code&gt; declaration establishes the connection and the &lt;code&gt;send&lt;/code&gt; declaration tells the form to submit values to the socket on &lt;code&gt;submit&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing HTMX with Other Front-end Libraries/ Frameworks
&lt;/h2&gt;

&lt;p&gt;As compared to some heavyweight frontend frameworks like Angular, React, or Vue, HTMX offers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple Learning Curve&lt;/strong&gt; : HTMX has a significantly lower learning curve compared to frameworks like React, Angular, or Vue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt; : Since HTMX minimizes the need for client-side JavaScript, web pages often load faster and consume less memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server Rendering&lt;/strong&gt; : HTMX naturally embraces server-rendered HTML, making it a good fit for those who prefer this approach.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Integration with Backend Technologies
&lt;/h2&gt;

&lt;p&gt;HTMX is backend-agnostic and can integrate seamlessly with virtually any server-side language or framework such as Django, Ruby on Rails, Flask, and others.&lt;/p&gt;

&lt;p&gt;You can easily couple RESTful APIs or GraphQL endpoints, giving you flexible options for data fetching and manipulation.&lt;/p&gt;

&lt;p&gt;HTMX native support of WebSockets and Server-Side events allows integration of real-time features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Community &amp;amp; Ecosystem
&lt;/h2&gt;

&lt;p&gt;While growing in popularity, its community is smaller compared to established frameworks like React, Angular, Vue, or Svelte. With vast community and documentation resources, the above-mentioned frameworks are widely used and are a go-to choice for many developers.&lt;/p&gt;

&lt;p&gt;As HTMX attracts more followers, enthusiasts, and contributors, its community will grow naturally. It is still very young as compared to other front-end frameworks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🚀 HTMX has been accepted into the first class of the GitHub Open Source Accelerator for 2023&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Best Practices and Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Server-side validation&lt;/strong&gt;: Take advantage of HTMX's server-side-centric model to handle data validation on the server, making the client lighter and more secure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utilize Event Hooks&lt;/strong&gt;: Make the most of HTMX's lifecycle events for fine-grained control over requests and DOM updates.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limitations of HTMX
&lt;/h2&gt;

&lt;p&gt;Like any other tool, library, or framework we use, HTMX also has limitations, and some of them are listed below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Limited tooling and No Type safety can make debugging a lot more difficult as compared to other frameworks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"JS-in-HTML" approach has been in development earlier also with JS libraries and frameworks like &lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;Alpine&lt;/a&gt;, &lt;a href="https://knockoutjs.com/" rel="noopener noreferrer"&gt;Knockout&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not ideal for SPAs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lack of Virtual DOM: HTMX does not use virtual DOM, which can sometimes make complex UI updates less efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No architectural approach for complex applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;While growing in popularity, HTMX is carving its niche, offering simpler yet powerful alternatives to other widely used frontend libraries and frameworks. It is quite good for smaller projects and in the end, everything comes down to the project requirements and its complexity.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. If so, kindly like and share the article and also &lt;a href="https://dev.to/amanpreet-dev"&gt;&lt;strong&gt;follow me&lt;/strong&gt;&lt;/a&gt; to read more exciting articles. You can check my social links &lt;a href="https://biodrop.io/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;HTMX &lt;a href="https://htmx.org/docs/" rel="noopener noreferrer"&gt;Docs&lt;/a&gt; &amp;amp; &lt;a href="https://htmx.org/examples/" rel="noopener noreferrer"&gt;Examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>htmx</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Unpacking the Latest Features and Enhancements of MongoDB 7.0</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Sat, 19 Aug 2023 06:03:51 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/unpacking-the-latest-features-and-enhancements-of-mongodb-70-2m8g</link>
      <guid>https://dev.to/amanpreet-dev/unpacking-the-latest-features-and-enhancements-of-mongodb-70-2m8g</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F6845513w9dkqt7qlwk9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6845513w9dkqt7qlwk9r.png" alt="MongoDB 7.0" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The recent release of MongoDB 7.0 continues this tradition of innovation, introducing a series of enhancements that promise to revolutionize the way developers interact with their data.&lt;/p&gt;

&lt;p&gt;Whether you are a seasoned MongoDB user or exploring it for the first time, the latest version offers exciting opportunities to take your development to the next level.&lt;/p&gt;

&lt;p&gt;MongoDB has found its way into various industry applications, from large-scale e-commerce platforms, Stream Processing, Transactional, and Analytical to intricate Internet of Things (IoT) networks.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's new in MongoDB 7.0?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  New Aggregations Operators
&lt;/h3&gt;

&lt;p&gt;MongoDB 7.0 brings two new aggregation operators &lt;code&gt;$median&lt;/code&gt; and &lt;code&gt;$percentile&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$median&lt;/strong&gt; : In a dataset, the median is the percentile value where 50% of the data falls at or below that value. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$percentile&lt;/strong&gt; : In a dataset, a given percentile is a value where that percentage of the data falls at or below that value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are looking to learn more about aggregations, you can check this article with sample datasets &lt;a href="https://dev.to/amanpreet-dev/mastering-mongodb-top-5-essential-aggregation-pipelines-1pdn"&gt;top 5 essential aggregations of MongoDB&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Series Collections
&lt;/h3&gt;

&lt;p&gt;Time series data is a sequence of data points, typically measures at successive points in time. for e.g. stock prices, temperature readings.&lt;/p&gt;

&lt;p&gt;Time series collections in MongoDB are specifically designed to handle time series data. These are optimized for storing, querying, and processing timestamped data.&lt;/p&gt;

&lt;p&gt;Time series collections were first introduced in version 5.0 of MongoDB and were having some limitations. The new MongoDB version 7.0 removes most of the time series limitations that were based on the &lt;code&gt;$delete&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Together with this, it also brings significant improvements to working with time series data which results in improved storage and query performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compound Wildcard Indexes
&lt;/h3&gt;

&lt;p&gt;MongoDB 7.0 supports creating wildcard indexes on a field or a set of fields. A compound wildcard index has one wildcard term and one or more additional index terms.&lt;/p&gt;

&lt;p&gt;To create a wildcard index on a single field use the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.collection.createIndex( { "&amp;lt;field&amp;gt;.$**": &amp;lt;sortOrder&amp;gt; } )

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

&lt;/div&gt;



&lt;p&gt;To create a wildcard index on all fields excluding &lt;code&gt;_id&lt;/code&gt; we can use the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.&amp;lt;collection&amp;gt;.createIndex( { "$**": &amp;lt;sortOrder&amp;gt; } )

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Atlas Search Index Management
&lt;/h3&gt;

&lt;p&gt;MongoDB's Atlas Search feature allows fine-grained text indexing and querying of data. This feature is only available on Atlas Clusters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fas7cnvj0zlqn3u5rw9o2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fas7cnvj0zlqn3u5rw9o2.png" alt="Atlas Search architecture" width="703" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Atlas Search index is a data structure that categorizes data in an easily searchable format. In simple words, it is the mapping between terms and documents that contain these terms.&lt;/p&gt;

&lt;p&gt;In MongoDB 7.0 we can now manage Atlas Search Indexes with &lt;code&gt;mongosh&lt;/code&gt; methods and database commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Large Change Stream Events
&lt;/h3&gt;

&lt;p&gt;Change Streams allow applications to access real-time data changes without complexity and risk of tailing the operation log i.e &lt;code&gt;oplog&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Change Streams can be beneficial for reliable business systems architectures, informing downstream systems once data changes are durable.&lt;/p&gt;

&lt;p&gt;In the new version of MongoDB, if you have change stream events larger than 16MB, we can use the new &lt;code&gt;$changeStreamSplitLargeEvent&lt;/code&gt; stage to split events into smaller fragments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;p&gt;Starting in MongoDB 7.0, security capabilities have been strengthened by making general availability of &lt;strong&gt;&lt;em&gt;Queryable Encryption&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Queryable Encryption offers end-to-end encryption of sensitive data while preserving the ability to run equality queries on that encrypted data.&lt;/p&gt;

&lt;p&gt;Working Model of Queryable Encryption:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwebimages.mongodb.com%2F_com_assets%2Fcms%2Fllcnxjsr478znmlnr-ezgif.com-optimize.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwebimages.mongodb.com%2F_com_assets%2Fcms%2Fllcnxjsr478znmlnr-ezgif.com-optimize.gif" alt="Gif displaying how queryable encryption works" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The latest version of MongoDB Enterprise version supports OIDC i.e. OpenID connect authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrading to MongoDB 7.0
&lt;/h2&gt;

&lt;p&gt;To upgrade a standalone from a previous major release, kindly follow the guidelines mentioned &lt;a href="https://www.mongodb.com/docs/v7.0/release-notes/7.0-upgrade-standalone/#std-label-7.0-upgrade-standalone" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are trying to install the latest version of MongoDB, visit this &lt;a href="https://www.mongodb.com/try/download/community" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the time of writing this article, MongoDB 7.0 was not available via Docker Hub but should be available in near time.&lt;/p&gt;

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

&lt;p&gt;With a full set of new features, security improvements, and other enhancements, MongoDB 7.0 is the perfect choice for organizations to take their development to the next level.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. If so, kindly like and share the article and also &lt;a href="https://dev.to/amanpreet-dev"&gt;&lt;strong&gt;follow me&lt;/strong&gt;&lt;/a&gt; to read more exciting articles. You can check my social links &lt;a href="https://linkfree.io/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://mongodb.com/docs/" rel="noopener noreferrer"&gt;MongoDB Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/docs/v7.0/release-notes/7.0/" rel="noopener noreferrer"&gt;Release Notes for MongoDB&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/blog/post/mongodb-announces-queryable-encryption" rel="noopener noreferrer"&gt;Queryable Encryption&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  General Acknowledgment
&lt;/h2&gt;

&lt;p&gt;The images used to describe the Atlas Search Index and Model of Queryable Encryption have been taken from the MongoDB website and are owned by MongoDB Inc.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Mastering MongoDB: Top 5 Essential Aggregation Pipelines</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Thu, 10 Aug 2023 08:53:53 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/mastering-mongodb-top-5-essential-aggregation-pipelines-1pdn</link>
      <guid>https://dev.to/amanpreet-dev/mastering-mongodb-top-5-essential-aggregation-pipelines-1pdn</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Ftn8fy2gfi3f5gf1f02kh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftn8fy2gfi3f5gf1f02kh.png" alt="top-5-aggregation-pipelines" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;MongoDB is a leading NoSQL database with a schema-less nature, and the ability to handle large volumes of data for modern web applications.&lt;/p&gt;

&lt;p&gt;Today MongoDB is been used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Streamline building AI-enriched applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Video Games that are global, reliable, and scalable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Real-time analytical views of all the most important data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Analyze and act on data from the physical world i.e. Internet of Things (IoT).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store, edit, and present all types of content management.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Basic knowledge of MongoDB. You can install MongoDB version 6.0 from &lt;a href="https://www.mongodb.com/try/download/community" rel="noopener noreferrer"&gt;here&lt;/a&gt; and can also use the &lt;a href="https://www.mongodb.com/cloud/atlas" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt; to try out these examples.&lt;/p&gt;

&lt;p&gt;You can run the examples by using the &lt;a href="https://mongoplayground.net/" rel="noopener noreferrer"&gt;MongoDB Playground&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Aggregation and its pipelines?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Aggregation:
&lt;/h3&gt;

&lt;p&gt;MongoDB inherits capabilities to perform complex transformations and calculations, extracting meaningful information we need to aggregate data.&lt;/p&gt;

&lt;p&gt;Aggregation simply refers to the process of collecting and processing data to produce a single consolidated output or result. E.g. calculating the sum, average, count, or other statistical measures from the dataset.&lt;/p&gt;

&lt;h3&gt;
  
  
  Aggregation Pipelines:
&lt;/h3&gt;

&lt;p&gt;In MongoDB, the aggregation pipeline processes documents i.e. the data records, and returns computed results. A pipeline is made up of one or more stages, and each stage transforms the data i.e. documents as it moves through the pipeline.&lt;/p&gt;

&lt;p&gt;The basic flow for any aggregation looks something like this as shown in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fuwp66kjgexlc75vz0u3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fuwp66kjgexlc75vz0u3g.png" alt="monogodb-aggregation-pipeline-diagram" width="800" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This framework allows for flexible, complex, and powerful operations on data, enabling complex queries and data transformations to be performed directly in databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Top 5 Essential Aggregation Pipelines
&lt;/h2&gt;

&lt;p&gt;The top 5 essential aggregation pipelines which every MongoDB developer should know are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$match&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$group&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$project&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$lookup&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$unwind&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will discuss each aggregation in detail - from prototype form i.e. syntax to examples in the following context. We will be using some sample datasets, so let's deep dive into this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sample Dataset
&lt;/h3&gt;

&lt;p&gt;Let's imagine we are handling data for an e-commerce platform. Below is the sample dataset for the &lt;code&gt;products&lt;/code&gt; and &lt;code&gt;orders&lt;/code&gt; collection to understand the top 5 essential aggregation pipelines.&lt;/p&gt;

&lt;p&gt;Sample Dataset for &lt;code&gt;products&lt;/code&gt; collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Products Collection
[
    {
        "_id": 101,
        "productName": "Laptop",
        "brand": "TechCorp",
        "specs": ["16GB RAM", "512GB SSD", "Intel i7"]
    },
    {
        "_id": 102,
        "productName": "Headphones",
        "brand": "AudioTech",
        "specs": ["Noise Cancelling", "Bluetooth", "8hr Battery"]
    },
    {
        "_id": 103,
        "productName": "Mouse",
        "brand": "TechCorp",
        "specs": ["Wireless", "Optical"]
    },
    {
        "_id": 104,
        "productName": "Keyboard",
        "brand": "TechWorks",
        "specs": ["Mechanical", "RGB Backlit"]
    }
]

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

&lt;/div&gt;



&lt;p&gt;Sample dataset for &lt;code&gt;orders&lt;/code&gt; collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Orders Collection
[
    {
        "_id": 1,
        "customerName": "Alice",
        "productId": 101,
        "quantity": 2,
        "price": 1000,
        "orderDate": "2023-08-01",
        "shippingCity": "New York"
    },
    {
        "_id": 2,
        "customerName": "Bob",
        "productId": 102,
        "quantity": 5,
        "price": 50,
        "orderDate": "2023-08-05",
        "shippingCity": "Los Angeles"
    },
    {
        "_id": 3,
        "customerName": "Charlie",
        "productId": 101,
        "quantity": 1,
        "price": 1000,
        "orderDate": "2023-08-05",
        "shippingCity": "New York"
    },
    {
        "_id": 4,
        "customerName": "Alice",
        "productId": 103,
        "quantity": 3,
        "price": 20,
        "orderDate": "2023-08-08",
        "shippingCity": "Boston"
    },
    {
        "_id": 5,
        "customerName": "Eve",
        "productId": 104,
        "quantity": 1,
        "price": 70,
        "orderDate": "2023-08-09",
        "shippingCity": "San Francisco"
    }
]

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

&lt;/div&gt;



&lt;p&gt;Please note: &lt;code&gt;productId&lt;/code&gt; in &lt;code&gt;orders&lt;/code&gt; collection refers to the product's &lt;code&gt;_id&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  $match
&lt;/h3&gt;

&lt;p&gt;Filters the documents to pass only the document that matches the specified condition(s) to the next pipeline stage.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$match&lt;/code&gt; has the following prototype form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{$match: { &amp;lt;query&amp;gt; } }

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

&lt;/div&gt;



&lt;p&gt;Now based on our sample data from &lt;code&gt;orders&lt;/code&gt; collection, let's write a &lt;code&gt;$match&lt;/code&gt; query to &lt;em&gt;filter orders that are shipped from New York City&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Check the query below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.orders.aggregate([
{$match: { shippingCity: "New York" } }
)]

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

&lt;/div&gt;



&lt;p&gt;Based on the dataset of &lt;code&gt;orders&lt;/code&gt; collection there will be two documents as a result. The result for the above aggregation pipeline will be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "_id": 1,
    "customerName": "Alice",
    "orderDate": "2023-08-01",
    "price": 1000,
    "productId": 101,
    "quantity": 2,
    "shippingCity": "New York"
  },
  {
    "_id": 3,
    "customerName": "Charlie",
    "orderDate": "2023-08-05",
    "price": 1000,
    "productId": 101,
    "quantity": 1,
    "shippingCity": "New York"
  }
]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  $group
&lt;/h3&gt;

&lt;p&gt;This stage separates documents into groups according to the "&lt;em&gt;group key&lt;/em&gt;". The output is one document for each unique group key. A group key is often a field or group of fields e.g &lt;code&gt;_id&lt;/code&gt;, &lt;code&gt;productId&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;💡 $group does &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; order its output documents.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$group&lt;/code&gt; stage has the following prototype form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    $group: {
        _id: &amp;lt;expression&amp;gt;, // Group Key
        &amp;lt;field1&amp;gt; : { &amp;lt;accumulator1&amp;gt; : &amp;lt;expression1&amp;gt; }, // Optional
        ...
    }
}

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

&lt;/div&gt;



&lt;p&gt;Let's determine the &lt;em&gt;total orders for each customer&lt;/em&gt;. Based on the &lt;code&gt;orders&lt;/code&gt; collection this can be determined by 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;db.orders.aggregate([
{ 
    $group: {
        _id: "customerName",
        totalOrders: { $sum : 1 }
    }
}
])

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

&lt;/div&gt;



&lt;p&gt;The above query will give us the following result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "_id": "Bob",
    "totalOrders": 1
  },
  {
    "_id": "Eve",
    "totalOrders": 1
  },
  {
    "_id": "Alice",
    "totalOrders": 2
  },
  {
    "_id": "Charlie",
    "totalOrders": 1
  }
]

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

&lt;/div&gt;



&lt;p&gt;You can see from the above results that the results are not in order and also only the &lt;code&gt;_id&lt;/code&gt; i.e. &lt;code&gt;customerName&lt;/code&gt; and the accumulators i.e. &lt;code&gt;totalOrders&lt;/code&gt; are displayed as output.&lt;/p&gt;

&lt;h3&gt;
  
  
  $project
&lt;/h3&gt;

&lt;p&gt;This aggregation is used to pass along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$project&lt;/code&gt; stage has the following prototype form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ $project: { &amp;lt;specification(s)&amp;gt; } }

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

&lt;/div&gt;



&lt;p&gt;Based on the &lt;code&gt;orders&lt;/code&gt; collection, let's process the documents &lt;em&gt;to get the total order value of each order&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;orders&lt;/code&gt; collection, we have two fields &lt;code&gt;price&lt;/code&gt; and &lt;code&gt;quantity&lt;/code&gt;. Let's use the &lt;code&gt;$multiply&lt;/code&gt; operator to get the total order value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.orders.aggregate([
{     
    $project : {
        orderValue: { $multiply: ['$quantity', 'price'] },
        customerName: 1
    } 
}
])

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

&lt;/div&gt;



&lt;p&gt;The output of the above query will be as follows: We can see from the results that only &lt;code&gt;customerName&lt;/code&gt;, &lt;code&gt;orderValue&lt;/code&gt;, together with &lt;code&gt;_id&lt;/code&gt;fields are projected in the next pipeline stage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "_id": 1,
    "customerName": "Alice",
    "orderValue": 2000
  },
  {
    "_id": 2,
    "customerName": "Bob",
    "orderValue": 250
  },
  {
    "_id": 3,
    "customerName": "Charlie",
    "orderValue": 1000
  },
  {
    "_id": 4,
    "customerName": "Alice",
    "orderValue": 60
  },
  {
    "_id": 5,
    "customerName": "Eve",
    "orderValue": 70
  }
]

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

&lt;/div&gt;



&lt;p&gt;💡 Suppression of the &lt;code&gt;_id&lt;/code&gt; field can be done by using &lt;code&gt;_id: 0&lt;/code&gt; in the $project stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  $lookup
&lt;/h3&gt;

&lt;p&gt;One of the most important aggregations. If you have basic SQL knowledge, it performs a left outer join to a collection in the same database to filter in documents from the "joined" collection for processing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw9wvfjxse2zh83hzsea5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw9wvfjxse2zh83hzsea5.png" alt="left-outer-join" width="537" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$lookup&lt;/code&gt; stage adds a new array field to each input document. The new array field contains the matching documents from the "joined" collection.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;$lookup&lt;/code&gt; has three types of prototype forms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Equality Match with a Single Join collection&lt;/strong&gt; : To perform an equality match between a field from the input documents with a field from the documents of the "joined" collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Join Conditions and Subqueries on a Joined Collection&lt;/strong&gt; : To perform correlated and uncorrelated subqueries with two collections, and perform other join conditions besides a single equality match, use the below syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Correlated Subqueries Using Concise Syntax&lt;/strong&gt; : The below syntax is used which removes the requirement for an equality match on the foreign local fields inside of an &lt;code&gt;$expr&lt;/code&gt; operator:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's write a query to &lt;em&gt;join products and orders&lt;/em&gt; using &lt;code&gt;$lookup&lt;/code&gt;. We are using the reference id &lt;code&gt;productId&lt;/code&gt; from the &lt;code&gt;orders&lt;/code&gt; collection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.orders.aggregate([
    {
        $lookup: {
            from: "products",
            localField: "productId",
            foreignField: "_id",
            as: "productDetails"
        }
    }
]);

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

&lt;/div&gt;



&lt;p&gt;The above query will display the result as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "_id": 1,
    "customerName": "Alice",
    "orderDate": "2023-08-01",
    "price": 1000,
    "productDetails": [
      {
        "_id": 101,
        "brand": "TechCorp",
        "productName": "Laptop",
        "specs": [
          "16GB RAM",
          "512GB SSD",
          "Intel i7"
        ]
      }
    ],
    "productId": 101,
    "quantity": 2,
    "shippingCity": "New York"
  },
  {
    "_id": 2,
    "customerName": "Bob",
    "orderDate": "2023-08-05",
    "price": 50,
    "productDetails": [
      {
        "_id": 102,
        "brand": "AudioTech",
        "productName": "Headphones",
        "specs": [
          "Noise Cancelling",
          "Bluetooth",
          "8hr Battery"
        ]
      }
    ],
    "productId": 102,
    "quantity": 5,
    "shippingCity": "Los Angeles"
  },
  {
    "_id": 3,
    "customerName": "Charlie",
    "orderDate": "2023-08-05",
    "price": 1000,
    "productDetails": [
      {
        "_id": 101,
        "brand": "TechCorp",
        "productName": "Laptop",
        "specs": [
          "16GB RAM",
          "512GB SSD",
          "Intel i7"
        ]
      }
    ],
    "productId": 101,
    "quantity": 1,
    "shippingCity": "New York"
  },
  {
    "_id": 4,
    "customerName": "Alice",
    "orderDate": "2023-08-08",
    "price": 20,
    "productDetails": [
      {
        "_id": 103,
        "brand": "TechCorp",
        "productName": "Mouse",
        "specs": [
          "Wireless",
          "Optical"
        ]
      }
    ],
    "productId": 103,
    "quantity": 3,
    "shippingCity": "Boston"
  },
  {
    "_id": 5,
    "customerName": "Eve",
    "orderDate": "2023-08-09",
    "price": 70,
    "productDetails": [
      {
        "_id": 104,
        "brand": "TechWorks",
        "productName": "Keyboard",
        "specs": [
          "Mechanical",
          "RGB Backlit"
        ]
      }
    ],
    "productId": 104,
    "quantity": 1,
    "shippingCity": "San Francisco"
  }
]

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

&lt;/div&gt;



&lt;p&gt;You can see from the above response that an additional field &lt;code&gt;productDetails&lt;/code&gt; is added which itself is an array and includes the matching documents based on the "joined" collection.&lt;/p&gt;

&lt;h3&gt;
  
  
  $unwind
&lt;/h3&gt;

&lt;p&gt;This aggregation deconstructs an array field from the input documents to output a document for each element.&lt;/p&gt;

&lt;p&gt;Each output document is the input document with the value of the array field replaced by the element.&lt;/p&gt;

&lt;p&gt;The prototype form used for &lt;code&gt;$unwind&lt;/code&gt; aggregation is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  $unwind:
    {
      path: &amp;lt;field path&amp;gt;,
      includeArrayIndex: &amp;lt;string&amp;gt;, // Optional
      preserveNullAndEmptyArrays: &amp;lt;boolean&amp;gt; // Optional, default value: false
    }
}

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

&lt;/div&gt;



&lt;p&gt;Let's use &lt;code&gt;$unwind&lt;/code&gt; aggregation &lt;em&gt;on the Product specs field &lt;/em&gt; of &lt;code&gt;products&lt;/code&gt; collection: The syntax for the query will be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.products.aggregate([
    {
        $unwind: {path: "$specs" }
    }
]);

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

&lt;/div&gt;



&lt;p&gt;The response to using &lt;code&gt;$unwind&lt;/code&gt; will be deconstructed form of &lt;code&gt;specs&lt;/code&gt; array field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "_id": 101,
    "brand": "TechCorp",
    "productName": "Laptop",
    "specs": "16GB RAM"
  },
  {
    "_id": 101,
    "brand": "TechCorp",
    "productName": "Laptop",
    "specs": "512GB SSD"
  },
  {
    "_id": 101,
    "brand": "TechCorp",
    "productName": "Laptop",
    "specs": "Intel i7"
  },
  {
    "_id": 102,
    "brand": "AudioTech",
    "productName": "Headphones",
    "specs": "Noise Cancelling"
  },
  {
    "_id": 102,
    "brand": "AudioTech",
    "productName": "Headphones",
    "specs": "Bluetooth"
  },
  {
    "_id": 102,
    "brand": "AudioTech",
    "productName": "Headphones",
    "specs": "8hr Battery"
  },
  {
    "_id": 103,
    "brand": "TechCorp",
    "productName": "Mouse",
    "specs": "Wireless"
  },
  {
    "_id": 103,
    "brand": "TechCorp",
    "productName": "Mouse",
    "specs": "Optical"
  },
  {
    "_id": 104,
    "brand": "TechWorks",
    "productName": "Keyboard",
    "specs": "Mechanical"
  },
  {
    "_id": 104,
    "brand": "TechWorks",
    "productName": "Keyboard",
    "specs": "RGB Backlit"
  }
]

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;p&gt;Like any other high-performance tool, MongoDB also performs best when it is used properly. Here are some of the best practices for making your MongoDB perform best.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Schema Design&lt;/strong&gt; : Use embedded documents for data that are used more often for &lt;em&gt;read&lt;/em&gt; operations. Use of references for data that won't be accessed every time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indexes&lt;/strong&gt; : Always index fields that are often required, for e.g &lt;code&gt;productId&lt;/code&gt; in the &lt;code&gt;orders&lt;/code&gt; collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Aggregation&lt;/strong&gt; : Use &lt;code&gt;$match&lt;/code&gt; early to reduce the amount of data that subsequent stages need to process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Modelling&lt;/strong&gt; : Avoid the usage of large arrays that will grow over time. For e.g &lt;code&gt;Product Specs&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limit the Result&lt;/strong&gt; : Use &lt;code&gt;$limit&lt;/code&gt; the operator to limit the result from large datasets, wherever applicable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  More Aggregation Queries to Try Out
&lt;/h2&gt;

&lt;p&gt;Do you want to try out more on aggregation, below are some of the queries for you to try out at your end.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Calculate Total Sales for Each Product Spec.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Group by date to find the total sales for each day.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filter orders that exceed a certain price (let's assume $500).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find out the total revenue from each city.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sort customers based on the number of items they purchased.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let me know the results in the comments section, you can also look into the solutions provided by me, check the References section, or &lt;a href="https://github.com/amanpreet-dev/essential-aggregation-pipelines" rel="noopener noreferrer"&gt;click here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to run the examples
&lt;/h3&gt;

&lt;p&gt;You can run the examples by using the MongoDB Playground. You can follow the instructions &lt;a href="https://mongoplayground.net/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to get started with MongoDB Playground.&lt;/p&gt;

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

&lt;p&gt;MongoDB is a NoSQL document database for modern developers working on high-performance applications. With its JSON-like documents, MongoDB allows developers to model data however the application requires. With nesting arrays and embedded sub-documents, it allows the modeling of complex relationships between data.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. If so, kindly like and share the article and also &lt;a href="https://dev.to/amanpreet-dev"&gt;follow me&lt;/a&gt; to read more exciting articles. You can check my social links &lt;a href="https://linkfree.io/amanpreet-dev" rel="noopener noreferrer"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.mongodb.com/docs/manual/" rel="noopener noreferrer"&gt;MongoDB Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;a href="https://mongoplayground.net/" rel="noopener noreferrer"&gt;MongoDB Playground&lt;/a&gt; to run the examples in your browser without installing anything.&lt;/p&gt;

&lt;p&gt;You can find the sample datasets and also the solutions to examples in my repo &lt;a href="https://github.com/amanpreet-dev/essential-aggregation-pipelines" rel="noopener noreferrer"&gt;https://github.com/amanpreet-dev/essential-aggregation-pipelines&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do not forget to give a star for the repo and also follow me on &lt;a href="https://github.com/amanpreet-dev/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>nosql</category>
    </item>
    <item>
      <title>Test-Driven Development (TDD) with TypeScript for Beginners</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Wed, 09 Aug 2023 06:45:38 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/test-driven-development-tdd-with-typescript-for-beginners-231o</link>
      <guid>https://dev.to/amanpreet-dev/test-driven-development-tdd-with-typescript-for-beginners-231o</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F3rr12bwaurjcl6pu5tr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3rr12bwaurjcl6pu5tr2.png" alt="TDD using TypeScript" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to TDD
&lt;/h2&gt;

&lt;p&gt;Before we deep dive into Test-Driven Development or TDD, let me ask a simple question to you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why developers do not write Tests often?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I being a software developer for the past 15 years have been asked/questioned this by/to my colleagues, juniors, and manual QA team members. Here are some of the common reasons.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Time Constraints&lt;/strong&gt; : Writing tests takes time and with developers under tight schedules usually skip tests to meet delivery timelines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Misconceptions that Tests aren't necessary&lt;/strong&gt; : Some developers believe that if they write code carefully, it won't have bugs which leads to overconfidence and perceptions that tests are unnecessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lack of Awareness or Training&lt;/strong&gt; : Not all developers are trained in testing or understanding the benefits of tests. Some might not be aware of tools, and frameworks used for specific languages or platforms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Short-Term vs Long-Term Perspective&lt;/strong&gt; : Developers should consider tests as an investment that pays off in the long run by reducing bugs and improving code readability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lack of Management Support&lt;/strong&gt; : If management doesn't recognize or advocate for the importance of testing, developers aren't appreciated and thus it becomes a less likely routine among developers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Test-Driven Development (TDD) is a software development methodology that emphasizes the importance of writing tests before writing the actual implementation code.&lt;/p&gt;

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

&lt;p&gt;Test-Driven Development or TDD is a software methodology that enables developers to design simple, clean, and tested code. It is a standard way of producing high-quality software which is incorporated with the best coding and design practices.&lt;/p&gt;

&lt;p&gt;Here is a step-by-step overview of the TDD process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write a Failing Test&lt;/strong&gt; : Write a test for the feature that you are about to develop and that doesn't work initially.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write the Minimum Code to Pass the Test&lt;/strong&gt; : Write the minimum amount of code needed to pass the test.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run the Test&lt;/strong&gt; : Execute the test. If it fails, modify the code and retest until it passes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Refactor&lt;/strong&gt; : Once the test passes, examine the code and see if it can be improved or optimized without changing its behavior. One of the most important steps is to ensure code quality and maintainability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Repeat&lt;/strong&gt; : Go back to the first step and repeat the process for the next piece of functionality or feature you want to develop.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Advantages of TDD in Software Development
&lt;/h3&gt;

&lt;p&gt;Below are the precise points highlighting the advantages of Test-Driven Development (TDD)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Code Quality&lt;/strong&gt; : TDD enforces writing tests, leading to more reliable and bug-free code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Faster Debugging&lt;/strong&gt; : By catching defects early in the development process, TDD reduces debugging time and minimizes the impact of errors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Confident Refactoring&lt;/strong&gt; : Developers can confidently refactor code since existing tests ensure that the intended functionality remains intact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous Integration&lt;/strong&gt; : TDD complements CI/CD pipelines by integrating automated tests, ensuring that the new changes don't break existing functionalities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better Collaboration and Increased Productivity&lt;/strong&gt; : Well-defined tests facilitate communication between developers and testers to understand the expected outcomes. TDD leads to better code design, saving time and effort during later stages of development.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How TDD works?
&lt;/h3&gt;

&lt;p&gt;"Red-Green-Refactor" is the mantra that summarizes the TDD cycle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgscnafdrb45p6vjbkgey.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgscnafdrb45p6vjbkgey.png" alt="red-green-refactor" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Red&lt;/strong&gt; : Writing a failing test means seeing a red indication from your test runner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Green&lt;/strong&gt; : Here, we write the minimum amount of code to pass the test thus getting a green indication in our test runner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Refactor&lt;/strong&gt; : Clean up the code, and make it more readable, and efficient without making the test fail. After refactoring the test should still pass.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Importance of Test Coverage
&lt;/h3&gt;

&lt;p&gt;Since code is written around tests, there is a higher chance of more code covered by tests. This leads to fewer bugs and easier maintenance.&lt;/p&gt;

&lt;p&gt;Remember it is not about 100% code coverage where our tests don't make sense or are not working as per expectations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with TDD and TypeScript
&lt;/h2&gt;

&lt;p&gt;Ok, a lot about TDD in theory, now let's get deep knowledge by actually doing some code-level examples. We will be using TypeScript and Jest as our testing frameworks.&lt;/p&gt;

&lt;p&gt;I will be using NodeJs v20.2 for this project, although it should work with almost all stable node versions. All the code will be available in the Github repo shared under the &lt;em&gt;References&lt;/em&gt; section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up a TypeScript Project
&lt;/h3&gt;

&lt;p&gt;To get started, open your terminal and create a new folder named &lt;code&gt;tdd-for-beginners&lt;/code&gt; (you can choose a name of your choice too) and move into that directory. Use the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir tdd-for-beginners &amp;amp;&amp;amp; cd tdd-for-beginners

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

&lt;/div&gt;



&lt;p&gt;Next, we will initialize our project using 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 init -y
# using -y flag tells npm init to automatically use the defaults.

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

&lt;/div&gt;



&lt;p&gt;by running the above command a new file will be created in the repository, named &lt;code&gt;package.json&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install TypeScript and ts-node
&lt;/h3&gt;

&lt;p&gt;Now, let's configure the TypeScript compiler. Run the following command inside your project directory to install TypeScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D typescript ts-node
# This will add the package under devDependencies

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

&lt;/div&gt;



&lt;p&gt;TypeScript is a typed superset of Javascript that compiles to plain Javascript. &lt;code&gt;ts-node&lt;/code&gt; is a tool that allows you to run TypeScript code directly, without the compile step. Install them as development dependencies using the above command.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Create a tsconfig.json file:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;TypeScript uses a file called &lt;code&gt;tsconfig.json&lt;/code&gt; to configure the compiler options for a project. Let's create this file in the root of the project directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Will create the tsconfig.json file with default options
npx tsc --init

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Testing Environment
&lt;/h3&gt;

&lt;p&gt;We will be using &lt;code&gt;Jest&lt;/code&gt; as the testing framework. Let's install the required dependencies by using 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 i -D jest ts-jest @types/jest

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

&lt;/div&gt;



&lt;p&gt;Before we write our first test, we need to configure a few things. Let's do them below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;To specify &lt;code&gt;jest&lt;/code&gt; as our test runner, update the &lt;code&gt;package.json&lt;/code&gt; file as follows&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure Jest: Create a &lt;code&gt;jest.config.js&lt;/code&gt; file at the root of your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TypeScript Configuration: Add the &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;exclude&lt;/code&gt; properties after &lt;code&gt;compilerOptions&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Example 1: Writing Our First Test for adding two numbers
&lt;/h2&gt;

&lt;p&gt;We will use TDD methodology to add two numbers and write tests for them. Now let's write our first test before we deep dive into more complex functionalities.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a Typescript file for the function e.g &lt;code&gt;addNumbers.ts&lt;/code&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a test file e.g &lt;code&gt;addNumbers.spec.ts&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now run the tests by using the below command:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now let's make the test pass by adding the required functionality in the &lt;code&gt;addNumbers.ts&lt;/code&gt; file. This follows our &lt;strong&gt;&lt;em&gt;Green&lt;/em&gt;&lt;/strong&gt; pattern.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now there is not much to refactor here, so we will try to cover this concept in other complex examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2: Writing a Palindrome Checker
&lt;/h2&gt;

&lt;p&gt;Let's create a simple utility function to check if the string is palindrome or not. We will first create two files &lt;code&gt;palindrome.ts&lt;/code&gt; and &lt;code&gt;palindrome.spec.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// palindrome.spec.ts 
import { isPalindrome } from './palindrome';

test('should return true for a palindrome', () =&amp;gt; {
  expect(isPalindrome('racecar')).toBe(true);
});

test('should return false for a non-palindrome', () =&amp;gt; {
  expect(isPalindrome('hello')).toBe(false);
});


// palindrome.ts
export function isPalindrome(str: string) {

}

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

&lt;/div&gt;



&lt;p&gt;Now when you run the command &lt;code&gt;npm test&lt;/code&gt; you will see a failed test or something like this. Remember the &lt;strong&gt;&lt;em&gt;RED&lt;/em&gt;&lt;/strong&gt; pattern.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F71ox852cqgbqtyvq7b2j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F71ox852cqgbqtyvq7b2j.png" alt="failed-test-case" width="800" height="806"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining the Test Case
&lt;/h3&gt;

&lt;p&gt;Now let's fix our test and for that, we should understand what the requirements are or in this case, what is a palindrome string.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An inputted string reads the same forward and backward for e.g - "&lt;em&gt;racecar&lt;/em&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The input string which is not the same when read in reverse, is not a palindrome. e.g "&lt;em&gt;hello&lt;/em&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function should ignore the case while determining whether the string is palindrome or not. e.g "&lt;em&gt;Madam&lt;/em&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function should handle spaces and recognize the input string accordingly. e.g "&lt;em&gt;A man, a plan, a canal, Panama&lt;/em&gt;"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The function should consider special characters while determining palindrome strings. e.g "&lt;em&gt;$#malyalam#$"&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Some of the edge cases to be considered: A single character or an empty string is also considered a palindrome. for e.g "&lt;em&gt;a&lt;/em&gt;", ""&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So in total, there are six test cases for which we need to write a code and make the test pass.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the Function
&lt;/h3&gt;

&lt;p&gt;Based on the above requirements, let's work on the code and check which test cases pass and which fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function isPalindrome(str: string) {
  const reversed = str.split("").reverse().join("");
  return str === reversed;
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Running the Test
&lt;/h3&gt;

&lt;p&gt;Based on the test cases defined earlier, we have six test cases to check whether our code works as expected or not.&lt;/p&gt;

&lt;p&gt;Now let's run the test with different input string values and see where our test fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("should return true for a palindrome", () =&amp;gt; {
  expect(isPalindrome("Madam")).toBe(true);
});

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

&lt;/div&gt;



&lt;p&gt;The above test fails as it doesn't consider case-sensitive, let's fix that in our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function isPalindrome(str: string) {
  str = str.toLowerCase(); 
  const reversed = str.split("").reverse().join("");
  return str === reversed;
}

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

&lt;/div&gt;



&lt;p&gt;Now the changes made as per the above code make our first three test cases pass. You can check the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F2e6iygu5pj0pg688yras.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2e6iygu5pj0pg688yras.png" alt="test-pass" width="800" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Let's work on the other test cases. Suppose you change the input string to "&lt;em&gt;A man, a plan, a canal, Panama&lt;/em&gt;", which is a palindrome string, lets's check our tests whether it gets passed or failed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("should return true for a palindrome", () =&amp;gt; {
  expect(isPalindrome("A man, a plan, a canal, Panama")).toBe(true);
});

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

&lt;/div&gt;



&lt;p&gt;The above test result will fail and the reason for it is not considering spaces, commas i.e. non-word characters though the string actually is a palindrome string. We need to remove non-word characters from the string. Let's refactor the code to make it pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Remove the non-word characters 
export function isPalindrome(str: string) {
  str = str.replace(/\W/g, "").toLowerCase();
  const reversed = str.split("").reverse().join("");
  return str === reversed;
}

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

&lt;/div&gt;



&lt;p&gt;Now by making the above changes our test is passed and it looks something like this. Remember the &lt;strong&gt;&lt;em&gt;Green&lt;/em&gt;&lt;/strong&gt; pattern&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fww0ts0abk7cxn8lwroco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fww0ts0abk7cxn8lwroco.png" alt="all-tests-pass" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now out of six defined test cases, Four have been passed successfully. Let's work on the remaining two test cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Pass a single character or an empty string
test("should return true for a palindrome", () =&amp;gt; {
  expect(isPalindrome("")).toBe(true);
});

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

&lt;/div&gt;



&lt;p&gt;The above code will work and make our test pass but what if we add some special characters in our input string e.g. "#$malayalam$#", this will also pass our test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test("should return true for a palindrome", () =&amp;gt; {
  expect(isPalindrome("$#malayalam#$")).toBe(true);
});

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

&lt;/div&gt;



&lt;p&gt;Thus all are test cases defined as earlier passes. The final test file to check all the test cases defined earlier will look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// All the test cases defined
test("should return true for a palindrome", () =&amp;gt; {
  expect(isPalindrome("racecar")).toBe(true);
  expect(isPalindrome("Madam")).toBe(true);
  expect(isPalindrome("A man, a plan, a canal, Panama!")).toBe(true);
  expect(isPalindrome("$#malayalam#$")).toBe(true);
});

test("should return false for a non-palindrome", () =&amp;gt; {
  expect(isPalindrome("hello world")).toBe(false);
  expect(isPalindrome("TypeScript")).toBe(false);
});

test("should handle empty strings", () =&amp;gt; {
  expect(isPalindrome("")).toBe(true);
});

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

&lt;/div&gt;



&lt;p&gt;The above examples demonstrate how to use TDD to develop small functions with TypeScript. We can apply the same principles and practices to more complex projects, increasing our confidence in the code's correctness and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;💡 Remember, the key is to write tests first and then implement the functionality, incrementally building up your codebase.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It's worth noting that TDD is predominantly associated with unit testing, but the methodology can also be applied (with modifications) to higher levels of testing, such as integration or end-to-end tests.&lt;/p&gt;

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

&lt;p&gt;Despite its benefits, TDD is a challenging way of developing high-quality software, especially for developers new to practice. It requires discipline to always write tests first and to resist the urge to jump directly into coding solutions.&lt;/p&gt;

&lt;p&gt;Using the TDD approach encourages developers to think about their code's requirements, design, and functionality from the start, ultimately leading to better software design and fewer defects.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. If so, kindly like and share the article and also follow me to read more exciting articles. You can check my social links &lt;a href="https://linkfree.io/amanpreet-dev" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;Below is the Github repo with all the code and configuration files.&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/amanpreet-dev/tdd-for-beginners" rel="noopener noreferrer"&gt;https://github.com/amanpreet-dev/tdd-for-beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have committed the code based on various reference points, you can check the &lt;a href="https://github.com/amanpreet-dev/tdd-for-beginners/blob/main/CHANGELOG.md" rel="noopener noreferrer"&gt;CHANGELOG&lt;/a&gt; and &lt;a href="https://github.com/amanpreet-dev/tdd-for-beginners/commits/main" rel="noopener noreferrer"&gt;Commits reference&lt;/a&gt; accordingly.&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>typescript</category>
      <category>testing</category>
      <category>jest</category>
    </item>
    <item>
      <title>Mastering the Art of Node Package Management</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Tue, 08 Aug 2023 05:41:55 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/mastering-the-art-of-node-package-management-kim</link>
      <guid>https://dev.to/amanpreet-dev/mastering-the-art-of-node-package-management-kim</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F7sx778gkdt72p3j4oup8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7sx778gkdt72p3j4oup8.jpg" alt="Package Management" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before we deep dive into the world of Node package manager, we need to understand and know what is a package manager.&lt;/p&gt;

&lt;p&gt;In simple words, a &lt;em&gt;package manager&lt;/em&gt; is a system or set of tools used to automate installing, upgrading, configuring, and using software or applications.&lt;/p&gt;

&lt;p&gt;A package manager is not just limited to Node.js, in fact, the OS which we use on a daily basis also uses package managers. E.g.&lt;/p&gt;

&lt;p&gt;For MacOS we use the &lt;code&gt;brew&lt;/code&gt; package manager. Similarly for Ubuntu, we use the &lt;code&gt;apt-get&lt;/code&gt; package manager, and for Windows, the package manager we use is &lt;code&gt;Winget&lt;/code&gt; or &lt;code&gt;Chocolatey&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A brief history of Node Package Managers
&lt;/h2&gt;

&lt;p&gt;Node.js comes with a default package manager NPM i.e. Node Package Manager. NPM is the world's largest software registry and hosts more than a million packages that are used by javascript developers daily.&lt;/p&gt;

&lt;p&gt;There are other package managers too such as Yarn introduced by Meta, in 2016. Then we have PNPM i.e. Performant Node Package Manager released in 2017.&lt;/p&gt;

&lt;p&gt;Together with &lt;code&gt;npm&lt;/code&gt;, &lt;code&gt;yarn&lt;/code&gt;, and &lt;code&gt;pnpm&lt;/code&gt; there were a few more node package managers introduced like &lt;code&gt;tink&lt;/code&gt; and &lt;code&gt;ied&lt;/code&gt; but later on, was discontinued.&lt;/p&gt;

&lt;h3&gt;
  
  
  All about the package.json file
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;package.json&lt;/code&gt; file is a fundamental part of every Node.js project or application and is used by all package managers including NPM, Yarn, and PNPM. The file serves as the manifest of your project and contains metadata about the project and its dependencies.&lt;/p&gt;

&lt;p&gt;Here is an example of what a &lt;code&gt;package.json&lt;/code&gt; file might look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "my-node-project",
  "version": "1.0.0",
  "description": "A sample Node.js project",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.17.13"
  },
  "devDependencies": {
    "@types/express": "^4.17.13"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

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

&lt;/div&gt;



&lt;p&gt;Here are some of the common properties you will see in a &lt;code&gt;package.json&lt;/code&gt; file&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;name&lt;/code&gt;: The name of your project and the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;version&lt;/code&gt;: The current version of the project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;description&lt;/code&gt;: A short description of the project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;main&lt;/code&gt;: The main entry point of your project. Usually, it is the first file that is executed for the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;scripts&lt;/code&gt;: It is an object which holds all the script commands that are part of the application's lifecycle such as starting the application, building, testing, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;dependencies&lt;/code&gt;: These are the packages that are required for your application to execute. These are installed using &lt;code&gt;npm install --save &amp;lt;package_name&amp;gt;&lt;/code&gt; E.g. &lt;code&gt;express&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;devDependencies&lt;/code&gt;: Some packages are required for development purposes only. These are installed using &lt;code&gt;npm i --save-dev &amp;lt;package_name&amp;gt;&lt;/code&gt; for e.g &lt;code&gt;types@express&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are a few more fields in the &lt;code&gt;package.json&lt;/code&gt; file like &lt;code&gt;keywords&lt;/code&gt;, &lt;code&gt;author&lt;/code&gt; and &lt;code&gt;license&lt;/code&gt; etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  NPM (Node Package Manager)
&lt;/h2&gt;

&lt;p&gt;NPM manages all the packages and modules for Node.js and consists of the command line client &lt;code&gt;npm&lt;/code&gt;. It gets installed into the system with the installation of Node.js. The required packages and modules in the Node.js project are installed, uninstalled, and removed using NPM.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Package installation and management&lt;/strong&gt; : Easily install, update, and remove packages with simple commands.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency resolution&lt;/strong&gt; : Automatically resolves and installs package dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;npm scripts&lt;/strong&gt; : Run predefined scripts to automate tasks, such as building, testing, and deploying your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Semantic versioning&lt;/strong&gt; : Manage package versions using semantic versioning, making it easy to control and update dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Security and auditing&lt;/strong&gt; : Scanning projects for vulnerabilities and automatically applying patches with &lt;code&gt;npm audit&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;To install, update or remove a package using NPM, simply run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# For installing a package
npm install &amp;lt;package_name&amp;gt;

# For updating a package
npm update &amp;lt;package_name&amp;gt;

# To removing a package
npm uninstall &amp;lt;package_name&amp;gt;

# To install all dependencies
npm install

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Yarn
&lt;/h2&gt;

&lt;p&gt;If you are looking for fast, reliable, and secure dependency management, &lt;code&gt;yarn&lt;/code&gt; is the ultimate choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;To install Yarn, we can use &lt;code&gt;npm&lt;/code&gt; which comes as the default Node.js package manager. Though they are a few alternatives too for installing Yarn. Let's find out them below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using &lt;code&gt;npm&lt;/code&gt;: Installing &lt;code&gt;yarn&lt;/code&gt; package globally allows us to use it across different projects via the command line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For macOS: we can install it via &lt;code&gt;Brew&lt;/code&gt; or &lt;code&gt;curl&lt;/code&gt;. Let us check the commands to be used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For Windows: You can install it by executing a .msi file, Link available &lt;a href="https://classic.yarnpkg.com/latest.msi" rel="noopener noreferrer"&gt;here&lt;/a&gt;, or by using a package manager like &lt;em&gt;Chocolatey&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  All about the &lt;code&gt;yarn.lock&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;The file &lt;code&gt;yarn.lock&lt;/code&gt; is created or updated whenever dependencies are added. By using a lock file and a deterministic installation algorithm, &lt;code&gt;Yarn&lt;/code&gt; can ensure that the structure of the &lt;code&gt;node_modules&lt;/code&gt; directory is the same in every environment, whether it's your local development machine or a production server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast &amp;amp; Efficient&lt;/strong&gt; : Parallel downloads with offline caching and selective version inclusion makes managing packages efficient&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deterministic installs:&lt;/strong&gt; yarn lock files ensures consistent dependency installations across different environments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Workspaces&lt;/strong&gt; : Easily manages multiple packages in a single repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Security&lt;/strong&gt; : Enforces strict SSL &amp;amp; checksums which ensures the integrity of downloaded packages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compatibility&lt;/strong&gt; : Yarn is compatible with the NPM registry, thus allowing access to millions of NPM packages.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;To install a package using Yarn, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To add a new package 
yarn add &amp;lt;package_name&amp;gt;

# To update an already installed package
yarn upgrade &amp;lt;package_name&amp;gt;

# To remove a package
yarn remove &amp;lt;package_name&amp;gt;

# To install all dependencies for a project
yarn install

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  PNPM (Performant Node Package Manager)
&lt;/h2&gt;

&lt;p&gt;In simple words, PNPM is a fast, disk-space-efficient package manager.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Using &lt;code&gt;npm&lt;/code&gt;: To install PNPM, we can use npm which comes as the default Node.js package manager.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;@pnpm/exe&lt;/code&gt; is packaged with Node.js into an executable, so it may be used on a system with no Node.js installed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For macOS: we can install it via &lt;em&gt;Brew&lt;/em&gt;. Let us check the commands to be used. Using the brew package manager:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For Windows: You can install using a package manager like &lt;em&gt;Chocolatey&lt;/em&gt; or &lt;em&gt;Winget&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  All about the &lt;code&gt;pnpm-lock&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;Like other package managers, PNPM also creates a lock file &lt;code&gt;pnpm-lock.yaml&lt;/code&gt; when you install packages. This file ensures that every install results in the exact same dependency tree, across all environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficient package storage&lt;/strong&gt; : Uses a global package store, saving disk space and reducing installation times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Strict Dependency management&lt;/strong&gt; : Prevents the accidental use of packages that are not explicitly listed in your project's dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Workspaces&lt;/strong&gt; : Supports mono-repos and simplifies dependency management for multi-package projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Content addressable caches&lt;/strong&gt; : Ensures packages are immutable and cacheable, improving performance and security. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compatibility&lt;/strong&gt; : Compatible with the NPM registry, and use most npm-compatible tools.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;To install, update or delete a package using PNPM, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To add a new package 
pnpm add &amp;lt;package_name&amp;gt;

# To update an already installed package
pnpm update &amp;lt;package_name&amp;gt;

# To remove a package
pnpm remove &amp;lt;package_name&amp;gt;

# To install all dependencies for a project
pnpm install

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Choosing the right package manager
&lt;/h2&gt;

&lt;p&gt;There's no one-size-fits-all solution when it comes to package managers. The choice depends on your project's requirements and your personal preferences. Here are some guidelines to help you decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you prioritize compatibility and the support of a vast ecosystem, NPM is a reliable choice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If speed, security, and offline capabilities are important to you, consider Yarn.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If disk space and installation efficiency are your main concerns, PNPM might be the best option.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Feature Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;NPM&lt;/th&gt;
&lt;th&gt;Yarn&lt;/th&gt;
&lt;th&gt;PNPM&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Workspace Support&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hoisted &lt;code&gt;node_modules&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✔️ (by default)&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Isolated &lt;code&gt;node_modules&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️ (by default)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Managing node.js versions&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Autoinstalling peers&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lock file&lt;/td&gt;
&lt;td&gt;package-lock.json&lt;/td&gt;
&lt;td&gt;yarn.lock&lt;/td&gt;
&lt;td&gt;pnpm-lock.yaml&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Patching dependencies&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic package execution&lt;/td&gt;
&lt;td&gt;✔️ via &lt;code&gt;npx&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✔️ via &lt;code&gt;yarn dlx&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✔️ via &lt;code&gt;pnpm dlx&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Content addressable storage&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Zero-Installs&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CLI available&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;td&gt;✔️&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;Javascript package managers have expanded to offer developers a wide range of choices beyond NPM. While NPM remains the default for many, Yarn and PNPM are also widely used, each bringing its own unique advantages.&lt;/p&gt;

&lt;p&gt;Choosing between NPM, Yarn, and PNPM should be based on the specific needs of your project or team.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. If so, kindly like and share the article and also follow me to read more exciting articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://npmjs.com" rel="noopener noreferrer"&gt;NPM Website&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://classic.yarnpkg.com/lang/en/" rel="noopener noreferrer"&gt;Yarn Website&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://pnpm.io/" rel="noopener noreferrer"&gt;PNPM Website&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>npm</category>
      <category>yarn</category>
      <category>pnpm</category>
    </item>
    <item>
      <title>Clickjacking: The Web Attack You Need to Know About</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Mon, 24 Jul 2023 11:22:12 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/clickjacking-the-web-attack-you-need-to-know-about-5an6</link>
      <guid>https://dev.to/amanpreet-dev/clickjacking-the-web-attack-you-need-to-know-about-5an6</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fnph0h408ibvz597q18pc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnph0h408ibvz597q18pc.jpg" alt="Clickjacking: The Web Attack You Need to Know About" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;15 years in development, 0 focus on cybersecurity. Until Zoom rejected my app for clickjacking. Scary stuff.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Stop falling for clickjacking scams&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A few weeks ago, my Zoom app was rejected as it failed their Security and Privacy Compliance review.&lt;/p&gt;

&lt;p&gt;I was a little bit worried and a bit disappointed with myself because having such a vast experience I never focussed on security, but this was the time to learn something new and also to make my web apps secure from Clickjacking attacks.&lt;/p&gt;

&lt;p&gt;Below is the screenshot of the email from the Zoom team.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzvqo04ad8b0ythlxmov3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzvqo04ad8b0ythlxmov3.png" alt="zoom-email" width="800" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Luckily the Zoom team was very helpful and they shared a few resources with me to resolve the Clickjacking issue.&lt;/p&gt;

&lt;p&gt;So what is Clickjacking? How can we prevent it? All these questions must be in your mind. So let's continue getting an in-depth analysis of Clickjacking.&lt;/p&gt;

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

&lt;p&gt;Clickjacking is a web attack where malicious websites trick you into clicking on hidden elements. These actions could lead to malware downloads, sharing sensitive info, or making unwanted purchases.&lt;/p&gt;

&lt;p&gt;Protect yourself by staying vigilant and avoiding suspicious links.&lt;/p&gt;

&lt;h3&gt;
  
  
  A working example of Clickjacking
&lt;/h3&gt;

&lt;p&gt;Suppose we have a website with a login form that looks this this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action="/login" method="post"&amp;gt;
    &amp;lt;label for="username"&amp;gt;Username:&amp;lt;/label&amp;gt;
    &amp;lt;input type="text" name="username" id="username"&amp;gt;&amp;lt;br&amp;gt;
    &amp;lt;label for="password"&amp;gt;Password:&amp;lt;/label&amp;gt;
    &amp;lt;input type="password" name="password" id="password"&amp;gt;&amp;lt;br&amp;gt;
    &amp;lt;input type="submit" value="Login"&amp;gt;
&amp;lt;/form&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;An attacker can create a malicious webpage that loads your login form (mentioned above) in an iframe and overlays it with another element, such as a fake button that says &lt;code&gt;Click Me to win a prize!&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- malicious webpage --&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Clickjacking Demo&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
      iframe {
        position: absolute;
        top: 100px;
        left: 100px;
        width: 400px;
        height: 200px;
        opacity: 0.5;
      }
      #overlay {
        position: absolute;
        top: 150px;
        left: 150px;
        width: 200px;
        height: 100px;
        background-color: red;
        opacity: 0.5;
      }
    &amp;lt;/style&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;iframe src="https://yourwebsite.com/login"&amp;gt;&amp;lt;/iframe&amp;gt;
    &amp;lt;div id="overlay"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;script&amp;gt;
      // this code listens for clicks on the overlay element and forwards them to the hidden iframe
      document.getElementById("overlay").addEventListener("click", function() {
        document.getElementsByTagName("iframe")[0].contentWindow.document.forms[0].submit();
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Now, when a user visits the attacker's page and clicks on the overlay element, they are actually clicking on the hidden login form of your website, which the attacker can use to steal the user's credentials.&lt;/p&gt;

&lt;p&gt;Clickjacking is also called &lt;strong&gt;UI redress attack&lt;/strong&gt; because the attack tampers with the user interface of the exploited website&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdcc4unaxi2u7bzss53xv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdcc4unaxi2u7bzss53xv.png" alt="ui-redress-attack" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Unmasking Clickjacking: The Hidden Threat
&lt;/h3&gt;

&lt;p&gt;Clickjacking attacks can be carried out using a variety of techniques, including iframes, invisible buttons, and social engineering.&lt;/p&gt;

&lt;p&gt;Clickjacking attacks can be used to steal sensitive information, spread malware, or manipulate user behavior.&lt;/p&gt;

&lt;p&gt;Now let's add preventive actions in our above code using &lt;code&gt;X-Frame-Options&lt;/code&gt; header with the &lt;code&gt;DENY&lt;/code&gt; or &lt;code&gt;SAMEORIGIN&lt;/code&gt; option to prevent your login form from loading in an iframe on another domain or origin&lt;/p&gt;

&lt;p&gt;To prevent clickjacking attacks, web developers can use various techniques such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Implementing the X-Frame-Options HTTP response header:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For applications using nginx as a web-server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is also 1 more option available &lt;code&gt;ALLOW-FROM uri&lt;/code&gt; - This option is used when there is a specific URI that is allowed to frame your website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using Content Security Policy i.e. CSP&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Check the implementation
&lt;/h3&gt;

&lt;p&gt;You can check the implementation of clickjacking prevention with the following guidelines -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After adding the &lt;code&gt;X-Frame-Options&lt;/code&gt; header in your server configuration and also setting the Content Security Policy (CSP) headers in your server&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access your website on a web browser&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the browser's developer tools to access the network tab.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Browse to the page or action for which you want to check for clickjacking protection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Look for corresponding HTTP response headers&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By examining the HTTP response headers in the browser's tools, you can verify if clickjacking preventive measures are correctly applied.&lt;/p&gt;

&lt;h2&gt;
  
  
  😱 Unbelievable Clickjacking Cases!
&lt;/h2&gt;

&lt;p&gt;Some of the famous Clickjacking attacks have been&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pcworld.com/article/537104/article-8056.html" rel="noopener noreferrer"&gt;Adobe fixes Clikcjacking flaw&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://threatpost.com/google-patches-clickjacking-bug/112568/" rel="noopener noreferrer"&gt;Google patches Clickjacking bug&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.computerworld.com/article/2518461/facebook--likejacking--attacks-continue-with-flesh-appeal.html" rel="noopener noreferrer"&gt;Facebook likejacking attacks continue with flesh appeal&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://shiflett.org/blog/2009/feb/twitter-dont-click-exploit" rel="noopener noreferrer"&gt;Twitter Worm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jotform.com/answers/2729622-potential-vulnerabilities-report" rel="noopener noreferrer"&gt;Jotform&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the above examples of our some very very big companies indeed.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔐 Protect Your Online Life: Enhance Web Security
&lt;/h3&gt;

&lt;p&gt;In a hyper-connected digital era, where vulnerabilities lurk at every click, it's crucial to safeguard our online presence. Clickjacking attacks are not new and as a developer, we should take full responsibility in applying the prevention methods.&lt;/p&gt;

&lt;p&gt;Well, luckily using the above-mentioned methods, I was able to verify my Zoom app and prevent it from being suspended.&lt;/p&gt;

&lt;p&gt;Below is the email reference for it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fu3rrzbtqkvkwmsiczx4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fu3rrzbtqkvkwmsiczx4b.png" alt="zoom-success" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Clickjacking is a persistent threat that demands continuous attention from web developers and users alike. By staying informed about the potential risks and adopting best practices for web security we can prevent Clickjacking attacks.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. &lt;strong&gt;If so, kindly like it or share it with others so they can also see it.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>clickjacking</category>
    </item>
    <item>
      <title>Unpacking the Latest Features of NestJS v10</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Mon, 24 Jul 2023 07:30:09 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/unpacking-the-latest-features-of-nestjs-v10-5b3k</link>
      <guid>https://dev.to/amanpreet-dev/unpacking-the-latest-features-of-nestjs-v10-5b3k</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fvmsnfi5npp0d0w1iq0ct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvmsnfi5npp0d0w1iq0ct.png" alt="Unpacking the Latest Features of NestJS v10" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;NestJS v10 is here! This progressive Node.js framework offers efficient, reliable, and scalable server-side app building.&lt;/p&gt;

&lt;p&gt;As per the StackOverflow Developer survey, NestJS is one of the top 10 most popular backend frameworks in the world. Check the survey results &lt;a href="https://survey.stackoverflow.co/2023/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It has more than 58k Github stars on its &lt;a href="https://github.com/nestjs/nest" rel="noopener noreferrer"&gt;repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's New?
&lt;/h2&gt;

&lt;p&gt;Recently NestJS has officially released v10 for building efficient and enterprise-grade, server-side applications. Let's deep dive and see what are the new features of the new version.&lt;/p&gt;

&lt;h3&gt;
  
  
  SWC Integration
&lt;/h3&gt;

&lt;p&gt;SWC stands for Speedy Web Compiler. It is an extensible Rust-based platform that can be used for both compilation and bundling. To speed up your development process, using SWC with NestJs CLI is a great way.&lt;/p&gt;

&lt;p&gt;Installation of required packages is as follows:&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 --save-dev @swc/cli @swc/core

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

&lt;/div&gt;



&lt;p&gt;You can now use SWC with NestJs CLI as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nest start -b swc
# or
$ nest start --builder swc

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

&lt;/div&gt;



&lt;p&gt;Another way to use SWC by default is via &lt;code&gt;nest-cli.json&lt;/code&gt; file, as mentioned below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "compilerOptions": {
        "builder": "swc"
    }
}

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

&lt;/div&gt;



&lt;p&gt;By default, SWC does not perform any type-checking itself, so to enable this option we need to use &lt;code&gt;--type-check&lt;/code&gt; flag, as mentioned below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nest start -b swc --type-check
# or
$ nest start --builder swc --type-check

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 SWC is approximately 20 times faster than the default TypeScript compiler.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Redis Wildcard Subscriptions
&lt;/h3&gt;

&lt;p&gt;Added support for Redis wildcard subscriptions. This feature allows one to subscribe to all messages that match a given pattern.&lt;/p&gt;

&lt;p&gt;To start using Redis-based microservices, install the required packages as follows:&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 ioredis

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

&lt;/div&gt;



&lt;p&gt;By default wildcard subscription is &lt;code&gt;false,&lt;/code&gt; to enable the wildcard subscription&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.REDIS,
  options: {
    host: 'localhost',
    port: 6379,
    wildcard: true, // this is the new option available
  },
});

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

&lt;/div&gt;



&lt;p&gt;With wildcards enabled, we can use glob-type patterns in our subscriptions, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ne?tjs&lt;/code&gt; subscribes to &lt;code&gt;nestjs&lt;/code&gt;, &lt;code&gt;nextjs&lt;/code&gt; and &lt;code&gt;nuxtjs&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ne*tjs&lt;/code&gt; subscribes to &lt;code&gt;netjs&lt;/code&gt; and &lt;code&gt;neetjs&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's Improved or Changed?
&lt;/h2&gt;

&lt;p&gt;Together with new changes, there are some improvements in the latest v10 of NestJS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Changes to Cache Module
&lt;/h3&gt;

&lt;p&gt;Caching is a great and simple technique to improve your app's performance.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;CacheModule&lt;/code&gt; is now available as a standalone package &lt;code&gt;@nestjs/cache-manager&lt;/code&gt;. Earlier this package was available under &lt;code&gt;@nestjs/common&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To install the required packages:&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 @nestjs/cache-manager cache-manager

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Overriding Modules in tests
&lt;/h3&gt;

&lt;p&gt;Overriding of modules in tests has been introduced in v10. This feature is useful when you want to mock the entire module all at once instead of mocking each provider individually.&lt;/p&gt;

&lt;h3&gt;
  
  
  CLI Plugins and TypeScript &amp;gt;= 4.8
&lt;/h3&gt;

&lt;p&gt;NestJS CLI plugins will now require TypeScript &amp;gt;= v4.8. This is due to breaking changes in TypeScript v4.8.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dropping Support for Node.js v12
&lt;/h3&gt;

&lt;p&gt;Since Node.js v12 went &lt;em&gt;end of life (EOL)&lt;/em&gt;, to use the new features of NestJS v10, a minimum version of Node.js v16 or higher is required.&lt;/p&gt;

&lt;p&gt;This also allows the NestJS package to be compiled as ES2021 by default which results in a smaller library size and better performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration Guidelines
&lt;/h2&gt;

&lt;p&gt;To migrate your existing NestJS project to the latest version, follow the links available below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/migration-guide" rel="noopener noreferrer"&gt;Migration from v9 to v10&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/v9/migration-guide" rel="noopener noreferrer"&gt;Migration from v8 to v9&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/v8/migration-guide" rel="noopener noreferrer"&gt;Migration from v7 to v8&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;NestJS v10 brings a lot of new exciting and improved features as compared to its older versions. If you are using NestJS in your projects please share it via comments.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. &lt;strong&gt;If so, kindly like it or share it with others so they can also see it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;Below are some of the references which you can follow for more in-depth knowledge&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/microservices/redis" rel="noopener noreferrer"&gt;Redis Microservices&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/recipes/swc" rel="noopener noreferrer"&gt;SWC or Speedy Web Compiler&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nestjs</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Recursively Moving Files in a Directory with Node.js and TypeScript</title>
      <dc:creator>Amanpreet Singh</dc:creator>
      <pubDate>Fri, 23 Jun 2023 10:41:17 +0000</pubDate>
      <link>https://dev.to/amanpreet-dev/recursively-moving-files-in-a-directory-with-nodejs-and-typescript-2loc</link>
      <guid>https://dev.to/amanpreet-dev/recursively-moving-files-in-a-directory-with-nodejs-and-typescript-2loc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Recently, my Marketing team was facing an issue with uploading the files on the server. The folder structure of their files was somewhat like the one below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Folder A/
 Folder B
    Folder 1
       file 1
       file 2
       file n
    Folder 2
       file 1
       file 2
       file n

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

&lt;/div&gt;



&lt;p&gt;They had to select each file one at a time for some processing which was time-consuming as they had to select a folder within a folder, Multiple file selection was still possible but not with folders, they consulted with me for some suggestions and instantly I thought that making the files move to one folder will solve their issue.&lt;/p&gt;

&lt;p&gt;Now moving the files from folder to folder recursively was not an easy task as there were around 1k files so I thought making this work programmatically will solve our issue and below is a summary of how I did this.&lt;/p&gt;

&lt;p&gt;I used Node.js and Typescript for this as they have all the components which we need and are also very easy to understand.&lt;/p&gt;

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

&lt;p&gt;To get started, you'll need to have Node.js and npm (Node Package Manager) installed on your machine. If you haven't installed them yet, you can download and install them from the official Node.js website (&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;https://nodejs.org/&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;For this article, I am using Node v20.2.0&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide
&lt;/h2&gt;

&lt;p&gt;Once you have Node.js and npm installed, follow the step-by-step guide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create and Initialize a new Node.js project
&lt;/h3&gt;

&lt;p&gt;Open a terminal, create a new directory where you want to store your project files, and run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Folder name can be anything but do make it easily readable.
mkdir recursive-files &amp;amp;&amp;amp; cd recursive-files

# This command will create a new default package.json file in your project directory.
npm init -y

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install TypeScript and ts-node
&lt;/h3&gt;

&lt;p&gt;TypeScript is a typed superset of Javascript that compiles to plain Javascript. &lt;code&gt;ts-node&lt;/code&gt; is a tool that allows you to run TypSctipt code directly, without the compile step. Install them as development dependencies using 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 i typescript ts-node -D

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a tsconfig.json file:
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;tsconfig.json&lt;/code&gt; file is used to specify the root files and the compiler options to compile the project.&lt;/p&gt;

&lt;p&gt;For creating a &lt;code&gt;tsconfig.json&lt;/code&gt; file use 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;# Will create the tsconfig.sjon file wit default options which can be modified as needed
npx tsc --init

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Time to write the code:
&lt;/h3&gt;

&lt;p&gt;Create a new &lt;code&gt;.ts&lt;/code&gt; file in your project directory. You can use the following command to create a new file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch index.ts

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Importing necessary modules:
&lt;/h3&gt;

&lt;p&gt;We will be using &lt;code&gt;fs&lt;/code&gt; and &lt;code&gt;path&lt;/code&gt; modules from Node.js. The &lt;code&gt;fs&lt;/code&gt; modules allow us to interact with the file system, whereas the &lt;code&gt;path&lt;/code&gt; module allows us to work with file and directory paths.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import fs from 'fs'
import path from 'path'

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Defining the &lt;code&gt;moveFiles&lt;/code&gt; function:
&lt;/h3&gt;

&lt;p&gt;The below function takes two arguments: &lt;code&gt;sourceDir&lt;/code&gt; (the directory from which we want to move the files) and &lt;code&gt;targetDir&lt;/code&gt; (the directory where you want to move the files). The function returns a Promise that resolves when all the files have been moved or rejected if there's an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function moveFiles(sourceDir:string, targetDir:string): Promise&amp;lt;void&amp;gt; {
    return new Promise((resolve), (reject) =&amp;gt; {
        // Code for moving the files
    });
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reading the source directory &amp;amp; Iterating over the files:
&lt;/h3&gt;

&lt;p&gt;We will be using &lt;code&gt;fs.readdir&lt;/code&gt; function to read the contents of the source directory. This function takes a callback that is called with two arguments: an error (if there is one) and an array of filenames.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Reading the files from source directory
   fs.readdir(sourceDir, (err, files) =&amp;gt; {
      if (err) {
          reject(err)
          return;
      }
    console.log('List of files', files);
);

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

&lt;/div&gt;



&lt;p&gt;We're using &lt;code&gt;Array.prototype.forEach&lt;/code&gt; to iterate over the array of filenames. For each filename, we're doing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Constructing the old and new paths: We're using &lt;code&gt;path.join&lt;/code&gt; to construct the full paths to the file in the source and target directories.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking if the file is a directory or a regular file: We're using &lt;code&gt;fs.lstatSync&lt;/code&gt; to get the stats of the file, and then checking if it's a directory or a regular file with &lt;code&gt;Stats.prototype.isDirectory&lt;/code&gt; and &lt;code&gt;Stats.prototype.isFile&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the file is a directory, we're calling &lt;code&gt;moveFiles&lt;/code&gt; recursively to move the files to the subdirectory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the file is a regular file, we're using &lt;code&gt;fs.rename&lt;/code&gt; it to move the file to the target directory. This function takes a callback that is called with an error (if there is one).&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Continue from the above code
     console.log('List of Files', files);

      files.forEach((file) =&amp;gt; {
        const oldPath = path.join(sourceDir, file);
        const newPath = path.join(targetDir, file);
        const stat = fs.lstatSync(oldPath);

        if (stat.isDirectory()) {
          moveFiles(oldPath, targetDir)
            .then(() =&amp;gt; {
              if (files.indexOf(file) === files.length - 1) {
                resolve();
              }
            })
            .catch((err) =&amp;gt; reject(err));
        } else if (stat.isFile()) {
          fs.rename(oldPath, newPath, (err) =&amp;gt; {
            if (err) {
              reject(err);
              return;
            }

            if (files.indexOf(file) === files.length - 1) {
              resolve();
            }
          });
        }
      });

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Handling errors:
&lt;/h3&gt;

&lt;p&gt;If there is an error at any point like reading the directory, getting the stats of a file, or moving a file, Promise will be rejected with the error.&lt;/p&gt;

&lt;p&gt;Please note we are using &lt;code&gt;fs.lstatSync&lt;/code&gt; it as a synchronous method. For the asynchronous version, we can use &lt;code&gt;fs.lstat&lt;/code&gt; to avoid blocking the event loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling the &lt;code&gt;moveFiles&lt;/code&gt; function:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;moveFiles('SourceDir', 'TargetDir')
.then(() =&amp;gt; console.log('All files moved successfully'))
.catch((err) =&amp;gt; console.log(err))

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

&lt;/div&gt;



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

&lt;p&gt;The above script will recursively read all directories and files under the specified directory and will successfully move each file into one target folder as specified.&lt;/p&gt;

&lt;p&gt;Please note the above code will not work for the files with the same name in different directories, the files will be overwritten and we need to add some logic to check if a file with the same name already exists in the target directory.&lt;/p&gt;

&lt;p&gt;I hope you have learned something new as I did. &lt;strong&gt;If so, kindly like it or share it with others so they can also see it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;You can find the gist of the sample code &lt;a href="https://gist.github.com/amanpreet-dev/e20497a53c82eff3cc70fbe41215d57f" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>files</category>
    </item>
  </channel>
</rss>
