<?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: Harsh Rastogi</title>
    <description>The latest articles on DEV Community by Harsh Rastogi (@theharshrastogi).</description>
    <link>https://dev.to/theharshrastogi</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%2F563327%2Fffee123f-8731-4249-869f-6e48bcda7561.jpeg</url>
      <title>DEV Community: Harsh Rastogi</title>
      <link>https://dev.to/theharshrastogi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theharshrastogi"/>
    <language>en</language>
    <item>
      <title>Factory Method In Brief: Design Pattern</title>
      <dc:creator>Harsh Rastogi</dc:creator>
      <pubDate>Thu, 13 Oct 2022 08:26:58 +0000</pubDate>
      <link>https://dev.to/theharshrastogi/factory-method-in-brief-design-pattern-1cmi</link>
      <guid>https://dev.to/theharshrastogi/factory-method-in-brief-design-pattern-1cmi</guid>
      <description>&lt;p&gt;This pattern is a Creational Design Pattern that provides an interface or abstract for creating the type of object in a superclass but allows the subclass to change the type of object that will be created.&lt;/p&gt;

&lt;p&gt;It suggests that developers should replace object creation using&lt;br&gt;
the &lt;code&gt;constructor&lt;/code&gt; or using the &lt;code&gt;new&lt;/code&gt; operator. This sounds strange but an object will be created using only the &lt;code&gt;constructor&lt;/code&gt; that is called inside a &lt;strong&gt;factory method&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This may look complex at first glance but believe me, it promotes loose coupling and code reuse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's understand with an Example&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;p&gt;Imagine you want to add different logging behavior for different environments of applications i.e &lt;code&gt;Production&lt;/code&gt;, &lt;code&gt;Development&lt;/code&gt;, &lt;code&gt;Test&lt;/code&gt;, and &lt;code&gt;Debug&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;&lt;code&gt;Production&lt;/code&gt;&lt;/strong&gt;: Only &lt;code&gt;loglevel=error|warn&lt;/code&gt; is displayed.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;&lt;code&gt;Development&lt;/code&gt;&lt;/strong&gt;: All &lt;code&gt;loglevel=info|error|warn|debug&lt;/code&gt; is displayed.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;&lt;code&gt;Debug&lt;/code&gt;&lt;/strong&gt;: Only &lt;code&gt;loglevel=debug&lt;/code&gt; must be displayed.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For simplicity let's only work with two environments i.e &lt;code&gt;Production&lt;/code&gt; and &lt;code&gt;Development&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Implementation without using a pattern
&lt;/h2&gt;


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

class Logger is
    env: string

    public method LogError(message: string): void is
        print message

    public method LogInfo(message: string): void is
        if env = ("Development" or "Test" or "Debug") then
            print message

    public method LogWarning(message: string): void is
        print message

    public method LogDebug(message: string): void is
        if env = ("Development" or "Test" or "Debug") then
            print message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Implementation of Logger
function DoSomeWork(): void is
    ...
    logger = new Logger()
    ...
    var condition = isValid();

    if condition is true then
        logger.LogInfo("Some work is completed");
        return
    ...
    logger.LogError("Unable to complete work")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Implementation of &lt;code&gt;Logger&lt;/code&gt; involves so much of &lt;code&gt;if-else&lt;/code&gt; or '&lt;code&gt;if&lt;/code&gt; (the same thing can be performed using the &lt;code&gt;switch&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Imagine in the future&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some new environment is added let's say &lt;code&gt;Cloud&lt;/code&gt;. So it brings new problems and conditions with it.

&lt;ul&gt;
&lt;li&gt;Making developer change implementation of class &lt;code&gt;Logger&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Updating condition according to it&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Business rules changed for logger

&lt;ul&gt;
&lt;li&gt;So again developer has to change the internal of &lt;code&gt;Logger&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Thus, this implementation is not maintainable and prone to unwanted behavior to occur.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Implementing Factory Method
&lt;/h2&gt;

