Node.js Sequelize Installation and MySQL Usage
What Will You Learn in This Guide?
Just as Nginx installation and configuration is critical, database management is the heart of modern applications.
In this guide, you will learn how to manage MySQL database securely, sustainably and professionally using Sequelize ORM in Node.js projects.
We handle the entire process hands-on, from installation to complex table relationships, from migration management to performance optimization.
Technical Analysis (Stage 1)
Main Technical Topic:
Using Sequelize, a popular ORM in the Node.js ecosystem, with MySQL.
Solved Problem:
Performing CRUD operations, table relationships and schema management with JavaScript objects without writing raw SQL.
Steps Followed:
- Preparation of the project environment
- Sequelize, MySQL2 and Dotenv installation
- Secure database connection
- Model definition and CRUD operations
- Relationships (1-1, 1-N, N-N)
- Raw SQL and Migration management
What is Sequelize?
Sequelize is an Object-Relational Mapping (ORM) library based on Node.js.
Supports MySQL, PostgreSQL, MariaDB, SQLite and MSSQL.
Advantages:
- Reduces the risk of SQL Injection
- Generates readable and maintainable code
- Defines relationships simply
- Makes schema change safe with migration
1️⃣ Installation and Configuration
1.1 Creating a Project Directory
mkdir genixnode-proje && cd genixnode-proje
npm init -y
npm install sequelize mysql2 dotenv
- These commands create the Node.js project and install the necessary packages.
1.2 Environment Variables (.env)
DB_NAME=genixnode_db
DB_USER=root
DB_PASS=sifreniz123
DB_HOST=localhost
DB_DIALECT=mysql
- This file separates sensitive information from the code.
2️⃣ Establishing the MySQL Connection
require('dotenv').config();
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASS,
{
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT
}
);
sequelize.authenticate()
.then(() => console.log('Bağlantı başarılı!'))
.catch(err => console.error('Bağlantı hatası:', err));
- This code tests MySQL connection with Sequelize.
3️⃣ Defining a Model and Adding Data
const { DataTypes } = require('sequelize');
const Kitap = sequelize.define("kitaplar", {
baslik: { type: DataTypes.STRING, allowNull: false },
yazar: { type: DataTypes.STRING, allowNull: false }
});
- This model represents the books table in the database.
3.1 Creating a Table and Adding Records
sequelize.sync().then(() => {
return Kitap.create({
baslik: "Temiz Kod",
yazar: "Robert C. Martin"
});
}).then(kitap => {
console.log("Kitap eklendi:", kitap.baslik);
});
- This code creates the table and adds sample data. (Suitable for development environment.)
4️⃣ Sequelize Associations
// Bire-Bir
Ogrenci.belongsTo(Sinif);
// Bire-Çok
Yazar.hasMany(Kitap);
// Çoka-Çok
Ogrenci.belongsToMany(Kurs, { through: 'OgrenciKurslar' });
- These relationships make complex queries simple.
5️⃣ Raw SQL Queries
sequelize.query(
'SELECT * FROM kitaplar WHERE yazar = :yazarAdi',
{
replacements: { yazarAdi: 'Robert C. Martin' },
type: sequelize.QueryTypes.SELECT
}
).then(sonuclar => console.log(sonuclar));
- This method runs non-ORM, but secure SQL.
6️⃣ Soft Delete
const Kitap = sequelize.define("kitaplar", {
baslik: DataTypes.STRING
}, {
paranoid: true
});
- paranoid: true does not delete data; deletedAt populates the field.
7️⃣ Migration and Production Usage
1. sequelize.sync() → Development environment only
2. For Production use Sequelize CLI + Migration
3. Every schema change must be versioned
7.1 Performance Tips
1. Do not pull unnecessary columns with attributes
2. Add Index to frequently used fields
3. Paginate large lists
4. Use raw query if necessary
Frequently Asked Questions (FAQ)
Is sequelize.sync() used in production? No. Migration should be preferred.
Why is .env important? It prevents passwords from leaking to GitHub.
Which DBs does Sequelize support? MySQL, PostgreSQL, SQLite, MariaDB, MSSQL.
Is raw query safe? Yes, if replacements are used.
Result
With this guide, you learned how to use Sequelize in Node.js projects with the right architecture, securely and scalably. Now you have a solid foundation in CRUD, relationships, migration and performance.
🚀 You can publish your projects with high-performance servers on the GenixNode infrastructure.

