Create model, migration, seeds with Sequelize

Tram Ho

What is sequelize

Sequelize is an ORode Node.js that supports linking with databases like Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. In this article I will create the model, migrations and seed with sequelize-cli. Let’s get started ?

Setting

First of all we need to install sequelize, we use yarn to install sequelize if you do not have yarn installed, you can see here https://classic.yarnpkg.com/en/docs/install/#debian-stable .

1 Install sequelize:

2 Install the database management system:

Here I use mysql, you can use the other database management systems as follows:

3 Install sequelize-cli:

Next we will create Project with the command

Running the above command will create 4 folders as follows:

  • config: contains the image file telling CLI how to connect to the database.
  • migrations: contains all migration files.
  • models: contains all models of the project
  • seeder: contains all seeds files

Before creating the model we need to reconfigure a bit so CLI knows how to connect to the database, by opening the file: config.js/config

You need to change the username with the password so that it matches the username in your database administration system. A few things to note is that the diaclect Sequelize CLI is mysql by default so when you use another database you need to change it accordingly, if you have not created the database then you need to run the sequelize db:create command sequelize db:create to create the database specified in the config.js file. Okay now we will start creating migration always ?

Create Model (and Migration)

We just need to run the command:

Here we just need to pay attention to:

  • name: model name
  • attributes: list of attributes

Above after running the command will create a model named Contact with the attribute is realName , the address in the model directory, and a file 20200305015249-create-contact.js in the migration directory This is the contact.js file:

Here I have additional attributes like phone , brithhday

Similarly I created another model called Event. Here I define the relationship between Event and Contact as 1-n so we need to declare in the Contact.associate section I have to consider its relationship to the Event as belongTo with the foreign key is eventId . Similar to Event I also have:

Next, run the migration:

Run Migration

Run migration with the command:

This command will execute the steps:

  • Calling the SequelizeMeta table in the database, this table is responsible for saving the migrations that have run on the current database.
  • Next, look for files in the mucj migration that haven’t been run thanks to the above SequelizeMeta table and run them
  • Finally, create a table with names and properties as in the migration file

Create seed

To create the seed we run the command:

In the above command, I created a seed named seed-event into the Event table. Now go to the seeders directory to edit it ? ))

This is the seed file after editing. The only thing left is to run it up ? )))

run the seed

To run the seed we only need to execute the command

This command will execute all seed. A small note that the seeder is not the same as the migration is that its execution history is not saved so you want to change the seed, just go to the editing file and then run it again for the migration when you want to perform the Now that you have changed, you need to create a new migration file.

conclude

So I worked with you to install sequelize and create models, migration, with seed, in this article I just made creating without using undo commands, and manually created seeds without using facker, creating Multiple records for a table. So please support me so I can motivate more for the next part ? . The article is still very lacking and is looking forward to the contributions of everyone, all questions please comment below for me and everyone to answer offline. Thank you for reading.

References :

https://sequelize.org/master/manual/migrations.html

Share the news now

Source : Viblo