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:
The 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
The migrate:rollback
command will revert the last batch of migrations by going through each migration in reverse order and calling the down
function.
yarn cli migrate:rollback
The 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 knex
command.
To seed the database, there’s a single db:seed
command.
yarn cli db:seed
Running 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 make:migration
with:
grind make:migration create_users_table
will create/database/migrations/###-create_users_table.js
as a basic migrationgrind make:migration --create=users
will also create/database/migrations/###-create_users_table.js
, however it generates boilerplate code to create theusers
table.grind make:migration --alter=users
will create/database/migrations/###-alter_users_table.js
, and it will generate boilerplate code to alter theusers
table.grind make:migration --alter=users alter_users_add_disabled
will create/database/migrations/###-alter_users_add_disabled.js
, and it will generate boilerplate code to alter theusers
table.
You can generate a seed file via grind make:seed
. You can invoke make:seed
with a couple of different arguments:
grind make:seed users
will createdatabase/seeds/##-users.js
, but will not infer a table name.grind make:seed --table=users
will also createdatabase/seeds/##-users.js
, but it will also set the table name for you.