Grind’s Database is built on Knex.js, so for full documentation on schema building check out the Knex documentation.
This document assumes you have a basic understanding of how migrations & seeds work in Knex and focuses on their integration into Grind.
Grind’s Database integrates Knex tightly into Grind, so all CLI management of your database will be through Grind’s CLI and not through the
knex command. This allows us to leverage all existing Grind config and models, without having to worry about building a separate Knexfile.
Grind offers three different CLI commands for managing migrations:
migrate:latest command will run all outstanding migrations on your database. If you run this against a new database, it will first setup the database and then run the migrations.
yarn cli migrate:latest
migrate:rollback command will revert the last batch of migrations by going through each migration in reverse order and calling the
yarn cli migrate:rollback
migrate:current-version command will output the current version of your migrations.
yarn cli migrate:current-version
Like migrations, you seed the database through
yarn cli and not through the
To seed the database, there’s a single
yarn cli db:seed
db:seed will run through and execute each seed file.
Use Caution! Unlike migrations, seeds do not have a concept of state. Every time you run
db:seed, it runs all seeds again, not just new seeds you’ve added. This means that existing data will likely be cleared out by your seeds each time they’re ran.
You can generate a migration via
grind make:migration. There are a few different options for you to invoke
grind make:migration create_users_tablewill create
/database/migrations/###-create_users_table.jsas a basic migration
grind make:migration --create=userswill also create
/database/migrations/###-create_users_table.js, however it generates boilerplate code to create the
grind make:migration --alter=userswill create
/database/migrations/###-alter_users_table.js, and it will generate boilerplate code to alter the
grind make:migration --alter=users alter_users_add_disabledwill create
/database/migrations/###-alter_users_add_disabled.js, and it will generate boilerplate code to alter the
You can generate a seed file via
grind make:seed. You can invoke
make:seed with a couple of different arguments:
grind make:seed userswill create
database/seeds/##-users.js, but will not infer a table name.
grind make:seed --table=userswill also create
database/seeds/##-users.js, but it will also set the table name for you.