<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dubymar Tollinchi</title>
    <description>The latest articles on Forem by Dubymar Tollinchi (@dubymarjtr).</description>
    <link>https://forem.com/dubymarjtr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F705268%2Fe3b4aae9-8e0a-4009-aa26-3238b9aae2e5.jpeg</url>
      <title>Forem: Dubymar Tollinchi</title>
      <link>https://forem.com/dubymarjtr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dubymarjtr"/>
    <language>en</language>
    <item>
      <title>The Thread States and Life Cycle in a Java Multithreaded Environment</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Mon, 16 May 2022 18:49:16 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/the-thread-states-and-life-cycle-in-a-java-multithreaded-environment-305g</link>
      <guid>https://forem.com/dubymarjtr/the-thread-states-and-life-cycle-in-a-java-multithreaded-environment-305g</guid>
      <description>&lt;p&gt;Before diving into the life cycle of threads in Java, let’s start with some basic concepts about this topic. A thread is a lightweight subprocess; it represents the smallest unit of processing, thus making it possible that multiple threads can use a shared memory area and run concurrently. Despite this, they are independent, meaning that in the case of an exception, one doesn’t affect the others. They exist inside a process, and this process can have multiple threads. At the same time, many processes can exist in the operative system. The task of running more than two threads simultaneously is called multithreading, and it maximizes CPU memory and saves time. &lt;br&gt;
The life cycle of threads consists of the following states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;New&lt;/strong&gt;: whenever a new thread is created, meaning that the code has not been run and the thread has not been executed yet, is called the new state. It is the very first state a thread can be in.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Active&lt;/strong&gt;: a thread can go from a new to an active state after the &lt;code&gt;start()&lt;/code&gt; method is executed. This state can be split into two states:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Runnable&lt;/strong&gt;: in a multithreaded environment like Java, several threads can be executed at the same time. Each one gets a fixed amount of time to run, and it’s the thread scheduler who provides that time and moves the thread to the running state. Once a thread is done running, it gives the CPU to another thread to run. All other threads that are waiting to run are said to be in a runnable state, which can be represented as a queue that contains all threads in order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Running&lt;/strong&gt;: it is the moment where a thread gets the CPU to be able to run. Threads come in order from the runnable state to the running state and generally go back to the runnable state after they are run.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blocked/Waiting&lt;/strong&gt;: whenever a thread is temporarily inactive is either in the blocked state or waiting state. To explain these states, we need to talk about inter-thread communication and the concept of synchronization. In Java, there is this concept of a “monitor” (a synchronized block), which we can picture as a box that can only contain one thread at a time. When a thread has a lock on this block, it is in its running state. The rest of the threads that are waiting for a monitor lock are in the blocked state. Threads remain idle until the &lt;code&gt;wait()&lt;/code&gt; method is called, placing them in a waiting state. In this state, the main thread is waiting for another thread to finish its execution, and once this happens, it then calls &lt;code&gt;notify()&lt;/code&gt; so the main thread can acquire the monitor lock, and therefore, go to the active state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timed Waiting&lt;/strong&gt;: if a thread is being run and it enters a critical section of code that is not possible to leave, other threads can be waiting forever. This concept of eternal waiting is known as starvation, and to avoid this, a specific amount of time is given to each thread in the timed waiting state. For example, the &lt;code&gt;sleep()&lt;/code&gt; method puts a thread in this state, and after the specific amount of time runs out, the thread wakes up. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminated&lt;/strong&gt;: a thread moves to the terminated state (also called dead state) when it finishes its job. For example, when the &lt;code&gt;run()&lt;/code&gt; method completes its execution, it would represent a normal termination. If any unusual event occurs, such as an unhandled exception or a segmentation fault, it is called an abnormal termination. Once a thread is killed, it cannot go back to any other state. 
Threads go from one state to another from the very beginning of the program execution. Java’s multithreaded environment allows multiple threads to be executed and moved between states, but since they share the same memory area, the CPU can be used by only one thread at a time. This thread will be the only one in a running state, while the rest of them will be in other different states. Since threads are lightweight subprocesses, they take little time to switch from one to another. This saves plenty of time when running a program, unlike process-based multitasking (known as multiprocessing), where each process has a different space in memory, is heavyweight, and switching between them can be time-consuming.
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Building an Express back-end for basic CRUD operations</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Wed, 08 Dec 2021 20:14:00 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/building-an-express-back-end-for-basic-crud-operations-4m0n</link>
      <guid>https://forem.com/dubymarjtr/building-an-express-back-end-for-basic-crud-operations-4m0n</guid>
      <description>&lt;p&gt;When enrolling for college, I had to choose between Web Development and Web Design. This decision was quite easy because I thought "making a website look pretty is something that most developers can do (not saying it's easy tho!), but making a website be completely functional? That's more of a complex job, and that's a challenge I want to take". &lt;/p&gt;

&lt;p&gt;In my Web Technologies class, after learning the basics of JavaScript and working with front-end, it was time to swim into the deep waters of the back-end of a website. I was assigned to create RESTful routes that interact with a MongoDB database using Node.js and Express.js. It was very confusing at the beginning, but after building a few small projects applying the same steps, I feel more comfortable to share the knowledge I acquired with this community. &lt;/p&gt;

&lt;p&gt;So let's dive into it!!&lt;/p&gt;

&lt;p&gt;The first thing we need to know about is Node.js, and I already wrote a &lt;a href="https://dev.to/dubymarjtr/understanding-node-js-5n8"&gt;post&lt;/a&gt; about it, so go check that out so you understand why and how it is used. After installing Node, we will use the terminal to install the required dependencies, in this case are: &lt;code&gt;dotenv&lt;/code&gt;, &lt;code&gt;express&lt;/code&gt;, and &lt;code&gt;mongodb&lt;/code&gt;. Luckily, I used a template that already had all these installed, but if your project does not have any of these, just type &lt;code&gt;npm install express&lt;/code&gt; on the terminal, repeating the same syntax with the other packages. &lt;/p&gt;

&lt;p&gt;The second step is to create a &lt;code&gt;.env&lt;/code&gt; file, which will contain the database URL that will connect our application to MongoDB. This file will be untracked, since we do not want to include it in our commits. This is more of a security reason, because why will we want strangers accessing to our database, right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;DB_CLIENT_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;databaseurl&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third step is to go into our config.js file and declare the configurations for our project. In this case, I have the port, database URL, name, and collection name. Having this information here and not in other files will make our website easier to maintain, having to change it just here and not everywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;clientURL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DB_CLIENT_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sample_airbnb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;collectionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listingsAndReviews&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can notice, there is no way to see the actual database URL. For this project I used an Airbnb database sample from MongoDB, but in case I want to use another database, I can change the name and collection right there and that will be applied to the entire project.&lt;/p&gt;

&lt;p&gt;The fourth step is to create a db/conns (database connections) folder, and inside create a client.js file, that will contain a reusable Mongo client to connect to our application. Here is where we use that &lt;code&gt;mongodb&lt;/code&gt; package that we installed earlier. &lt;em&gt;Note&lt;/em&gt;: this is the only file inside of that folder, everything else from now on will continue to exist inside the server folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongoClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clientURL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB Client 🏃🏾‍♂️&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error starting MongoDB Client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Exit process with failure&lt;/span&gt;
    &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SIGINT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;close&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB Client disconnected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have our Mongo Client successfully connecting to our database, we can proceed to create our routes in the router.js file. First, we will start with a test route to connect to our API. We need to import the content from config.js, client.js, and our &lt;code&gt;Router&lt;/code&gt; from &lt;code&gt;express&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./db/conns/client.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// create collection variable to reduce code duplication&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collectionName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// localhost:3000/api&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello from API router&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To utilize this router we need to import it to our index.js file, using express as the framework that will make our lives easier. Express gives developers all the tools they need to create HTTP servers, allowing to receive and send data as JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./router.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// json middleware allows to send and receive JSON data&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run our server we can type &lt;code&gt;npm start&lt;/code&gt; in our terminal, and then go to any explorer and type &lt;a href="http://localhost:3000/api"&gt;http://localhost:3000/api&lt;/a&gt; and that will show "Hello from API router". Now that our testing route is working, we can proceed to create the rest of our routes. In this project we are using basic CRUD operations, which are Create, Read, Update, and Delete. In some of these route we will need to use &lt;a href="https://insomnia.rest/"&gt;Insomnia&lt;/a&gt;. These platform will allow us to send JSON data, since we are not doing that from the browser. The way we will send this information will be on the request body.&lt;/p&gt;