&lt;p&gt;The developer creates an abstract or interface called &lt;code&gt;Logger&lt;/code&gt; and provides the concrete implementation for various subclasses of the common superclass &lt;code&gt;Logger&lt;/code&gt; for various environments i.e &lt;code&gt;ProductionLogger&lt;/code&gt;, &lt;code&gt;DevelopmentLogger&lt;/code&gt;, &lt;code&gt;TestingLogger&lt;/code&gt; and &lt;code&gt;DebugLogger&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So each method of the subclass of &lt;code&gt;Logger&lt;/code&gt; must contain its implementation and business rules.&lt;/p&gt;

&lt;p&gt;I know it's hard to grasp to understand because adds up complexity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq62ad123ze0f6t3twgi2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq62ad123ze0f6t3twgi2.jpg" alt="UML Diagram For Logger(Superclass) and Concrete Implementation(Subclass). Diagrams show Is-A relation between abstract class and concrete class"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For those who don't understand the UML diagram, it shows the IS-A relationship between the abstract &lt;code&gt;Logger&lt;/code&gt; class and subclasses (&lt;code&gt;DevelopmentLogger&lt;/code&gt;, &lt;code&gt;TestingLogger&lt;/code&gt; etc) such that &lt;code&gt;DevelopmentLogger&lt;/code&gt; is a &lt;code&gt;Logger&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The developer creates an &lt;code&gt;interface&lt;/code&gt; named &lt;code&gt;ILoggerBuilder&lt;/code&gt;. That is responsible to provide an object of type &lt;code&gt;Logger&lt;/code&gt; according to the requirement or some logic. the UML diagram shows the IS-A relationship between the abstract &lt;code&gt;Logger&lt;/code&gt; class and subclasses (&lt;code&gt;DevelopmentLogger&lt;/code&gt;, &lt;code&gt;TestingLogger&lt;/code&gt; etc) such that &lt;code&gt;DevelopmentLogger&lt;/code&gt; is a &lt;code&gt;Logger&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The developer creates an &lt;code&gt;interface&lt;/code&gt; named &lt;code&gt;ILoggerFactory&lt;/code&gt;. That is responsible to provide an object of type &lt;code&gt;Logger&lt;/code&gt; according to the requirement or some logic.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EnvironmentLoggerFactory&lt;/code&gt; implements &lt;code&gt;ILoggerFactory&lt;/code&gt; that is responsible for creating an appropriate object of Subclass of &lt;code&gt;Logger&lt;/code&gt; according to the environment on which the application is executing or say &lt;strong&gt;there is complex logic for building some object&lt;/strong&gt;;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v4o8gtyhyskfyemkofu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5v4o8gtyhyskfyemkofu.jpg" alt="UML diagram for interface ILoggerFactory and Concrete Implementation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's write some pseudo-code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class Logger is
    public abstract method LogInfo(message: string): void

    public abstract method LogError(message: string): void

    public abstract method LogDebug(message: string): void

    public abstract method LogWarn(message: string): void
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ProductionLogger inherits Logger is
    public method LogInfo(message: string): void is
      // Does Nothing
      return

    public method LogError(message: string): void is
        print message

    public method LogWarn(message: string): void is
        print message

    public method LogDebug(message: string): void is
        // Does Nothing
        return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DevelopmentLogger inherits Logger is
    public method LogInfo(message: string): void is
      print message

    public method LogError(message: string): void is
        print message

    public method LogWarn(message: string): void is
        print message

    public method LogDebug(message: string): void is
        return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestingLogger inherits Logger is
    public method LogInfo(message: string): void is
      // Does Nothing
      return

    public method LogError(message: string): void is
        print message

    public method LogWarn(message: string): void is
        print message

    public method LogDebug(message: string): void is
        // Does Nothing
        return
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DebugLogger inherits Logger is
public method LogInfo(message: string): void is
// Does Nothing
return

    public method LogError(message: string): void is
        // Does Nothing
        return

    public method LogWarn(message: string): void is
        // Does Nothing
        return

    public method LogDebug(message: string): void is
        print message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now developer adds &lt;code&gt;EnvironmentLoggerFactory&lt;/code&gt; that implements &lt;code&gt;ILoggerFactory&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;interface ILoggerFactory is
  CreateLogger(): Logger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EnvironmentLoggerFactory implements ILoggerFactory
  public CreateLogger(): Logger is
    env = getCurrentEnv()

    if env
      is "Production" then return new ProductionLogger()
      is "Development" then return new DevelopmentLogger()
      is "Testing" then return new TestingLogger()
      is "Debug" then return new DebugLogger()

    throw new Exception("No logger attached to current environment")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;Implementation of Logger&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function DoSomeWork(): void is
    ...
    logger = new EnvironmentLoggerFactory().CreateLogger()
    ...
    var condition = isValid();

    if condition is true then
        logger.LogInfo("Some work is completed");
        return
    ...
    logger.LogError("Unable to complete work")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Again imagine the future&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some new environment is added let's say &lt;code&gt;Cloud&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SOLUTION&lt;/strong&gt;: Now the developer has to develop a new subclass &lt;code&gt;CloudLogger&lt;/code&gt; that inherits &lt;code&gt;Logger&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Rules can be implemented without thinking that they will break in the development or production environment.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Business rules changed for the logger, for example, all the logs in the cloud must be stored in a database

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SOLUTION&lt;/strong&gt;: Just implement a concrete class &lt;code&gt;CloudDatabaseLogger&lt;/code&gt; that inherits &lt;code&gt;Logger&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Applicability
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When developers do not know the exact type of objects that code should work with&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Factory method separates object creation from the code that uses it&lt;/li&gt;
&lt;li&gt;It provides an ease to extending the construction of an object&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;When the developer wants to save resources by reusing existing objects rather than by rebuilding them&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This can be achieved using caching or memoization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  Generic Implementation of Pattern
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;All the products must be must inherit or implement&lt;code&gt;Product&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add a factory method inside &lt;code&gt;CreatorFactory&lt;/code&gt; that always returns an object of base type &lt;code&gt;Product&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Replace all constructor calls in the code with the factory method.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Developer can pass argument inside this factory method to control object creation behavior&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Avoid tight coupling&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Single Responsibility Principal&lt;/em&gt;, thus all object creation code is separated, allowing code management easy.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Open/Close Principal&lt;/em&gt;, the developer can introduce a new product according to changes without breaking client code.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The factory method is a powerful way to create complex objects it promotes loose coupling, the single responsibility principle, and the open/close principle.&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://github.com/harshrastogiexe" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To know more about this pattern, check this video by &lt;strong&gt;Christopher Okhravi&lt;/strong&gt;&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/EcFVTgRHJLM?start=757"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://read.amazon.in/kp/embed?asin=B000SEIBB8&amp;amp;preview=newtab&amp;amp;linkCode=kpe&amp;amp;ref_=cm_sw_r_kb_dp_W0JBDZ7VEY39TVV01N79" rel="noopener noreferrer"&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>design</category>
      <category>architecture</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why I Switched To PNPM?</title>
      <dc:creator>Harsh Rastogi</dc:creator>
      <pubDate>Mon, 01 Nov 2021 07:40:00 +0000</pubDate>
      <link>https://dev.to/theharshrastogi/why-i-switched-to-pnpm-j84</link>
      <guid>https://dev.to/theharshrastogi/why-i-switched-to-pnpm-j84</guid>
      <description>&lt;p&gt;Package Manager For Node JS&lt;/p&gt;

