Sign up for Hasura Newsletter
Loading...

Dev Environments

Local Development

The Hasura instance that is running locally on your machine with docker-compose is the dev environment setup. The Hasura CLI can be used to serve the console for automatic management of migrations and metadata.

Staging Environment

Now let's create a staging environment and try to replicate the schema and metadata that we have in our local dev setup.

We are going to make use of Hasura Cloud for the staging environment. Hasura Cloud gives you a scalable, highly available, globally distributed, fully managed, secure GraphQL API as a service!

Click on the following button to create a new project on Hasura Cloud:

Note: It is free to signup and no credit card is required.

Once you register and sign in, you should see the following welcome screen and a new Hasura project will be created automatically for you:

Hasura Cloud Welcome Page

Once the project is initialised, you can click on Launch Console button on the pop up screen. If you already have a Hasura Cloud account before, you can manually create a new project by clicking on the + New Project action at the top, followed by Launch Console.

Hasura Console

This will open up Hasura Console for your project. It should look something like this:

Hasura Console

The next step is to connect the database to Hasura. We can make use of Neon's free Postgres database tier to set this up. Head to the Data tab on the Console and click on Connect Database.

We have two options to connect a database:

  • Connect an existing database
  • Create a new database (free)

To quickstart this process, we are going to create a new Postgres DB from scratch using Neon Postgres. Click on Create New Database (Free) tab. In this tab, you now have an option to click on the Connect Neon Database button. Note that Neon gives you 3 free Posgres database instances.

Create Neon Database

After logging in to Neon and clicking on Create Neon Database, Hasura Cloud will perform the following for you:

  • Create a Postgres database on Neon
  • Fetch database URL that you can use to configure Hasura

It will take a few seconds to connect to Neon Postgres and initialise. Once the connection is established, you will be taken to the Data Manager page on the Console, listing the database that we just connected.

Neon database created

Now copy the project URL that looks like https://myproject.hasura.app. (replace myproject with your hasura project name).

Head back to the terminal, on to the Hasura project directory. Execute the following command:

hasura migrate apply --endpoint https://myproject.hasura.app --admin-secret xxxxx --database-name default
hasura metadata apply --endpoint https://myproject.hasura.app --admin-secret xxxxx

Now, try refreshing the Hasura Console on the Cloud project and see if the database schema is reflecting there. Essentially we have replicated the schema and metadata on to a new Hasura instance and new Postgres database.

As we keep changing the schema locally, we can keep applying the above two commands to apply the same changes to the staging environment.

Note: You can also create a project on Hasura Cloud for development. All the steps required to sync between dev and staging would remain the same. Typically, the webhook URL handlers need to be exposed to a public endpoint that Hasura Cloud can access and hence they cannot be localhost urls. We recommend using something like ngrok to expose a local server running for Actions/Remote Schemas/Events to a publicly accessible endpoint.

Squashing Migrations

As we keep changing the database, the migration directory gets noisy with too many files created in the dev iteration process. Once a feature is fixed, you might want to combine and squash all the migration files related to it into a single file. This can be achieved using the squash command of the Hasura CLI. Execute the following command:

hasura migrate squash --name "squashed-migration" --from 123 --database-name default --endpoint https://myproject.hasura.app

and replace the value for --from appropriately.

Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2022 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin