Linux command
knex 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Initialize Knex project
npx knex init
Create migration
npx knex migrate:make [migration_name]
Run migrations
npx knex migrate:latest
Rollback migration
npx knex migrate:rollback
Create seed file
npx knex seed:make [seed_name]
Run seeds
npx knex seed:run
Show migration status
npx knex migrate:status
说明
Knex.js is a SQL query builder for Node.js. The CLI manages database migrations and seeds, providing version-controlled schema changes. Knex supports PostgreSQL, MySQL, SQLite3, and other databases. Migrations define schema changes; seeds populate test data.
参数
- init
- Create knexfile.js configuration.
- migrate:make _name_
- Create new migration.
- migrate:latest
- Run pending migrations.
- migrate:rollback
- Undo last migration batch.
- migrate:status
- Show migration status.
- seed:make _name_
- Create seed file.
- seed:run
- Run seed files.
- migrate:up _filename_
- Run the next pending migration (or a named one).
- migrate:down _filename_
- Roll back the last completed migration (or a named one).
- migrate:list
- List completed and pending migrations.
- migrate:unlock
- Forcibly release the migration lock (use after a crashed run).
- --env _ENVIRONMENT_
- Pick the section of knexfile to use (default: _development_, or NODE_ENV).
- --knexfile _PATH_
- Specify a custom knexfile path.
- --client _DIALECT_
- Override the database client dialect.
- --debug
- Print SQL statements as they execute.
FAQ
What is the knex command used for?
Knex.js is a SQL query builder for Node.js. The CLI manages database migrations and seeds, providing version-controlled schema changes. Knex supports PostgreSQL, MySQL, SQLite3, and other databases. Migrations define schema changes; seeds populate test data.
How do I run a basic knex example?
Run `npx knex init` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does init do in knex?
Create knexfile.js configuration.