&lt;p&gt;It is system for that automate the process of adding/upgrading/removing and managing dependencies for a Node JS project&lt;/p&gt;

&lt;p&gt;There are many package managers for Node JS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NPM (Node Package Manager)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most popular package manager for Javascript and also default package manager for Node JS.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Yarn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It was released by Facebook Inc. in 2016. It was create to overcome problems and performance that were in NPM at that time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PNPM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is alternative package manager for Node Js for replacement of NPM, but faster and efficient.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is better than Yarn and NPM?
&lt;/h2&gt;

&lt;p&gt;Imagine you install a package, Let call it Package_X. Imagine &lt;strong&gt;&lt;a href="https://lodash.com/" rel="noopener noreferrer"&gt;Lodash&lt;/a&gt;&lt;/strong&gt; is one of dependency of Package_X. Now you install diffrent package call it Package_Y which also has &lt;strong&gt;&lt;a href="https://lodash.com/" rel="noopener noreferrer"&gt;Lodash&lt;/a&gt;&lt;/strong&gt; as it dependency. Thus in a single project there two copies of Lodash.&lt;/p&gt;

&lt;p&gt;If there are 100 packages using Lodash, you are going to have 100 copies Lodash&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;PNPM allows yoo to save tons of space&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is faster than both npm and yarn. Because Yarn copies files from cache wheareas pnpm links files into global score.&lt;/p&gt;

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

&lt;p&gt;Note that PNPM doesn't flatten the dependency tree&lt;/p&gt;

&lt;p&gt;Look how earlier &lt;code&gt;node_modules&lt;/code&gt; tree looked like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
|  Package_X/
|  | &amp;gt; node_modules/
|  |   | &amp;gt; Package_Z/
|  |       | index.js
|  |       | package.json
|  |   index.js
|  |   package.json
|
|  Package_Y/
|  | &amp;gt; node_modules/
|  |   | &amp;gt; Package_Z/
|  |       | index.js
|  |       | package.json
|  |   index.js
|  |   package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  This way to managing has some issuse
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Deeply nested dependency tree, causing long directory names in system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Packages are copied and pasted several times when they are required in different dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But now after update in NPM &lt;a class="mentioned-user" href="https://dev.to/version"&gt;@version&lt;/a&gt; 3, they added flattening so structure 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;node_modules/
|  Package_X/
|  |   index.js
|  |   package.json
|
|  Package_Y/
|  |   index.js
|  |   package.json
|
|  Package_Z/
|  |   index.js
|  |   package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But pnpm follow diffrent apporch rather than flattening tree it keeps it same&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;/node_modules&lt;/code&gt; folder created by pnpm, each package has it's own dependency but dependency tree is never deep as in earlier version of npm. It keeps all dependencies flat with use of &lt;a href="https://en.wikipedia.org/wiki/Symbolic_link#:~:text=In%20computing%2C%20a%20symbolic%20link,and%20that%20affects%20pathname%20resolution." rel="noopener noreferrer"&gt;Symbolic Links&lt;/a&gt; or &lt;a href="https://www.computerhope.com/jargon/j/junction.htm#:~:text=A%20junction%2C%20also%20called%20an,to%20the%20Windows%20command%20line" rel="noopener noreferrer"&gt;Junction&lt;/a&gt; (in windows)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
|  Package_X/
|  | &amp;gt; node_modules/
|  |   | &amp;gt; Package_Z/ -&amp;gt; ../../../Package_Z/1.0.0
|  |   index.js
|  |   package.json
|
|  Package_Y/
|  | &amp;gt; node_modules/
|  |   | &amp;gt; Package_Z/ -&amp;gt; ../../../Package_Z/1.0.0
|  |       | index.js
|  |       | package.json
|  |   index.js
|  |   package.json
|
|  Package_Z/
|  |   index.js
|  |   package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open terminal&lt;/li&gt;
&lt;li&gt;Execute following command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; pnpm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npx pnpm add &lt;span class="nt"&gt;-g&lt;/span&gt; pnpm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Small Project Using pnpm
&lt;/h2&gt;

&lt;p&gt;We are going to build Restfull API that get name of two person and calculate the love percentage between them&lt;/p&gt;

&lt;p&gt;Exexute below commands&lt;/p&gt;

&lt;p&gt;Create a directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;mkdir &lt;/span&gt;love-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialise it as pnpm project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to use Express for it.&lt;br&gt;
Note pnpm commands is quite similiar to both npm and yarn. We are going to replace &lt;code&gt;npm install [PACKAGE_NAME]&lt;/code&gt; with &lt;code&gt;pnpm add [PACKAGE_NAME]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So adding following packages to your projects&lt;/p&gt;

&lt;p&gt;Execute below commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm add express cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm add &lt;span class="nt"&gt;-D&lt;/span&gt; @types/express @types/cors nodemon typescript concurrently
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add these below script to &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node dist/index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"concurrently &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;tsc -w&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;nodemon dist/index.js&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt;
&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;p&gt;We are going to discuss only about the PNPM side of things only&lt;br&gt;
have look of source code &lt;a href="https://github.com/theharshrastogi/love-calculator-api" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dont Forget To Follow Me&lt;/strong&gt; -&amp;gt; &lt;a href="https://github.com/theharshrastogi" rel="noopener noreferrer"&gt;Harsh Rastogi&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to build convert typescript code into javascript&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;npm&lt;/strong&gt; we do &lt;code&gt;npm run build&lt;/code&gt; but in &lt;strong&gt;pnpm&lt;/strong&gt; we have to execute&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and to start dev server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and to start server in production mode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  pnpm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benchmarks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4ngf5x7mqznpvx12i1g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4ngf5x7mqznpvx12i1g.png" alt="Benchmark compares the performance of NPM, Yarn, PNPM"&gt;&lt;/a&gt;&lt;/p&gt;

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

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

&lt;p&gt;If you are searching that gives you better speed and performance then pnpm is better, I personally suggest you to use pnpm instead of npm and Yarn. If you are not using it then you a chance to try it.&lt;/p&gt;

&lt;p&gt;Yarn sends date to Facebook, which don't make yarn suitable in some scenarios. NPM has also security issue that's why there is Yarn now.&lt;/p&gt;

&lt;p&gt;In above benchmarks we can see PNPM is better in all aspects.&lt;/p&gt;

&lt;p&gt;Happy Coding :)&lt;/p&gt;

</description>
      <category>pnpm</category>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>You Don't Know About Populate</title>
      <dc:creator>Harsh Rastogi</dc:creator>
      <pubDate>Sun, 06 Jun 2021 09:23:41 +0000</pubDate>
      <link>https://dev.to/theharshrastogi/you-don-t-know-about-populate-252b</link>
      <guid>https://dev.to/theharshrastogi/you-don-t-know-about-populate-252b</guid>
      <description>&lt;p&gt;To Understand populate, you have to understand the relation between collections.&lt;/p&gt;

&lt;p&gt;Consider a library where we have&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A Collection Of Books&lt;/strong&gt; which is consist of &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;author&lt;/code&gt; and &lt;code&gt;another informational fields&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A Collection Of Users&lt;/strong&gt; which is consist of &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;other stuff&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A Collection Of Issue&lt;/strong&gt; which is consist of user and books, just for now ignore other stuff&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6fdt7s34viwirtxih33.jpg" alt="Database Collection Showing User And Books"&gt;
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Lets Begin
&lt;/h2&gt;

&lt;p&gt;Think of how we can store &lt;strong&gt;Issue Collection&lt;/strong&gt; record&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By saving user data and book data&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &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="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"user"&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="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"65230asa434r345d34kd3wbb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Noida"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"john@example.com"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"book"&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="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Harry Potter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"6b6s6a9d6fj87s8978s86s7s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"J.K. Rowling"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"publication"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"XYZ"&lt;/span&gt;&lt;span class="w"&gt;
      &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="w"&gt;
  &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;p&gt;the problem with this approach is you end up with collecting duplicate data(User is also present in User collection and same with Books). This makes your database containing duplicate fields and hard to maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  But what makes it hard? I will access record fields easily!
&lt;/h3&gt;

&lt;p&gt;My friend, this is hard because imagine if user has updated this email field or, the books field have been edited then we have to update records two times in &lt;code&gt;Issues&lt;/code&gt; Collection and &lt;code&gt;Books&lt;/code&gt; or &lt;code&gt;User&lt;/code&gt; Collection.&lt;/p&gt;

&lt;p&gt;To make database redundant, we should move to second approach&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By Saving &lt;code&gt;User _id&lt;/code&gt; and &lt;code&gt;Book _id&lt;/code&gt; in &lt;code&gt;Issues&lt;/code&gt; Record&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;65230asa434r345d34kd3wbb&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;book&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6b6s6a9d6fj87s8978s86s7s&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with this approach we are storing a reference of user and book in their respective collections.&lt;/p&gt;

&lt;h3&gt;
  
  
  So How I Get Records? This Looks To Complex!!
&lt;/h3&gt;

&lt;p&gt;My friend, here the populate comes to aid(_Why They called it &lt;code&gt;Populate&lt;/code&gt; because finds data with their unique id and replace the ObjectId).&lt;/p&gt;

&lt;h4&gt;
  
  
  Advantages
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Database is not redundant.&lt;/li&gt;
&lt;li&gt;Updating records in easy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's Implement This
&lt;/h2&gt;