&lt;p&gt;Let's start with the first route, which will be &lt;strong&gt;create&lt;/strong&gt; a listing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// post a new listing&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/listings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newListing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newListing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second route will allow to &lt;strong&gt;read&lt;/strong&gt; all listings. In this case, the &lt;code&gt;{}&lt;/code&gt; inside the find method brings all listing, but we can also add any filters that we want inside &lt;code&gt;{}&lt;/code&gt;. Let's have in mind that these filters must be in the MongoDB language, which is different from a later update of these route where we have optional filters, using in that case JavaScript filters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// get all listings&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/listings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;listingsData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({}).&lt;/span&gt;&lt;span class="nx"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listingsData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third one will take a listing id and &lt;strong&gt;update&lt;/strong&gt; that listing. In the Insomnia body we can send the "payload", which is the updated data for our listing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// update a listing&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/listings/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedListing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;updateOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$set&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updatedListing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the fourth one to complete our CRUD operations is to &lt;strong&gt;delete&lt;/strong&gt; a listing using its id.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// delete a listing&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/listings/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deletedListing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deleteOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deletedListing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This project has more routes, such as reading an specific listing, getting all reviews from one listing, creating, updating and deleting a review from one listing. For your reference, this is the &lt;a href="https://github.com/dubymarjtr/airbnb-project"&gt;project repo&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;These basic routes allow us to perform CRUD operations in our database, and although it is just a server-side project, this gives you an idea on how to create a full-stack website, if you already know how to connect to the client-side. Node.js and Express.js made possible to create and work with the server without knowing any other server-side language, such as PHP. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>mongodb</category>
      <category>express</category>
    </item>
    <item>
      <title>JavaScript References</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Thu, 28 Oct 2021 19:36:42 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/javascript-references-1k7g</link>
      <guid>https://forem.com/dubymarjtr/javascript-references-1k7g</guid>
      <description>&lt;p&gt;A few years back when I was studying Electronic Engineering, one of the requisites was Programming I, II, and III. &lt;strong&gt;C++&lt;/strong&gt; was the first programming language I learned, and I must say it was the reason I chose my current career (I realized I hated dealing with hardware). One of my professors was an old lady that used to teach in the classroom writing code on the board, instead of using a computer. Although I thought it was a waste of time, writing things down several times helped me understand the core concepts. &lt;/p&gt;

&lt;p&gt;I remember a class when she explained &lt;strong&gt;references&lt;/strong&gt;, and how every piece of data has a place in memory. At first, it was quite difficult to understand, but finally, she used the word "&lt;strong&gt;pointer&lt;/strong&gt;". Ok, I know what a pointer is and I can picture it (kind of like an arrow, I will say), now what? Well, let's say we have a variable &lt;em&gt;named&lt;/em&gt; &lt;code&gt;myFavCar&lt;/code&gt; that stores a &lt;em&gt;value&lt;/em&gt; of &lt;code&gt;"Ferrari"&lt;/code&gt;. This piece of data is stored in someplace in the computer's memory, and it can be accessed by using its name, which is &lt;strong&gt;pointing&lt;/strong&gt; to that place in memory. So, whenever we want to display the value of a variable, the function &lt;code&gt;console.log&lt;/code&gt; uses the variable name as an argument and gets the data from where is stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myFavCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ferrari&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myFavCar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "Ferrari"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Primitives vs. Objects
&lt;/h2&gt;

&lt;p&gt;If we want to reassign that value to "Lamborghini" instead, we should start talking about &lt;em&gt;immutability&lt;/em&gt;, and how primitives (&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Null&lt;/code&gt;, and &lt;code&gt;Undefined&lt;/code&gt;) and &lt;strong&gt;immutable&lt;/strong&gt; and how &lt;code&gt;Objects&lt;/code&gt; (pretty much everything else in JS) &lt;em&gt;can&lt;/em&gt; be modified. Since we are using a &lt;code&gt;String&lt;/code&gt; variable, is not like Lamborghini will override &lt;del&gt;Ferrari&lt;/del&gt;. We can't modify a &lt;code&gt;String&lt;/code&gt;, so what is going to happen is that myFavCar now will be pointing to &lt;strong&gt;another&lt;/strong&gt; place in memory where this new value is stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myFavCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ferrari&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;myFavCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lamborghini&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myFavCar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "Lamborghini"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes a lot of sense when I think about String methods returning a new String instead of modifying the current String value. That's why if we want to keep that new value, we have to store it in a new variable (&lt;em&gt;aka&lt;/em&gt;, pointer to a new place in memory).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;text3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;text1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "Hi there"&lt;/span&gt;

&lt;span class="c1"&gt;// this does not modify text1 or text2 values&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How does mutation work?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;mutation&lt;/strong&gt; is something that we want to avoid at all costs, and that means making sure none of our functions change their arguments or anything outside of them. This kind of function is called &lt;strong&gt;pure function&lt;/strong&gt; (I learned that today!). Whenever we pass an object as an argument for a function, that function can modify the object that's outside of the scope. A way to avoid this kind of mutation is to use the spread operator (&lt;code&gt;...&lt;/code&gt;) and save a copy of the object and modify and return that copy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;dob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2/13/1987&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123 Park Ave&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;subscriptionAct&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// create a variable that stores a copy of the object&lt;/span&gt;
   &lt;span class="c1"&gt;// this will be a NEW object&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;customerUpdated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// modifies the copy&lt;/span&gt;
   &lt;span class="nx"&gt;customerUpdated&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionAct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;customerUpdated&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// the function will return a new object&lt;/span&gt;
