Junior — Middle
Explain what Seeds are in the context of databases and what they are used for.
sobes.tech AI
Answer from AI
Seeds in the context of databases are initial data loaded into the database for testing, development, or initial application setup. They help populate tables with sample data so that the application can work with real or near-real data.
Using seeds allows:
- Quickly preparing the database for local development.
- Automating the filling of test databases.
- Ensuring data consistency during deployment.
An example of using seeds in Node.js with the Sequelize library:
// seeders/20230601-demo-user.js
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Users', [
{ username: 'user1', email: 'user1@example.com', createdAt: new Date(), updatedAt: new Date() },
{ username: 'user2', email: 'user2@example.com', createdAt: new Date(), updatedAt: new Date() }
], {});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Users', null, {});
}
};
Thus, seeds are a convenient way to populate the database with initial data.