<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pratham Chouhan on Medium]]></title>
        <description><![CDATA[Stories by Pratham Chouhan on Medium]]></description>
        <link>https://medium.com/@prathamchouhan1?source=rss-80c0f55aee49------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Rr4djhl3QGRzNxYTWRz8WA.jpeg</url>
            <title>Stories by Pratham Chouhan on Medium</title>
            <link>https://medium.com/@prathamchouhan1?source=rss-80c0f55aee49------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:59:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prathamchouhan1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DAY 4: JavaScript Journey]]></title>
            <link>https://medium.com/@prathamchouhan1/day-3-javascript-journey-4eb439262350?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/4eb439262350</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[datatypes-in-javascript]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Tue, 29 Aug 2023 13:00:28 GMT</pubDate>
            <atom:updated>2023-08-29T13:03:48.376Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be documenting learnings from the Javascript Course by Hitesh Choudhary Sir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wi1mY_Zv0JLt3eWR51VsMw.png" /></figure><h3>Datatypes and ECMA Standards</h3><p>Welcome back to the series, we are continuing our JavaScript series, in today&#39;s article we will see some data types and some hidden concepts of JavaScript.</p><p>So we will make a new file in the folder 01_basics and name it 02_datatypes.js. Let&#39;s begin with datatypes but before that let&#39;s discuss about JavaScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Na10rerM2ksnyEABiCJQSg.png" /><figcaption><strong>New File</strong></figcaption></figure><p>JavaScript which was written in books 8 to 10 years ago and today&#39;s JavaScript there are a lot of differences between the two. In older JavaScript, there were no classes, modules, or arrowfunctions present, but now they are present.</p><p>Code that was written in the old format, the committee or the authorization didn&#39;t want the older code to be of no use because of the new code we are using, and when we use new code then we should get the same standard that whole new code should be treated the same because at last, the whole code would run by the engine.</p><p>So for this in JavaScript, we write double quotes and write “<strong>use strict</strong>”, as soon as we write use strict now our whole code will be treated as a newer version of JavaScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q30Au3Q0JvqUWFRKGCoKEw.png" /></figure><p>Because of this remember one thing there is no method from which we can write use no strict, so once we have written use strict then in the whole file of your code will be treated as a new standard or engine.</p><blockquote><strong>Now today the standard is that we don&#39;t even need to write, it will automatically run in the strict mode.</strong></blockquote><p>But “<strong>use strict</strong>” is a good syntax that I will use new syntax classes, modules, and all. Now with this, we have many other things. First of all the <strong>alert</strong> statement, we will use 1000 times and whenever we write “hello” we will be surprised when we run it. We can read hello or some people teach us, <strong>arithmetic operations</strong>, Arithmetic is a fancy name for addition, subtraction, and operations like that.</p><p>So if we write 3+3, we know that output should be 6.</p><blockquote><strong><em>alert (3+3);</em></strong></blockquote><p>But when we run the code in the terminal with the command</p><blockquote><strong><em>node 01_basics/02_datatypes.js</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rHkmt_S70Agu3h6yrmrUzw.png" /></figure><p>If we run then we get an error. Why did this error come? Because we are now talking about Node and all the people, that teach us they will either teach us to bind the file HTML or they will come on the developer console and write,</p><p>alert (3+3)</p><p>Then obviously a pop-up will come and will say 6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*emLr71CAtieG1ZGnnKXVAg.png" /><figcaption><strong>Popup on Browser</strong></figcaption></figure><p>So we will see about this in detail because whatever we are writing on this console is present in the document.</p><blockquote><strong>As I have already told that JavaScript engine is present in the browser.</strong></blockquote><p>So all of the work is done from the console. But to use alert() in Node there is a different syntax. It is available but till now we can&#39;t do this. So please be careful, now we have seen about basics. We need to know one more thing that is used extensively in examples, i.e. <strong>when we can use spacing, and when we can&#39;t.</strong></p><p>As we have seen in use strict, we have used a semicolon but after alert we haven&#39;t so most of the files that we write like React, Node then we avoid semicolons and the most important thing in coding is readability. Let&#39;s see and examples as we have seen already</p><blockquote><strong><em>console.log (3+3)</em></strong></blockquote><p>In this, also we can write basic operations and after that only we have to print “Pratham”</p><blockquote><strong><em>console.log(3+3) console.log(“Pratham”)</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CWPKUwUHfeQBKiR1uEqzhw.png" /><figcaption><strong>Error</strong></figcaption></figure><p>Now you will see that you will get an automatic error, so the problem is not where we can put spaces and where semicolons and when we put semicolon in the above statement, the error will be gone</p><blockquote><strong><em>console.log(3+3); console.log(“Pratham”)</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pkH1BiljGHP0_yFfijdjhA.png" /><figcaption><strong>No Error</strong></figcaption></figure><p>But if you see real-world coding practice then this is bad. If code is executed and run that doesn&#39;t mean that everything is correct. Code should be readable, and future-proof, then what is the experience? So here we have removed the code readability, so if anyone comes and reads it then it will be very difficult to read it once 3+3 is happening and then in continuation, we have started another line.</p><p>We didn&#39;t have a shortage of lines. We can remove the semicolon and write the second statement in another line so that is much more readable and good practice.</p><p>Code readability is the highest preference. So there are the basic talks. Now we come on documentation so whenever we want any documentation I will always prefer <strong>MDN</strong> because it&#39;s very easy and written very beautifully. But this is not the original documentation, this documentation is from Mozilla organization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pzBsPZOGP9-jaObBBaY7SQ.png" /><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"><strong>MDN Website</strong></a></figcaption></figure><p>The original document is at tc39.es, which is ECMAScript. In the starting days when JavaScript was coming, everyone had different standards different browsers, and different standards. So all thought to come together and ECMA script organisation was defined. That will write standards, not JavaScript but standards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_2UsQ0AyTWjnrwNmH3gDmg.png" /><figcaption><a href="https://tc39.es/ecma262/"><strong>ECMA Website</strong></a></figcaption></figure><p>If you put loop then this should be output and in this way, we should take input, after that what we do is matter of choice. But the standard is minimum and you should follow that.</p><p>So all the memes that you see on the internet that one plus one we get something and if we get other something different occurs so all these are the story of standards. Later on, we will see in the standard that there are priority algorithms and the first this happens then in the specified way we should give output and all the game is here.</p><p>And if you see the ECMA if you want to read something, a matter of fact this documentation is not easy and is only for browser and engine coders. It takes a lot of time to read standards and let&#39;s say we want to see what Global Object is then we can find it out when we should ignore white space and what are the Unicode characters and when can ignore comments and when we shouldn&#39;t write multiline comment, we can all see that in <strong>ECMA script Language: Lexical Grammar.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eZpC1KLTnFvDMVGe09dkMg.png" /></figure><p>We will also get tokens and in which way we are allowed to write names and in which way we are not allowed. So all this detail come here and we call this <strong>specification</strong>. Now we don&#39;t need to go in this detail that much that we read all specifications. But we get some important things here and we will come back here in the series later.</p><p>So remember two documentations:</p><ol><li><strong>ts39.es</strong></li><li><strong>MDN</strong></li></ol><p>On MDN we will work the most so that&#39;s all about basic introduction. Now on <strong>ts39.es </strong>when we go we get <strong>Data types</strong>, Global Objects itself is a datatype type, and other we can see Numbers and Dates. So there are a lot of that is in JavaScript and we have 02_datatypes.js files so obviously we will discuss some datatypes.</p><p>A good thing is that in JavaScript there is no a lot of the datatypes. Most of the work is done very easily. For example: we have took a variable name and we have written “Pratham” so this is <strong>string</strong> datatype.</p><blockquote><strong><em>let name = “Pratham”</em></strong></blockquote><p>In the same way we have took age and given without double quotes 20. So this is <strong>Number.</strong></p><blockquote><strong><em>let age = 20</em></strong></blockquote><p>Now we write another variable isLoggedIn that we will use when user will login to our website so we can write true or false or these are of type <strong>Boolean.</strong></p><blockquote><strong><em>let inLoggedIn = false</em></strong></blockquote><p><strong>How many types of data types do we have?</strong></p><p>So they are not much, they are all simple and let&#39;s write in comments.</p><ol><li><strong>Number: </strong>Now number has a range when we will see documentation 2 to power 53, so it is its range. That is the range that we get in memory address.</li><li><strong>BigInt</strong>: Now it&#39;s not like that if after number or any value comes then we can&#39;t score we have bigint and in most of the programming we will not use bigint. But if a number is too large then we use bigint, especially in trading, stock market or if there is a very big website string.</li><li><strong>String</strong>: Another type is string we can use that in single quote or double quotes my suggestion is to use double quotes as it is very easy.</li><li><strong>Boolean</strong>: Now another one is boolean and boolean is simply yes or no JavaScript is not that much complicated. So we get two values here true, false and moving forward we will work on this.</li><li><strong>Null</strong>: Now an interesting thing null. This null has many stories this actually is standalone value</li><li><strong>Undefined</strong>: Now what is null and undefined? <strong>Undefine</strong> is when you have not define value examples, we have seen let state and not assigned any value, so that is undefined we have declared variable but we have not given any value. <strong>Null</strong> is the representation of empty value now if you write “” then this is empty string because it type has been being define string. But null is a special type and we can assign it like <strong>let state = null </strong>That we are intentionally telling that it is not defined, it is empty. Empty means like we have sent some request from server and told to tell temperature now if it send 0 temperature that there is some problem with temperature is server, so in that case we do not need zero because zero is temperature so we will prefer null.</li><li><strong>Symbol</strong>: Symbol is very unique and its major implementation w e will see when we will talk about react. When we make so many components and we need to identify that this component is unique, then to find that uniqueness we use symbol. Most of the figma tools, all these use symbols a lot and to define that which is individual component. So these are <strong>primitive data types.</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCuXLc02FxyHWmBMWC_I7w.png" /></figure><p>They have names and categorisation have already been done. Now after this we have one that is used most is<strong> Object. </strong>And we will see about objects in detail what are objects and all of that in future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iidbNtipeS1Vcf-8XPiPjw.png" /></figure><p>Now we will go on console.log() and we have a value called <strong>typeof</strong> and now they are two ways to use this either be write <strong>typeof</strong> and the value that we need to find type we should write</p><blockquote><strong><em>console.log(typeof “Pratham”)</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sqxfKDC0v4ve8pM8LWBvog.png" /></figure><p>Obviously, <strong>string</strong> will come and other is if we have taken a variable age and we can find about the variable that we need to find a type of age.</p><blockquote><strong><em>console.log(typeof age);</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BNWZVeveLmjq2OYW-D-bvA.png" /></figure><p>So the output is a <strong>Number</strong>. An interesting thing is when the ask null what is it’s type and that is the reason for errors in JavaScript. When we see the type of null and in it then we get <strong>object</strong>.</p><blockquote><strong><em>console.log(typeof null);</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*41bu1XbVsuPPtR_hxYsoUw.png" /></figure><p>At this, some people say is the drawback of JavaScript and some say this is the method and the same way we wanted to see the type of <strong>undefined</strong> we get undefined only.</p><blockquote><strong><em>console.log(typeof undefined);</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNL5YvbCDnJiFO0NLzNTAA.png" /></figure><p>So undefined in itself is a data type null is an object. So some people say it&#39;s language error. So please remember when you take errors of undefined and null then what is the output. So output of <strong>undefined</strong> is <strong>“undefined”</strong> because it is a type in JavaScript and the type of <strong>null</strong> is “<strong>object”</strong> and now what is all of this how it is used? We will talk about this in detail and how strings can be defined in different ways and into different ways and what are the uses in the following series.</p><h3>GitHub Link:</h3><p><a href="https://github.com/chouhanpratham/js">GitHub - chouhanpratham/js: A code repo for JavaScript</a></p><h3>Notes PDF Link:</h3><p><a href="https://drive.google.com/file/d/1kayljDe2NySfee_lFGqIjoOgH3C9St7W/view?usp=sharing">Javascript Day 4.pdf</a></p><h3>Conclusion:</h3><p>But for today that’s all for you you can. You can check the ECMA documentation for the specification of the data types. So that’s all about datatypes. In the next article, we will see more exciting things.</p><h3>Acknowledgment :</h3><p>Thanks to Hitesh Choudhary Sir.</p><p>If you like my work, please like, follow, and share.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4eb439262350" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Database Works?]]></title>
            <link>https://medium.com/@prathamchouhan1/how-database-works-a1daf8e6b29c?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1daf8e6b29c</guid>
            <category><![CDATA[execution-engine]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[storage-engine]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 12:31:43 GMT</pubDate>
            <atom:updated>2023-08-27T12:39:56.348Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRozPaCjzU_naiijFM6vxg.png" /></figure><p>Today we will be going to discuss a lot about databases, I’ll spark enough curiosity in you so that you truly understand what it takes to make a database and why there is a database war going on in the market.</p><p>I’ll spark enough of the question inside you that you will now question every inch of the database and you will be able to truly understand that.</p><p><strong>What makes a database a good database and how you can select a database for yourself?</strong></p><p>Although this is generalized I’m making this myself here but this is a generalized database engineering side of it. It doesn’t mean that this is a side of MySQL or Postgres or MongoDB or any other database like Vector database but first, this is a generalized side of it.</p><p>Not only that this article will give you enough of the details so that you in case you are curious about the <strong>Blockchain.</strong></p><p><strong>What makes blockchain?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L7lHkEl3zow9thEL01DleQ.jpeg" /><figcaption><strong>Blockchain</strong></figcaption></figure><p>You will get a tiny bit of a hint of it so for all the enthusiasts in the blockchain this is the right place.</p><p>So first of all couple of questions before we go and embark on the journey of what a database and how a database system entirely works.</p><p><strong>Q. What’s the inner engineering of it? What are the different components and the parts that are stitched together to make it work?</strong></p><p><strong>Q. Have you ever wondered and asked yourself a question about why should choose a database and what the importance database is giving you?</strong></p><p><strong>Q. What’s the difference between choosing MySQL and Postgres both are relational databases but what makes them different?</strong></p><p>Apart from claiming the speed that they give us why should I choose something like a MongoDB instead of a MySQL? Okay, MongoDB is a document database but what makes it a document database also have you ever asked yourself a question that whatever the database is doing I know that it’s writing something inside the disk.</p><p>So whether you choose MySQL or Postgres or MongoDB It ultimately writes it inside the disk itself and that writing part is a little different.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JcvfyAxf9oBqXk22FvXujQ.jpeg" /><figcaption><strong>Database</strong></figcaption></figure><p>A<strong> Relational Database</strong> obviously writes into a tabular format like an Excel sheet or a databases like MongoDB it just dumps it out into the object format. But once they actually put it into the file ultimately the file is it just a sector on a disk.</p><p>Whether your SSDs or hard disk it’s engraved there it’s magnetically written there so how do these databases actually get that update facility?</p><p><strong>How they are able to go inside the disk and actually rewrite that sector?</strong></p><p>If you ever studied computer science that’s not possible once that’s magnetically engraved that’s it needs to be wiped off and completely rewrite, there is no update facility in the drive.</p><p>So that’s where the engineering side of things actually spark and that’s where I enjoy the most that makes you a real engineer.</p><p>Let’s go to our database. So we have a diagram. Let me just walk you through it one by one and you don’t have to worry too much this is all manageable and you will understand a lot of it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gmFpzoNEVKG8OT2OyLv1tQ.png" /><figcaption><strong>Database Diagram</strong></figcaption></figure><p>So first of all we have ORM/ODM part.</p><p><strong>What is ORM/ODM?</strong></p><p>In case you have heard about these days popular MERN stack, be a MERN stack developer you use a library known as <strong>mongoose</strong>. Now using a mongoose is not really compulsory, a mongoose is technically an ORM a representational client which makes our life easier.</p><p>We can go ahead and directly interact with the MongoDB client or the MySQL client but the interaction and those default drivers are not so easy to manage, understand and they don’t provide me tons of facilities on top of it that’s why when we want tons of facilities like mongoose and there are MySQL versions of it, I prefer to use ORM just to make my life easier.</p><p><strong>ORM is Object Relational Mapping.</strong></p><p>But there is no compulsion like that for example if you have ever written some code in C++ there’s a library Crow. There is no such ORM for the crow if you want to interact with MongoDB you have to use the MongoDB direct client and that’s also fun. But it doesn’t give you that kind of validation that mongoose gives you but hey that’s the part of it.</p><blockquote><strong>ORM talks to the client and the client talks to the database itself.</strong></blockquote><p>That’s why a lot of people you will see that if you read more articles people debating about that there is no point or no advantage of using ORM probably you are using it just to make another bottleneck.</p><p>You should be directly using a client because at least one layer is skipped, so this is the part that is used for talking to the client this whole big gigantic thing that you see is your database. MySQL, Postgres, and MongoDB all have these components.</p><p><strong>What’s interesting here is at the bottom?</strong></p><p><strong>Disk </strong>that’s where the magic happens and a lot of the databases are fighting on different segments of these databases that’s why you’re seeing there is a database war right now.</p><p>Some are fighting on how we are going to move the data to the disk, some are fighting over how we have actually changed our CEO yes database <strong>Execution Engine</strong> that’s known as their CEO, some actually say that we have actually rethought how this index file works that’s their whole USB is.</p><p>There are a lot of things going on so let me just first walk you through what happens once you have actually gone through with the ORM and the client.</p><p>On the client, there could be anyone or there could be both so once you have done that you have written some query for that whether that query is dot find or that query is Select star from users where first name equals to hit whatever that is,. That’s a query and that query first and foremost always and always hits this portion of the block called <strong>Query Parser and Query Optimizer.</strong></p><p>Some database tools will say that this is not two this is just one some will say this is three, and some will say this is four but the whole idea is there is two segments or two parts in which it is divided. The first one is the <strong>Parser</strong> part of it another one is the <strong>Optimizer</strong> part of it.</p><p>The<strong> parser </strong>exists in every language because you’re writing everything in the characters nobody understands Select Star what does that even mean? So in that case we need a parser agent.</p><p><strong>What this parser agent is?</strong></p><p>It converts that into AST(Abstract syntax trees) which is basically like saying an understandable tree structure so there’s a parsing that happens. Parsing simply means I want to convert that into tokens.</p><p>All over the keywords like if, else, select, where, an equal sign, and dot find all of that are tokens so I want to extract the tokens out of it then I want to run through it with the syntax and semantics. Whether you have written the proper syntax have you have a typo whether it’s according to my structure there’s a sequence of it so all the semantics should be proper, all the syntax should be proper and then I will convert that into a tree.</p><p>And that’s why a lot of people say learning data structure is good because a lot of things are being converted into tree but those people are mugging the data structure so much that they forget to show you the impact of trees.</p><p>So everything that you write whether that’s C++ or your data structure queries are converted into a tree. Now these trees are actually being thrown at the <strong>optimizer.</strong></p><p>Now this optimizer is really cool and it always and always talks about the cost part and this is like in your company there is always an accountant who says why you bought this, why you are paying 3 lakhs to this guy while you’re paying 30 lakhs to this guy yeah this guy always talks about the money part.</p><p>But in the database, it’s not literally the money the cost could be a different parameter for the database for example it could be a faster execution it could be don’t do execution twice or it could be I can grab the data faster but that will cost more so there’s a lot of optimization that needs to be done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TQevPvjD85WVGDVQITcQfA.jpeg" /></figure><p>Optimization has no strict definition for some, the optimization is time, and for some the data optimization is execution speed, there are a lot of variables and this is where a lot of databases actually fight.</p><p>Database first is getting <strong>Parsed</strong> and then it is being optimized now in every single database there is this <strong>Execution Engine</strong> that is the CEO of the database management system.</p><p>This decides how the client request will come and how I will send it back as quickly as possible just like the CEO consider this as an Elon Musk of the entire database he wants to do everything super fast. Now it will push the optimizer to give me the data fast, it will also go ahead and ask how can we retrieve the data back from the file.</p><p><strong>Execution Engine</strong> is the master of the ship, everything happens through it and it should be aware of everything that’s the whole job of the execution engine. It doesn’t literally do everything like it does some stuff but it doesn’t do everything, it actually asks everybody to do things faster and faster and update it.</p><p>Then there is a <strong>Cache</strong> as well, I’ll come back onto the cache in a minute but you don’t have to worry more about that.</p><p>Now there is another block here that has a lot of utilities and every single database company tries to increase its utility function more and more. The basic one of course includes the <strong>authentication,</strong> <strong>authorization,</strong> <strong>backups,</strong> <strong>metrics to track</strong>, <strong>metrics</strong> <strong>to evaluate, and clustering</strong> every single database company is trying to use these are known as <strong>Utility Functions</strong> and every database has its own utilization.</p><p>Now remember one thing no matter what database engine you’re using you’re always getting the data from the disk which is a <strong>costly operation.</strong> It’s not easy to invoke your <strong>Kernel</strong> and grab some data and get that so every single database has this cache operation that if this operation or if this data is frequently being accessed let’s not always call the kernel and rather put it into the <strong>cache.</strong></p><p>Again the cache is exactly the same as you study in your OS. If there is a page and if we get the page hit or a page missed exactly the same. So you get the cache if that is where your CEO always asks for the cache first this is the query it is being passed it is being optimized so do we have access to the cache, if we have access to the cache throw it back to the client don’t even think for a second.</p><p>On top of that, this utility is always run based on their need or not for example whether you are authorized to ask me that question or not.</p><p><strong>Whether you are a reliable client or not, whether there should be a timely backup of this data or not?</strong></p><p>These are utilities so we are not bothered much about it now what we are bothered about it, where a lot of databases are fighting these days is this block called <strong>Data File and Index File</strong>.</p><p>So there are two portions of how your data is going to be magnetically engraved in the database, the first one being the b and the second one is the <strong>index file.</strong></p><p>Now your data file is actual data there is nothing in it and yes there are debates and there is competition on that for example some data files should look like tables and rows that’s what the <strong>relational databases</strong> are. Some say the data file should be written in the <strong>document format</strong>, there are <strong>vector</strong> formats even so this is where a lot of fight happens a lot of variety of the databases are given to you.</p><p>The database will be able to extract the data faster if you just give me that document back and I’ll do the majority of stuff in my execution engine, or some say let’s just execution engine handle the part first and then go to the data file to specifically look for rows and columns and get only that data.</p><p>Again the <strong>index file</strong> is actually the <strong>Metadata</strong> because if you notice if you have ever gone into the <strong>MongoDB Atlas</strong> you’ll find that there are different indexes that you can create and these are searchable and obviously you’re not going to search directly from the data file there should be some <strong>metadata,</strong> <strong>data about the data.</strong></p><p>So that you know how to find the data which I’m looking for and that’s where some people say that hey we’ll have just one primary index, secondary index. Some say we will allow you to have each field marked as index databases.</p><p>All the whole thing this saving and all of this done is via the <strong>Storage Engine</strong> that is your<strong> junior CEO </strong>we can say. This helps you to store all of these data indexes on the disk, so the more powerful your data storage engine is the more powerful your database is and this storage is actually done in the different data formats.</p><p>Because it’s a structure you want to store your data and that’s where you learn about the B trees and B+ trees anybody who is doing their Master’s or Ph.D. once in a while in computer science they have to study these B trees and B+ trees and even have to read the research paper of how we can optimize it more further and go beyond the B+ trees, there’s the hash index there’s a lot more.</p><p>Now that you understand why the databases are storing or having a war on the <strong>Storage Engine</strong> portion as well as on the <strong>Data File and Index File </strong>portion then comes the <strong>Manager.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GyKHLZu7XSDj2JkJdP6aZw.jpeg" /><figcaption><strong>Manager</strong></figcaption></figure><p><strong>E</strong>very database is trying to hire more and more managers not literally higher but you can say they are injecting more and more managers. The first these are the common managers:</p><ol><li>Transaction Manager</li><li>Lock Manager</li><li>Recovery Manager</li></ol><p>There are more actually these days the first one is the transaction manager. Now the <strong>transaction manager</strong> is somebody who guarantees the transaction.</p><blockquote><strong>A transaction simply means if it happens then it happens otherwise it doesn’t happen partially.</strong></blockquote><p>This transaction manager doesn’t work alone it actually takes help from other managers but this is a guarantee because no matter what the database is the transaction should be guaranteed. A lot of people say is in MongoDB there is no transaction guarantee, no you are wrong there absolutely wrong, transaction is a guarantee and is a feature of the database.</p><p>Even if it’s a MongoDB either you save the entire data or you don’t save it that is the transaction nobody wants to have a database even a database will not exist.</p><p>A lot of people use databases for their transaction history in the payment gateways as well you don’t want that sometimes it saves the data sometimes it does not. So transaction simply means a guarantee that it will store the data or will do a complete rollout and will give the error that’s the whole job and how it does it is actually dependent on the <strong>lock manager</strong>.</p><p><strong>Lock manager</strong> does two things if you want to write the data it gives you exclusive access to that portion of the disk so that you can write it and nobody bothers you during that time because databases are trying to use your computers a lot as much as possible and if you know these days everything is happening concurrently.</p><p>The database is not like your JavaScript that it runs on a single thread that it tries to use as much as possible of your concurrency, but it is also required that the lock manager should give you a guarantee that if you’re writing something no two parallel process should come and write that, that’s important but if you want to read it, it actually allows the shareable read so if somebody wants to read it read operation should be faster otherwise nobody will use the database.</p><p>So read operations are shared and all the concurrent processes can actually take place and share the data. It does it through a log table mechanism so in case you want to learn that learn about the log table simple process there is nothing too much to discuss.</p><p><strong>Recovery manager</strong> is an interesting manager which I love a lot. Now recovery manager is something really interesting it uses a simple data structure which is<strong> Append-Only Data Structure.</strong> So if you have ever wondered.</p><p><strong>How the log tables are able to keep track of all the logs that we’re making?</strong></p><p>Sometimes we mess up but these logs are actually responsible to roll me back in time that’s exactly what the recovery manager is, it allows the database to have a point of time where everything was correct, so through these logs and through this spatial data structure which is known as <strong>append-only data structure</strong> you are able to go back in the time.</p><p>Now this append-only data structure is not something new it was always there in the logs and in fact, the concept of how you’re going to keep logs in the database actually came to us only because of this append-only data structure.</p><p><strong>What is an Append-only Data Structure?</strong></p><p>That means you cannot update, it you cannot delete it, you can only just keep on appending it you can keep on adding on top of it and that’s my friend is your <strong>Blockchain.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jsyM9COYU7WemlKvqV2GrQ.jpeg" /><figcaption><strong>Blockchain</strong></figcaption></figure><blockquote><strong>Literally this is your Blockchain you cannot go back in time, you cannot append it you, cannot delete it only the chains can be moved forward.</strong></blockquote><p>So all those enthusiasts will say I just care about the blockchain only I don’t care about logs or databases my friend you missed the basics it came in here from append-only data structures so again this is really really fun.</p><p>Now a couple of more things are super interesting here right now in the initial days a lot of people actually had a competition onto this, all your utilities.</p><p>Utilities this is where a lot of people fought and said this is my SQL version, this is my SQL version one or other than of them gave us, try to give us more utilities and more control over our recovery manager, that’s it that was the game.</p><p>But these days things are on a different scale, there are a lot of debate and you’ll find a couple of databases in which are just focusing on the cache, Redis is one of them which has like crazy cache ower and again the same thing happens in the Radius even there is no difference.</p><p>A lot of them are fighting over this Storage Engine and a lot of them are fighting even what kind of data should I store and how should I store it and what metadata should I store it.</p><h4>Notes PDF Link:</h4><p><a href="https://drive.google.com/file/d/1YJeVxyBI9CNVtQ0EuwLC90fMcT8iNZvn/view?usp=sharing">How Database Works.pdf</a></p><h4>Conclusion:</h4><p>So that’s where you see the crazy war of the database is going on. This article helps us in finding out why the database is so much powerful behind the scenes and engineering side of it.</p><h4>Acknowledgment :</h4><p>Thanks to Hitesh Choudhary Sir.</p><p>So this is all about the details that I wanted to share about Database. If you like it please share, comment, and follow.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1daf8e6b29c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DAY 3: JavaScript Journey]]></title>
            <link>https://medium.com/@prathamchouhan1/day-3-javascript-journey-3e35070da5e6?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e35070da5e6</guid>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[const]]></category>
            <category><![CDATA[var]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sat, 26 Aug 2023 10:40:25 GMT</pubDate>
            <atom:updated>2023-08-26T10:40:25.036Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be documenting learnings from the Javascript Course by Hitesh Choudhary Sir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*06rgefWXRuggZdtmJwnFMg.png" /></figure><h3>Let, const and var ki kahani</h3><p>Welcome back to the series. Until now, we have learned how to learn JavaScript, who developed it, and what JavaScript engines are.</p><p>After setting up the environment. Now we are starting with variables and constants. Before that, I want to ask a question.</p><p><strong>Why do you want to learn JavaScript?</strong></p><p>We want to build something, make some products, and if your goal is to make an e-commerce website or a social media application on a mobile app that looks like an Uber or Zomato clone on mobile or web.</p><p>If you want to make something like that then you are on the correct path because using JavaScript we can make all of that so we will learn to reach the goal.</p><p>So in this whole journey, I will not give vague examples that we declare variables for this but we will discuss through a Journey.</p><p><strong>Why are we learning JavaScript and what are we doing?</strong></p><p>Now we will declare and assume that later on we need to make a website which is an <strong>e-commerce</strong> <strong>website</strong>. So for that, we need to call a user to our website and we need the user to fill out the registration form.</p><p>In this registration form, we need to provide him/her unique ID so that when he/she comes to our website or registers in the database, we can uniquely identify the user.</p><p>Till now we are only locally thinking. But this perspective is what we need to do and that&#39;s why we are learning to make things easier.</p><p>So as the new user comes we need to get the name, email, city, state, and all of this we need to store. Yes, we need to store that in the database we talk about them later.</p><p><strong>But we will now talk about, how we can store now?</strong></p><p>When we store anything, we need a memory space that’s here something will be stored. And all the game in programming is to store the thing and get from that location in an optimized way. So for that the first topic we study is called <strong>variables and constants</strong>.</p><p><strong>Constant </strong>means that once we have declared, then we do not need to change it or we aren’t able to change it. So if by mistake we change its value then no effect will be there, the value will not change.</p><p>Other are <strong>variables</strong> that later on may get changed or anything. So let&#39;s see in JavaScript how we can call users on our website and ask for registration information from the user.</p><p>So we will again go to our folder <strong>01_basics</strong> and we will create a new file whose name will be <strong>01_variables.js</strong> because we are studying variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NDBwge0i2Ur2CoAkNJZckQ.png" /><figcaption><strong>New Folder</strong></figcaption></figure><p>Now we will first study investigation because it&#39;s most important. Later on I will tell how from documentation we should study investigation learning that will help you in future to study new languages. So first let&#39;s see how to declare<strong> constant</strong>.</p><p>We need to write the <strong>const</strong> keyword and then we will write <strong>accountId</strong> now this is a pattern to declare a variable, and many people will tell you in different ways.</p><p>Some will say that account Id are two words so the second letter first alphabet should be capital so we have written accountID. Some will say we should write like this <strong>account_id</strong> or in this way <strong>account_Id.</strong></p><p>All the ways are correct, all have their own rules, and whatever we think is correct you should follow. That&#39;s why my simple rule is that while declaring a variable its name should be easily readable.</p><p>In the programmers, world accountId is the most common syntax and it&#39;s not that we write this in Java another way in C, these things will be said by those who have only 2 to 3 years of experience. But in the end, the goal is to read the name easily and it is understandable.</p><p>So we have written accountId and given 144553 randomly we are doing an investigation.</p><blockquote><strong>const accountId = 144553;</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Ci3bkb3QDxPurHKxsMSyg.png" /></figure><p>Now we will use the keyword <strong>let</strong> and then we will write <strong>accountEmail</strong> and we will write email “prathamchouhan12@gmail.com”, and we will use another keyword <strong>var</strong> and we will write <strong>accountPassword</strong> and write “123456” and notice that the accountEmail and accountPassword we are writing in double quotes, and then we will write <strong>accountCity</strong> equals to “Jaipur”.</p><p>Now we will do our investigation, let&#39;s see some theoretical basics first. In JavaScript, there are some constant keywords and we have already seen that the difference between text files and JavaScript is basically of these keywords.</p><p>These keywords are already defined in the compiler of JavaScript like Node.js or V8 engine you have used, so it is defined that if this keyword is present, then follow the syntax, and as we have seen when <strong>console.log</strong> then to output on the screen.</p><p>So in the same way <strong>var, let and const </strong>are the keywords whenever they occur then assign them in the memory, so given the name of memory as account_Id and the value to be assigned is 144553.</p><p>So <strong>const</strong> means that this value can&#39;t be changed. Now the value that we have assigned once will be locked and sometimes intentionally it is very important to lock the value.</p><p>The first thought that comes into our mind is that everything should change and whenever we need, we can change it. But it&#39;s not like that, sometimes things should not change. Most of the things should not change.</p><blockquote><strong><em>const accountId = 144553;</em></strong></blockquote><blockquote><strong><em>let accountEmail = “prathamchouhan12@gmail.com”</em></strong></blockquote><blockquote><strong><em>var accountPassword = “123456”;</em></strong></blockquote><blockquote><strong><em>accountCity = “Jaipur”;</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*91873nyjEj0YWLRUtaZVdg.png" /></figure><p>Now let&#39;s see changing all of the above. First, we have <strong>accountId</strong> and give it 2, now you will say it is changing but you have written that in a changing but remember that all this code will be analyzed by Node.JS, so it will let you do that or not. That&#39;s the most important thing.</p><p>Now we will come to <strong>console.log()</strong> this is the easiest shortcut to simply write <strong>log</strong> and as soon as you get suggestion, you should enter console.log(); will be written and in further future, I will tell you how to make shortcuts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mC7KR0df_KxeComkY0bFig.png" /><figcaption><strong>Shortcut for conole.log()</strong></figcaption></figure><p>Now we want to print accountId. Now we will open the Terminal and run it.</p><blockquote><strong><em>node 01_basics/01_variables.js</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BdFQcc3eN-m2soG-ZYPdHQ.png" /></figure><p>As soon as we run, it says that no, no it can&#39;t be done this way. <strong>Assignment to constant variable</strong> will not be done by it. So obviously we need to comment it means two slashes or shortcut <strong>Ctrl + /</strong> or <strong>⌘/.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08iFa9UyDmIAYuSWOT0l4g.png" /></figure><p>So an investigation has been completed that if we write const keyword, then we can&#39;t change that and write that, this is not allowed in a comment. Now whenever we write with two slashes Node.js will not execute that line. So now it will print accountId 144553.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zB6ZwLqNjOjFiThHrE8Oag.png" /></figure><p>So from this, we have not come to know how we will get user registered but we will come on that, but for now we will do this one only. Now we want to add some value in <strong>accountEmail</strong>. So we will write<strong> accountEmail</strong> equals to “pc@pc.com”, and with this we will also do for <strong>accountPassword</strong> to “212121” and then will get <strong>accountCity</strong> we will change from “Jaipur” to “Pune” now to print all these variables. We need to write again and again <strong>log </strong>and then in suggestion it will be selected and then write accountEmail and so on is a lot of work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ve6wj5bA1EOS6fQYPLvwdg.png" /></figure><p>So now we will try to change this and now we will use another method of the console which is table <strong>console.table(),</strong> which is a very interesting method and we can write [ ] and write all the names of the variables like accountId, accountEmail, accountPassword and at the last accountCity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kLrIsaNNgqe1gnNRtqLPjA.png" /></figure><p>Now as we run the file we will get the tabular structure with all the variables, so it is easy as compared to what we were doing earlier with console.log.</p><blockquote><strong><em>console.table([accountId,accountEmail,accountPassword,accountCity]);</em></strong></blockquote><p>Now we have accountId, updated Email, Password, and also City.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gxBmLzla63-mzQWlKprsHg.png" /></figure><p>Now we will lead the investigation and let&#39;s write let so as a variable has been declared and a space in memory has been reserved. Which is putting my values there and their values can also be changed. It is also happening with var also. A space is assigned in memory and we can also change that.</p><p><strong>What’s the difference between let and var, why do we need to use let and why can’t we use var everywhere?</strong></p><p>We have also seen in accountCity we have written neither var nor let, but how it&#39;s printed, it should not have been printed.</p><p>So the story is as follows in JavaScript we can declare constants and variables like this. To declare <strong>constant</strong> we have only one way that we have to use <strong>const</strong> keyword.</p><p>But to declare <strong>variables</strong> there are two ways, one is <strong>let</strong> and the other is <strong>var</strong>. Now we don&#39;t use <strong>var</strong> there&#39;s a story behind it. JavaScript language had a problem that it does not know about the scope. We will talk about scope later but for now when we write curly braces <strong>{ } </strong>these are called <strong>scope</strong>, and we will see how the scope is very useful in different places like when we use <strong>if and else.</strong></p><p>So in JavaScript, there was a problem with scope and in the initial days it didn&#39;t work on the basis of scope, <strong>blockscope</strong> was a thing and there was no control of JavaScript over that.</p><p>So the problem was like if a programmer comes and then he writes <strong>accountPassword</strong> and he added some value and then again another programmer came and then you wrote another file and in condition like for and change the<strong> accountPassword </strong>variable.</p><p>So the problem was when the <strong>accountPassword</strong> was used then this main <strong>accountPassword</strong> would get changed. So if someone has used name variable at 10 places, then it will get changed in 10 places so for that there were many problems.</p><p>So later on some advanced parts of JavaScript came that from now on we will only use <strong>let</strong>. Let has resolve the problem of scope that we faced earlier. So if you look at modern tutorials all will say two things, if you want to work in JavaScript, then you should use <strong>const</strong> and<strong> let</strong> and forget that var was something and it&#39;s not like that. We have totally removed some old code.</p><p>And it&#39;s not like we have totally removed var, some old code it’s still using it. But I am going you note that a new type of comment. For this new comment, you should write /* and then again */. And in between you can write be comment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WJ3Clyxc7Id5xRkBZbEo5g.png" /></figure><p>So let’s summarize this const and let what we need to use, and var we do not need to use</p><p>Let&#39;s see a new story, JavaScript is a safe language that is it will help us to reserve memory without using lead or var. But this method is not correct and you should never use that, but remember that this could also happen. There is a possibility that a variable can be declared in this way.</p><p>One more thing we need to see is that if our client says that he wants to take <strong>accountState</strong> we will use let, we have declared a variable but not put any value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MRTrzE4YvUJYz-Ls9WysTA.png" /></figure><p>Now in JavaScript, if you put or not put a semicolon it doesn&#39;t matter. Now we will print<strong> accountState</strong> and we will get <strong>undefined.</strong> So we came to know that if in JavaScript we declared a variable and not initially it, then JavaScript assumes to be undefined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s6OXUId5C1D-BzhcKSXL3A.png" /></figure><p>So we have seen all the var, let, const, block, and comments because we wanted to learn with experience and plus project-oriented. So that simple is JavaScript. If you study in the way of investigation learning.</p><h4>GitHub Link:</h4><p><a href="https://github.com/chouhanpratham/js">GitHub - chouhanpratham/js: A code repo for JavaScript</a></p><h4>Notes PDF Link:</h4><p><a href="https://drive.google.com/file/d/1Xw8_ApXaiC6leTS-kNtIKQ0TNUuyr7PZ/view?usp=sharing">Javascript Day 3.pdf</a></p><h4>Conclusion:</h4><p>So this is how we will work in an online environment. We have a benefit online because of all the code files and resources we can get easily.</p><h4>Acknowledgment :</h4><p>Thanks to Hitesh Choudhary Sir.</p><p>If you like my work, please like, follow, and share.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e35070da5e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DAY 2: JavaScript Journey]]></title>
            <link>https://medium.com/@prathamchouhan1/day-2-javascript-journey-4b9ab8c860b7?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b9ab8c860b7</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[codespace]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Thu, 24 Aug 2023 11:54:32 GMT</pubDate>
            <atom:updated>2023-08-24T11:55:19.559Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be documenting learnings from the Javascript Course by Hitesh Choudhary Sir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*06rgefWXRuggZdtmJwnFMg.png" /></figure><h3>Save and Work on GitHub</h3><p>We are continuing at JavaScript series. Lastly, we saw how we can install Node.JS and VS code can create a runtime environment and we can code.</p><p>But sometimes we do not have the access to same computer or our computer is in low configuration. So for this in the market, there are lots of online code execution environments available.</p><p>Now all these environment is not just like we have written code and run it. Some are like that help you to make a portfolio and for this, we will talk about GitHub.</p><p>Git is not only a portfolio website but it has various uses. It is mainly used when lots of software engineers come and work together then it is important. The versioning system helps to keep the track record of the project.</p><p>Today we will see how we can make our code environment using Node.JS and JavaScript for execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9HSxmXC1ga9mylqt7Y0mbA.png" /><figcaption><a href="https://github.com/"><strong>GitHub</strong></a></figcaption></figure><p>First, we need to go to get up and create an account. Now we need to create a new repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0o8YRkCzZF6M-rzBbESsIA.png" /><figcaption><strong>New Repository</strong></figcaption></figure><p>Now we will write the name of the repository as <strong>js</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wTgYyRoDvhaAIm9Bj1NJdQ.png" /></figure><p>And we can give the description as <strong>a code repo for the JavaScript series</strong>. We will keep the repository public and we will add a README file into it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KJiY_dxRCMCNJXGAVHm0VA.png" /><figcaption><strong>Description and Public Repository</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pvgoa6y8uatRWd4xAPrqaQ.png" /><figcaption><strong>Add a README File</strong></figcaption></figure><p>Simply click on <strong>Create Repository</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VgO6Qxd8F8oEvklkk_cfwQ.png" /><figcaption><strong>Create Repository</strong></figcaption></figure><p>Hear the process to set up the environment. It is very easy and needs to follow only 4 to 5 steps that we should remember.</p><p>Some steps you don&#39;t need to repeat also. So here comes our repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVhqlD4v9pOMTYWCZorM2g.png" /><figcaption><strong>JS New Repository</strong></figcaption></figure><p><strong>Now, How does Git work, git commands push work?</strong></p><p>We don&#39;t need to do all of that in an online environment that we will use. There are various ways to use Git. This is one of them.</p><p>Now we will go on <strong>Code </strong>and I need <strong>Codespaces</strong>. There will be <strong>Local</strong> also if you want to download all the files then we can also do that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WEcnT1XswVbFM_MGwzmwA.png" /><figcaption><strong>Codespaces</strong></figcaption></figure><p>Then in Code we will click on <strong>Create Codespaces on Main</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WEcnT1XswVbFM_MGwzmwA.png" /></figure><p>I’m confirm, that Codespeces will be free in the future but in circumstances, where GitHub makes them paid or they are not affordable then we can simply download the Zip and we can go to VS code and write all the code.</p><p>Everything is the same. Even GitHub is of Microsoft&#39;s and VS code is of Microsoft&#39;s. So here in Codespaces also we get VS code. So the environment will be the same but small changes we need to do here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cd2EV68fgo4lbM9OG0xq2A.png" /><figcaption><strong>VS Code opened in an Instance</strong></figcaption></figure><p><strong>What happening behind the scene is?</strong></p><p>For us a computer is being started or what we call an<strong> instance.</strong> In this computer, till now there is no software but only they have installed VS code, and that’s all.</p><p>Now as we have installed in our local system Node.JS then we have written code files here also we have to do the same. We have to create a Node.JS environment but the process here to create an environment is slightly changed here as we can see here we do not have a desktop.</p><p><strong>How to Download?</strong></p><p>It’s very Easy. We need to click on the F<strong>ile</strong> and then go on <strong>View</strong> and then click on the <strong>Command Palette</strong> or you can simply learn the shortcut</p><p><strong>⇧⌘P or Shift+Command+P</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W6bPDHFYjzCa2KJmF_kPAg.png" /></figure><p>So to make a list of shortcuts for yourself and then write a <strong>container.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gxrIWAL_jlYzIza4_hdYiQ.png" /></figure><p>As we write container the first option will come <strong>Add Dev Container Configuration Files. </strong>This means that you have this computer instance, in this, we will add some files of configuration through which if we want Node.JS, we will get the default Node.JS and if we want Python then by default, Python will be there.</p><p>So we will click on the <strong>Configuration File</strong>. Now when we search Node.js it will say Node.js is not available. Now we need to click on <strong>Show all Definitions</strong>. Now again we write node.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AtG5MfWTtcinZ3hgCnKOeQ.png" /></figure><p>Now there are various note environments here, we need to select the environment that says <strong>Node.js and JavaScript</strong> so we will click on that. In the last video we installed version 20 but here is also version 20. So there is no effect whether your version is 18, 19, or 20. Basis are the same so we will select the default that is coming.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PVRl-HnsqEQm727dNV7UcA.png" /></figure><p>Now it tells do we need some additional tools for now we do not need them. We will use it later so we will click on <strong>Ok</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0BzDGUCeret3dHrAHxshvg.png" /></figure><p>As we click on ok then in this environment, it will add a code execution environment file and it will say there are some changes. So should I rebuild now?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YTvOXYq7ScBwFFgFfgHU7w.png" /></figure><p>So you press the <strong>Rebuild Now. </strong>Now your machine will be <strong>Restarted </strong>again and now we have the configuration file that I need by default. Node.js is installed in the system. So by default, we will get in the system now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bcRU4AIrfp8tPvp8xeOEfg.png" /></figure><p>But we have to learn some more new things. This might take time to rebuild. So now this time or environment has been reloaded again. Now we need to see how we can create test.js file and execute it through Node.js so our work will be done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4k-4jUOcKVD7yBvxkaDVdQ.png" /></figure><p>Now we will click on the new folder and name it 01_basics because we need to first learn basics. Now we will create a new file in the folder and we will name it text.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yn6HoGV9RLLbEuYYrm_qVg.png" /></figure><p>Now what we have written earlier</p><blockquote><strong><em>conosole.log(“Pratham”);</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q-dZSW0OBrXn_kxSU1nXaQ.png" /></figure><p>Now we will save the file.</p><p><strong>How to Execute the file?</strong></p><p>We need to go to <strong>View</strong> and then open<strong> Terminal</strong>. If you memorize the shortcut <strong>⌃`</strong> we can use that.</p><p>Now her file test.js is not available directly. It’s in the 01_basics folder, so we will write a command</p><blockquote><strong><em>node 01_basics/test.js</em></strong></blockquote><p>And we press enter we will get the string Pratham</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bcp1pPqGIELmHbrV0iYhgA.png" /></figure><p>The problem here is that we have added all this but when we go on the repository and reload it there is no file available so all the progress that we have made will go away. So now I will tell.</p><p><strong>How to save all of these files?</strong></p><p>The process to save a different from the command line and from Codespaces. We will work on Codespaces so let&#39;s learn here only. So we need to go on the fourth icon <strong>Source Control</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7gMoBtAuC88pNQWeMO9b6A.png" /></figure><p>Now all the files that are present, it will track in which file we have made changes. So simply click the <strong>+</strong> icon and <strong>Staged changes</strong> and all that you need to save.</p><p>Now all the files are ready to be saved we will write a message like <strong>container config done</strong> and press <strong>Commit. </strong>Now this is step 1 of making changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2n3g2nWtY5IyIQQIfJTb6g.png" /></figure><p>In step is to click on the three dots … and click <strong>Push.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D9pR9XfphiXDCdnfQ6bSSQ.png" /></figure><p>As soon as we <strong>Push</strong> only our file will be saved and all the files that we need to save will be in Step 1. As we have done we can go to the repository and reload it.</p><p>We will see all the files are available.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aoxdtJP9WzENtRN6ik1HTg.png" /></figure><p>Now whenever you need all the files. We can click on <strong>Code</strong> then <strong>Local</strong> and then <strong>Download Zip</strong> so you will get all the Source code.</p><p>Now behind the scenes, I will add some changes in the dev_container fight, there will be not so many changes and I will install some extensions. So I will add the configuration files in my repository and if you need the same file then come to my repository open it copy all that is in the file and paste it into your file. So after that, you know all <strong>Commit</strong> and <strong>Push.</strong></p><h4>GitHub Link:</h4><p><a href="https://github.com/chouhanpratham/js">GitHub - chouhanpratham/js: A code repo for JavaScript</a></p><p>Now more thing we have done all our work so you will simply close the tab and go.</p><blockquote><strong>But the machine that we have started is still not off.</strong></blockquote><p>By default GitHub will turn off the machines that are not active but if you have to save your hours.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rxjzq3J9nrGH8vNjLKNckQ.png" /></figure><p>Because per month we get a limited amount of hours to simply save our hours we need to click on Codespaces and the machine that is running you should delete it, don&#39;t worry your code files are already been saved and you can go on repository link and can spin a new machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1v4PQfVgrCRpGO3UUXePiQ.png" /></figure><h4>Notes PDF Link:</h4><p><a href="https://drive.google.com/file/d/1tW9qSy2DSYlI4nC0oAjer2tR5ODxl-SM/view?usp=sharing">Javascript Day 2.pdf</a></p><h4><strong>Conclusion:</strong></h4><p>So this is how we will work in an online environment. We have a benefit online because of all the code files and resources we can get easily.</p><h4>Acknowledgment :</h4><p>Thanks to Hitesh Choudhary Sir.</p><p>If you like my work, please like, follow, and share.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b9ab8c860b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DAY 1: JavaScript Journey]]></title>
            <link>https://medium.com/@prathamchouhan1/day-1-javascript-journey-c487b6df5673?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/c487b6df5673</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vscode]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Tue, 22 Aug 2023 11:43:27 GMT</pubDate>
            <atom:updated>2023-08-22T11:44:27.347Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be documenting learnings from the Javascript Course by Hitesh Choudhary Sir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*06rgefWXRuggZdtmJwnFMg.png" /></figure><h3><strong>Setting up an Environment in the local machine for JavaScript</strong></h3><p>Today we will start our JavasSript series. We will not only discuss a lot about theory, but we will learn by coding. There will be theory but only sufficient enough.</p><p>Today we will start with a very basic and important step. So follow along with me, so with this your concepts will be cleared.</p><blockquote><strong>What mindset we should keep when we learn JavaScript?</strong></blockquote><p>And this would be very beneficial later on, we will discuss the environment, and how to establish the system where we need to write code.</p><p>So let&#39;s get started.</p><p>First of all, we will make a new folder on our laptop or desktop. It doesn’t matter whether you are on MAC, Linux, or Windows. This is JavaScript and it will be executed on the browser, which is the same for all.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4gL-rE3peCXgtL2UJehZwg.png" /></figure><p>And we will name the folder JS. Now we will open our editor. I will also tell you the choices of Editor, just wait for a while.</p><p>And we will now open the <strong>VS Code (Visual Studio Code)</strong>which will be our editor and you will get many other options. And we will simply drag the folder and drop in the VSCode. So while doing this the whole folder will get opened here.</p><p>And now we will make a new file and we give this file name <strong>test.js</strong> and in this, we will write a small step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6YRspWynnvDqtOnjKEIzAA.png" /></figure><p><strong>console.log(“Pratham”);</strong></p><p>Now the most important question here is not How to execute the file? while the question here is.</p><h4>What’s the difference when we write a .js file? Why are we not writing .txt? What will be the software that would be able to execute it?</h4><blockquote><strong>So let’s discuss suppose a text file whose extension is .txt and the other .js file they do not have a major difference. Both can be treated as source code.</strong></blockquote><p>Now this is a very controversial statement that both .txt and .js we are comparing both. But in a true sense, that’s what it is.</p><p>In .js also we have some text and in .txt also. But in .js the text that is written is not some random text. Here we have specially used a word <strong>console</strong>.</p><p>What is the special meaning of a <strong>console</strong>?</p><p>If we do <strong><em>consol </em></strong>then it will have no meaning or some different meaning since spelling is incorrect. So the source code we write has some special meaning. First console the dot then log then in parentheses and double quotes and text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lLfcOn58H6ZVr3CWYzt5hw.png" /></figure><p>This is a structure or <strong>syntax </strong>of the programming language. And any programming language has some extensions related to it.</p><p>Like .js for JavaScript, .py for Python, and others as well. Till now what we have written there is no difference between a text file and a js file.</p><p><strong>Difference between both files is :</strong></p><blockquote><strong>Which file we can inject into the software and can execute it?</strong></blockquote><p>Now there are various types of software like compilers. interpreters but all of these software have the capability to understand the structure of syntax.</p><p>So whenever we write console it is already defined in a file. If we want to see the extension in the file .js or .py then we can accept that file and whatever is written in the file like main or console them.</p><p><strong>What do we need to do on console?</strong></p><p>From the console, we need to take input or process some data or we need to show some data in output. All these happen in that software., which is sometimes called a <strong>compiler or interpreter</strong>.</p><blockquote><strong>Now for years in JavaScript, this software was hidden in the Browser.</strong></blockquote><p>So the syntax that most of the people who teach, first we need to make an index.html file and then we need to add a script and then we will make a JavaScript file and execute that.</p><p>Because for many years that software wasn&#39;t easily available. It was available but to configure it and setup was a lengthy process. So most people don’t do it at that time.</p><p>So for years, you got the software on browsers like Firefox, Chrome, or Safari we write in index.html file and add script and run it from that only. Because that software was attached to the browser.</p><p>But in recent days, the software that was in the browser was separated, and whoever wants to execute the JavaScript can do. So for this reason, JavaScript is now not only the <strong>frontend</strong> language but is used in the <strong>backend</strong> also, in mobiles and pretty much everywhere. And that was the reason for JavaScript to come alone.</p><h4>Now let’s talk about the Environment</h4><p>The way we see C, C++ environment which we download, install in the same way we have JavaScript environment available. Now there are lots of environments the first one is <strong>Node.js</strong> which is the most popular.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bXELHYOlOOWhva_WsL9qKw.png" /><figcaption><a href="https://nodejs.org/en"><strong>Node.js Website</strong></a></figcaption></figure><p>Node.js will be able to execute our JavaScript files. It is very easy because now we don’t have to make index.html file and then learn JavaScript. Earlier it was taught in this way but in 2023 we will use Node.js.</p><p>So our environment is Node.js, it’s not that the software that was in the browser was always Node.js, different types of execution engines were there.</p><p>Like the V8 engine and many more, one is Node.js similar to Deno.js. Both Node.js and Deno.js are of the same founder Ryan Dahl.</p><p>So we will download the Node.js, there are two options available.</p><ol><li><strong>LTS (Long-Term Support)</strong>: If I am working on a production-grade environment then we will choose LTS.</li><li><strong>Current</strong>: The second one we can install to check the new features.</li></ol><p>And we will install the Node.js and we will get the license agreement and we will agree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtdCsKJzvTaEjUxvJhXlPg.png" /></figure><p>And that’s it we have installed Node.js on our laptop or desktop. We can also download Deno.js in the same way. But most of the software development cycle use Node.js so will also use that.</p><p><strong>Where should we write code?</strong></p><p>The most popular tool in the market is Visual Studio Code (VSCode) although it is from Microsoft still, it’s everyone’s favorite.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NDC6VV3y2oM_jMVvof9FFA.png" /></figure><p>One that is becoming popular is <strong>JetBrains Fleet</strong>. It is in public Beta, there are no add-ons and plugs available right now. But it’s good and it might be possible that in future everyone uses that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nBfNKXjOZX8XVKe2BZVHrg.png" /></figure><p>Another is <strong>Sublime Text </strong>so whatsoever you choose but choose a <strong>code editor</strong> only.</p><p>The benefit of using a code editor is that the color console that we get, the color corrections, and auto-indentation, help in writing the code and that&#39;s why we use them.</p><p>So I hope that you have understood when the file is in .js or .txt then as soon as we put these files in any software then the software will say.</p><p><strong>No, I will not take the .txt file I will take the .js file</strong>.</p><p>And in that also, the tree parser that we have will have some keywords that it knows. Like console, log, etc. So it will execute that.</p><h4>How to execute the code?</h4><p>Now to execute this is very simple. We will go on view and then select the Terminal to open it. We can also memorize the shortcut: <strong>⌃`</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v9yW1_ApKHZizbr57cRFfA.png" /></figure><p>And our terminal will open and we simply need to write:</p><blockquote><strong><em>node -v</em></strong></blockquote><p>So we can know the node has been successfully installed or not, sometimes you need to restart your computer.</p><p>If we get some number as the output that means our work is done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XzdFKERWsEYccyjVAvVq8g.png" /></figure><p>Now we need to write, node followed by filename with .js extension:</p><blockquote><strong><em>node test.js</em></strong></blockquote><p>So it will take all the input console.log(“Pratham”);, and the string value “Pratham” will be printed at output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uFrkbABrZ8q1NpaR2L9w1A.png" /></figure><h4>Notes PDF Link:</h4><p><a href="https://drive.google.com/file/d/1XA8dyC6_AAaZ0svw5-raEHjWbNAQ0CLy/view?usp=sharing">Javascript Day 1.pdf</a></p><h4>Conclusion:</h4><p>So this is the basics about how JavaScript works and how we don’t write in the browser nowadays, or in the console these days.</p><p>This is one way in which we can execute our code. In the next blog, we will learn how to use another way that is even more simple than this and will help you to make your portfolio we will do all the work on the browser, not on the console there are other ways also available.</p><h4>Acknowledgment :</h4><p>Thanks to Hitesh Choudhary Sir.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c487b6df5673" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does a Browser Work?]]></title>
            <link>https://medium.com/@prathamchouhan1/how-does-a-browser-work-b15b0322950e?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/b15b0322950e</guid>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[rendering]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[network]]></category>
            <category><![CDATA[js-engine]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sun, 20 Aug 2023 12:08:53 GMT</pubDate>
            <atom:updated>2023-08-20T12:12:23.413Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QOe6APt8hNfpXPxkmIrmpw.png" /></figure><p>Today we will see how the browser works. This article will talk about the inner engineering side of the text and the most common things that you’re using and before that, this article is the essence of teaching and understanding those points and getting those wow factor that oh this much happens behind the scene.</p><p>In this I’ll walk you through what happens in the inner side of the browser, this will not only help you to understand but also appreciate the technicalities that happen behind HTML, CSS, JavaScript, and even React and you’ll be able to mark yourself a better developer because you understand what’s happening and how the things can be made more performant optimized if you know this much of inner details, not only that we’ll also talk a little bit about CSS and the C++ how they are interrelated.</p><p>Because HTML is one of the most fascinating things I’ll make you that fascinating thing, I’ll share that with you as well.</p><p>I’ll also discuss briefly what the browser is and how you should approach the browser side of it and little details.</p><p>Let’s go ahead and start the journey. First I really think that people need to appreciate more about what HTML is.</p><h4>Q. What really HTML is? How much impressive this is?</h4><p>A lot of people think that because I’m writing HTML just I’m saving a document with the dot HTML(.html) extension and I’m writing an H1 tag or P tag and that’s how a text is being displayed on my web browser.</p><blockquote><strong>I think you couldn’t be much more wrong than this.</strong></blockquote><p>HTML is a no powerful programming language even the internet can debate about that it’s not even a programming language.</p><p>If HTML is not even a programming language when did we give it so much power that it can convert into your browser which is in itself an <strong>operating system</strong> almost a full-blown operating system and it can render the entire pixels for us.</p><p>Don’t you find it fascinating it has no memory control, it cannot control your processors, your LEDs, and still this small language can display just a text on the screen itself.</p><p><strong>When did this language got so much power?</strong></p><p>The truth is no this language didn’t got that power but technicalities are that you never write HTML. HTML is just a parser on top of CSS that you write and CSS is still responsible, C++ is responsible for rendering out the elements. I’ll walk you through what that is because that’s a really long and bold statement to put out on the internet that<strong> “technically you don’t ever write HTML”</strong>.</p><p>Yes, you write HTML but that’s actually an abstraction layer on top of it. In the real world engineering still those hardcore languages like C++ and all of them work behind the scene. So this is already fascinating and I’ll walk you through with that.</p><p>First, let’s understand the browser part of it because that’s where you will be finding a lot of things in much more detail about how that is.</p><blockquote><strong>First of all stop looking browser as just a piece of software that’s not the case anymore. Browser is far more of an operating system these days it’s a full-blown system these days.</strong></blockquote><p>It would not be wrong to call it almost an operating system. It’s that powerful it has so much more capabilities in fact I’d like to walk you through some of the capabilities of how that actually goes so.</p><p>So we are on this browser behind-the-scenes details. So let me first walk you through what the browser is these days if you want to really understand the browser always take this big picture. If we are talking about the browser let’s just assume this big thing is your browser.</p><p>The first and major portion of your browser is actually kind of consumed in red portion so this portion is majorly your <strong>data portion</strong> or you can say data persistent portion. Now data is different types in the browser itself it could be your cookies, it could be your local storage these days even you can hold your javascript directly into here, save it and run it all the time. So all the data that you see actually goes in here, so that’s your data portion of it.</p><p>Now on the very top of this model, actually this is the layer where you see most of the things and this is the part where most of your interaction will happen and you are going to see the UI. Majorly whatever you see or your the interface is actually what you see here so this is your interface.</p><p>So all those beautiful videos, the images, and the text that you see this is the layer only in which you interact. But behind the scene there is a ton of thing that goes around onto this one.</p><p>One major portion of it that actually constitutes all of the things is actually if I go ahead and work on with this one this one is actually your <strong>Browser Engine.</strong></p><p>Now this browser engine is obviously different but don’t get into the debate of how the browser engine is different.</p><p>It might be different for Chrome, for Safari but ultimately their core working is mostly the same it was never changed. All those people have adopted different versions of it but in reality, the core working part cannot change it otherwise the entire internet will get changed.</p><p>So the browser engines are mostly the same. These are the engines that do a lot of magic. The browser engine is actually divided into two portions of it or you can say two part of it.</p><ol><li>The first portion is all about <strong>rendering</strong> and that’s where the majority of the magic is and that’s where we’ll be talking majority of it.</li><li>The other part of it is your <strong>JS engine</strong> and this is what you see majorly on the internet like your V8 engines, Dinos and Node and whatnot they are all that kind of a thing.</li></ol><p>So JSengine is the part we are not going to be focusing on, right now I’m more concerned about the <strong>rendering engine</strong> because this is the guy who is responsible for your HTML and CSS and even a little bit of your JS part and its optimization as well.</p><p>So yeah there’s a lot that is happening we’ll talk more about this one. But most of the people just are aware of this there is a lot more in the browser part one thing which actually fascinates me all the time is the <strong>Network</strong> part of it.</p><p>Browsers are so much capable of networking, not from now from day one. Browser is the software that helps you to talk to the internet load your HTML file, load your CSS file from the remote server, and display them.</p><blockquote><strong>Browsers need to have remarkable networking capabilities.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzy9jy10xr9rZC0cpvXW6g.png" /></figure><p>The more you are going to learn about the browser, in reality, you will be able to understand more about the networking capabilities of the browser. So that’s one portion of it.</p><p>Now not only this there is a lot more information that goes in the browser for example there are actually <strong>timers</strong> in the browsers as well which help you to do a lot of things.</p><p>In reality, your JavaScript engines or your core JavaScript doesn’t have any timers. But how do we use a set timer, set timeout, and a lot of set intervals and all these methods you are able to use these methods because these methods are actually being borrowed via API from the browser itself.</p><p>If you use them in the code node. Node also has an implementation of these timers into the Node Engine itself. That’s why you’re able to use this. So that’s already very fascinating. We’ll definitely discuss this networking part but this is how you should look at the browser now, so much capabilities.</p><p><strong>What less Browser has from an operating system?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ZEZO-FD7xfphpNEl5QzMw.png" /></figure><p>What more is there to constitute it and call it an operating system, that’s already fascinating. But now let me walk you through this was just a prerequisite part.</p><p>Yeah, that’s where things get interesting.</p><p>Now let’s go ahead and talk about the major portion, which is going to help you to understand the HTML portion of it. So the whole idea is that just tell me one.</p><h4><strong>Q. How does a browser loads an HTML and display text on your screen?</strong></h4><p>That is all we are going to understand in this. You might think that hey that’s a very easy thing but no you need to actually take a moment pause in there and understand there’s a lot that is going on in this system.</p><p>Just go on to Wikipedia and just write a simple code into your JavaScript console document.getElementByName and just write H2 there. The response you’ll get is not all the H2S or not all the arrays of H2 but rather you actually get a Node List.</p><p><strong>How does that happen?</strong></p><p>So on Wikipedia or any page whenever we get that we always and always come back with this node list. We never wrote HTML in the node list format. It’s never there, it’s always the p tag, h tag, and all those tags. So there must be somebody who actually converts our tags into the node list. Yes, that is exactly where the <strong>rendering engine </strong>comes into the picture and most of the engines are written in C++. So yeah technically you are writing HTML but end of the day it is being understood only by C++ first.</p><p>That’s why I say you are writing HTML but it’s not going into the HTML format it’s actually going into the C++ format. So let’s understand the journey of how HTML works.</p><p>So a couple of steps that you need to understand the first step of any browser actually works is to simply go ahead and load a file.</p><p>Now this might include the capability of local storage or from networking but in the browser, it is the way it is being written is always about loading everything from Network.</p><p>So even your computer is almost considered as there is another computer instead of the browser treating it as a computer and it loads that file so your computer or other people’s Network computer doesn’t really matter.</p><p>The whole idea is to a load file, but there are a couple of things you need to understand.</p><blockquote><strong>The job of a browser is actually very simple, is to display the data that is your first point and the second point is to allow you the ability to interact with it and these are complex applications.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VejnXyZ73FZnlTW0hRleWw.png" /></figure><p>By the display, I really mean to say that you will see the image first then a heading first, and then a button, and the ability to track those interactions that I clicked on the image or the text or the button that is where the complex part starts, but the display part is equally complex.</p><p>Let me walk you through that one by one, how this actually goes up here, and again the majority of the work is actually done by the <strong>browser rendering engine.</strong></p><h3>Step 1: How your browser will be able to load file?</h3><p>When the browser loads any file or even any engine loads any file what it sees is just the raw data, so simply go ahead and draw bytes that’s all it is.</p><p><strong>What do you mean by raw bytes?</strong></p><p>Raw bytes that’s how a computer stores your data zeros and ones it doesn’t understand you have saved the file in the HTML format you have written some characters into it the computer doesn’t understand it.</p><p>So step one is always<strong> to get the raw bytes</strong> that’s how all the computer program works, once those raw bytes are there then the step two.</p><h3>Step 2: Conversion</h3><p>Convert these raw bytes into a sequence of characters.</p><p>Yeah, that’s where the conversion of the character happens, and this is where actually you tell your file that is your HTML you define my character sequence should be UTF-8 or there is always a default, but you tell them that the characters in which you should convert my raw bytes is in Chinese, it should be Japanese, it should be Hindi, it should be English.</p><p>So those character sequence, you have to tell it and that’s why I say it’s really important information to give the browser to actually understand that how this is happening.</p><p>That’s your character encoding of the HTML file and again this is something interesting. Once this character sequence is done every single programming language does this and HTML is no exception in that.</p><blockquote><strong>That’s why a lot of people debate whether HTML is a programming language or not.</strong></blockquote><p>Then the next step is to generate a token out of it.</p><h3>Step 3: Tokenization</h3><p>Then the next step is to generate a token out of character sequence and the process in detail is known as <strong>tokenization.</strong></p><p>This tokenization means that there are some words that I need to pick it up from a programming language perspective it’s like selecting words like if, else, for, while, and similarly in the world of tokenization of characters, it actually picks out all the words like for example when you say h1, when you say paragraph p, html, and body all these things are actually taken up and taken up separately in the memory that’s your process of tokenization.</p><p>That’s always a step. Okay, loaded the file, and converted it into raw bytes that’s why it is it was saved on the disk and then converted into a character based on the UTF information that you shared then converted into tokenization.</p><p><strong>What happens after the tokenization?</strong></p><p>That’s where things get interesting because after the tokenization a couple of things you need to understand and you will get a lot of information out of it as well.</p><p><strong>What is HTML?</strong></p><blockquote><strong>HTML is a document that’s how computer terminologies talk to it.</strong></blockquote><p>That’s one thing because you’ll be understanding something really important with this so just keep that in mind. Next up is that once we have done the tokenization we need to put a structure around it, just like when you save any data in your Excel sheet or your database you need to create a structure out of it if you don’t have a structure we really can do much so a big object yes literally a big object is created out of these tokens and there’s a lot of information in these token about each entity.</p><p>For example, when you say just h1 tag so first of all it says, what tag it is so it’s an h1 tag. Does it have any title into it? Because you can put some parameters there or props properties so it has a title it says “sum”. Does it have any data inside it so then it says data or sometimes it’s known as <strong>value?</strong> What’s the value inside it? So the value is “Medium”.</p><p>So a whole object is actually created out of it and there is a proper structure of how this object needs to be there so every single entity. Now Imagine this was done for h1, a similar kind of object will be done for paragraph p , similar kind of object will be done for the body.</p><p>That’s a long process, it’s really a long process so everything is actually converted into an object and that’s where our next-term logic comes up into that so everything was converted into an <strong>objec</strong>t.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z-fr1eW0AxxP13rvgr25pQ.png" /></figure><p>But having these objects scattered around through the places is not really useful at all even a tiny bit useful we need to arrange them with proper relation, for example, somebody needs to be a sibling somebody needs to be a parent there’s a lot of things that need to go around and that conversion of these objects into a structure that’s what we call a <strong>model</strong> and that’s where your term actually came into the picture <strong>document object model (DOM).</strong></p><p>So you’re writing a lot of gibberish into the document we are extracting information out of it, tokenizing it converting them into an object, and then preparing a model out of it that’s your <strong>document object model (DOM).</strong></p><p>Literally, that’s how the term actually came into the picture all right so a lot of things now the tokenization has done and all of that now okay what’s the next step, because we know how the terms actually came up but this is not yet displayed on the screen. We need to establish a <strong>relation</strong> between them. So the next step is a relation is actually being portrayed.</p><h3>Step 4: Relation</h3><p>So the next step is a relation is actually being portrayed. So once this relation is done now that I know which one is a parent which one is a sibling, grand sibling, grandchild, and all of that then the next step.</p><h3>Step 5: Nodes</h3><p>The most important step is actually to convert it into nodes or a node list. Yes, a node list is converted because now in this list you actually know how the things are there so that’s what the whole idea is so for example in this node list if you go and say hey I have for example an HTML this is my HTML this will have a list inside. Inside this list or just the bottom of this list I will have a body there are other things as well but here I’ll have an h1, and h2 so these are siblings these kinds of relations or nodes are being generated.</p><p>And these are lists of nodes and this list of nodes, this is exactly being given to you by this <strong>rendering engine</strong> that’s Java, it’s not yet painted on the screen.</p><p>Yes, this has not been painted yet so far and we haven’t done a single thing on the browser screen.</p><p>Now we have done with nodes, and a node list is being generated but that’s not being painted yet. I’ll walk you through the painting process in the rendering process of it but this is all done and is kept separate and this is called <strong>DOM generation.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pcaFmtJwKWzfq622Acrq0A.png" /></figure><p>So out of this node list is generated and that’s why you see that whenever you try to access from a JavaScript all the HTML pages the node list is given to you back because that’s what it is actually going on the web page itself, not your HTML and that’s where the node list actually comes into the picture and technicality now we have a DOM(document object model) in front of us.</p><p>Just having a DOM doesn’t means we have something on the screen itself that means there is in the memory a document object model is ready but what next. There’s so much more to it so let me go ahead and work on that okay so the DOM is being created.</p><p>Next up is, we understand the HTML part.</p><p><strong>What about the CSS part?</strong></p><p>Yeah, that is even a little bit more crucial because once actually you have the CSS it’s actually not easy to go with that because the entire raw byte format is going to remain the same we have the raw data from the raw data you actually go ahead and do a character conversion to it as well so there is no difference into it.</p><p>Once the character conversion is done the same process will happen just like we saw here in raw byte character conversion the same tokenization will happen. Once the tokenization happens we also create a structure out of it so literally objects are created out of it so again the objects are created out of it and this time we know that we are actually working on CSS so out of the CSS now objects are being created and obviously these objects need to have some relation as well.</p><p>So further down relations are being set up now this time relation is a little different, but yes the relations are being created and further, a model is being established that how this should all portray the things I’ll talk about that don’t you worry.</p><p>But this is exactly how your keyword CSSOM comes, which we find so many times in the interview is <strong>CSSOM (CSS Object Model)</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_QUmdduaVi5K08cv3O5SnQ.png" /></figure><p>So there is DOM(Document Object M), CSSOM (CSS Object Model), and there are a lot more others as well. But that’s the basic idea about it. Moving further we have seen that okay somehow our DOM is ready our CSSOM is also ready.</p><p><strong>Can now we go ahead and paint the picture on the web?</strong></p><p>No you cannot what happens in the behind the scene details of it is that when you simply go ahead and do all of these things, the moment the browser sees or loads an HTML file it will start working on its DOM, the document object model.</p><p>But the moment it sees a link to your CSS it also is going to start a simultaneous or you can say parallel going to work with the CSS object model(CSSOM).</p><blockquote><strong>But these two things DOM and CSSOM are completely independent of each other.</strong></blockquote><p>They have no idea of each other as of now there are some rules that how we are going to establish the relation in it. Yes, these rules are there, these relation information are there but they’re not talking to each other yet.</p><p>Moving on further that’s where a new concept comes up further which is known as <strong>Render Tree.</strong></p><p>This tree is known by a lot of other names in different books and resources articles but there is something known as a <strong>render tree</strong> which is generated in this step now.</p><p><strong>What happens in the Render Tree?</strong></p><p>It gathers the information, what the information that we have. Yes, we</p><p>have the DOM, CSSOM so all this information is collected and given to the<strong> browser engine.</strong> So your Browser Engine is a smart guy with the maths.</p><p>After this process and in the render tree, a lot of mathematical calculation is done because your browser is 100% of the time aware that how much real estate on the screen it has smaller one, bigger one, and all of that.</p><p>And it needs to do the calculation. The CSS actually manipulates a lot of space on the screen itself, that it should be 10 pixels wide, it should be 100 pixels wide, and the image should be 20% or 30%, you do a lot of mathematical calculations and all these calculation is done almost instantaneously by the browser engine, so powerful, and that’s where you finally get your rendering tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wFBbEtCw22KLVUJFHd2plg.png" /></figure><p>Yeah, and this is exactly the moment where actually your painting starts.</p><p><strong>What do you mean by painting?</strong></p><p>See the technicalities or the technical terms or the jargon, whenever there is a web page being rendered on the browser to see it that’s what we call a <strong>painting</strong> and this is the most interesting and important part that finally the painting starts.</p><p>Now comes the most interesting thing okay painting is done and we see all the data on our browser so let’s just say this was an image this was your text here it was your text and here it was your image so everything is great. All right quite a lot of information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gwuxdNZ7Xe1FVPeMJu_DDQ.png" /></figure><p><strong>What about the JS part?</strong></p><p>We haven’t talked about it what happened to JavaScript. Here’s an interesting thing about JavaScript a lot of people don’t like it and especially this will give you help you to understand a lot of things.</p><blockquote><strong>The moment your browser sees a script tag no matter where it sees it will stop executing the DOM, CSSOM, and whatever the work it’s doing the first preference always go to JavaScript.</strong></blockquote><p>There’s JavaScript so it has the capability of manipulating my DOM, it has the capability of manipulating my CSS as well so what’s the point of rendering the entire thing if JavaScript is going to manipulate that?</p><p>So the browser first collects all the information from the JavaScript and then goes ahead and does the rendering part of it and that’s why you see and hear the words like <strong>hydration </strong>because we’ll pass on the JavaScript at the very end in some of the frameworks latest ones like Remix and all of that and one of the problem which everybody faced in the React, was first and foremost all the JavaScript is shipped to the browser that means nobody will be able to see the website until less all the JavaScript is being shipped because once it’s shipped browser will do it immediate calculation and will portray the picture.</p><p>But the idea should be that first and foremost the priority should be that HTML and CSS should reach the browser so that start doing the painting work and at the end of it the JavaScript should reach it so that now we can track the interactivity part of it, that’s interesting.</p><p>But there is even one more interesting part on top of this, notice I have talked about the <strong>relation between uh the JS and the DOM</strong> and we know that if your DOM is there and even somebody encrypts a script tag that means your DOM execution will be halted until your JavaScript execution or its reading part is complete but there’s another side of it.</p><p><strong>What happens with CSSOM and JavaScript?</strong></p><p>Here comes the crazy part this is a little bit hard to believe all right so the part we are talking about is what happens if the CSSOM is not yet ready. and we have hit the JavaScript tag yeah this is where I want to bring your attention that your JavaScript execution will be halted.</p><blockquote><strong>A lot of people think that hey no matter what happens your JavaScript execution will not stop it always gets priority but no your CSSOM even gets more priority and even your JavaScript execution is going to get halted until and unless your CSSOM is ready.</strong></blockquote><p>So there are a lot of things that are going on so first of all your document object mode(DOM) if it hits before building the entire DOM it hits the JavaScript it’s going to say <strong>first I’m going to take care of the JavaScript</strong> and even if it hits at that time as CSSOM it’s going to say <strong>I’ll stop everything</strong> so CSS can be sometimes a bottleneck to actually increase the performance of your application.</p><p>Now JS execution will be halted until CSSOM is ready to portion is changing drastically with the libraries and frameworks we are trying to push in a lot of details that DOM should be a priority, then the CSS should be a priority and JavaScript should be lost and a whole bunch of other things.</p><p>This portion is debatable a lot of details and talks and research is needed on that I’m still doing that to actually properly understand this but yeah this is what I have found about all the documents I’ve read, the blogs I’ve read, and trying to understand the browser working as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S93DkDSBTHBKmSkl2n3rZA.png" /></figure><p>There’s a crazy now there is also one more thing you will hear about this one is <strong>deferring the JavaScript. S</strong>o by default whatever you do in the world of JavaScript, they are not deferred but you can actually use the keywords like <strong>async</strong> which actually helps you to defer your JavaScript and load it at the very end of the page. Although it might be marked at the top of the page but yes you actually are able to understand this now that I can mark my JavaScript as async load that script at the very end of this whole thing I’m doing because I know how the painting is entirely being done.</p><p>So yes it’s a really long detail.</p><h3>Notes PDF Link:</h3><p><a href="https://drive.google.com/file/d/1WcmIxe_JHjfQ5Q6hYTGXM4IUrBRMFzMB/view?usp=sharing">How Does Browser Work.pdf</a></p><h3>Conclusion:</h3><p>So I hope you understood this part there were a lot of inner details about “<strong>How the Browser Works?</strong>”, the inner detail hope it helped you to understand a little bit about the HTML part, the CSS part, and the rendering part. How actually a C++ engine takes your HTML and portrays the pixels on the screen?</p><h3>Acknowledgment :</h3><p>Thanks to Hitesh Choudhary Sir.</p><p>So this is all about the details that I wanted to share about Browser. I you like it please share, comment, and follow.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b15b0322950e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to design a Database for a Project?]]></title>
            <link>https://medium.com/@prathamchouhan1/how-to-design-a-database-for-a-project-c470a113b745?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/c470a113b745</guid>
            <category><![CDATA[realationships]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sat, 19 Aug 2023 13:48:19 GMT</pubDate>
            <atom:updated>2023-08-19T13:48:19.865Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the most commonly asked questions in job interviews. So today we are going to discuss designing Database Schema in the context of a Relational Database Management System (RDBMS).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*JiWKgKK8_2_tzXgy.jpg" /></figure><p>So this is the problem statement.</p><h4>Problem Statement:-</h4><blockquote><strong>To develop a survey application for a product company.</strong></blockquote><p>So the biggest mistake you can do in an interview when somebody gives us a problem statement like this is if you start writing tables or writing columns or relationships between tables straight away as soon as you get this one line.</p><p>Remember that popular saying:-</p><blockquote><strong>The sooner you start coding, the later you finish it.</strong></blockquote><p>It is so much applicable when it comes to database designing also.</p><p>Your tables, the relationship between your tables the constraints such as primary key, uniqueness everything is governed by the functional requirements of the application that you are designing.</p><p>So we cannot basically start designing tables or databases without understanding the requirements in detail. So the first thing we need to do when we get this generic problem statement is “<strong>we</strong> <strong>need to ask questions</strong>”.</p><h4>Q. What is Data Modelling?</h4><blockquote><strong>Database has to be designed and not just created.</strong></blockquote><p>We know making a database is like getting tattooed it’s very very tough to correct our mistakes so it’s very important that we get it right while doing it the first time. So don’t just jump into creating tables and columns and keys think about it think.</p><p><strong>Think about the problem that you’re solving. Think about the application that you are building.</strong></p><p>NoSOL is better in this regard with its flexible schema but it can’t be used to solve every problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*taS2RiF-JScBZ3UBvQRphg.png" /></figure><p>We are trying to create a survey application for a product manufacturing company. Now as I said don’t jump into database modeling yet. This vague problem statement is clearly not enough to start designing your relational database management system(RDBMS).</p><h4><strong>Q. Is this enough to start designing your RDBMS?</strong></h4><p>Ask questions, and get more clarity. Don’t rush into I will have table number one, table number two, index number. Don’t do that ask questions.</p><p>Follow the protocol, ask questions, and get clarity on the requirements. And then follow the following steps:</p><h3><strong>Step 1: Defining the purpose of the DATABASE</strong></h3><p>Number one is defining the purpose of the database, this is important to have the larger picture in mind. Before we get into the nitty-gritty or fine details of you know tables and columns and primary keys and foreign keys and everything we must have the larger picture, we must know why are we why are we creating this database.</p><h4>Q. What is the reason for the existence of this project?</h4><p>For example: In our survey application the company wants to conduct this survey to have business intelligence or maybe just to keep historical records but you must know what is the purpose of this application.</p><p><strong>Q. What is the owner set to gain from the project?</strong></p><ol><li>To choose DB system.</li><li>To optimize the DB system.</li></ol><h3>Step 2: Listing down all the major Functional Requirements</h3><p>Number two is we must list down all the major functional requirements so for demonstration purpose here I will list down four of them.</p><p>Survey application requirements for a product company:-</p><ol><li>A survey should be or can be reused for multiple products.</li><li>A user can only take a survey when he or she purchases a particular product.</li><li>The survey can have three types of questions, one is a subjective question for example what do you think of our payments service, there could be single choice objective question where you have four radio buttons and the user has to click one of them or there could be check boxes so multiple options and multiple possible responses so we have three types of questions.</li><li>Surveys shall collect product feedback about the company as well as some useful user demographics information.</li></ol><p>Once I have the functional requirements with me move to next step.</p><h3>Step 3: Listing down the Entities to identify Tables</h3><p>Now based on your functional requirements just list down the entities don’t worry about which of these entities will become your tables.</p><p>The entities for example from the requirement it seems there will be:-</p><ul><li>Company</li><li>Entity</li><li>Product</li><li>User</li><li>Question</li><li>Answers</li><li>Response</li><li>Report</li></ul><p>Once you’ve listed down the entities, you need to identify which of these entities will be your tables.</p><p>For example: Report here in our case doesn’t need to be a table.</p><p>Report is we’ll put some SQL join queries and we will get some data from other tables and probably create a PDF document. I don’t need to create a report table because report requirements might change and I do not want to lock down my report requirement.</p><p>The rest of them could be our tables all right. So once we do that let’s move on to next step.</p><h3>Step 4: Deciding the Relationships between Tables</h3><p>Functional Requirements and normalization techniques will dictate the creation of relationships between tables. Some tables will be created just to map the relationship.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*atdKktiy6FEq4vyx1ZwVvw.png" /></figure><ol><li>A company has many products.</li><li>A survey is used by one or more products so surveys are reusable.</li><li>A survey is taken by 0 or more users.</li><li>User response to a survey for one product purchase so the user will respond to a survey in the context of a product.</li><li>A survey has many questions.</li><li>That goes without saying a question has zero or more answers now in the answers here are basically choices, so if a question is a subjective question there are no answers there are no choices so it will have zero answers in that case if a question is a multiple choice question like our radio box question or checkbox question it will have few answers.</li><li>A survey for a product will be responded to by a user.</li><li>Each question from a survey will have a response from a user.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nsdgDHVNYRwYjwyxV_wIjA.png" /></figure><p>So we have created relationships between tables using functional requirements. And this is very important because this will dictate the creation of primary keys and foreign keys.</p><h3>Step 5: Deciding the columns, keys, and constraints</h3><p>Some fields could be unique for example email ID of a user has to be unique so that we have you know unique users responding to surveys. We want to generate reports about user demographics so you cannot make the country column of the user as nullable column it has to be NOT NULL.</p><p>All these things will again come back to your functional requirements. Columns will be based on functional requirements.</p><h4>Constraints:</h4><ol><li>Primary Keys</li><li>Foreign Keys</li><li>Uniqueness</li><li>NULL/ NOT NULL</li><li>Cascade delete</li></ol><p>Now we know all the steps that need to be followed in the database design process.</p><h3>Data Modeling Process Steps Nutshell:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WLwPCasAbwB56qpNJdiv2g.png" /></figure><p>Let’s move on to the final schema that we are gonna created. So our first table will be the <strong>product</strong>, and we will have columns such as ID, product name, category, and company ID because every product will belong to a company.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*2-Elq0t-ree5Zd0Bj_G7vA.png" /></figure><p>We start this application with just one company but tomorrow we may want to extend this application to other companies as well. So it’s important to have one more table called as a<strong> company</strong> and we’ll have columns such as ID which is a primary key and then name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*nLKdYhq3Y3BWb0K9O98z0A.png" /></figure><p>Now one company has many products because the product table has a column called company ID which will be a foreign key.</p><p>The<strong> user</strong> table will have attributes or columns such as ID, name, email, age, and country. Now you remember what was the larger picture the purpose of our database to get some reports based on user demographics so which dictates the column that we choose in our user table so probably we would like to add one more column such as <em>gender,</em> if the business owners want to know the statistics such as how many female users are purchasing one particular product then probably this column is required so the choice of columns the selection of columns is also dictated by the functional requirements as well as sometimes the larger picture that is the purpose of the database in our case it was business intelligence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*NZgZAqtuqDBUSexpAA0r9w.png" /></figure><p>The next table that we will need is <strong>survey </strong>of course ID will be there, we will give a title and that’s about it so remember that here everywhere ID is the primary key in all these tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/1*wfdEkC3TflrVVpm2ipHYNA.png" /></figure><p>Now a survey will have questions so the next table is <strong>question </strong>it has ID as the primary key, type so the question can be of type let’s say we’ll give them some values 0, 1, or 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*NIF0XOecnMXlGvubgNmjyQ.png" /></figure><p>0 for the subjective question, 1 for the radio button question, and 2 for the checkbox kind of question, where you can choose multiple answers from the choices. Next, every question will have a text like, What do you think about our payment process or What do you think about our delivery process something like that, every survey will have questions so the question table will have a foreign key survey_ID that will be established relationship between survey and question now in case of objective questions, we’ll need to have another entity or table called <strong>answer </strong>it will have columns such as ID primary key, text again the choice is basically for objective question let us say question has four choices user was satisfied, user was not really satisfied, the process was bad, process was good.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*0D_O8YQyI3hQs46VHYnQOw.png" /></figure><p>Something like that so that will be the choice option text and then because this is in the context of a question will have a foreign key question_ID remember subjective questions with type 0 will not have an entry in the answer table they will not have any choices they will have text, the subjective questions have to be taken care of how will user respond to a survey.</p><p>So we’re going to create a table or entity called <strong>responder</strong> this is basically a table for mapping the relationship between survey, user, and product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*YiJ1qw_VXO5s_4_ShrTm2Q.png" /></figure><p>User_ID is a foreign key, product_ID is a foreign key, and survey_ID is a foreign key so this is basically a user responding to a survey, for a product, so we are mapping the relationship with our user when she or he purchases a product will give a survey about that product that relationship is mapped in this responder model or table</p><p>Finally, we need to store the responses of the user so we’ll have a <strong>response </strong>table and it will have columns such as ID as a primary key, then question_ID because every response will be for a question, answer_ID if it’s a multiple choice question the response will be for one particular choice, out of four options of subjective questions. We don’t need answer_ID but we need response text for example as said what we think about our payment process so in that case response will be stored in the form of this response text field and then finally to map this response with the responders will have responder_ID which will be a foreign key on the responder table. So we’ll know that this response is by this user for this survey in the context of this product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*Eb2LLL5lL_5K4ndrO2ro2w.png" /></figure><p>This is pretty much it. This is our schema for our survey application for a product company problem statement.</p><p>Let’s quickly run through some of the questions that you might have for example:</p><h4>Q. How to determine which survey to use for which product?</h4><p>For that we can store it in a separate table <strong>productSurvey, </strong>it’s a kind of pivot table we will have ID, product_ID, and survey ID so this will tell our application that let&#39;s say for product_ID one we need to have survey_ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*nZQ_cBaZKWO9oZdj4EpUNg.png" /></figure><p>So that entry will be stored here so imagine some data like this so product number one survey number one product number two survey number three something like that so that mapping of which survey to be presented to a user for one particular product will be stored here.</p><p>Again you must be wondering but that information is already captured here in the responder table. But productSurvey is to make a decision in the application.</p><p>Because tomorrow for one particular product let&#39;s say product_id one we may want to change the survey that is being used today we are using survey number one for product_ID one but let’s say we are changing requirements so we want to give survey_ID for product_ID one.</p><p>So we can make the change here in this table and accordingly in our application when the user is asked to give survey for that particular product we’ll choose that survey from this table.</p><h4>Q. When the product changes the survey it is using, then what happens to old entries?</h4><p>When the product changes the survey that is using product_ID two is using survey_ID number three what if I want to change for product two I want to use new survey_ID let’s say 15 now what happens to the old entries where users have already responded to survey_ID three for the purchase of product_ID two they already responded here we have noted them so even if we change this mapping that instead of three now product_ID two will use 15 number survey.</p><p>We still have those old entries here because in earlier times any user for product_ID two was using survey_ID three right so all those old responses will still be intact even if you change this mapping to survey_ID fifteen this is the flexible design, in that regard next question would be.</p><h4>Q. In the response table why do we have question_ID when we already have the answer_id?</h4><p>The <strong>answer</strong> table already has the question ID. So why are we having question_ID and answer_ID both in the response table?</p><p>If we have the answer_ID it will automatically get the question_ID right but again that will be short-sighted because remember for subjective questions that are of type zero we did not have an entry in the answer table because subjective questions do not have choices these are basically this table is four choices right for objective questions for our subjective questions there will be no entry here so in that case, if you do not maintain the question_ID in the response table for that response to a subjective question you won’t know for which question it is correct.</p><p>So it is important to have both question_ID for subjective questions and answer_ID for objective questions in your response table.</p><p>So now before we conclude we must think about a few things.</p><h3>Reusability</h3><h4>Q. Is this design reusable?</h4><p>One survey could be used for multiple products, just by maintaining this mapping so surveys are reusable.</p><p>As we saw this whole application right now we are using for one company but because we have this company_ID foreign key in the <strong>product </strong>table tomorrow we can use the same application for multiple companies. So that the entire application is usable.</p><p>One more possibility of reusability here is of questions. We can reuse the questions across different surveys right now we don’t have that provision but we can totally extend that by adding a small pivot table called a <strong>surveyQuestion</strong> so it will have two foreign keys basically survey_ID and question_ID. So we can say survey_ID number 1 will have question_ID number 1, survey_ID number 2 will have question_ID number 1, and survey_ID number 3 will have question_ID number 1 so we are reusing the questions so we can determine which survey will have which question and then simply we can reuse the questions in you know multiple surveys so you can totally do that by extending this design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*MicqUCp4KnA5OLKpkXn1Ig.png" /></figure><p>So again this design is <strong>extensible.</strong></p><p>If you want to make that change we can also go back to listing our functional requirements and we can just do a quick walkthrough of whether all these requirements are met through our design or not so once you do that the last step.</p><h3>Last Step: Draw the ER Diagram for the schema</h3><p>We need to create an artifact of this design using some notation called an <strong>ER diagram</strong>.</p><h3>Notes PDF Link:</h3><p><a href="https://drive.google.com/file/d/1FoaSoMo9iDVAentivWholIUu0iIIUPJm/view?usp=sharing">DBMS How_to_design_Database .pdf</a></p><h3>Conclusion:</h3><p>Designing a database for a project requires a holistic approach that takes into account the project’s unique requirements, scalability, security, and performance needs. By carefully considering these factors and following best practices, you can create a well-structured and efficient database that effectively supports the project’s goals and objectives. And we have seen the crucial steps to be taken into account while designing a database.</p><h3>Acknowledgment :</h3><p>Thanks to Kaustubh Joshi Sir, BBarters Youtube.</p><p>If you like my work, please like, follow, and share.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c470a113b745" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unveiling the Veil: How Node JS Works?]]></title>
            <link>https://medium.com/@prathamchouhan1/unveiling-the-veil-how-node-js-works-3465a89b4eb3?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/3465a89b4eb3</guid>
            <category><![CDATA[client-server]]></category>
            <category><![CDATA[libuv]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sun, 13 Aug 2023 14:09:04 GMT</pubDate>
            <atom:updated>2023-08-13T14:09:04.730Z</atom:updated>
            <content:encoded><![CDATA[<p>Today we will see behind the scene basics of how Node.js works. Usually, we use Node.js for a lot of things maybe we are learning React, Node, or maybe View.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aMz139InhQPQswYe9fOlOA.png" /><figcaption><strong>Node JS Client-Server Relation</strong></figcaption></figure><p>Whenever you’re learning chances are high that you are using Node.js or any similar library for that matter, and even these days if we are learning to code JavaScript we are not learning the JavaScript in the browser itself you are using Node.js to execute that.</p><h4>What makes Node.js? How it works?</h4><p>The internal model behind it will help us understand behind the scene working of Node.js. And how powerful Node.js is. Not only that once we understand behind the scene mental model of Node.js we’ll be able to write so much better code with Node.js whether it’s a file system access or a network access the functionality is repeatable it just repeats everything exactly the same with different libraries. But behind the scene working always remains the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jw9V__6jYhm2amP74D_0lw.png" /></figure><p>Here, we are going to explore behind the scene of Node.js. How it works? We’ll be writing some portion of the code. You just need to see this is the code, this is the portion where it works and then we will break it down step by step in a diagrammatic manner that how each step is being done.</p><p>It will help us to understand a lot about computer basics as well in case if you are not familiar with that.</p><h4>First understanding the scenario. What we are trying to build?</h4><p>We are trying to build a web server with Node.js. This is the most easiest thing that we will be doing and a lot of people use libraries like express and all these other things. We are not using that I just want to show you how coldly it is done and what are the important parts and pieces of that and later on you can use those libraries and all of that.</p><p>But I want to show you how it is being done in the world of Node.js so that later on we can break it down and understand what happens behind the scene and the computer networks.</p><p>So let me walk you through that how it’s being done that’s the most important and easiest thing.</p><p>In Node.js you might have seen have libraries access so we can just go ahead and actually require these libraries. So if I go ahead and say. Hey, I want to require these libraries, a lot of libraries are available for us. So require one special library known as <strong>http</strong>.</p><p>There is a very similar library that does almost similar kinds of things we’ll talk about which is <strong>net</strong> library. We’re not interested in that. And I’ll just go ahead and store a reference const and let’s call this one as http feel free to call whatever you like “<strong>http</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sjNkOmOBaRprrYoP0BSyOw.png" /><figcaption><strong>http Library</strong></figcaption></figure><p><strong>Why and How I’m able to do this?</strong></p><p>Because Node.js allows us to have access to http library. I’ll walk you through the node documentation that is super important. So let’s just say Node.js gives us access to http, it would be difficult to say that there are modules purely but let’s just say these are labels.</p><p>Because this is something that actually can control the network of your computer and JavaScript trust me has no capability of controlling either network or file system.</p><p>JacaScript language doesn’t do that. For that, we need languages like C or C++. So this http label is actually holding the references of the capabilities of networking.</p><p><strong>What this http label allows us to do?</strong></p><p>This http gives us a lot of methods and capabilities. Notice here it also says the http interfaces Node.js. So yes it’s a label interface. It’s not our ordinary variable it has so much of more functionality that you can actually invoke do so much of the powerful things.</p><blockquote>In that one of the method we’ll see a lot is <strong>createServer</strong>.</blockquote><p><strong>What is Server?</strong></p><p>Server is just a computer somebody else’s computer which is up and running all the time and is listening for some request someone is sending and I’ll serve that. That’s why it’s called a <strong>Server</strong>.</p><p>It serves literally. So that’s what it has and what you’ll see inside this is, usually there is a request listener and a responder so usually this is how you actually kind of memorize this.</p><p>This is how it looks like, so there is a callback in it we go ahead and say this is a request and this is a response. I’ll walk you through what these request and response literally are.</p><p>And then we will actually store this entire thing into a variable. So this is going to be const we usually call this as server that’s how we do it and obviously this server actually lists sent to some Port.</p><p>So you can directly go ahead and say that port is either from environment variable whatever that is or maybe 4000. Whatever works for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yl0VIOpOcJO863N41ITSGg.png" /></figure><p>Once this variable port is defined. This is just the number magic that comes from the server which actually goes ahead and listens to it and pass on to the port.</p><p>This is optionally you can pass on a call on a callback that once it is listening or for the first time it is actually up and running then you can go ahead and fire up a callback and say either in curly braces or directly a console.log() that says “Server is Running”.</p><p>So this is very basic of it what is more interesting is when we created this server we can actually have access to this request and response which is the most interesting thing in the entire Node.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pzxiVrmshpB4S8nijFg0kw.png" /></figure><p>Let’s come back into createServer and you’ll notice that the response(res) as a lot of methods and the request(req) has a lot of properties.</p><p>For example, from the request, I can grab what the path user is trying to access even with a little bit of configuration I can access its cookies. Who is sending me, and at what time it was sent, headers information, browser information that’s literally so much of information. That’s all about the request.</p><p>The first parameter response is actually a collection of methods and there’s a little more of it. There are actually streams that are coming in I’ll walk you through the exact technicalities about it which you will appreciate.</p><p>For example, I can go ahead and change my status code to 200, we can set the headers. There is so much more you can do but mostly at the end of it you are going to go ahead and say end and say “Bye” something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H6a8PV2NZ_f8OF_SPJFZtw.png" /></figure><p>So this end() is actually an end of the stream that is being sent from the server to whoever is making a request.</p><p>So that’s the basic. There’s nothing too interesting here. You could have done this in one line or probably less or more, you can say better syntax by using the library like <strong>Express </strong>but that’s not behind the scene understanding of the Node.js. This is all we have done so far.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y7rdXktw4V0i0378Z3Bg-g.png" /><figcaption><strong>Running the code</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zAeSn2lyBDt-eP--9INGvQ.png" /><figcaption><strong>Output at web-browser localhost:4000</strong></figcaption></figure><p>Now I will take you back on.</p><p><strong>What is happening and How you should be learning behind the scene basics of it?</strong></p><p>We have learned how we can create a basic server that’s all given to us. But now let’s go ahead and walk through how you can actually take all of this into the documentation part.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vl3vQerao5PbYe9bz1RFPg.png" /><figcaption><a href="https://nodejs.org/en"><strong>Node JS Official Website Download</strong></a></figcaption></figure><p>So this is the Node.js page and this is a libuv. We’ll come up back on to this one, we call this as I actually not coined the libuv term it was given in the conference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RohQjAgIpp-cBo6yMF3XLw.png" /><figcaption><a href="https://libuv.org/"><strong>Library libuv Website</strong></a></figcaption></figure><blockquote>Node.js and libuv are <strong>onion layers </strong>means the more you will peel it, more you’ll cry.</blockquote><p>Not my words taken from a conference. In Node.js most of the time what people does is either choose the stability version LTS or the Current version. They always go into the API docs. No matter where your API docs go you just see that this is a buffer, cryptos, and http module, you usually store and learn from it. So we talked about http this is where I should be looking up for that, what can we do?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7J6fbwYKIRNOy-Mndox-ow.png" /><figcaption><strong>API Docs Page</strong></figcaption></figure><p>I can access requet.method, request.path great way. I’m not saying that you are doing wrong. That is one of the great way of learning but you missed that nobody told you.</p><p><strong>How you should be learning Node.js documentation?</strong></p><p>First, before learning about it modules that are given to you. Yes, these are modules so you should not be learning from them like, you should be learning from but not from the very basics.</p><p>Go onto the home page and look at the top this is the DOCS you should be exploring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*teZHHPT-Sdj0cDqDB5gUsA.png" /><figcaption><strong>DOCS at the top bar need to be Read</strong></figcaption></figure><p>Let me walk you through how to explore this. So click on the DOCS and this is where all of the references guide ES features and everything is available.</p><p>This Guide is the least read by most people even though I’ve seen the level one, level two engineers don’t read about it.</p><p>You are blessed and I have my own time to read about it and that’s why I going into it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_O8IVJX40NvxWHGj8rqdPQ.png" /><figcaption><a href="https://nodejs.org/en/docs"><strong>DOCS Page</strong></a></figcaption></figure><ul><li>Go into the guide section and this is where you see the General guide, the Node.js core concepts, and the Module-related guides. What I have found is there are some people who love to learn and study about it. They want to learn about the Node.js core concept so they’ll go into this guide.</li><li>Again going into this I found it’s not really ideal to learn directly as there are little higher-level concepts.</li></ul><p>The guide which I actually loved is the <strong>Anatomy of an HTTP Transaction</strong>.</p><p>So they literally dissect each part of what happens in the HTTP request, there is so much packed knowledge here once you understand this piece of information. Then you understand everything how file systems are accessed, how crypto modules work everything is Node.js.</p><p>Because trust me your JavaScript is not capable. JavaScript is not at all capable language but it’s the Node.js APIs that make it so much capable of doing so many things.</p><p>So if I go ahead and click on they have actually broken the concept line by line and a couple of points which I would like to mention here is I would like to mention here is first of all notice what it says. <strong>We also assume a bit of familiarity with Node.js Event Emitters and Streams</strong>.</p><p>Yes, you should have but don’t worry I’ll explain to you what that is in a brief so you don’t have much of a requirement. They have broken line by line.</p><p><strong>How the createServer works? How the methods URL headers and all of that?</strong></p><p>We will not go into the request body because that’s also another beast to actually discuss. We will talk about the basics of :</p><p>How the response headers are being sent? How the general connections is being done? Where does this libuv actually take part?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23ksFMqrasZmGnL0fF5oxA.png" /></figure><p>So, this is really very interesting I’ll walk you through it. So, this portion where I’m talking about all of my references and notes, obviously it takes a little bit of interpretation. Now one thing you need to understand.</p><p><strong>How the server-client relationship works and where your computer comes in? Where the Node.js comes in?</strong></p><p>I will use the basic diagram for this one. Let’s just say that you have a client. These days every request comes from a mobile so we’ll say this is a mobile. And the whole big block is your server. Yes, that big of stuff is your server.</p><p><strong>Why a big server?</strong></p><p>Servers are not really that big they are just a computer that is up and running all the time but since there’s so much concept going on that’s why I need the bigger diagram for the server.</p><p>So let’s just say there is a request that comes up so usually the request that comes up to any computer or any kind of server is going to come up in <strong>Port 80</strong>. It&#39;s usual but you can obviously go ahead and change that here, on the server there is a long request for ports more than 64000 and we have to make sure that on one port we are addressing the situation.</p><p>Hey, there might be some request that might come up and we are listening on that port and that’s why we say we are listening on port number 4000 or something. That’s actually happening in the development segment only. You never ever go into the 4000 because all the requests by default comes upon port 80.</p><p>And when you actually spin up your server like you might have heard about PM2 or Nginx or these kinds of servers Apache it’s their job to actually listen to the request and then redirect into your application and you configure them. That my application listens on port 8000 or 4000 whatever, but the ideal interface on the top always listens on port 80. That’s basic computer science.</p><p>Now what happens after that is the blue portion is actually a computer. I will just call this literally a <strong>Computer. </strong>And the Green Portion is where the Node.js works we call it <strong>Node.js</strong>. We need another one that is the portion where <strong>JavaScript</strong> works happen. That’s how the entire system is divided.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yHQTdZ4cWBbq3igyYWAwRQ.png" /></figure><p>Node.js does not have the default capability to have networking access because, at the end of the day, it is JavaScript. JavaScript doesn’t listen to the ports it has not the capability to have access to your interface card which is known as a <strong>network card</strong>. So your <strong>NIC</strong> or <strong>network card </strong>is a core computer thing. It is not going to be read by that and this is exactly where a new thing comes into the picture which is known as “<strong>libuv”</strong>.</p><blockquote><strong>Libuv</strong> library is majorly in C, libuv is a multi-purpose support library with a focus on asynchronous I/O.</blockquote><p>If you look at the documentation of libuv and we want to see the design overview. There’s so much to this and when people say that they can explain to me libuv they always say “libuv is a library that is in Node.js and has a thread pool, event ports, etc.”</p><p>Yeah, that’s great but understand about the I/O operation and all of that but what you don’t understand is where it comes into the picture. Because this is a library that is an interface between your computer and Node.js.</p><p>Your computer understands the networking request that is all going to come in but somebody needs to pass on these requests to Node.js <strong>that’s where libuv comes into the picture</strong>.</p><blockquote><strong>That’s the first part of behind-the-scenes in Node.js.</strong></blockquote><p>Now you understand the above part and we’ll not peel the libuv layer because as I said earlier libuv and Node.js are called the <strong>onion layers</strong>. The more you peel the more you’ll cry. So this is the basic you should know. Now whenever there’s a request that comes up this request is not in the text format.</p><p>It is something like “/pratham” or “/login” this is not how it works.</p><blockquote><strong><em>Request </em>actually comes up in streams of data and this data is a bytes of data.</strong></blockquote><p>So bytes and streams these are the words you are going to hear a lot about. Now if you want to have access to this you can technically, that’s a stream of data and for that <strong>net</strong> library is available in Node.js, but we are not interested in that.</p><p>So that’s why we use another library that can actually talk to the server and in the Node.js code, we saw it so this is where the “<strong>http”</strong> library comes up. This actually converts bytes into strings. I don’t want to deal up with the strings and bytes and all of that I will just go ahead and work with the strings.</p><p>So we’ll go ahead and say string, so to understand the strings we actually go ahead and directly talk to the string and the <strong>http </strong>library that’s the part of it.</p><p>Now a couple of more things are there so whenever a request comes up the libuv library actually has some interesting things going on. It actually goes ahead and converts into two boxes, Box 1 and Box 2.</p><blockquote><strong>Box 1 </strong>— It is going to be majorly utilized for an incoming section.</blockquote><blockquote><strong>Box2</strong> — It is majorly utilized in the outgoing section.</blockquote><p>Ans in case you haven’t got the hint yet. Yes, these are literally <strong>request </strong>and <strong>response</strong>. The thing that we say in the code part is request and response these things exactly are just boxes. Box 1 and Box 2 are literally big JavaScript object that’s it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ppFcVNeO-GXRXikWSpgJWw.png" /><figcaption><strong>Node JS Code for Creating Server</strong></figcaption></figure><p>And the entire <strong>request </strong>that we have consists of a lot of properties.</p><p><strong>Fro where it is coming up? What are the headers and a lot of information?</strong></p><p>Most of the information is actually inside JavaScript object and they are props or known as <strong>properties</strong>. Because we have to just extract the information that’s why they are in the request. There is no such thing in the entire Node.js which calls them always as a request, they actually have different names for it that’s how it is actually being done.</p><blockquote>The<strong> request object </strong>is an instance of<strong> Incoming Message.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rgXigrDWyODquMbrfnTWiQ.png" /></figure><p>Just wanted to prove the point that in all of your methods, URL we call it as request but we can call it Superman. There is no such thing that is stopping us and requests are an instance of incoming messages and in these incoming messages, we have properties.</p><p>On the other hand, the next thing that you see is the <strong>response</strong> that we send. This majorly comprises <strong>methods</strong>. Its mentioned in the Node.js documentation itself that the first one is majorly property and the second one is majorly method. It&#39;s not like they don&#39;t have any methods or properties in each other.</p><blockquote>Majorly constituted of <strong>methods</strong> is what <strong>response</strong> is.</blockquote><p>While the core method that actually everyone needs to take care of is the dot end that we saw in the code.</p><p><strong>.end() </strong>is the method that is actually at the very end of your stream. Yes, that&#39;s why they said in the documentation earlier that you should know about the streams.</p><p>Since the <strong>response</strong> is a stream chain we first send the status code then we send the JSON response and then we end it so that the other guy knows that the stream has ended there. That’s why you end it at the very end.</p><p>The majority of the people who work with the libraries like <strong>Express </strong>have never seen the end because Express does it automatically behind the scene. Now we understand that. Okay, this is all happening but when we create the Server, the request and response all of that.</p><p><strong>How does the request and response trigger? How does it work?</strong></p><p>That’s a very big mystery so what happens is in Javascript there are EventEmitters. We need to understand the EventEmitters and Streams. That’s where the portion of EvenyEmitters comes into the picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23ksFMqrasZmGnL0fF5oxA.png" /></figure><p>So in JavaScript, all we do is define the different routes. For example, there is a route, “/pratham”, another route “/insta” and “/linkedin”. So we wrote a route and on each, a different thing happens.</p><p>On “/pratham” we can show the profile, on “/insta” we can talk the DB(Database), and on “/linkedin” we can get data from DB and post.</p><p>Whatever happens, that’s actually core JavaScript capabilities. And we never run those functions. If you take a moment and think about how JavaScript executes things it’s actually single-threaded.</p><p>And it&#39;s a constant execution. It keeps on executing the moment we save the file if we have put parenthesis around it will execute the code. Nut we never execute the file. The file is executed when a request comes to you and yes this is another job of the libuv to find out of the property that somebody is requesting a URL of pratham so what should I do?</p><p>This is where it emits an event and it’s the job of Node.js to actually look into the code file through the JavaScript that somebody is requesting the “/pratham” URL.</p><p>And based on that it actually executes the function. So it keeps everything in the memory sort of that’s very fascinating that we don’t execute the code.</p><blockquote><strong>Code </strong>is executed on some event that happens.</blockquote><p>Already so much is happening behind the scene. That is EventEmitters come into the picture. So we go ahead and execute this code. Not only do we execute this code the moment we actually take all of this code and execute it by using parentheses. These are execution and it’s the Node.js who execute that. That’s why Node.js is in green.</p><blockquote>But Node.js not only execute this here’s the most crazy part that it took so much time to understand. <strong>Node.js is actually literally take the request and response objects and gives you access in each one of them directly here.</strong></blockquote><p>So they directly go ahead and inject your requests and your response into parentheses of “/pratham” and that’s why in each of these you will be able to see request and response. Yeah, so much crazy things.</p><p><strong>What is happening during the execution of code in Node.js?</strong></p><ul><li>A request comes to your server it&#39;s in the bytes and it&#39;s in the bytes and it is converted into a string. Libuv actually tricks all the request headers that are coming in.</li><li>Then it&#39;s crafting on object out of request behind the scene and it inserts all the properties like request headers, browser information, IPs, and whatnot all this information into the object and then it also emits an event that hey you are looking for a slash route out of this object.</li><li>Node.js looks for an equivalent function that should be executed. Adds the request and response object into places and then once we are done.</li></ul><blockquote><strong>Node.js actually streams that response back. We have no idea about it. Get’s into the libuv again and then it transmits it back to the user.</strong></blockquote><p>Yeah this is behind the scene of Node.js</p><p>This still bothers me so much that so much superpower and imagine JavaScript is doing nothing. It just is an interface you’re literally doing the majority of work in the libuv library.</p><p>Since JavaScript is the simplest one that’s why we write the majority of our code in JavaScript. But hard work is done by the Node.js so much of injection into the knowledge so much of the things that are being done.</p><p>So that’s so much already about intense software work. So we have got the idea of how the Node.js works behind the scene, so much is happening.</p><p>Now once you have actually understand this. So let me walk you again. We have idea about the mental model. The computer is actually an interface. Right now we talked about so much about the http interface only but this could be a file system. But the mental model remains the same. In the file system there might be another library which gives access of the file system.</p><p>Node.js does ton of work for us and we just call JavaScript, add some methods into it.</p><p>Now let me walk you through little bit of behind the scene into the docs as well. We need to study about that. And now you’ll be able to understand a lot about this talk.</p><p>First of all it says <strong>Create the Server</strong>. And now we have understood why it says EventEmitters and Streams. Because that’s what the code running part is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23ksFMqrasZmGnL0fF5oxA.png" /></figure><p>Node.js server application by using the createServer we know that the function that’s passed in to createServer is called once for every HTTP request made against that server so it’s called the <strong>request handler</strong>. It serves on the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j_aIYupM2pCERjXxxu6g4A.png" /></figure><p>When HTTP request hits the server nodes call the request handler function with a few handy object for dealing with the transaction, request &amp; response. Literally takes those object inject them into those methods that’s what they are said.</p><p>In order to actually serve requests, the listen method needs to be called on the server object. In most cases, all you’ll need to pass is listen in the port number. This is where they actually mentioned that Node.js make this relatively painless by putting a handy property in the request object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8j78Dngfy21LuaieD9mlTw.png" /></figure><p>This is the line. This is what makes our life easy. Again request is an instance of an incoming message that what they are saying. The method here will always be the normal HTTP method/verb.</p><p>There is so much into your request that you can just keep on extracting. You can extract your raw headers and all of that. I have deliberately skipped the part of request body and errors because that’s a different beast and probably we will need another article to talk about it.</p><p>But this is what is happening and when sending the response body notice that this is the potion I would like to bring your attention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hjXHYHEJJUS67r3s2KbqEA.png" /></figure><p>This is a stream of response that keeps on coming. You can keep on writing and that’s where the libraries like Express make your life easier because this is not programmer friendly. Keep on writing this much o the syntax is a really bad idea.</p><p>That’s why the library comes up and allows us to send response in JSON and all of that but all in all look at this since the response object is a WritableStream. It keeps on coming like a water stream. Until we hit the .end() method.</p><p>The end function on the stream can also take in some optional data to send the last bit of the data.</p><h3>Notes PDF Link:</h3><p><a href="https://bit.ly/nodejspratham">How Node JS Works.pdf</a></p><h3>Conclusion:</h3><p>You get the idea that is why I have called this a gigantic behind the scene of Node.js. I hope you enjoyed this and now you’ll be able to appreciate more when there is Node.js.</p><h3>Acknowledgment :</h3><p>Thanks to Hitesh Choudhary Sir.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3465a89b4eb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CRUD Operations using Express.js]]></title>
            <link>https://medium.com/@prathamchouhan1/crud-operations-using-express-js-28c580a1d9bf?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/28c580a1d9bf</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[post]]></category>
            <category><![CDATA[get]]></category>
            <category><![CDATA[put]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Tue, 08 Aug 2023 12:46:58 GMT</pubDate>
            <atom:updated>2023-08-08T12:54:09.261Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/0*Tz-yKgKG7iPgkhWl.png" /></figure><p>Today we will create an API to understand how API works. We will understand how we can handle all the requests like Get, Post, Put, and Delete.</p><p>So first we will make a directory and name it as Node-Example. Now we want to make a node project which means we want to create package.json and in this, we will store all the dependencies. To create package.json we have a command: <strong>npm init</strong> here some questions are asked like the name of the project and the name of the main file, etc.</p><p>If we do not want to answer all those questions and want to initialize with the default then we can write the command: <strong>npm init –yes </strong>.So with this command package.json file will be created with default values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OIokkvGVsYZRbDWtI4_fKw.png" /><figcaption><strong>Terminal With the Command</strong></figcaption></figure><p>Now we will install a dependency called “<strong>express</strong>” using express we can handle requests and responses so we will type the command: <strong>npm install express </strong>and<strong> </strong>the internet connection should be on because the module and packages are downloaded from the internet.</p><p>As we download the dependencies they will be shown in the package.json file this is because when we move our project somewhere then we will know which dependencies we need to install so the dependencies get read and are automatically installed. And it will also show the version of Express installed.</p><p>Now we will create a file named “<strong>index.js</strong>” from where our application will start. First we will simply write</p><blockquote><strong><em>console.log(“index.js”)</em></strong></blockquote><p>And in the terminal to run we write <strong>node index.js</strong> and the result is this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9r5rYtHTnD7mD7-0d29aUQ.png" /><figcaption><strong>Result of the console statement in index.js file</strong></figcaption></figure><p>If we make any changes then it will not be seen and we need to run again and again so to eliminate this we will use <strong>nodemon</strong>. So when we install it, it will automatically show the update so let&#39;s install the nodemon.</p><p>The command to install nodemon is : <strong>npm install nodemon –g</strong></p><p>When we add –g it will be downloaded globally and as we use it in many projects so it will be beneficial. Now if we want to run index.js using nodemon then we need to write the command: <strong>nodemon index.js </strong>now it will watch the file and we save the file it will reload. If we make any changes to the file so the file will automatically run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oZnqKBsSLKucirR51pmFPA.png" /><figcaption><strong>Command to Download the nodemon package</strong></figcaption></figure><p>Now coming to the main topic Express js so we need to handle request. So we need to create a server which means we will send a request from localhost and the server need to handle it. We have <strong>localhost:3000/api/students</strong> when we hit get request then we need our server to send the list of students in JSON format so we need to handle all. All of this work is handled through API.</p><p>To start with we need to create a server. So we need to import the express in index.js so we will use the require function</p><blockquote><strong><em>const express = require(“express);</em></strong></blockquote><p>So the express will be imported into the index.js file. With the express, we need to create a server. Express is like a function and when we will call it then we will get an application object and we will store it in the const app variable.</p><blockquote><strong><em>const app = express();</em></strong></blockquote><p>Now using the app we can do anything that we want to do on the application object. Like we say app.listen().</p><p>Listen is a method, the port on which we will listen so we need to pass port to this listen so we will pass 3000. So now as soon as you hit 3000 on the browser the server will listen to it.</p><p>When we will search on browser localhost:3000 then we will get the message Cannot GET/ so this means our server is working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iMq0VMMHkAwM6JKzKHx4bg.png" /><figcaption><strong>Output at localhost:3000</strong></figcaption></figure><p>So we can pass a function in the parameter.</p><p><strong>What the function will do?</strong></p><p>When the listen will happen then we will receive a message. So we will write</p><p>console.log(“Listening on Port 3000”); and the result will be the message will be printed on the console so this is running properly.</p><p>Now we go to localhost:3000 and we want to send some request then we need something to handle it. Till now there is nothing that can handle. So we need to handle the request.</p><h3>GET Request:</h3><p>get() is a function that will handle the get request in this, we have a parameter “/” slash means we are pointing to the home directory or root directory. So as soon as I hit this URL the function will run, so we can pass a function which will be a parameterized function whose one parameter is req (request) and the second one is res (response).</p><p><strong>req</strong> will represent the request object means if we have sent then there is lot of information that we can need in the future so all that information is in req object.</p><p><strong>res </strong>object is response object using which we will send response to the client.</p><p>In the arrow function we will write</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JPS0oQYNWsIpIzSBgSP3Kw.png" /></figure><p>And when we reload the web page then we will get the result</p><p>Because we went to localhost:3000 and app.get function is handling it and it sent the Hello World.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sEWIh3LsLL_HcPgI-wlDhQ.png" /><figcaption><strong>Get request Output on localhost:3000</strong></figcaption></figure><p>Now we will work with JSON because we mainly use JSON in API. So we will use json() with res and we can pass a message and we have created JSON object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F6nzXv5POhxafZBS4yfv4g.png" /></figure><p>When we reload the page then we get the JSON. So API is working properly and we have handled the <strong>get request</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hNa_7S65HeNTL8b-PYEpA.png" /><figcaption><strong>Get Request in JSON format at localhost:3000</strong></figcaption></figure><p>Let us have a student list we want to create fake data so we have a website <a href="https://www.mockaroo.com/">mockaroo.com</a>, this website is used because when we work with API then we need test data so we will not type, let&#39;s say we need 100 users at a time. We can get the users using the following procedure :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*grZ53--hiTbjkt3fkvauZQ.png" /><figcaption><a href="https://www.mockaroo.com/"><strong>Mockaroo Website for test data</strong></a></figcaption></figure><ul><li>When we will visit the website we can create fake data.</li><li>We can select and add the Field Name, Type and then we can select the number of Rows and the Format here we need JSON so we select it.</li><li>Then we can preview the data which is in JSON format.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rETOYi66iVpgL6BqxP8c9A.png" /><figcaption><strong>Preview of the test data in Mockaroo Website.</strong></figcaption></figure><p>So we will select all the data and copy it. We will create a new file named <strong>students.js </strong>and paste it into a new file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4gSHdyvKPqohT7_59pOyew.png" /><figcaption><strong>students.js file</strong></figcaption></figure><p>Now we want to access this students object outside the file. We will need to export it. So we will write the code to students.js file at the end of the object</p><blockquote><strong><em>module.exports = students ;</em></strong></blockquote><p>So we have exported the students object from <strong>students.js</strong> file.</p><p>Now we want to access the students object so we will load it in <strong>index.js </strong>so will write</p><blockquote><strong><em>const students = require (“./students”);</em></strong></blockquote><p>./ means we are in current folder so it will return an array of students.</p><blockquote><strong><em>T</em>here is a convention whenever we make an api we write “/api” and then the name of url we want so that we can know that it is an api.</strong></blockquote><p>Now in app.get() we will not pass the message we will pass the students list so the code is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BJJumCZY7spFO_eFYjEPow.png" /></figure><p>Now again we will reload the localhost:3000 then we get the students object in JSON format, So this is how API works. When we hit an API url it will give a JSON. Now we need to work on the URL as we are getting students like localhost:3000/api/students. From this, we come to know that we will get students. So from URL, it should be clear what data we will get. We need to follow convention.</p><p>So for the home or root directory, we have</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q3vWo_65QLMz732AOzMWwg.png" /><figcaption><strong>Home page URL</strong></figcaption></figure><p>Now we want to send the students list on URL localhost:3000/api/students so we have the code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gDMsOA3PVfbKiAnpBD_3Sw.png" /><figcaption><strong>URL for localhost:3000/api/students</strong></figcaption></figure><p>And we will get the desired result. So this is how we handle the get request from get we come to know that we as users want something from the server. So generally in <strong>get</strong> request is to give data to the user or client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R3Jdbw1bltkXmvuqJv6Giw.png" /><figcaption><strong>Result JSON object at localhost:3000/api/students</strong></figcaption></figure><h3>POST Request:</h3><p>If the client says<strong> post </strong>request then the client needs to save data in the server. So let’s see how to save data on the server.</p><p>Let&#39;s handle the <strong>post</strong> request. So <strong>app.post()</strong> is a method and we are sending the post request on localhost:3000/api/students and here also we have two parameters req and res.</p><p><strong>req</strong> will have some data for the request and <strong>res </strong>will have data to generate a response. Now when we post some data here then we need to send some data in the body we will see that little later.</p><p>First, let&#39;s run the code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rKseSAdoCpYPB18UfKj_-g.png" /></figure><p>When we come to the browser to check for changes then we do not have a method to send <strong>post </strong>requests.</p><p>To send the post request we have a method for this we need to install <a href="https://www.postman.com/"><strong>POSTMAN</strong></a>.</p><p><strong>What is Postman?</strong></p><p>POSTMAN is used to send requests like GET, PUT, POST, DELETE, etc so the requests which we cannot hit from the browser which is except GET we cannot send any request from the browser, then we need to either create a form or use the POSTMAN.</p><p>So this is what the POSTMAN will look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rRlTAHT6I0E-MNaQKBEvg.png" /><figcaption><a href="https://www.postman.com/"><strong>POSTMAN</strong></a></figcaption></figure><p>We need to click the + icon and then select the POST and enter the URL localhost:3000/api/students and we will send the response. Then we will get the response <strong>Students Post Request</strong>. So post request has been successfully sent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hvnJEdOY8fR4nDMLSZxKqQ.png" /><figcaption><strong>POST Request</strong></figcaption></figure><p>Now if we want to send data with the post request then we need to go to Body in Postman and then select raw and format as JSON and we will type all the data we need to send in JSON format. So let&#39;s say we need to pass the name so we write.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kBrhekyddfBkZhj8Yb9acQ.png" /><figcaption><strong>Sending the post request</strong></figcaption></figure><p>So it will be sent by Post request but how we will read this data? We know about the req (request) object which contains all the information about the data so we can print that req here. With body we are sending data that we can read.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UJdMgbMqJVpOSOmBVka8yQ.png" /></figure><p>Now we will send the request again from Postman and when we check the console we will get undefined because as we know that with the body we send data that the req object read using <strong>req.body </strong>but we do not get the JSON because there is a reason behind it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rvq7rLiLpMHn-zMnh9H6KQ.png" /><figcaption><strong>We get undefined at the terminal</strong></figcaption></figure><p>Because to read the body we need to do a configuration, the configuration is that if any request is coming then in the middle there should be one that converts the JSON that we have sent in the req object into a javascript object and set the object in req.body property. There is no one till now.</p><p>So the one that does is called <strong>Middleware</strong>. As soon as we use a middleware it will read the JSON convert it into a javascript object and save it in the req.body property and later we will use that. To use middleware we need to add the code</p><blockquote><strong><em>app.use(express.json());</em></strong></blockquote><p>So the above code will parse the JSON in the req.body , now when we will send the request then we will get the data in object from on console. We can do this only by using a single line of code as seen above. It returns the function and that function run before handling the post request its work is to convert the JSON into an object and set it in req.body .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4NAEHHFoudm5zQe4OHrS7Q.png" /><figcaption><strong>Now we can read the data sent by Post request</strong></figcaption></figure><p>As of now, we have read the data now we need to save it, so the server has the data now it only need to save it. So in the students list we will append it. So for the students list we need id, first_name, last_name, and email.</p><p>Id is not required because it will be generated automatically when I send first_name, last_name, and email. So we will create an object in the post request let the name be user and it is of const type.</p><p>Now we need to set values so the<strong> first_name</strong> value will be <strong>req.body.first_name</strong>, again we have another property <strong>last_name</strong> and the value will be <strong>req.body.last_name</strong>, now the third property is email and the value will come from <strong>req.body.email</strong>.</p><p>Now from where the id will come from for this whatever the size of the students list, we will add 1 to it we can say it auto incremented.</p><p><strong>id: students.length + 1;</strong></p><p>So we have created a user and now we need to append this in the students list so we will use push() and we will push this user object.</p><p><strong>students.push(user);</strong></p><p>And generally whenever we create an object and save it in the database, when the API sends the data and saves then the id is auto-generated. So there are chances that the user who creates the object might need the id who has sent the request so we generally return the whole object. If another object has come then the user we have sent has been saved so we will do this using.</p><p><strong>res.json(user);</strong></p><p>If the user object is successfully saved then we will get the JSON, if there is an error like we have not passed the email then we will send the error. So we will put the condition that if the email is not there we want to send an error we will set a response status code.</p><p>There are various status codes, for success the code is 200, for failed it is 500 and for server error, it is 404. And we will send 400 which is for bad request and then we will send the JSON message as email is required since we are using the if code then the rest following code will also run so we will return the JSON message.</p><p>So the combined post request will look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gE5WkY1kZ9U8rUzimR3-jA.png" /></figure><p>So when we test the name only then we will get the response “email is required” and the status code is 400 i.e. bad request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PmaSqFEykb0FLEMASv78AQ.png" /><figcaption><strong>Error Response when wrong or insufficient data is sent using Post Request</strong></figcaption></figure><p>Now when we send the whole data in the correct format then we will get the response with id, first_name, last_name, and email. So our post request is successful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PmaSqFEykb0FLEMASv78AQ.png" /><figcaption><strong>Successful Post Request with Response</strong></figcaption></figure><p>Now if we send the <strong>Get </strong>request then we will get all the students list with the latest one appended to it. So we can append the students list API also does this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OOdP0fl6w33lQ4ecG3QX9g.png" /><figcaption><strong>Get Request with all the students list with data appended to it.</strong></figcaption></figure><p>So this was all about how to handle the<strong> post </strong>request. Now lets study about the <strong>put</strong> request.</p><h3><strong>PUT Request:</strong></h3><p>In <strong>PUT</strong> request we update the data. As we have get and post methods likewise we have put method.</p><p><strong>app.put()</strong> is used for the put request. Now we need to update the students data so we will create the same request as “/api/students” so we will come to know that we are updating the students list. Now how will we come to know which student we need to update.</p><blockquote><strong>There is a convention here that we will pass the id here or some unique value.</strong></blockquote><p>Since id is unique here so I will pass the id in the URL so from this we come to know which student we want to update. And the id will come dynamically so we will write</p><blockquote><strong><em>“/api/students/:id”</em></strong></blockquote><p>Again here we will have req and res two parameters now here we have a task of how we will read the id here, so we call it params “url.params”. So we will make an id of type let as follows:</p><blockquote><strong><em>let id = req.params.id;</em></strong></blockquote><p>So the id we will use in the URL will get the same id here also now we will console.log(id) and the res.json(id) to test.</p><p>Now we will go to Postman and we will send the put request and we need to write the id also in the URL like localhost:3000/api/students/1 we need to update the student with id=1. So now it will return the id because we can read it on the server which literally means that the id that I have sent has been read.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pseloKKOz-6FVvl68B7Ctw.png" /><figcaption><strong>PUT Request with URL localhost:3000/api/students/1</strong></figcaption></figure><p>Now first I will search and then I will update the data so the data I will send with body, like the data we have sent in the post request will send all the data we want to update. We need to select the raw and format it as JSON and paste all the changes we want.</p><p>First, we need to read the data so we read it like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7rtDGcDqXpgM-_F-tAWUUw.png" /></figure><p>Now we need to update but the unique is id so we need to search the id student to search the student array we can do this using <strong>findIndex()</strong> in this we pass the function in which only one student will come and on each student, we will apply condition if it is true or false. If it is true then the index will return from here. We will need an index when a student.id is the same as id but we need to parse the is with Number.parseInt(). If this condition is true then we will get an index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9VVprs3BFJkP8-8B7L2Ntw.png" /></figure><p>If we have index greater than or equal to 0 then we need to update the student object. So we will have the std and we will access student from index so we will get the student object and now we need to update it. And the whole process will look like this. So we have updated the last_name, first_name, and email and now I need to send the updated student response (res). So we say res.json(std) and if we do not get it then we have else in which we have response status 404 which means not found.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T8_MFrd3hqiUsVZ3lzkwdQ.png" /></figure><p>Now when we send the<strong> put</strong> request then we get the updated student.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WuEQjQBUFoOt_3-igAgBAA.png" /><figcaption><strong>PUT Request for id=2 and updates</strong></figcaption></figure><p>Now if we send the <strong>get </strong>request then we will get the updated list of students.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5GXT6nCVwevq9eXG12d4w.png" /><figcaption><strong>GET Request with updated id=2</strong></figcaption></figure><p>So we have seen how to handle the <strong>put </strong>request. Now let&#39;s see the delete request.</p><h3><strong>DELETE Request:</strong></h3><p>In this, we will see how to delete the object. As we need to send the delete request so we will see <strong>app.delete(); </strong>and we will send the request on “/api/students/:id” and we will come to know which student to delete with the help of id. We will read that id using params and we have two parameters req and res now we need to read the parameter so we have an id which is unique as follows</p><blockquote><strong><em>let id = req.params.id;</em></strong></blockquote><p>Now we need to do the searching as we have done in put request with the code</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*9VVprs3BFJkP8-8B7L2Ntw.png" /></figure><p>And we have the same condition of if else and if we get the index then we need to delete. To delete from an array we have <strong>splice()</strong> and from which index you want to delete we can do that and the number of objects. But generally before deleting the object we need to return that object. And we will send the object to the client and if this object is not deleted then in else we have a request status of 404 which is for bad request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2UF8Y6MvzlDPU_o5P9FmSA.png" /></figure><p>Now to check we will go to the postman we will select DELETE and enter the URL localhost:3000/api/students/1 let say we want to delete the object with id =1. So now we will send the request and we will receive the deleted object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JnUh4cM5vx57u4wyWEOdlQ.png" /><figcaption><strong>DELETE Request to delete the student with id=1</strong></figcaption></figure><p>So we have learned how to handle delete request.</p><p><strong>Conclusion :</strong></p><p>So we have seen the most used requests there are lots of requests there but the most used are GET, POST, PUT, and DELETE Requests. And we have seen all of them in detail.</p><p><strong>PDF NOTES</strong>: <a href="https://bit.ly/3KuF1lS">https://bit.ly/3KuF1lS</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28c580a1d9bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Day 10: C++ Journey]]></title>
            <link>https://medium.com/@prathamchouhan1/day-10-c-journey-60406f8ab0d1?source=rss-80c0f55aee49------2</link>
            <guid isPermaLink="false">https://medium.com/p/60406f8ab0d1</guid>
            <category><![CDATA[c-plus-plus-language]]></category>
            <dc:creator><![CDATA[Pratham Chouhan]]></dc:creator>
            <pubDate>Sat, 22 Oct 2022 10:13:09 GMT</pubDate>
            <atom:updated>2022-10-22T10:13:09.267Z</atom:updated>
            <content:encoded><![CDATA[<p>I will be documenting all the learnings from the C++ course by Saurabh Shukla Sir.</p><h3>Constructor</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YySKpHM-I03PTP_YK1eWPA.png" /></figure><p>Constructor is a member function of a class. In C++ we make class and in class we make member function and in these function one is constructor.</p><blockquote><strong>The name of the constructor is same as the name of the class.</strong></blockquote><p>This means that if there is a class whose name is student then there will be function in that class with name student then that will be considered the constructor of that class.</p><blockquote><strong>Constructor has no return type, so can’t use return keyword.</strong></blockquote><p>Like if we do not write function return type then it will be considered int. But in this here it is not like that. Here, constructor function has no return type.</p><p>For this reason we cannot write return keyword in constructor function.</p><blockquote><strong>Constructor must be an instance member function and that is, it can never be static.</strong></blockquote><p>Now let’s see a small program to see how constructor is made in class.</p><ul><li>We have made a class Complex to represent complex number. And we have made in private two variables a and b which will store real and imaginary part.</li><li>Then in public we have made constructor.</li></ul><p>We are making constructor and the class name is Complex, so this function name should also be Complex.</p><p>Notice that we have not written return type in this constructor function. If we want we can do the coding in constructor.</p><p>Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsT_KFzbZqBQXaQcItu0Jg.png" /></figure><h4>How to Call Constructor?</h4><blockquote><strong>Constructor is implicitly invoked when an object is created.</strong></blockquote><p>Means constructor is not called by us, but it is called automatically when we create an object.</p><blockquote><strong>Constructor is used to solve the problem of initialization.</strong></blockquote><p>Constructor is made to solve the problem of initialization. To understand this let’s see a program.</p><p>We will make main function in continuation of the above code. And in main function we will make object of Complex class.</p><p>And in main function that constructor ran or not we have written cout statement in constructor. If this will print then this means that constructor come to know.</p><p>Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nfJMJ0NLrDZUBf-JTqjzVg.png" /><figcaption><a href="https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/CONSTRUC.CPP">https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/CONSTRUC.CPP</a></figcaption></figure><h4>Now what will happen if I make more than one objects?</h4><p>Like we make c1, c2, and c3. Then answer is three times Hello Constructor. Because that many times objects are created that many times constructor will run.</p><p>So this is an instance member function. Si it will print three times Hello Constructor.</p><h4>What is the problem with initialization?</h4><p>But first understand, why it is called Constructor?</p><p>From name it looks like it constructs something. If I give you answer it might be sound confusing.</p><blockquote><strong>Constructor makes the object an object.</strong></blockquote><p>Let’s try to understand this. Many times people tell us that. “Be a man”. What does that mean?</p><p>We are already a man/ human. But then also some one say/ tell us to be a human/ man.</p><p>It might be possible that he is mot able to see the characteristics of a human being inside us.</p><blockquote>On a serious note. To make an object a object is the work of a constructor.</blockquote><p>So in C++ we write Complex c1; , so in our point of view c1 has became object. but in true sense it has not became an object. Till the time it is not initialized it will not become an object.</p><p>To understand this let’s take a real world example:-</p><p>Suppose we want to represent a car. To do this we will make a class of car. In that class we will make some member variables and functions and then make class car object.</p><p>At the time we make car class object. The variables of object contain garbage value. So due to the presence of garbage value we can’t say that this object is representing an actual car.</p><p><strong>But what is object?</strong></p><p>Object is real world entity.</p><p>Now the answer to the question. What is the problem of initialization?</p><p>Object contains garbage vale. So by physical appearance it may look that we have made an object of car class. But this object is not able to represent car. So this is the problem of initialization.</p><blockquote>Constructor solve this issue.</blockquote><h4>How constructor resolves this issue?</h4><p>Constructor is such a guaranteed function which is called as the object is created. So the object you have to do after the object is created you should write in the constructor. Generally initialization of member variable.</p><p>It may happen that we call the function by object of car class, which is member of class. Let say shift_gear. So this when called will change the value of gear.</p><p>But if in object of car class we have not put any value then there is garbage value and it will not represent any car. And in this situation if by mistake someone call shift_gear by object of car class then there will be change in garbage value. And this will be wrong.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bnz23uUjA_Iywh3qtuxn8Q.png" /></figure><p>So in programming, we can think that class is made by someone and the object of class is made by someone one else then the person who will make object will call functions and he will not know the importance of what initialization is important so by mistake it can call some functionality and it will be wrong.</p><p>And OOPs strongly recommends that coding should be done in such a way that there is minimal scope of error. So if we do the initialization work in constructor then this problem will be solved.</p><p>So this is the work of constructor and if it will do the initialization that in true sense the work of constructor will be done and it will make the <strong>object</strong>.</p><p>Till now we observed some basic knowledge about constructor and now our objective is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XX9vEVYceO7c-BDvsL3C4A.png" /></figure><p>Above we have made Complex class. If we have made a constructor like this in which we have not passed a single argument then the way we are making object is perfect.</p><p>But if we want that we can pass arguments in constructor then we can take arguments also. Like the way we make function, of take something nature and like that only we have x and y arguments here.</p><p>And we may want to pass the value of x in a and value of y in b. We have made a meaningful constructor here and how will this constructor will be called.</p><p>So this constructor want value of x and y. But if object is made like the previous code then it will give error.</p><blockquote>Because in Complex class there is no constructor that does not take argument/ parameter. And the way we are making object that only means to call a constructor in which no argument is passed.</blockquote><p>So if we want to pass arguments in constructor then we have to make object and then put parenthesis and then mention the values. And this means c1 object is made and for that constructor was called and in that3 and 4 are passed. These 3 and 4 will go in x and y. And from x and y it will go to a and b of which a and b of c1 a and b.</p><p>But if we want at the time of creating object we only want to pass a value only.</p><p>We have made c2 name object and in that we only want to write value of a then it will be possible only when a constructor like this is in our class that takes single argument of int type. So to do this we can make another constructor in class in which we will pass only one argument in formal argument, and let say this is real part so it will take value to a and we do not want to make changes in b. So in this way we have made another constructor and it has one argument.</p><p>When two versions of the constructor are present in the class, and that is why it is called <strong>constructor overloading</strong>.</p><p>This is same as function overloading. So when we call function with one or two argument than that one constructor will be invoked.</p><p>Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D-nlRjfxx66IIk3mJGullg.png" /><figcaption><a href="https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/CONSTRU.CPP">https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/CONSTRU.CPP</a></figcaption></figure><p>Let’s suppose we made an object of complex class c3 and we do not want to give any arguments. So we will create another constructor in above code and make the function with no argument and initialize a=0 and b=0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DkowqonkX4x3ySDUNUvmtg.png" /></figure><p>Yes this is another way. Both the ways of writing are different but their meaning is same.</p><p>When we are writing two arguments the ways are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GBLJU49-E7YdMwY3mJqeGg.png" /></figure><p>When we are passing only one argument then ways are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1EH03uY36Dt9jSZ3WzMCTA.png" /></figure><p>So these are the notations that we can use according to the situations.</p><blockquote><strong>But till now while making the program we have not used the constructor before studying it. Obvious there will be error.</strong></blockquote><p>There will be error because while making objects c1, c2, and c3. While making c1 I am making two arguments, while making c2 I am passing one argument and while making c3 we are not passing any argument.</p><p>So we make objects and we have not passed any argument then will there be error?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*i7m16Ld0jEU5HXw7N0S8IA.png" /></figure><p>No, there will be no error <strong>because like this we have done earlier. We have made classes but not made constructor then when we make object and then also constructor was called</strong>.</p><p>So a new thing we come to know. Object when made that time constructor will be called. So do not guess on that object will be made the constructor will run or not if our class does not have any constructor will run or not.So always keep in mind:</p><blockquote>Whenever object is made, constructor will be called definately.</blockquote><p>This means that if in class we have not made constructor then also constructor will be called. Because compiler from its own side will create constructor in class.</p><p>Yes, compiler has made constructor in our class. And that constructor is called the <strong>default constructor. Specially we call it implicit default constructor. </strong>Because we have not made/ created it compiler has created it.</p><h4>The compiler that has created constructor why it is not seen in the code?</h4><p>Because when it will make the result will be in the object file and not in the source file. So compiler makes an object file. In that file, one constructor will have been entered.</p><p>The speciality of this constructor is that the constructor made by compiler in this no argument is passed. Yes zero parameter. And its body does not have any coding. But due to this when we make object then for that constructor will be called.</p><p>That’s why till now while making object no error came but constructor was called then also.</p><p>Now suppose you have made only one constructor.</p><h3>Rule is:</h3><blockquote>If in class there is if any one constructor then compiler will not make default constructor.</blockquote><p>So this means in our class there is no constructor of non-parameterized type.</p><p>Eg of Default Constructor:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*lqlRlNR1dJ0AFspPcStcug.png" /></figure><p>Eg of Parameterized Constructor:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*NyS3aIy1dzZJe_2rn3r8rg.png" /></figure><p>Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6srSauJ9tmcKazBuxtjMgg.png" /><figcaption><a href="https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/OVERLAOD.CPP">https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/OVERLAOD.CPP</a></figcaption></figure><p>In the above code lets say we have written in main function.</p><blockquote>Complex c4=c1;</blockquote><p>or</p><blockquote>Complex c4(c1);</blockquote><p>then think which constructor will run. All the three constructor will run. Here no error will be there.<strong> Because the reason is here also constructor is running.</strong></p><p>But non of the above constructor which compiler is runnning. So what constructor is running?</p><p>This the constructor which compiler is making. But we have studied about compiler that it will not create constructor if we have made constructor in class.</p><p>So see carefully.</p><blockquote>Compiler in our class make two constructor :</blockquote><blockquote>- DEFAULT CONSTRUCTOR</blockquote><blockquote>- COPY CONSTRUCTOR</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*YVErnLJv-HEKZFz7Utmnpw.png" /></figure><p>Now let’s learn how to make copy constructor. Other code as before remain same. Now we add the constructor of copy.</p><p>So we make constructor of name Complex and pass argument in &amp;c of complex type, and in the body we will write the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*qNEEVcNsSrS-o_HlayY0Rg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*xCJ1E-uG4rMQftcZpsrWCg.png" /></figure><p>Here in constructor we have taken reference variable in which c1 reference will come and now it is completely perfect.</p><p>Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HXYwgmg2w2SubB9y5DUfdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wz_khUX9FMbVfzMsOcANCg.png" /><figcaption><a href="https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/COPYCONS.CPP">https://github.com/chouhanpratham/C-Plus-Plus-Programming-Practice/blob/main/COPYCONS.CPP</a></figcaption></figure><p><a href="https://drive.google.com/file/d/1xFt3lHQ2ZYZXdabRve3hH3ZL2FEDOK9D/view?usp=sharing">C++ Day 10.pdf</a></p><h3>Acknowledgment :</h3><p>Thanks to Saurabh Shukla Sir.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60406f8ab0d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>