In our previous seeding experience with trees, we worked with straightforward numerical and location data. Now, we're facing a more nuanced challenge with our insects database. We need to seed data that must conform to specific formatting rules, like title casing for names and character limits for facts. This is similar to how a museum curator must ensure all specimen labels follow precise formatting guidelines when creating a new exhibit.
Our data will come from research about the world's smallest insects, and we need to ensure this data meets all our model's validation requirements while maintaining scientific accuracy.
Let's create our seeder using the Sequelize CLI:
npx dotenv sequelize-cli seed:generate --name smallest-insects
In server/db/seeders/XXXXXX-smallest-insects.js:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
// Data about the world's smallest insects
const insects = [
{
name: "Fairyfly Wasp",
description: "A parasitic wasp that is one of the smallest known insects",
territory: "Worldwide",
fact: "Some species of fairyfly are small enough to land on the tip of a human hair",
millimeters: 0.5,
createdAt: new Date(),
updatedAt: new Date()
},
{
name: "Bolivian Ant",
description: "One of the smallest ants ever discovered",
territory: "Bolivia",
fact: "These ants build their colonies in the stems of bamboo plants",
millimeters: 1.0,
createdAt: new Date(),
updatedAt: new Date()
},
{
name: "Scarlet Dwarf Dragonfly",
description: "The smallest species of dragonfly",
territory: "East Asia",
fact: "Despite their tiny size, they are skilled aerial predators",
millimeters: 20,
createdAt: new Date(),
updatedAt: new Date()
},
{
name: "Featherwing Beetle",
description: "Among the smallest free-living insects",
territory: "North America",
fact: "Their wings are feather-like, giving them their common name",
millimeters: 0.3,
createdAt: new Date(),
updatedAt: new Date()
},
{
name: "Eastern Grass Pygmy Grasshopper",
description: "A tiny species of grasshopper",
territory: "Eastern United States",
fact: "They prefer moist habitats with short grass",
millimeters: 13,
createdAt: new Date(),
updatedAt: new Date()
}
];
await queryInterface.bulkInsert('Insects', insects);
},
down: async (queryInterface, Sequelize) => {
// Remove the seeded insects by their unique names
await queryInterface.bulkDelete('Insects', {
name: [
"Fairyfly Wasp",
"Bolivian Ant",
"Scarlet Dwarf Dragonfly",
"Featherwing Beetle",
"Eastern Grass Pygmy Grasshopper"
]
});
}
};
Our insect data must meet several validation requirements that we established in our model:
Each word in the insect name must be properly capitalized. For example:
Facts must be concise and under 240 characters. For example:
// Good fact (under 240 characters):
"Some species of fairyfly are small enough to land on the tip of a human hair"
// Bad fact (would be too long):
"Some species of fairyfly are small enough to land on the tip of a human hair.
They are parasitic wasps that lay their eggs inside the eggs of other insects.
Despite their tiny size, they play a crucial role in controlling pest populations
in many ecosystems around the world..."
Execute the seeder:
npx dotenv sequelize-cli db:seed:all
Verify the data:
sqlite3 db/dev.db "SELECT name, millimeters FROM Insects ORDER BY millimeters ASC;"
When seeding this data, we need to be careful about several things:
Ensure measurements are consistent across all records. Consider:
Be consistent with text formatting across records:
Before running the seeder, verify that:
When working with scientific data, consider these practices:
Ensure all measurements and facts are accurate by:
Keep your seed data organized by:
Consider these potential enhancements: