ExpressJs Database Migrations and Seeds
Syntax
npx knex migrate:latest
npx knex seed:run
Example
Create a new migration file:
npx knex migrate:make migration_name
Create a new seed file:
npx knex seed:make seed_name
Output
- Migrations: creates or modifies tables in the database.
- Seeds: populates the database with data.
Explanation
- Migrations are used to manage changes to the database schema over time. It allows for better version control and easier collaboration with other developers.
- Seeds are used to populate the database with sample data.
Use
- Migrations are used to create or modify tables in the database.
- Seeds are used to populate the database with initial data.
Important Points
- Migrations should be written to be reversible.
- Seeds should be written to be idempotent.
- Migrations should be run in the order they were created.
- Migrations and seeds should only be run during the development phase.
Summary
Migrations and seeds are a crucial part of database management in ExpressJS. They allow for easier database version control and initial data seeding. Migrations should be written with reversibility in mind and run in the order they were created, while seeds should be idempotent and only run during development.