Seeding data with Sequelize

We should fill the empty Posts table with our fake data. To accomplish this, we will use Sequelize's feature for seeding data to our database.

Create a new folder, called seeders:

mkdir src/server/seeders

Now, we can run our next Sequelize CLI command, in order to generate a boilerplate file:

sequelize seed:generate --name fake-posts --seeders-path src/server/seeders

Seeders are great for importing test data into a database for development. Our seed file has the timestamp and the words fake-posts in the name, and should look as follows:

'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkDelete('Person', null, {});
*/
}
};

As you can see in the preceding code snippet, nothing is done here. It is just an empty boilerplate file. We need to edit this file to create the fake posts that we already had in our backend. This file looks like our migration from the previous section. Replace the contents of the file with the following code:

'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Posts', [{
text: 'Lorem ipsum 1',
createdAt: new Date(),
updatedAt: new Date(),
},
{
text: 'Lorem ipsum 2',
createdAt: new Date(),
updatedAt: new Date(),
}],
{});
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Posts', null, {});
}
};

In the up migration, we are bulk inserting two posts, through the queryInterface and its bulkInsert command. For this, we pass an array of posts, excluding the id and the associated user. The id is created automatically, and the user is saved in a separate table later on. The QueryInterface of Sequelize is the general interface that Sequelize uses to talk to all databases.

In our seed file, we need to add the createdAt and updatedAt field, since Sequelize does not set up default values for the timestamp columns in MySQL. In reality, Sequelize takes care of the default values of those fields by itself, but not when seeding data. If you do not provide these values, the seed will fail, because NULL is not allowed for createdAt and updatedAt.

The down migration bulk deletes all rows in the table, since this is the apparent reverse action of the up migration.

Execute all of the seeds from the seeders folder with the following command:

sequelize db:seed:all --seeders-path src/server/seeders --config src/server/config/index.js

Sequelize does not check or save if a seed has been run already, as we are doing it with the preceding command. This means that you can run seeds multiple times if you want to.

The following screenshot shows a filled Posts table:

The demo posts are now inside of our database.

We will cover how to use Sequelize with our Apollo Server, and how to add the relationship between the user and their posts, in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.8