Beginner’s Guide to Using SQL Databases in Javascript with Sequalize and ExpressJS

Posted by

AM Coder – Sequalize with ExpressJS 101

Sequalize with ExpressJS 101

Welcome to AM Coder’s beginner’s guide to using Sequalize with ExpressJS to set up and use SQL databases in Javascript.

Basic Setup

First, make sure you have Node.js and NPM installed on your machine. Then, you can create a new project folder and navigate to it in your terminal. Run the following command to initialize a new Node.js project:

npm init -y

Next, you need to install Sequalize and Express. Run the following command to install the necessary dependencies:

npm install express sequelize mysql2

Use of SQL Databases in Javascript

Once you have set up your project and installed the required packages, you can start using Sequalize to interact with a SQL database in your ExpressJS application. Here’s a basic example of how to define a model and perform CRUD operations:


      const { Sequelize, DataTypes } = require('sequelize');
      const express = require('express');
      const app = express();

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

      const User = sequelize.define('User', {
        firstName: {
          type: DataTypes.STRING,
          allowNull: false
        },
        lastName: {
          type: DataTypes.STRING
        },
        age: {
          type: DataTypes.INTEGER
        }
      });

      app.get('/users', async (req, res) => {
        const users = await User.findAll();
        res.json(users);
      });

      app.post('/users', async (req, res) => {
        const { firstName, lastName, age } = req.body;
        const newUser = await User.create({ firstName, lastName, age });
        res.json(newUser);
      });

      app.put('/users/:id', async (req, res) => {
        const { id } = req.params;
        const { firstName, lastName, age } = req.body;
        await User.update({ firstName, lastName, age }, { where: { id } });
        res.send('User updated successfully');
      });

      app.delete('/users/:id', async (req, res) => {
        const { id } = req.params;
        await User.destroy({ where: { id } });
        res.send('User deleted successfully');
      });

      app.listen(3000, () => {
        console.log('Server running on port 3000');
      });
    

With the above code, you can perform basic CRUD operations on a ‘User’ model in your SQL database. This is just a simple example to get you started, and there are many more features and capabilities that Sequalize provides for working with SQL databases in Javascript.

For a more in-depth tutorial, be sure to check out the official Sequalize documentation and explore the various options and functionalities available to you.

Happy coding!

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Pareshbpatel
6 months ago

Nice intro to using Sequilize to create a node Express Server. Thanks, Alex.

{2023-03-19}