knex-automigrate is a database migration tool built on top of Knex.js, offering a schema-based approach instead of traditional sequential migrations. It allows developers to define the desired state of database tables and views in dedicated `table_*.js` and `view_*.js` files within their migration directory. The tool then automatically generates and applies the necessary SQL statements to bring the database schema in sync with these definitions, abstracting away the manual `up` and `down` migration logic. Currently at version 0.1.8, the project appears to be under active maintenance, with recent updates supporting newer Knex.js versions (e.g., peer dependency `^3.1.0`). Its key differentiator is its declarative, state-based migration model which simplifies schema evolution and avoids the complexity of managing explicit forward and backward migration scripts for each change, contrasting with Knex's traditional delta-based migration system.
npm install knex-automigrateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically use `knex-automigrate` by setting up a `knexfile.js`, creating a `table_users.js` schema, and then executing the auto-migration process. It simulates the CLI workflow for a SQLite database.
Always review the project's GitHub releases or changelog (if available) before upgrading, and thoroughly test migrations in non-production environments.
Ensure all schema definition files are named like `table_your_table_name.js` or `view_your_view_name.js` and placed in the configured migrations directory.
If using other dialects (e.g., PostgreSQL, SQLite) and requiring advanced index management, manual Knex migrations might still be necessary for those specific operations, or verify if support has been added in newer undocumented versions.
Install `knex-automigrate` as a development dependency (`npm install --save-dev knex-automigrate`) and run it via `npx knex-automigrate` or by defining scripts in your `package.json` (e.g., `"migrate": "knex-automigrate migrate:auto"`).
Ensure `knexfile.js` exists in the current working directory, or specify its path using the `--knexfile [path]` option (e.g., `knex-automigrate --knexfile ./config/knexfile.js migrate:auto`).
Rename your migration files to start with `table_` for table schemas (e.g., `table_your_table_name.js`) or `view_` for view schemas (e.g., `view_your_view_name.js`).
For advanced index management on unsupported dialects, consider implementing these changes using traditional Knex.js `knex.schema.alterTable` migrations until full support is added to knex-automigrate.