Exploring CRUD Operations and Fetch Methods in JavaScript
Tags: webdev, javascript, beginners, database
Published: 2023-08-18 21:34:28

## Introduction:  

&nbsp;&nbsp;&nbsp;&nbsp;For web developers, understanding how to manipulate data is crucial. An important acronym for describing these operations is CRUD: Create, Read, Update, and Delete.  These operations form the backbone of data management.
&nbsp;&nbsp;&nbsp;&nbsp;Fetch methods are an example of the means in which developers can use JavaScript to interact with APIs and databases with ease.  In this blog I will be going into detail about some of the functionality of CRUD operations and explore the Fetch API.  
&nbsp;  

## Understanding the Basics of C.R.U.D. Operations  
&nbsp;  
### Create:  
  
&nbsp;&nbsp;&nbsp;&nbsp;The first word in the CRUD acronym, **Create**, entails the adding *new* data to a data source. This can include a range of actions from submitting a form on an App to creating a user profile.  
&nbsp;    
### Read:  
  
&nbsp;&nbsp;&nbsp;&nbsp;The second word in the CRUD acronym, **Read**, describes the process of retrieving data from a source. This could, for example, include fetching a user's profile information after logging in, or returning data from a search function.  
&nbsp;  
  
![Picture of a search bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3b88o6nl9kur5icm7ka3.png)  
&nbsp;  

### Update:  
  
&nbsp;&nbsp;&nbsp;&nbsp;The **U** in CRUD stands for **Update**.  When you update data, you're modifying an existing data-point.  You could be replacing data with entirely new data or even just modifying a specific piece of data.  An example would be modifying your profile picture, or editing an embarrassing list of your favorite bands.  
&nbsp;  

![picture of Band Names crossed out](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1iakb6aqx3673xkz22r.png)  
&nbsp;  

### Delete:  
  
&nbsp;&nbsp;&nbsp;&nbsp;The final word of the CRUD acronym is **Delete**.  This operation, as its name implies, encompasses deleting data from a source. An example of **Delete** would be removing a regrettable post on a social media platform.  
&nbsp;  
  
![Image of #covfefe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8by5tpzmjb1ypgwocymk.png)  
&nbsp;  
  
## Understanding Fetch API  
&nbsp;    
&nbsp;&nbsp;&nbsp;&nbsp;The Fetch API functions as a versatile (and powerful) tool in JavaScript for accomplishing your CRUD goals and interacting with API's. It is one of the first thing new developers learn after grasping the basics of JavaScript.  
&nbsp;  
### fetch() GET Method; Reading the Data:  
  
&nbsp;&nbsp;&nbsp;&nbsp;The fetch() function will start a GET request to a specific URL (placed in the fetch parentheses 'fetch(urlHere)'), and return a "Promise" that turns into whatever server you are accessing's data into a response from the server. In turn, this response can be turned into a readable format, such as JSON.  
&nbsp;  

![visual for fetching data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i716o0b8410mijz1byej.png)  
&nbsp;  

### fetch() POST Method; Creating the Data:  
  
&nbsp;&nbsp;&nbsp;&nbsp;When you are writing or creating new data, you will be utilizing the fetch() POST method. When using the POST method, you have to supply the URL (same as with the GET method!), and an object that holds specifies the type of method ("POST" in this example), the headers, and the specific data you want to send to the server.  
&nbsp;  
  
![visual for posting data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma1zxsxr0vlc3ezyxol0.png)  
&nbsp;  
  
### fetch() PUT or PATCH Method; Updating the Data:  
  
&nbsp;&nbsp;&nbsp;&nbsp;To update data, you would utilize the fetch() PUT or PATCH methods.  Like the POST method, you need to use the server URL and provide an object with the information you need to edit, along with how to that information.  
&nbsp;  
  
![visual for patching data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mi2cxpflrozixekqzhu.png)  
&nbsp;  
  
### fetch() DELETE; Deleting the Data:  
  
&nbsp;&nbsp;&nbsp;&nbsp;To remove data from a dataset, you will utilize the fetch() DELETE method.  It will send a request to the server to delete a particular set of data, and the server in turn will remove the specified bit.  
&nbsp;  
  
![visual for deleting data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gpcj3mwf7wzafdf6hp0.png)  
&nbsp;  
  
## In Conclusion:  
  
&nbsp;&nbsp;&nbsp;&nbsp;Learning the ins and outs of CRUD operations and how to utilize fetch methods in order to achieve those CRUD operations will be imperative to your development as a developer.  These tools and strategies will empower you to create cool and dynamic web applications that have a highly interactive user experience.  Mastering how to utilize **Create**, **Read**, **Update**, and **Delete** using fetch methods **GET**, **POST**, **PATCH**, and **DELETE** will be a major step in your learning journey.  
&nbsp;  
  
### On a More Personal Note About My Experiences:
&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;I chose to write about this particular subject in my first blog-post because I know i struggled with the concept of understanding how fetch() communicates with the server.  While learning about it I was hoping to find a less technical explanation with visual aids, but I struggled to find anything that I found satisfactory in that regard.
&nbsp;&nbsp;&nbsp;&nbsp;I am really hoping that this post can provide that to another person out there in the world.  I would also like to add that if anyone is struggling to understand coding concepts and functions:  *Don't give up!*  
&nbsp;&nbsp;&nbsp;&nbsp;Learning a new skill is incredibly difficult, but hopefully the task can be rewarding and push you to grow through the experience.  I implore you to keep struggling (or if not if you are a savant), and whether you succeed or fail, know that in the end you have bettered yourself, one way or another.