Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 18, 2022 01:20 pm GMT

Nhost CLI: from Zero to Production

In the previous tutorials, that are covered in the documentation, we tested various parts of Nhost, such as:

  • Database
  • GraphQL API
  • Permission
  • JavaScript SDK
  • Authentication

All changes we did to our database and API happened directly in the production of our Nhost app.

Its not ideal for making changes in production because you might break things, which will affect all users of your app.

Instead, its recommended to make changes and test your app locally before deploying those changes to production.

To do changes locally, we need to have a complete Nhost app running locally, which the Nhost CLI does.

The Nhost CLI matches your production application in a local environment, this way you can make changes and test your code before deploying your changes to production.

Recommended workflow with Nhost

  1. Develop locally using the Nhost CLI.
  2. Push changes to GitHub.
  3. Nhost automatically applies changes to production.

What youll learn in this guide:

  • Use the Nhost CLI to create a local environment
  • Connect a GitHub repository with a Nhost app
  • Deploy local changes to production

Setup the recommended workflow with Nhost

What follows is a detailed tutorial on how you setup Nhost for this workflow

Create Nhost App

Create a new Nhost app for this tutorial.

Its important that you create a new Nhost app for this guide instead of reusing an old Nhost app because we want to start with a clean Nhost app.

Create new app

Create new GitHub Repository

Create a new GitHub repository for your new Nhost app. The repo can be both private or public.

Create new repo

Connect GitHub Repository to Nhost App

In the Nhost Console, go to the dashboard of your Nhost app and click Connect to GitHub.

Connect Github Repo

Install the Nhost CLI

Install the Nhost CLI using the following command:

sudo curl -L https://raw.githubusercontent.com/nhost/cli/main/get.sh | bash

Initialize a new Nhost App locally:

nhost init -n "nhost-example-app" && cd nhost-example-app

And initialize the GitHub repository in the same folder:

echo "# nhost-example-app" >> README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M maingit remote add origin https://github.com/[github-username]/nhost-example-app.gitgit push -u origin main

Now go back to the Nhost Console and click Deployments. You just made a new deployment to your Nhost app!

Deployments tab

If you click on the deployment you can see that nothing was really deployed. Thats because we just made a change to the README file.

Deployments details

Lets do some backend changes!

Local changes

Start Nhost locally:

nhost dev

Make sure you have Docker installed on your computer. Its required for Nhost to work.

The nhost dev command will automatically start a complete Nhost environment locally on your computer using:

  • Postgres
  • Hasura
  • Authentication
  • Storage
  • Serverless Functions
  • Mailhog

You use this local environment to do changes and testing before you deploy your changes to production.

Running nhost dev also starts the Hasura Console.

Its important that you use the Hasura Console that is started automatically when you do changes. This way, changes are automatically tracked for you.

Hasura Console

In the Hasura Console, create a new table customers with two columns:

  • id
  • name

Hasura Create Customers Table

When we created the customers table there was also a migration created automatically. The migration was created at under nhost/migrations/default.

$ ls -la nhost/migrations/defaulttotal 0drwxr-xr-x  3 eli  staff   96 Feb  7 16:19 .drwxr-xr-x  3 eli  staff   96 Feb  7 16:19 ..drwxr-xr-x  4 eli  staff  128 Feb  7 16:19 1644247179684_create_table_public_customers

This database migration has only been applied locally, meaning, you created the customers table locally but it does not (yet) exists in production.

To apply the local change to production we need to commit the changes and push it to GitHub. Nhost will then automatically pick up the change in the repository and apply the changes.

You can commit and push files in another terminal while still having nhost dev running.

git add -Agit commit -m "Initialized Nhost and added a customers table"git push

Head over to the Deployments tab in the Nhost console to see the deployment.

Deployments tab after changes

Once the deployment finishes the customers table is created in production.

Customers table in Hasura Console

