,

Creating a Database Using Express JS: A Step-by-Step Guide

Posted by

Step-by-Step Guide to Set Up a Database with Express JS

Step-by-Step Guide to Set Up a Database with Express JS

Express JS is a popular web application framework for Node.js that provides a robust set of features for building web applications. One of the key components of building a web application is setting up a database to store and manage data. In this article, we will provide a step-by-step guide to setting up a database with Express JS.

Step 1: Install Required Packages

Before setting up a database with Express JS, you will need to install the required packages. One of the most common packages used for working with databases in Express JS is sequelize, which is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. You can install sequelize using npm:

npm install --save sequelize
npm install --save mysql2 // for mysql database

Step 2: Configure Database Connection

Once you have installed the required packages, you will need to configure the database connection in your Express JS application. You can create a new file for database configuration, such as db.js, and add the following code:


const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: 'mysql'
});

module.exports = sequelize;

Step 3: Define Models

After configuring the database connection, you will need to define models for the data you want to store in your database. Models in sequelize are JavaScript classes that represent tables in your database. You can create a new file for your models, such as user.js, and define the user model as follows:


const Sequelize = require('sequelize');
const sequelize = require('./db');

const User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING
  },
  lastName: {
    type: Sequelize.STRING
  }
});

module.exports = User;

Step 4: Sync Database

Once you have defined your models, you will need to sync the database with the models. You can do this by adding the following code to your main Express JS file, such as app.js:


const sequelize = require('./db');
const User = require('./user');

sequelize.sync()
  .then(() => {
    console.log('Database and tables created!');
  });

Step 5: Use Models in Routes

Finally, you can use the models you have defined in your routes to read from and write to the database. You can create a new route for creating a new user, for example:


const express = require('express');
const User = require('./user');

const router = express.Router();

router.post('/users', (req, res) => {
  User.create({
    firstName: req.body.firstName,
    lastName: req.body.lastName
  })
  .then(user => {
    res.json(user);
  });
});

module.exports = router;

By following these steps, you can set up a database with Express JS and start building a robust web application that can store and manage data. By using the sequelize ORM, you can easily work with various databases and take advantage of its powerful features for working with data in your Express JS application.