Support Online
Skip to main content

Using Sequelize with Node.js and MySQL: Comprehensive Installation Guide

What Will You Learn in This Guide?

In this guide, you will learn how to manage the MySQL database in Node.js projects with Sequelize ORM.
Connection setup, model definition, CRUD operations, table relationships and production suggestions are discussed.

Technical Summary

  • Subject: Node.js + Sequelize + MySQL
  • Purpose: Secure and sustainable data management without writing SQL
  • Solved Problem: Complex queries and fragile database structures
  • Scope: Model, CRUD, relationships, .env, performance

Preliminary Preparations

  • A development environment with Node.js installed
  • A working MySQL server
  • Basic JavaScript and SQL knowledge

1. Project Setup and Installation of Packages

mkdir genixnode-orm-projesi
cd genixnode-orm-projesi
npm init -y
npm install sequelize mysql2 dotenv
  • This step creates the project structure and installs the necessary packages.

2. Configuring Database Connection Securely

  1. Connection information should be kept in the .env file.

DB_ISMI=genixnode_kutuphane
DB_KULLANICI=genixnode_admin
DB_SIFRE=GucluSifre123!
DB_HOST=localhost
DB_DIALECT=mysql
  1. Starting the Sequelize Connection

require('dotenv').config();
const { Sequelize } = require("sequelize");

const sequelize = new Sequelize(
process.env.DB_ISMI,
process.env.DB_KULLANICI,
process.env.DB_SIFRE,
{
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT
}
);

sequelize.authenticate()
.then(() => console.log("Veritabanı bağlantısı başarılı"))
.catch(err => console.error("Bağlantı hatası:", err));
  • This code tests MySQL connection.

3. Creating a Model (Table)

  1. In Sequelize, each table is represented by a model.

const { DataTypes } = require("sequelize");

const Kitap = sequelize.define("kitaplar", {
baslik: {
type: DataTypes.STRING,
allowNull: false
},
yazar: {
type: DataTypes.STRING,
allowNull: false
},
yayin_tarihi: {
type: DataTypes.DATEONLY
},
konu_id: {
type: DataTypes.INTEGER
}
});

sequelize.sync().then(() => {
console.log("Kitaplar tablosu hazır");
});
sync() yalnızca geliştirme ortamında kullanılmalıdır.

4. Basic CRUD Operations

  1. Adding a Record

Kitap.create({
baslik: "Simyacı",
yazar: "Paulo Coelho",
yayin_tarihi: "1988-01-01",
konu_id: 1
});
Kayıt Okuma
js

Kitap.findAll();
js

Kitap.findOne({ where: { id: 1 } });
Kayıt Silme
js

Kitap.destroy({ where: { id: 2 } });

5. Inter-Table Relationships (Associations)

  1. One-to-Many

const Ogrenci = sequelize.define("ogrenciler", { isim: DataTypes.STRING });
const Sinif = sequelize.define("siniflar", { seviye: DataTypes.INTEGER });

Sinif.hasMany(Ogrenci);
Ogrenci.belongsTo(Sinif);


Sinif.findAll({ include: Ogrenci });
Many-to-Many (Çoka-Çok)


const Ders = sequelize.define("dersler", { ad: DataTypes.STRING });
const OgrenciDers = sequelize.define("OgrenciDers", {});

Ders.belongsToMany(Ogrenci, { through: OgrenciDers });
Ogrenci.belongsToMany(Ders, { through: OgrenciDers });

6. Safety and Performance Tips

  1. Never write passwords in code

  2. Do not use sequelize.sync() in production environment

  3. Choose a migration system

  4. Add indexes to frequently queried fields

  5. Configure connection pool settings


Frequently Asked Questions

1. Does Sequelize reduce performance? No. In most scenarios the difference is negligible.

2. Why is migration important? It prevents data loss in live systems.

3. What databases are supported? MySQL, PostgreSQL, MariaDB, SQLite and MSSQL.

4. Can I write Raw SQL? Yes, it is possible with sequelize.query().


Result

Sequelize is a powerful ORM solution for Node.js and MySQL projects. When configured correctly, it is secure, scalable and easy to maintain. You can safely use this architecture on the GenixNode infrastructure.