Weve now completed the recommended workflow with Nhost:

  1. Develop locally using the Nhost CLI.
  2. Push changes to GitHub.
  3. Nhost deploys changes to production.

Apply metadata and Serverless Functions

In the previous section, we only created a new table; customers. Using the CLI you can also do changes to other parts of your backend.

There are three things the CLI and the GitHub integration track and applies to production:

  1. Database migrations
  2. Hasura Metadata
  3. Serverless Functions

For this section, lets do one change to the Hasura metadata and create one serverless function

Hasura Metadata

Well add permissions to the users table, making sure users can only see their own data. For this, go to the auth schema and click on the users table. then click on Permissions and enter a new role user and create a new select permission for that role*.*

Create the permission with custom check:

{  "id": {    "_eq" : "X-Hasura-User-Id"  }}

Select the following columns:

  • id
  • created_at
  • display_name
  • avatar_url
  • email

Then click Save permissions.

Hasura User Permissions

Now, lets do a git status again to confirm the permission changes we did were tracked locally in your git repository.

Git status

We can now commit this change:

git add -Agit commit -m "added permission for uses"

Now lets create a serverless function before we push all changes to GitHub so Nhost can deploy our changes.

Serverless Function

A serverless function is a piece of code written in JavaScript or TypeScript that takes an HTTP request and returns a response.

Heres an example:

import { Request, Response } from 'express'export default (req: Request, res: Response) => {  res.status(200).send(`Hello ${req.query.name}!`)}

Serverless functions are placed in the functions/ folder of your repository. Every file will become its own endpoint.

Before we create our serverless function well install express, which is a requirement for serverless functions to work.

npm install express# or with yarnyarn add express

Well use TypeScript so well install two type definitions too:

npm install -d @types/node @types/express# or with yarnyarn add -D @types/node @types/express

Then well create a file functions/time.ts

In the file time.ts well add the following code to create our serverless function:

import { Request, Response } from 'express';export default (req: Request, res: Response) => {  return res    .status(200)    .send(`Hello ${req.query.name}! It's now: ${new Date().toUTCString()}`);};

We can now test the function locally. Locally, the backend URL is http://localhost:1337. Functions are under /v1/functions. And every functions path and filename becomes an API endpoint.

This means our function functions/time.ts is at http://localhost:1337/v1/functions/time.

Lets use curl to test our new function:

curl http://localhost:1337/v1/functions/timeHello undefined! It's now: Sun, 06 Feb 2022 17:44:45 GMT

And with a query parameter with our name:

curl http://localhost:1337/v1/functions/time\?name\=JohanHello Johan! It's now: Sun, 06 Feb 2022 17:44:48 GMT

Again, lets use git status to see the changes we did to create our serverless function.

Now lets commit the changes and push them to GitHub.

git add -Agit commit -m "added serverless function"git push

In the Nhost Console, click on the new deployment to see details

Deployments details for function

After Nhost has finished deploying your changes we can test them in production. First lets confirm that the user permissions are applied.

Hasura Console permissions table

Then, lets confirm that the serverless function was deployed. Again, well use curl:

curl https://your-backend-url.nhost.run/v1/functions/time\?name\=Johan

Serverless Function test

Conclusion

In this tutorial, we have installed the Nhost CLI and created a local Nhost environment to do local development and testing.

In the local environment, weve made changes to our database, to Hasuras metadata, and created a serverless function.

Weve connected a GitHub repository and pushed our changes to GitHub.

Weve seen Nhost automatically deploying our changes and weve verified that the changes were applied.

In summary, weve set up a productive environment using the recommended Nhost workflow:

  1. Develop locally using the Nhost CLI.
  2. Push changes to GitHub.
  3. Nhost deploys changes to production.

In addition to all that, the Nhost team is always happy to support you with any questions you might have onDiscord and Github!


Original Link: https://dev.to/nhost/nhost-cli-from-zero-to-production-220h

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To