Chuck's Academy

Node.js

Data Management in Node.js

Data management is one of the fundamental pillars in backend development. In Node.js, we can interact with databases efficiently using both SQL and NoSQL databases. In this chapter, we will learn how to connect to relational and non-relational databases, and perform CRUD (Create, Read, Update, Delete) operations on them.

Connecting to Databases in Node.js

Node.js makes it easy to connect to multiple types of databases using specific libraries. Below, we will explore how to work with MySQL (SQL) and MongoDB (NoSQL).

Connecting to MySQL with Node.js

MySQL is one of the most widely used relational databases in backend development. To connect to a MySQL database in Node.js, we use the mysql2 package.

First, let's install the package:

bash
"Run 'npm space install space mysql two' in the terminal to install the mysql2 library."

Once installed, we can set up a basic connection to MySQL as follows:

javascript
"In this example, we use the 'mysql two' library to create a connection to our MySQL database. We define the connection parameters, such as host, user, password, and database, and then establish the connection. If there is an error, we display it in the console; otherwise, we display the message 'Connected to MySQL database'."

Connecting to MongoDB with Node.js

MongoDB is a very popular NoSQL database in the world of web development. To connect to MongoDB in Node.js, we use the mongoose package, which facilitates interaction with this database.

Install mongoose:

bash
"Run 'npm space install space mongoose' to install the Mongoose library."

Here is an example of how to connect to a MongoDB database using Mongoose:

javascript
"In this code, we use the Mongoose library to connect to a local MongoDB server on port twenty-seven thousand seventeen. If the connection is successful, we display the message 'Connected to MongoDB'; if not, we display the error in the console."

CRUD Operations in Databases

Once we have the connection established, we can perform CRUD operations. Below, we will see examples of how to perform these operations in both MySQL and MongoDB.

CRUD Operations in MySQL

CREATE

To insert a new record into a MySQL table, we use the following code:

javascript
"This code inserts a new record into the 'users' table, specifying the name and email. If the insertion is successful, the results are shown; otherwise, the error is displayed."

READ

To fetch data from a table, we do the following:

javascript
"This code selects all records from the 'users' table. If the query is successful, the results are displayed in the console."

UPDATE

To update an existing record in MySQL, we use the following code:

javascript
"This code updates the email of a user with ID one in the 'users' table. If the operation is successful, the message 'Data updated' is displayed."

DELETE

Finally, to delete a record, we use the following code:

javascript
"This code deletes the record with ID one from the 'users' table. If the operation is successful, the results are displayed."

CRUD Operations in MongoDB

CREATE

To insert a new document into a MongoDB collection, we use the following code:

javascript
"Here we define a schema called 'userSchema' and create a new user using that schema. We then use the save method to store the new user in the database."

READ

To fetch documents from a MongoDB collection, we do the following:

javascript
"This code fetches all users from the collection and displays them in the console. If an error occurs, we display it with a message in the console."

UPDATE

To update an existing document, we use the following code:

javascript
"This code updates the email of a user named Alice. If the operation is successful, the message 'User updated' is displayed."

DELETE

To delete a document from the collection, we use:

javascript
"This code deletes a user named Alice from the database. If the operation is successful, the message 'User deleted' is displayed."

Summary

In this chapter, we have learned to connect our Node.js applications to relational (MySQL) and non-relational (MongoDB) databases, and perform CRUD operations on both. These skills are fundamental for any modern backend application.


Ask me anything