&lt;span class="c1"&gt;// we save it in a variable&lt;/span&gt;
&lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updateInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the spread operator, &lt;code&gt;customerUpdated&lt;/code&gt; will point to the customer object. So pretty much we will have customer and customerUpdated pointing at the &lt;strong&gt;same&lt;/strong&gt; object. This is called &lt;em&gt;copy by reference&lt;/em&gt;, and it means that if we modify either one, it will affect both of them. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Copy by value&lt;/em&gt; can be done with primitives, since they can't be modified, they just create a new place in memory to store that new value (even if it's a copy).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "hello"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints "b"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;a&lt;/code&gt; copies the value that's stored in &lt;code&gt;b&lt;/code&gt;, which in this case is a String. After reassigning &lt;code&gt;b&lt;/code&gt; a new value, we can notice that &lt;code&gt;a&lt;/code&gt; didn't change. That's because it copied just the value, but does not reference that address where &lt;code&gt;b&lt;/code&gt; is pointing at.&lt;/p&gt;

&lt;p&gt;Understanding these concepts was difficult and very confusing at the beginning, but after a few years of programming in different languages, I was able to understand, being a crucial part of data structures and algorithms. I used this &lt;a href="https://daveceddia.com/javascript-references/"&gt;article&lt;/a&gt; as a reference for writing this post, which helped me understand these concepts even more. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Introduction to Database Schemas</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Wed, 20 Oct 2021 17:18:55 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/introduction-to-database-schemas-9dk</link>
      <guid>https://forem.com/dubymarjtr/introduction-to-database-schemas-9dk</guid>
      <description>&lt;p&gt;About I year ago I took &lt;em&gt;Introduction to Databases&lt;/em&gt; at my college, where I first learned &lt;strong&gt;SQL&lt;/strong&gt;. I consider it to be quite a simple language to understand and write, considering my basic experience in that class. I knew database is an important concept for web developers to understand, so I made sure I understood the foundations. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are database schemas and how to implement one?
&lt;/h2&gt;

&lt;p&gt;When learning about &lt;strong&gt;databases schemas&lt;/strong&gt;, the best way to describe them would be as a "blueprint". This blueprint will contain the shape and format of the data, as well as the necessary patterns to retrieve the information when requested. This is called &lt;em&gt;logical schema&lt;/em&gt; and it is the first step to developing a database. &lt;/p&gt;

&lt;h3&gt;
  
  
  Designing schemas for relational databases
&lt;/h3&gt;

&lt;p&gt;As I learned with relational (or static) databases, we do this applying Entity-Relationship (ER) models, which are diagrams that &lt;em&gt;describe entities, their attributes, and how they relate to one another&lt;/em&gt;. Using ER models in the early stage of designing the database is very convenient since it gives you a clear idea of how your data will be stored, connected, and retrieved.  &lt;/p&gt;

&lt;p&gt;In class, the first thing I used to do was determine the &lt;strong&gt;entities&lt;/strong&gt; that I had, and by an entity I mean a real-life "object" or "thing" that exists, something that we can identify. These entities have &lt;strong&gt;attributes&lt;/strong&gt;, which describe the characteristics of these objects. One (or a combination) of these attributes should be unique, which would be used as the identifier. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bo26zuwqnggy7mkjpej.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bo26zuwqnggy7mkjpej.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, the &lt;em&gt;fun&lt;/em&gt; part: figuring out how the entities are going to relate to one another. This is done using minimum and maximum cardinality, which will determine one-to-one, one-to-many, or many-to-many relationships. For example, &lt;strong&gt;students and classes&lt;/strong&gt;: students can take many classes, and classes can be taken by many students. This easy example represents a many-to-many relationship, but in real life, even if you know how your data will be, figuring this out can be a headache.&lt;/p&gt;

&lt;p&gt;Once everything is looking good we start developing the &lt;strong&gt;physical schema&lt;/strong&gt;. The first step is to declare the tables, one per entity, and declare its attributes as columns. A very important step is to identify the primary key, which sometimes can be unique or composite. These keys will be used as foreign keys in other tables to relate to one another.&lt;/p&gt;

&lt;p&gt;In my basic experience, relating tables can get complicated. Basic relationships between tables might just include a primary key of one table as the foreign key in another table, but also might include several tables with primary keys from different tables as foreign keys in one or more tables. So, it can get pretty messy.&lt;/p&gt;

&lt;p&gt;During this stage, we use &lt;strong&gt;normalization&lt;/strong&gt; to minimize data redundancy and inconsistencies. Normalization is a process where we split tables depending if we encounter certain elements that can be treated in their separate table. Another thing to have in mind is predicted query patterns and what tables will be accessed more than others. By doing this we can identify database indexes, so we can &lt;em&gt;speed up the retrieval process&lt;/em&gt;. &lt;/p&gt;
&lt;h3&gt;
  
  
  Designing schemas for non-relational and NoSQL databases
&lt;/h3&gt;

&lt;p&gt;This process is often quite different. Non-relational databases are used for &lt;em&gt;high performance using a limited number of predefined queries&lt;/em&gt;. These database schemas are designed depending on the application that will use them. &lt;/p&gt;

&lt;p&gt;The first step is to determine the primary queries (pseudo queries, since the database, does not exist yet) the database needs to run. Knowing this will help us understand how we can structure our data in a way that is optimized to retrieve.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1294730&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;john.doe@email.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123 Main St&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;orderID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Laptop&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;599.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
              &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;orderID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;product&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bed sheets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;35.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 
            &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Working with JavaScript, we can use an object as an example. This object will track information for a &lt;strong&gt;customer&lt;/strong&gt; and its &lt;strong&gt;orders&lt;/strong&gt;, so here we can have an idea of how the information can be accessed. First access the customer object, which in this case is one simple object, but realistically it will be an array of objects. Once there, we use the customer id to locate the record that we want. Then we can access its properties, such as email or address. We can also access a nested array of objects (or records in this case) such as &lt;strong&gt;orders&lt;/strong&gt;. Doing this we can know all the orders placed by costumers.&lt;/p&gt;

&lt;p&gt;This is very convenient and easier to work with by the fact that all the information related to an entity can be stored in one single record, unlike relational databases. But non-relational databases are not great at combining data from multiple entities in a single query, so we have to decide the best way to represent our data. Avoiding duplicated data will reduce the number of items to maintain. &lt;/p&gt;

&lt;p&gt;In my JavaScript project for this semester, I am planning to create an e-Commerce, where I can implement a non-relational database using MongoDB to get started with back-end development. &lt;/p&gt;

</description>
      <category>database</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Comparing Database Types to MongoDB</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Wed, 13 Oct 2021 18:47:40 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/comparing-database-types-to-mongodb-1389</link>
      <guid>https://forem.com/dubymarjtr/comparing-database-types-to-mongodb-1389</guid>
      <description>&lt;p&gt;A &lt;strong&gt;library&lt;/strong&gt;. We know this place has tons of books, novels, magazines, and other products in-store. If we think that this library is a &lt;em&gt;database&lt;/em&gt;, we can think of their organization method (alphabetical, category, author, etc...) as a &lt;em&gt;database type&lt;/em&gt;. &lt;strong&gt;Database types&lt;/strong&gt; are patterns and structures used to organize data.&lt;/p&gt;

&lt;p&gt;The beginnings of database types go back to the early-60s, and through the years these technologies experienced an amazing development. Some of these historic types have served as the base to advanced database technologies, however, new types have been developed to solve new issues and requirements in modern DBMS. In 1969, &lt;strong&gt;relational databases&lt;/strong&gt; were introduced. This database type worked with tables, having primary keys and foreign keys that connect each table. Like this, tables can organize specific information and refer to other tables' information if needed.&lt;/p&gt;

&lt;p&gt;To access and manipulate this DBMS, a querying language named &lt;strong&gt;SQL&lt;/strong&gt; (Structured Query Language) was created. It connects tables through primary-foreign keys and allows to &lt;em&gt;filter data using constraints&lt;/em&gt;. Like this, we can write complex queries that are very powerful and useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* to retrieve all date in a table: */&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;TABLE_NAME&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* we can filter the data using constraints (WHERE).
If we have a PERSON table with attributes such as Name and Age we can filter the results depending on conditions */&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;PERSON&lt;/span&gt; 
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But as the years pass, newer and better technologies are being developed to facilitate developers' jobs. &lt;strong&gt;NoSQL databases&lt;/strong&gt; are modern database types that manage data differently from the standard relational pattern. Although the name might sound like is the opposite of SQL, it stands for "Not-only SQL" or "Non-SQL", meaning that &lt;em&gt;it may allow SQL-like querying&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Some of them are &lt;strong&gt;Key-value databases&lt;/strong&gt;, which have a key and a value (self-explanatory, right?). They can store simple data such as a JSON object, an image, or plain text. We access the data using the key name. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document databases&lt;/strong&gt; follow the same semantic as key-value databases, but this time they don't have random pieces of data, instead, they store &lt;em&gt;entire documents&lt;/em&gt;, often using formats like JSON or XML. Each document can have a different internal structure, and sometimes this type of database relates to both relational and key-value databases. &lt;strong&gt;MongoDB&lt;/strong&gt; is an example of a document database. &lt;/p&gt;

