Getting Started with Sequelize CLI using Faker

Bruno Galvao
Level Up Coding
Published in
3 min readApr 16, 2020

--

If you’ve recently gotten started using the Sequelize command line interface (CLI) to work with Sequelize, you’ve probably noticed that there’s not much you can do with your database if you don’t have any data.

Fortunately, an npm package called Faker can help fill that void. In this walkthrough, we’ll use the Sequelize CLI to create a simple project, then use Faker to generate reasonably convincing fake info for a group of fairly believable imaginary users.

Let’s start by installing Postgres, Sequelize, and the Sequelize CLI in a new project folder we’ll call sequelize-practice:

mkdir sequelize-practice
cd sequelize-practice
npm init -y
npm install sequelize pg
npm install --save-dev sequelize-cli

Next, let’s initialize a Sequelize project, then open the directory in our code editor:

npx sequelize-cli init
code .

To learn more about any of the Sequelize CLI commands below, see:
Getting Started with Sequelize CLI

Let’s configure our Sequelize project to work with Postgres. Find config.json in the /config directory and replace what’s there with this code:

{
"development": {
"database": "sequelize_practice_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"database": "sequelize_practice_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "sequelize_practice_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}

Cool, now we can tell Sequelize to create the database:

npx sequelize-cli db:create

Next we will create a User model from the command line:

npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string,userName:string,password:string,jobTitle:string

Running model:generate automatically creates both a model file and a migration with the attributes we’ve specified. For now, you won’t need to worry about (or edit) these in order to use Faker to generate seed data.

Now we’ll execute our migration to create the Users table in our database:

npx sequelize-cli db:migrate

Seeding with Faker

Now that our Sequelize project is set up, the fun can begin! We can ask Faker to create real-seeming information of all sorts, from generic lorem ipsum to imaginary email addresses and phone numbers.

Not only is this amusing — it also enables developers to test how their models, associations, and queries will behave once genuine data is entered.

Let’s install Faker:

npm install faker

Now we’ll tell the Sequelize CLI to generate a seed file:

npx sequelize-cli seed:generate --name users

This will generate a file in the /seeders subdirectory with a filename similar to 20190904165805-users.js. (The digits will be based on the current date and time.)

Replace the boilerplate code in that file with the code below:

const faker = require('faker');const users = [...Array(100)].map((user) => (
{
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
userName: faker.internet.userName(),
password: faker.internet.password(8),
jobTitle: faker.name.jobTitle(),
createdAt: new Date(),
updatedAt: new Date()
}
))
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Users', users, {});
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
}
};

Here we’re generating an array of 100 objects — and for each object, Faker will fill in the details using methods like faker.name.lastName() and faker.internet.email(). Then we just perform a bulk insert to add these entries to the database. Simple as that!

Once you’ve saved the file, execute it:

npx sequelize-cli db:seed:all

Drop into psql and query the database to find out what Faker cooked up:

psql sequelize_practice_development
SELECT * FROM "Users";

Seeding mistake? You can always undo: npx sequelize-cli db:seed:undo

Now you have a Sequelize project and a database with 100 fake users! Who did you end up with? Monique McTavish? Norm Sipowicz? Hansel Ramirez? Jane Doe? Jean-Luc Cho? It doesn’t matter! What’s important is that now you have data you can use to try out your Sequelize queries as your project takes shape.

Faker’s list of available fake data can help you customize seed data to fit your project’s needs. Try seeding two or more different tables after creating a Sequelize association!

This article was co-authored with Jeremy Rose, a software engineer, editor, and writer based in New York City.

More info on Sequelize CLI:

--

--