Think of database seeding like planting a garden. Just as you might start a garden by planting initial seeds that will grow into plants, database seeding is the process of planting initial data in your database that your application can work with. In our case, we're "planting" information about five of the largest Sequoia trees in our database.
This is particularly important for development and testing, as it gives us a consistent set of data to work with while building our application.
First, let's create our seeder file using the Sequelize CLI:
npx dotenv sequelize-cli seed:generate --name biggest-trees
This command creates a new file in your server/db/seeders directory with a timestamp prefix.
In your new seeder file (server/db/seeders/XXXXXX-biggest-trees.js):
'use strict';
// Import the model
const { Tree } = require('../models');
module.exports = {
up: async (queryInterface, Sequelize) => {
// Define our tree data
const trees = [
{
tree: 'General Sherman',
location: 'Sequoia National Park',
heightFt: 274.9,
groundCircumferenceFt: 102.6,
createdAt: new Date(),
updatedAt: new Date()
},
{
tree: 'General Grant',
location: 'Kings Canyon National Park',
heightFt: 268.1,
groundCircumferenceFt: 107.5,
createdAt: new Date(),
updatedAt: new Date()
},
{
tree: 'President',
location: 'Sequoia National Park',
heightFt: 240.9,
groundCircumferenceFt: 93.0,
createdAt: new Date(),
updatedAt: new Date()
},
{
tree: 'Lincoln',
location: 'Sequoia National Park',
heightFt: 255.8,
groundCircumferenceFt: 98.3,
createdAt: new Date(),
updatedAt: new Date()
},
{
tree: 'Stagg',
location: 'Private Land',
heightFt: 243.0,
groundCircumferenceFt: 109.0,
createdAt: new Date(),
updatedAt: new Date()
}
];
// Insert the data
await queryInterface.bulkInsert('Trees', trees, {});
},
down: async (queryInterface, Sequelize) => {
// Remove the seeded data
await queryInterface.bulkDelete('Trees', {
tree: [
'General Sherman',
'General Grant',
'President',
'Lincoln',
'Stagg'
]
});
}
};
The up function is like planting seeds in your garden. It performs three main tasks:
The down function is like pulling up plants from your garden. It's used to:
To seed your database:
npx dotenv sequelize-cli db:seed:all
To undo the seeding:
npx dotenv sequelize-cli db:seed:undo:all
You can verify your seeded data using SQLite:
sqlite3 db/dev.db "SELECT * FROM Trees;"
Keep your seed data organized and maintainable:
When creating seeders, always:
Database seeding is used in many scenarios: