Need random data for your database? Use Faker.js for that.

If you are a new and upcoming software developer/engineer, then you might have ran into times where you were developing a website/application and you needed to seed a certain amount of data for development purposes. What if you are asked to generate 20+ or 100+ of data? Don’t waste time on single-handedly writing out the data, and use Faker.js.

In this article, we will go through:

  • What is Faker.js
  • How to seed data
  • Visual representation in Postico

What is Faker.js?

  • Faker.js is a JavaScript library that generates realistic fake data in Node.js.
  • Useful when building and testing out your application.

There are so many different fields that you can generate random data for so please check out their documentation at https://github.com/Marak/faker.js.

How to use Faker and seed random data

The objective : Seed 20 random users into a database

  • First you have to install the library.

npm install faker

  • Then you want to require the library in your seed.js file.
  • Next, generate an array that will hold all the objects that will be generated.
  • Now we will create a function that will loop twenty times, each time they will generate an object containing all the random data and push it into usersArr. Then, invoke the function so it actually occurs.
  • Now to seed it into the database, map through the array and use the Sequelize create() method on each object of random data.
  • Once you run the file, you will see this in your terminal.

Visual representation on Postico

These are the 20 users created using Faker.js.

I hope this article can help anyone reading this in their development stage of their application/website.