Environment variables

Before starting your server, you need to configure the database link in .env* files.

Open this file for each environment and update DATABASE_URL for your database.

Setup database variable for the development environment:

  1. # .env.development
  2. DATABASE_URL="database_type://username:password@localhost/bookshelf_development"

Setup database variable for the test environment:

  1. # .env.test
  2. DATABASE_URL="database_type://username:password@localhost/bookshelf_test"

For jdbc urls you can’t set username and password to the left of @ you have to set them as parameters in the url:

  1. DATABASE_URL="jdbc-database_type://localhost/bookshelf_test?user=username&password=password"

Setup your database

After your database variables setup is done you need to create the database and run the migrations before being able to launch a development server.

In your terminal, enter:

  1. $ bundle exec hanami db prepare

To setup your test environment database, enter:

  1. $ HANAMI_ENV=test bundle exec hanami db prepare

Sequel plugins

Hanami models use ROM as a low-level backend. This means that you can easily use any Sequel plugins in your app. For this you need to define a gateway block in your model configuration, add the extension by calling extension on gateway.connection and pass the extension name in:

  1. # config/environment.rb
  2. Hanami.configure do
  3. model do
  4. gateway do |g|
  5. g.connection.extension(:connection_validator)
  6. end
  7. end
  8. end