Working with Databases

In module 9, we will dive into the integration of databases into
Express.js applications, a critical aspect of building dynamic and datadriven web applications.


9.1 Integrating Database……………….

Why Integrate Databases?
Integrating databases into your Express.js application allows you to
store, retrieve, and manipulate data persistently. Databases serve as
a central repository for information, enabling your application to
manage user data, content, and various resources.
There are different types of databases to choose from, including:
SQL Databases: These relational databases use structured query
language (SQL) for data manipulation. Examples include MySQL,
PostgreSQL, and SQLite.
NoSQL Databases: These non-relational databases are designed for
flexibility and scalability. Examples include MongoDB, Cassandra, and
CouchDB.

Setting up a Database Connection

To work with a database in your Express.js application, you first need
to establish a connection. The specific steps and configuration
depend on the database you're using.
Connecting to a MongoDB Database
To connect to a MongoDB database using the popular Mongoose
library, you typically do the following:
1. Install the Mongoose library: `npm install mongoose`.
2. Set up a connection to your MongoDB server:
- javascript
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydatabase', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection
error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
In this example, we connect to a MongoDB server running locally,
but you would replace the connection URL with your database's URL.
Connecting to an SQL Database
For SQL databases like MySQL or PostgreSQL, you'll need a database
driver such as `mysql2` or `pg` (for PostgreSQL).
1. Install the relevant database driver: `npm install mysql2` or `npm
install pg`.
2. Set up a connection to your SQL database:
- javascript
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase',
});
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL');
});
In this example, we connect to a MySQL database running locally.
You should adjust the configuration according to your database
setup.

Performing Database Operations

Once your Express.js application is connected to a database, you can
perform various database operations, such as querying, inserting,
updating, and deleting data. These operations are essential for
creating, reading, updating, and deleting records (CRUD operations)
in your application.
Let's look at some examples of performing CRUD operations:
Querying Data
To retrieve data from a database, you use queries. In SQL databases,
you write SQL queries, while in MongoDB, you use Mongoose's query
methods.
SQL Query Example:
javascript
connection.query('SELECT * FROM users', (err, results) => {
if (err) {
console.error('Error querying data:', err);
return;
}
console.log('Query results:', results);
});
MongoDB Query Example (Mongoose):
- javascript
const User = mongoose.model('User', userSchema);
User.find({}, (err, users) => {
if (err) {
console.error('Error querying data:', err);
return;
}
console.log('Query results:', users);
});
Inserting Data
To add new data to your database, you use insert operations.
SQL Insert Example:
- javascript
const newUser = { username: 'john_doe', email:
'john@example.com' };
connection.query('INSERT INTO users SET ?', newUser, (err, result) =>
{
if (err) {
console.error('Error inserting data:', err);
return;
}
console.log('Inserted record:', result);
});
MongoDB Insert Example (Mongoose):
- javascript
const newUser = new User({ username: 'john_doe', email:
'john@example.com' });
newUser.save((err, user) => {
if (err) {
console.error('Error inserting data:', err);
return;
}
console.log('Inserted record:', user);
});
Updating Data
To modify existing data in the database, you use update operations.
SQL Update Example:
- javascript
const updatedData = { email: 'new_email@example.com' };
const userId = 1;
connection.query('UPDATE users SET ? WHERE id = ?', [updatedData,
userId], (err, result) => {
if (err) {
console.error('Error updating data:', err);
return;
}
console.log('Updated record:', result);
});
MongoDB Update Example (Mongoose):
- javascript
const userId = 'someUserId'; // Replace with the actual user ID
const updatedData = { email: 'new_email@example.com' };
User.findByIdAndUpdate(userId, updatedData, (err, user) => {
if (err) {
console.error('Error updating data:', err);
return;
}
console.log('Updated record:', user);
});
Deleting Data
To remove data from the database, you use delete operations.
SQL Delete Example:
- javascript
const userId = 1;
connection.query('DELETE FROM users WHERE id = ?', userId, (err,
result) => {
if (err) {
console.error('Error deleting data:', err);
return;
}
console.log('Deleted record:', result);
});
MongoDB Delete Example (Mongoose):
javascript
const userId = 'someUserId'; // Replace with the actual user ID
User.findByIdAndDelete(userId, (err, user) => {
if (err) {
console.error('Error deleting data:', err);
return;
}
console.log('Deleted record:', user);
});
These examples illustrate how to perform basic CRUD operations in
both SQL and MongoDB databases.

