Chuck's Academy

Express JS

Connecting Express with Databases

Data management is a central aspect of any application. In this chapter, we will learn how to connect Express with databases, both SQL and NoSQL. We will focus on how to interact with databases to store, update, and retrieve data. Also, we will see how to use ORMs and ODMs to facilitate these tasks.

Connecting to SQL Databases (MySQL)

To connect to a MySQL database from Express, we can use the mysql2 package or sequelize as an ORM (Object-Relational Mapping) to abstract the interaction with the database.

Installing MySQL and Sequelize

First, let's install the necessary packages:

bash
"We install the mysql2 package to connect directly to MySQL and sequelize as an ORM that allows us to manage databases more abstractly."

Sequelize Configuration

Next, we configure Sequelize to connect to our MySQL database:

javascript
"Here we configure Sequelize with the database name, username, and password. We use the MySQL dialect and then authenticate the connection. If the connection is successful, a message is logged to the console."

Defining Models with Sequelize

Once connected, we can define models to represent our tables in the database. For example, if we have a users table, we can create a User model:

javascript
"We define the User model with three fields, name, email, and password, all of type string. We use Sequelize dot STRING to define the data type and allowNull to indicate they can't be null. Then, we sync the model with the database using User dot sync."

CRUD Operations with Sequelize

Now, let's see how to perform CRUD (Create, Read, Update, Delete) operations using Sequelize.

  • Create a new user:
javascript
"Here we are creating a new user with the fields name, email, and password. If the creation is successful, the created user is printed to the console."
  • Read users:
javascript
"We use User dot findAll to get all users from the database. The result is an array of users printed to the console."
  • Update a user:
javascript
"To update a user's password, we use User dot update and specify the field to update, password, along with a where condition to identify the user by their email."
  • Delete a user:
javascript
"To delete a user, we use User dot destroy and provide a where condition that identifies the user by their email."

Connecting to NoSQL Databases (MongoDB)

Now, let's see how to connect Express to a NoSQL database like MongoDB. For this, we will use mongoose, an ODM (Object Data Modeling) that simplifies the interaction with MongoDB.

Installing Mongoose

First, let's install the mongoose package:

bash
"We install mongoose with npm install mongoose, which allows us to connect to and manage data in MongoDB more easily."

Mongoose Configuration

To connect to MongoDB, configure mongoose as follows:

javascript
"Here we are configuring mongoose to connect to a local database called mydatabase. We use useNewUrlParser and useUnifiedTopology to ensure that MongoDB's new connection management system is used."

Defining Models with Mongoose

Just like with Sequelize, we can define models to represent documents in MongoDB. For example, a user model would be defined like this:

javascript
"We define a user schema with the fields name, email, and password. Then we create a model named User based on this schema."

CRUD Operations with Mongoose

Performing CRUD operations with Mongoose is similar to Sequelize, but with some differences in syntax.

  • Create a new user:
javascript
"We create an instance of the User model with the user's data and then call save to store it in the database. If it's successfully saved, the user is printed to the console."
  • Read users:
javascript
"We use User dot find to obtain all users in MongoDB. The result is an array of users printed to the console."
  • Update a user:
javascript
"To update a user's password, we use User dot updateOne specifying the user's email in a condition and the new value of the password field."
  • Delete a user:
javascript
"To delete a user in MongoDB, we use User dot deleteOne with a condition based on the user's email."

Conclusion

In this chapter, we have seen how to connect Express to both SQL and NoSQL databases, and how to perform CRUD operations with Sequelize and Mongoose. The ability to interact with databases is essential for any backend application.


Ask me anything