&lt;p&gt;How does &lt;strong&gt;MongoDB&lt;/strong&gt; compare to the relational model? As we mentioned above, the relational model stores different data in separate tables. Having dozens and dozens of tables can result in a massive amount of complexity to our application. This includes: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Being hard for people to use and understand.&lt;/li&gt;
&lt;li&gt;Difficult to add new features, having in mind all the tables related.&lt;/li&gt;
&lt;li&gt;Retrieving data from many tables involves code statements that might slow down our application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike the relational model, MongoDB stores data using a document, and here we can think about a real piece of paper with specific data. Documents are a simple way to structure your data, being easier for computers (and humans) to process and understand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// writing the same queries above, now using MongoDB&lt;/span&gt;
&lt;span class="c1"&gt;// .find() with a {} (empty document) inside , retrieves all the data within the collection&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;

&lt;span class="c1"&gt;// This query will retrieve all individuals over 18 years old&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;$gt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;}});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This DBMS makes adding new data easy, not having to worry about breaking any other data. It also knows how to coordinate multiple servers to store data, so if one server fails we don't have to worry about our application being affected.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>javascript</category>
      <category>node</category>
      <category>database</category>
    </item>
    <item>
      <title>Introduction to Databases</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Fri, 08 Oct 2021 15:48:25 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/introduction-to-databases-1191</link>
      <guid>https://forem.com/dubymarjtr/introduction-to-databases-1191</guid>
      <description>&lt;p&gt;Databases are &lt;em&gt;logical structures&lt;/em&gt; that can store and organized any kind of data for future use. They are an &lt;strong&gt;indispensable component of a web application&lt;/strong&gt;, thus they allow us to process, retrieve, or evaluate our data. They are managed by an application called &lt;strong&gt;DBMS&lt;/strong&gt; (Database Management System), which organizes data according to specific patterns (database models). The &lt;em&gt;relational model&lt;/em&gt; is perhaps considered to be the default paradigm, organizing data into cross-referenced tables, rows, and columns. &lt;/p&gt;

&lt;p&gt;Databases have two ways to store data:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data persistence&lt;/strong&gt;: since the data is store &lt;em&gt;on disk&lt;/em&gt;, even if the application or computer shuts down and restart, everything will continue there.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ephemeral storage&lt;/strong&gt;: the data is saved &lt;em&gt;in-memory&lt;/em&gt;, but it is volatile, meaning that it does not survive a system shut down. Although the data might not be saved, in-memory databases are usually faster. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that we now how data is store, how can we interact with it? Databases provide an interface for users or applications, that allow us to access and interact with what's stored there. There are four types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data definition&lt;/strong&gt;: allow us to create, modify, and remove definition's of the data structure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: allow us to insert, modify, and delete data within the database. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Retrieval&lt;/strong&gt;: allow us to access the stored data. It can be access as-is, or it can be filtered for a specific result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Administration&lt;/strong&gt;: these tasks are not directly related to our data, but it is important for user management, security, and performance monitoring.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating, modifying, or deleting data are &lt;strong&gt;potentially dangerous&lt;/strong&gt; operations, often known as "write operations". On the other hand, &lt;em&gt;retrieving&lt;/em&gt; data allow users to access that information for certain use, without modifying or interfering with the database itself.&lt;/p&gt;

&lt;p&gt;In the case that we don't want to use a database, there are a few options that, in some way, are also consider a database:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Local memory or temporary filesystems&lt;/li&gt;
&lt;li&gt;Serialize application data directly to the local filesystem&lt;/li&gt;
&lt;li&gt;Storing file-like objects directly to disk&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Database are used for many reasons, some of them are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storing and processing site data&lt;/strong&gt;, which affects how information is organized on the site. If we have an e-Commerce, our database can store user information, product availability, and orders history.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Analyzing information to make better decisions&lt;/strong&gt;, which relates to the basic functionality of the website. This operations, called analytics, provide answers to specific questions such as product trends, states where we ship the most, and factors that influence sales.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Understanding Node.js</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Tue, 05 Oct 2021 04:25:04 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/understanding-node-js-5n8</link>
      <guid>https://forem.com/dubymarjtr/understanding-node-js-5n8</guid>
      <description>&lt;p&gt;When the majority of us started learning about &lt;em&gt;web development&lt;/em&gt;, the first thing we came across was &lt;strong&gt;HTML&lt;/strong&gt;. Then we learned about &lt;strong&gt;CSS&lt;/strong&gt; to make our websites prettier and visually satisfying. When it was time to make our websites interactive and dynamic, we started diving into the amazing world of &lt;strong&gt;JavaScript&lt;/strong&gt;. But back in the day, JS was used just to code the client-side, meaning that &lt;em&gt;it could only be used in the browser&lt;/em&gt;. To interact with the server we needed to use other server-side languages such as Python, Ruby, or PHP. Until &lt;strong&gt;Node&lt;/strong&gt; was born.&lt;/p&gt;

