NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Creating migrations in a Node.js application is essential for managing database schema changes effectively. For this purpose, Sequelize is a popular ORM (Object Relational Mapping) library that supports migrations, especially when working with SQL databases. Here’s how to set up migrations using Sequelize and Knex.js, another widely used migration tool.

Option 1: Using Sequelize
Step 1: Install Sequelize and Sequelize-CLI
First, install Sequelize, the associated database driver, and Sequelize-CLI:

bash
npm install sequelize sequelize-cli
npm install pg pg-hstore # For PostgreSQL
# or
npm install mysql2 # For MySQL
# or
npm install sqlite3 # For SQLite
Step 2: Initialize Sequelize
Run the following command to set up Sequelize in your project:

bash
npx sequelize-cli init
This command creates a number of directories in your project, including config, models, and migrations.

Step 3: Configure the Database
Edit the config/config.json file to set up your database connection settings. For example:

json
{
"development": {
"username": "your_username",
"password": "your_password",
"database": "database_name",
"host": "127.0.0.1",
"dialect": "postgres" // or "mysql" for MySQL
},
...
}
Step 4: Create a Migration
To create a migration file, use the following command:

bash
npx sequelize-cli migration:generate --name create-users-table
This command generates a migration file in the migrations directory with a timestamp in the filename.

Step 5: Define the Migration
Open the newly created migration file in the migrations directory and define the up and down functions. For example:

javascript
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
unique: true,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
},
};
Step 6: Run Migrations
To apply the migration, run:

bash
npx sequelize-cli db:migrate
This command will create the Users table in your database. To revert the last migration, use:

bash
npx sequelize-cli db:migrate:undo
Option 2: Using Knex.js
If you prefer using Knex.js, here’s how to set up migrations:

Step 1: Install Knex.js
Run the following command to install Knex.js and the necessary database client:

bash
npm install knex
npm install pg # For PostgreSQL
# or
npm install mysql2 # For MySQL
Step 2: Initialize Knex
Initialize Knex in your project:

bash
npx knex init
This creates a knexfile.js config file you can edit for your database settings.

Step 3: Configure the Database
Edit knexfile.js to set up your database connection:

javascript
module.exports = {
development: {
client: 'pg', // or 'mysql'
connection: {
host: '127.0.0.1',
user: 'your_username',
password: 'your_password',
database: 'your_database',
},
},
...
};
Step 4: Create a Migration
To create a new migration file, run:

bash
npx knex migrate:make create_users_table
Step 5: Define the Migration
Open the generated migration file in the migrations directory and define it:

javascript
exports.up = function (knex) {
return knex.schema.createTable('users', function (table) {
table.increments(); // Auto-increment id
table.string('name');
table.string('email').unique();
table.timestamps(true, true);
});
};

exports.down = function (knex) {
return knex.schema.dropTable('users');
};
Step 6: Run Migrations
To run the migration, use:

bash
npx knex migrate:latest
To rollback the last migration, use:

bash
npx knex migrate:rollback
Conclusion
Both Sequelize and Knex.js offer robust migration tools that make managing database schema changes easy in a Node.js application. Depending on your project's needs, choose one that fits best. If you need more detail on specific steps or have further questions, feel free to ask!
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.