9.2 Using Object-Relational Mapping (ORM)

What is an Object-Relational Mapping (ORM)?
An Object-Relational Mapping (ORM) is a programming technique
that allows you to interact with databases using o bjects and classes,
rather than writing raw SQL queries. ORM tools bridge the gap
between your application's object-oriented code and the relational
database.
In Express.js applications, you can use ORM libraries to simplify
database interactions, manage database schemas, and streamline
CRUD operations.
ORM Examples
Using Sequelize (SQL ORM)
Sequelize is a popular ORM library for SQL databases like MySQL,
PostgreSQL, and SQLite.
To use Sequelize in an Express.js application:
1. Install Sequelize and the relevant database driver: `npm install
sequelize mysql2` (for MySQL).
2. Set up Sequelize and define your database models.
- javascript
const { Sequelize, DataTypes } = require('sequelize');
// Initialize Sequelize
const sequelize = new Sequelize('mydatabase', 'root', 'password', {
host: 'localhost',
dialect: 'mysql',
});
// Define a User model
const User = sequelize.define('User', {
username: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
});
// Synchronize the models with the database
sequelize.sync();
3. Perform CRUD operations using Sequelize
- javascript
// Create a new user
User.create({ username: 'john_doe', email: 'john@example.com' });
// Query all users
User.findAll().then((users) => {
console.log('All users:', users);
});
// Update a user
User.update({ email: 'new_email@example.com' }, { where: {
username: 'john_doe' } });
// Delete a user
User.destroy({ where: { username: 'john_doe' } });

Using Mongoose (MongoDB ORM)

Mongoose is an ORM-like library for MongoDB, which simplifies
interacting with MongoDB databases.
To use Mongoose in an Express.js application:
1. Install Mongoose: `npm install mongoose`.
2. Define a schema and model for your data.
- javascript
const mongoose = require('mongoose');
// Define a schema
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true, unique: true },
});
// Define a model
const User = mongoose.model('User', userSchema);
3. Perform CRUD operations using Mongoose
- javascript
// Create a new user
const newUser = new User({ username: 'john_doe', email:
'john@example.com' });
newUser.save();
// Query all users
User.find({}, (err, users) => {
console.log('All users:', users);
});
// Update a user
User.findOneAndUpdate({ username: 'john_doe' }, { email:
'new_email@example.com' }, (err, user) => {
console.log('Updated user:', user);
});
// Delete a user
User.findOneAndDelete({ username: 'john_doe' }, (err, user) => {
console.log('Deleted user:', user);
});
ORMs like Sequelize and Mongoose simplify database operations by
providing an abstraction layer between your application and the
database. This abstraction allows you to work with data in a more
object-oriented manner, making your code cleaner and more
maintainable.

9.3 Performing Database Operations

Routing and Database Operations
To integrate database operations into your Express.js application,
you typically create routes that handle different CRUD operations
and use database models or ORM methods within these routes.
Here's an example of how you might structure routes for a user
management system using Mongoose and Express:
- javascript
const express = require('express');
const router = express.Router();
const User = require('../models/user'); // Mongoose user model
// Create a new user
router.post('/users', (req, res) => {
const newUser = new User(req.body);
newUser.save((err, user) => {
if (err) {
res.status(500).json({ error: 'Failed to create user' });
} else {
res.status(201).json(user);
}
});
});
// Get all users
router.get('/users', (req, res) => {
User.find({}, (err, users) => {
if (err) {
res.status(500).json({ error: 'Failed to fetch users' });
} else {
res.json(users);
}
});
});
// Update a user by ID
router.put('/users/:id', (req, res) => {
const userId = req.params.id;
User.findByIdAndUpdate(userId, req.body, { new: true }, (err, user)
=> {
if (err) {
res.status(500).json({ error: 'Failed to update user' });
} else {
res.json(user);
}
});
});
// Delete a user by ID
router.delete('/users/:id', (req, res) => {
const userId = req.params.id;
User.findByIdAndDelete(userId, (err, user) => {
if (err) {
res.status(500).json({ error: 'Failed to delete user' });
} else {
res.status(204).send();
}
});
});
module.exports = router;
In this example, we define Express.js routes for creating, reading,
updating, and deleting users. The routes use Mongoose methods to
interact with the MongoDB database.
By organizing your routes and database operations in this manner,
you can create well-structured and maintainable Express.js
applications that interact seamlessly with your chosen database.

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
Tech Guides 2024 Privacy policy Terms of use Contact us Refund policy