Sequelize ORM Migration and Seed Operations
Sequelize ORM Migration and Seed Operations
Sequelize, one of the most popular ORM tools in Node.js based projects, provides great convenience for developers who want to manage database operations with code. Sequelize ORM Migration and Seed operations offer important tools for managing the database schema and adding test or demo data. In this article, we will examine in detail the concepts of migration and seed, how they are applied, and the main issues you may encounter.
What is Sequelize Migration and How Is It Used?
Migration refers to the process that enables the management of changes in the database schema via code and version control (e.g., Git). Migrations ensure that all structural changes such as adding/dropping tables, updating columns, or defining relationships are traceable and repeatable. With Sequelize ORM Migration commands, you can keep your project's database structure always up to date.
Creating a Migration
npx sequelize-cli migration:generate --name users-table
The command above creates a new migration file. The migration name "users-table" is given as an example. In the created file, you should edit the up() and down() functions:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface) => {
await queryInterface.dropTable('Users');
}
};
Applying and Reverting Migrations
npx sequelize-cli db:migrate # Applies all migrations
npx sequelize-cli db:migrate:undo # Reverts the last migration
Seed Operations: Adding Dummy Data
Seed operations, especially during development and testing phases, allow you to add sample or initial data to the database. When Sequelize ORM Migration and Seed Operations are used together, both your schema and your sample data remain completely under your control.
Creating and Using a Seed File
npx sequelize-cli seed:generate --name demo-user
In the seed file, data insertion/removal operations are performed via the bulkInsert and bulkDelete methods:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Users', [{
username: 'test_user',
createdAt: new Date(),
updatedAt: new Date()
}], {});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Users', null, {});
}
};
Applying and Reverting Seeds
npx sequelize-cli db:seed:all # Runs all seed files
npx sequelize-cli db:seed:undo:all # Deletes all added dummy data
Conclusion: The Importance of Migration and Seed in Modern Projects
Sequelize ORM Migration and Seed operations provide a code-managed, secure, and practical infrastructure in team-developed projects. While migrations allow full control over the database schema, seeds enable fast tests with repeatable sample data across different environments. Especially in large, scalable applications, these practices are important for code quality and sustainability. By configuring Sequelize ORM correctly, you can achieve maximum efficiency and minimum errors in migration and seed operations.

Yorum Gönder