&lt;p&gt;Create Book and User models&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&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="nb"&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;const&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&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;bookSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;publication&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&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;const&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Book&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bookSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets add some Documents('same as Records') To Collection&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="c1"&gt;// User.create() is equivalent to new User().save()&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;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Noida&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;john@example.com&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;harryPotter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;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;Harry Potter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;J.K. Rollings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;publication&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XYZ&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;h2&gt;
  
  
  Now User Issue a book from library
&lt;/h2&gt;

&lt;p&gt;So how we do it?&lt;br&gt;
Here comes the populate to aid&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Types&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&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;issuesSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&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="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&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="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ObjectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Book&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Issue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Issue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;issuesSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What the is &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;ref&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;type&lt;/code&gt;: it is property tells to store the ObjectId of that particular document(document is here User or book)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ref&lt;/code&gt;: It is a name of collection to find that ObjectId. Here "User" and "Book" are name of collections we created.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Let's Issue A Book
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Think John Comes To Library To Issue A Book Harry Potter&lt;/strong&gt;&lt;br&gt;
To issue a book we have to create a new Issue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;issuedBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Issue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;harryPotter&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What's going here
&lt;/h3&gt;

&lt;p&gt;Actually, we are saving john._id and harryPotter._id value to issue, in database it's 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;  { // issuedBook 1
     user: "65230asa434r345d34kd3wbb",
     book: "6b6s6a9d6fj87s8978s86s7s",
  }
  { // issuedBook 2
     user: "65230asa45645r4jjjl3434h",
     book: "6b6s6a9h5j3kh7j38fw4j3k2",
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This is how we save to refrences to user and books field&lt;/em&gt;&lt;br&gt;
&lt;em&gt;There are various ways to save refrences. Check out documentation &lt;a href="https://mongoosejs.com/docs/populate.html#saving-refs" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How To Populate Records ?
&lt;/h2&gt;

&lt;p&gt;Now, imagine libarian wants to check all issued book records.&lt;br&gt;
Well this is very simple to implement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Issue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./models/issue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// Import Issue model&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;issuedBooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Issue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({}).&lt;/span&gt;&lt;span class="nf"&gt;populate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;populate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;book&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand this what does that chain of calls doing&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Issue.find({})&lt;/code&gt;: This will find all records in &lt;code&gt;Issue&lt;/code&gt; Collection. &lt;em&gt;You can set conditions in find&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;populate('user)&lt;/code&gt;: parameter 'user' is same of field which we want to populate. &lt;code&gt;populate('user')&lt;/code&gt; will find user by thier perspecitve id and replaces &lt;code&gt;user&lt;/code&gt; field with actual user data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;populate('book)&lt;/code&gt;: same as above, replace book id with actual record&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exec()&lt;/code&gt;: This is very important function call. This will execute above all populate operation. If you forgot to call this. Your field will not be populated.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note: If During Populate Some Records Are Not Found Then These Records Are Replaced By &lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;issuedBooks&lt;/code&gt; value be like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  { // issuedBook 1
     user: {
        name: "John",
        address: "Noida",
        email: "john@example.com",
        _id: "65230asa434r345d34kd3wbb"
     },
     book: {
       name: "Harry Potter",
       author: "J.K. Rollings",
       publication: "XYZ",
       _id: "6b6s6a9d6fj87s8978s86s7s"
     }
  }
  { // issuedBook 2
     user: {
        name: "Peter",
        address: "Delta",
        email: "peter@example.com",
        _id: "65230asa45645r4jjjl3434h"
     },
     book: {
       name: "Invisible Man",
       author: "Ralph Elipson",
       publication: "ABC",
       _id: "6b6s6a9h5j3kh7j38fw4j3k2"
     }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;This seems like a useful tool in being able to define a declarative model for what your data should look like as a result of any query. There are some inherent weaknesses to a lack of true “joins”, but the Mongoose API does an elegant job of optimizing these types of queries under the hood by using poplulate.&lt;/p&gt;

&lt;p&gt;I’ve only recently begun using this, so if you know something that I don’t and would like to contribute to the discussion for anyone reading this article, feel free to comment below with any critiques, suggestions, random quotes, or song lyrics. Thanks&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>database</category>
    </item>
  </channel>
</rss>
