Create massive random data using faker.js

shrey vijayvargiya - May 31 '22 - - Dev Community

Generate an insane amount of random data using faker.js
Image description

Under the Hood
Recently, I found this npm module while growing through the daily.dev github repository. They have open-sourced their repository and anyone in the world can raise the PR to add new features or resolve some existing bugs.

This is quite a good approach for all developers to explore the production-based code.

This story begins when I was exploring the third party packages daily dev is using and I found this library called Faker.js.

https://fakerjs.dev/

What is Faker.js?
It's an easy guess for everyone, it will help to generate a massive amount of fake data. Faker provide fake data for 6 domains ranging from

  • Products,
  • Finance,
  • Addresses,
  • Hacker Jargon
  • Time-based data &
  • Localization
    You might be confused why would anyone in the world need to work with fake data. Here are the ways to use this massive sample data -

  • For testing the existing applications

  • For showcasing your projects by using fake data and handling POST and GET request
    It is helpfull when you can’t affect your database with actual data, it is helpfull when you want to test some application for production-based, and it is helpfull for developers to develop the sample projects using this sample fake data.

Installation and Working Process
Let's begin with installing the faker npm module and below is the command to install it.

yarn add '@faker-js/faker';
Once installed it's ready to use. Note you can use it in a vanilla javascript project by adding the below script tag.

<script type="text/javascript" src="https://unpkg.com/@faker-js/faker"></script>

Enter fullscreen mode Exit fullscreen mode

Faker is compatible with Node, Java and Python and by making some changes with the tsconfig file you can use it with typescript also.

In order to have faker working properly, you need to check if these compilerOptions are set correctly in your tsconfig file:

{
    "compilerOptions": {
     "esModuleInterop": true,
     "moduleResolution": "Node"
    }
  }
Enter fullscreen mode Exit fullscreen mode

Then import it as you did in the javascript project.

API
Not only sample names but a lot of different domain sample data can be generated using faker. Here are the some cases with examples —

  • Address From getting building numbers to the city or to the country name, you can get the data using faker.
faker.address.buildingNumber() // => "5786"
 faker.address.cardinalDirection() // => "South"
 faker.address.city() // => "Larrymouth"
Enter fullscreen mode Exit fullscreen mode

Similarly, you can get all kinds of different sample data such as -

  • Date
  • Localization
  • Animal
  • Company
  • Domain name
  • Phone
  • Name
  • Music …. and many more For complete details go to the actual documentation over here.

Conclusion
There is not much to cover regarding faker, whatever is important is covered in the story. I will leave the rest case with you on how you can use this sample data.

Also, if you are developing sample projects then I am adding some stories that will certainly help you. That’s it for today, until next time, have a good day, people.

Our website - iHateReading

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .