1. What is Sequelize and why is it needed
\n\nSequelize is a powerful ORM (Object-Relational Mapping) library for Node.js that supports working with relational databases: PostgreSQL, MySQL, MariaDB, SQLite, and Microsoft SQL Server. ORM allows developers to interact with the database using an object-oriented approach instead of writing raw SQL queries.
\n\nMain advantages of using Sequelize:
\n- \n
- SQL Abstraction — working with tables through JavaScript objects and methods \n
- Security — automatic protection against SQL injections \n
- Migrations — managing database schema changes \n
- Relationships — simple implementation of table relationships (1:1, 1:M, M:N) \n
- Validation — built-in data validation mechanisms \n
- Transactions — support for atomic operations \n
Sequelize is ideal for creating REST APIs, web applications on Express.js, bots, and any projects requiring reliable work with SQL databases.
\n\n2. Installation
\n\nInstalling Sequelize and the driver for your database is done via npm:
\n\n// Base package\nnpm install sequelize\n\n// Driver for PostgreSQL\nnpm install pg pg-hstore\n\n// Driver for MySQL\nnpm install mysql2\n\n// Driver for SQLite\nnpm install sqlite3\n\n// Driver for MSSQL\nnpm install tedious\n\nFor a quick start with SQLite (no server installation required):
\nnpm install sequelize sqlite3\n\n3. Quick Start with Code Example
\n\nLet's create a simple application for managing users:
\n\n// index.js\nconst { Sequelize, DataTypes, Model } = require('sequelize');\n\n// 1. Database connection (SQLite)\nconst sequelize = new Sequelize({\n dialect: 'sqlite',\n storage: './database.sqlite',\n logging: false // disable SQL query logging\n});\n\n// 2. Define User model\nclass User extends Model {}\nUser.init({\n id: {\n type: DataTypes.INTEGER,\n autoIncrement: true,\n primaryKey: true\n },\n name: {\n type: DataTypes.STRING,\n allowNull: false\n },\n email: {\n type: DataTypes.STRING,\n allowNull: false,\n unique: true,\n validate: {\n isEmail: true\n }\n },\n age: {\n type: DataTypes.INTEGER,\n validate: {\n min: 0,\n max: 150\n }\n }\n}, {\n sequelize,\n modelName: 'User',\n timestamps: true // adds createdAt and updatedAt\n});\n\n// 3. Sync with DB and test operations\nasync function main() {\n await sequelize.sync({ force: true }); // recreates the table\n console.log('User table created');\n\n // Create a user\n const user = await User.create({\n name: 'Ivan Petrov',\n email: 'ivan@example.com',\n age: 30\n });\n console.log('User created:', user.toJSON());\n\n // Find all users\n const users = await User.findAll();\n console.log('All users:', JSON.stringify(users, null, 2));\n\n // Find by ID\n const foundUser = await User.findByPk(1);\n console.log('User found:', foundUser.name);\n\n // Update\n await User.update({ age: 31 }, { where: { id: 1 } });\n console.log('Age updated');\n\n // Delete\n await User.destroy({ where: { id: 1 } });\n console.log('User deleted');\n}\n\nmain().catch(console.error);\n\n4. Main Methods and Functions
\n\n4.1. Sequelize.define() / Model.init()
\nDescription: Defining a model (table) with fields and their types.
\nParameters:
\n- \n
modelName— model name \n attributes— object with field definitions \n options— model settings (tableName, timestamps, indexes, etc.) \n