Transcript
Howdy, I was recently asked whether there was a way to create dummy or fake data for testing in a sails project. One way to accomplish this is using /config/bootstrap.js
. Although I can automate the creation of the users, I’ll still need some fake users for sails to create. Instead of manually typing in each user’s attributes, I found this really handy online tool called generatedate. You have a bunch of options of how you want your data generated. In this example I’ll select name, company, and email selecting the simple json format. I’ll copy all of these users and head over to the bootstrap.js
file.
I’ll copy our new users into an array called dummyUsers. Next I’ll use the count()
method on our user
model. If there are any records in the User model than I’ll call the callback and nothing will be altered in the database. However, if there is no data in the model, I’ll use the create()
method on the User model, passing in the array dummyUsers
and then calling the callback. Note that the boostrap.js
is run once each time sails is started via sails lift
.
So, let’s see if that worked. I’ll go over to the terminal and enter sails lift. Next, I’ll get a list of user by entering /user
into the browser. And there’s our hundred or so users.
I’ve posted a link to the github repo of this project and I hope this was helpful. Thanks as always for watching.