&lt;p&gt;But, what is Node and how does it work? Node is a &lt;em&gt;runtime environment&lt;/em&gt; that allows us to run JavaScript on a &lt;strong&gt;physical machine&lt;/strong&gt;, rather than in the browser. At the same time, it is open-source, whereby millions of developers can create libraries and modules that can be downloaded for us to use. &lt;/p&gt;

&lt;p&gt;Node uses V8, which is &lt;strong&gt;Google Chrome's JavaScript engine&lt;/strong&gt;. Both the &lt;em&gt;browser&lt;/em&gt; and &lt;em&gt;Node&lt;/em&gt; use JavaScript as their programming language, but unlike the browser, with Node, we don't interact with the DOM. We have innumerable APIs that Node provides through their modules, which will allow us to program the server-side &lt;em&gt;without learning any other language&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;To get started with Node, we first have to &lt;em&gt;download&lt;/em&gt; it to our computers from its &lt;a href="https://nodejs.dev/download/"&gt;website&lt;/a&gt;. Packages are available for all major platforms, and it's very easy to install. Select the correct operating system and once being installed, click next all the way through the end, and that's it! &lt;/p&gt;

&lt;h2&gt;
  
  
  npm
&lt;/h2&gt;

&lt;p&gt;npm stands for &lt;strong&gt;Node Package Manager&lt;/strong&gt;, and as it is self-explanatory, it manages all modules with the necessary dependencies for our project. Nowadays, there are more than 350,000 packages declared in Node.js. &lt;/p&gt;

&lt;p&gt;To start using node, first, we go to our project and create a &lt;code&gt;package.json&lt;/code&gt; (if it's not already part of the project). After that, we go to our terminal and type &lt;code&gt;npm install&lt;/code&gt; or &lt;code&gt;npm i&lt;/code&gt;, then Node goes to &lt;a href="https://www.npmjs.com/"&gt;nmpjs.com&lt;/a&gt; and downloads all dependencies and creates a folder named &lt;code&gt;node_modules&lt;/code&gt; where it's all stored. At the same time, it stores all &lt;em&gt;names and versions&lt;/em&gt; of those dependencies in the &lt;code&gt;package.json&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;This &lt;code&gt;package.json&lt;/code&gt; can do many different things, unrelated to one another, and it has no requirements other than following the JSON (JavaScript Object Notation) format. Some basic declarations that we can make in our &lt;code&gt;package.json&lt;/code&gt; file are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "my-project",
  "version": "1.0.0",
  "description": "A JS project",
  "main": "src/main.js",
  "private": true,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's take a look at something interesting:&lt;br&gt;
In our &lt;code&gt;package.json&lt;/code&gt; we specify the packages' version in our project, as well as the minimum being used and the new versions to be installed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If we type &lt;code&gt;~0.1.0&lt;/code&gt;, it will update patch releases, like &lt;code&gt;0.1.1&lt;/code&gt;, but not &lt;code&gt;0.2.0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If we type &lt;code&gt;^0.1.0&lt;/code&gt;, it will get updates that don't change the leftmost non-zero, like &lt;code&gt;0.1.1&lt;/code&gt;, &lt;code&gt;0.1.2&lt;/code&gt;, but not &lt;code&gt;0.2.1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If we type &lt;code&gt;0.2.1&lt;/code&gt;, that is the exact version that we will always use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the case that we &lt;em&gt;do not type the specific version that we will use&lt;/em&gt;, when someone on the other side of the world tries to download the project and run &lt;code&gt;npm i&lt;/code&gt;, the latest versions will be downloaded, which will result in a project with different versions of dependencies. This was a very specific problem that &lt;code&gt;package.json&lt;/code&gt; left unsolved, but now we have something called &lt;code&gt;package-lock.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Package-lock.json&lt;/code&gt; stores the &lt;em&gt;current versions that our project is using&lt;/em&gt;, and installs &lt;strong&gt;the right one&lt;/strong&gt; from npmjs.com when running &lt;code&gt;npm i&lt;/code&gt;. In case we want to update the dependencies' version, we run &lt;code&gt;npm update&lt;/code&gt; and it will update them in the &lt;code&gt;package-lock.json&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>npm</category>
    </item>
    <item>
      <title>Async + Await</title>
      <dc:creator>Dubymar Tollinchi</dc:creator>
      <pubDate>Mon, 04 Oct 2021 03:48:59 +0000</pubDate>
      <link>https://forem.com/dubymarjtr/async-await-2l10</link>
      <guid>https://forem.com/dubymarjtr/async-await-2l10</guid>
      <description>&lt;h2&gt;
  
  
  What is control flow?
&lt;/h2&gt;

&lt;p&gt;It is the &lt;strong&gt;order&lt;/strong&gt; in which statements, instructions, or functions &lt;strong&gt;are executed in our code&lt;/strong&gt;. JavaScript is an &lt;em&gt;asynchronous&lt;/em&gt; language, meaning that it reads code line by line, from top to bottom.&lt;/p&gt;

&lt;p&gt;If there is a line that needs to &lt;em&gt;fetch&lt;/em&gt; any information from a database, for example, it will kick off that data fetching and &lt;br&gt;
continue reading the next lines immediately, instead of waiting for the fetching to be done. &lt;/p&gt;

&lt;p&gt;JavaScript being &lt;em&gt;asynchronous&lt;/em&gt; makes it difficult to write any code that will suddenly stop or block any application process. In the past some of the solutions for this issue were implementing callbacks, which is &lt;em&gt;a function that gets passed as the last argument of another function&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;This was fine until you encounter what developers call &lt;strong&gt;Callback Hell&lt;/strong&gt;, which is a callback within a callback, within a callback, and so on, which makes it very difficult to read and maintain the code. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a promise?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;promise&lt;/strong&gt; is the &lt;em&gt;state of the result that gets return&lt;/em&gt;, so instead of having 10 lines of code indented with all those callbacks you can do it step by step. If something bad happens to the data, then you have an error handling, such as try catch. If you get the data successfully, then you execute a code, and like this, you write &lt;code&gt;.then&lt;/code&gt; for each action that you want. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Async + Await&lt;/strong&gt; is the same as promises but with a better syntax. Rather than having &lt;code&gt;.then&lt;/code&gt;, we can put &lt;code&gt;await&lt;/code&gt; in front of the promise and it will temporarily freeze that line of code until the promise is &lt;em&gt;resolved&lt;/em&gt; or &lt;em&gt;rejected&lt;/em&gt;, and then continues reading the following lines.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;promises.all()&lt;/code&gt; are &lt;strong&gt;Mega promises&lt;/strong&gt;, which are no other thing than a promise made of many promises, and it will return a promise where it will only resolve itself when all pieces of data are resolved. &lt;/p&gt;

&lt;h2&gt;
  
  
  Browser API's
&lt;/h2&gt;

&lt;p&gt;Most new browser API's are build on native promises. &lt;strong&gt;Fetch&lt;/strong&gt; is the way to get data from an API, so we can use await fetching data. &lt;/p&gt;

&lt;p&gt;The fetch API is like a &lt;em&gt;double promise&lt;/em&gt;, because you fetch some data from your API and then you need to &lt;em&gt;convert that data&lt;/em&gt; (most of the time we want to convert it into JSON). &lt;strong&gt;Axios&lt;/strong&gt; is also based on promises and they have some JSON defaults, so we don't need to convert the data.&lt;/p&gt;

&lt;p&gt;JS's &lt;code&gt;util&lt;/code&gt; package has a &lt;strong&gt;promisify&lt;/strong&gt; function that &lt;em&gt;converts callback-based functions to promise-based functions&lt;/em&gt;, so we can use our code on callback-based API's.&lt;/p&gt;

&lt;p&gt;Since &lt;strong&gt;Async + Await&lt;/strong&gt; is not built in every browser, something that we can do in order to make our code compatible with everything, is to go to Babel.com and &lt;em&gt;convert the code&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error handling
&lt;/h2&gt;

&lt;p&gt;Error handling methods, such as &lt;code&gt;try-catch&lt;/code&gt;, let you place the code that you want to execute in your program in the &lt;code&gt;try&lt;/code&gt; part, and if anything happens, any errors can be handle in the &lt;code&gt;catch&lt;/code&gt; part.&lt;/p&gt;

&lt;p&gt;There is no way to &lt;strong&gt;abort or cancel a promise&lt;/strong&gt;, when, for example, you have two fetch requests going at the same time. One thing that you can do is &lt;code&gt;abortController()&lt;/code&gt;, which will &lt;em&gt;cancel one or more web requests&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
