NodeJS

Seeding data in MySQL table using Sequelize and faker.js in NodeJS

Seeding mysql table with sequelize seeder and faker.js

In this guide, We will learn about seeding data in MySQL table using Sequelize ORM and faker.js. Seeding a table simply means adding fake data in the database for testing and demonstration purposes.

In our last post, we did setup Sequelize with NodeJS application and created the table Users. We should start from there and add more columns to the table.

1- Adding more columns to the Users table

To add more columns to existing table in Sequelize, we need to create another migration using npx sequelize-cli migration:create --name add-more-columns-users command.

We now have new migration file created under migrations directory. Go ahead and change this file as follows:

It should be noted here that the first argument of the removeColumn and addColumn function is the name of the table and not of Model. Now run npx sequelize-cli db:migrate, and you should have the Users table as below:

2- Create your first Seeder file in Sequelize with sequeliz-cli

In order to seed the data, we will run npx sequelize-cli seed:generate --name add-users command. The seed file should now appear in seeders directory. Open the seed file and change it’s content as follows:

Again, the first parameter of queryInterface’s bulkInsert method is the name of the table in MySQL db, Don’t confuse it with model name.

We will run the seed now by issuing npx sequelize-cli db:seed:all command. On successful execution, one record should be available in the Users table now. Following points are important to consider before moving forward with the seeders:

  • Unlike migrations, Sequelize doesn’t keep any record of already executed seeds. Whenever we run db:seed:all or db:seed --seed filename.js, the code within the up method of the seed file will be run.
  • Similarly, the code in down method will run when db:seed:undo:all command is executed.
  • db:seed:all executes every file in seeders directory. While db:seed --seed filename.js runs only specific seed file.

3- Combining faker.js with Sequelize Seeders

If we are willing to give our application a real world look, We should have detailed and distinguished data in the DB. In my opinion, faker.js is the best available npm package to achieve this.

faker.js has some built-in objects like name, address, internet, commerce and many more to choose from for our specific requirement. These objects generate random data for different use cases.

We will utilize faker.js name, address and internet objects to fill the data. Open the seed file generated previously and modify it as follows:

In step 2, we only added one record, but here we have compiled an array of objects (JSON) to feed the bulkInsert function. Every time we call a method of faker.js object, it returns a random output. Let’s run the seed again with npx sequelize-cli db:seed:all.

Users table now contains 5000 records as we iterates the loop 5000 times and adding an object in the array on every iteration.

I already have some records added before this is why the 5050 rows are being returned.

Conclusion:

No matter how good backend logic your application has, when you present it to the client it should give a good first impression. The UI should look decent which is only possible when content is available in the DB.

If you have liked this post, consider subscribing to my blog and sharing it.

Tagged , ,

About Zohaib Shah

Passionate software engineer with expertise in Django, Laravel and NodeJS. Working with different SaaS based products and API connected apps. Software Architect of UK's leading online travel agency. Get in touch
View all posts by Zohaib Shah →

Leave a Reply

Your email address will not be published.