You find yourself needing to seed hundreds of fake data points. Perhaps it’s for a new feature you want to test out in a project or maybe it’s just for some assignment. You could create all the points manually in the seed file, but that would be painfully tedious… What do you do? Perhaps there’s a node module that could do all of that tedious work for you?!
Faker.js is an interesting node module that allows you to generate all sorts of fake data. Although the module is very powerful and useful, there isn’t a lot of good documentation on how to utilize it. So, I will teach you how.
Before diving into the actual implementation of faker.js, I want to explain some of the fake information that the module is able to generate. Faker has classes of data available: address, commerce, company, database, date, finance, git, hacker, image, internet, lorem, music, name, phone, random, system, time, and vehicle.
In each class, there are a myriad of options for fake data available. The entire list is available as part of the module docs here.
First, you want to install the node package using node package manager
$ npm install faker.js
Next, you want to require faker in whichever file you’re working on. In my case it happens to be my project’s seed.js file.
const faker = require('faker')
Generally, the implementation goes as follows. You declare a variable and set it equal to faker.class.dataPoint(). Here’s a few examples…
const fakeComment = faker.lorem.sentence()
const randomEmail = faker.internet.email()
const xName = faker.name.firstName()
If, you wanted to chain together a bunch of fake information, you could go about it by using faker.fake().
const fullName = faker.fake({{name.lastName}} {{name.firstName}} the {{name.jobTitle}})// This might output something like Bryan Ryu the SWE :)
Here’s how to actually seed hundreds of datapoints
Finally, you are done! Once you run your seed file, your database should be filled with however many datapoints you specified in the for loop.