# 06 - Build a FaunaDB database

Watch the YouTube video:

https://www.youtube.com/watch?v=YOUTUBE_VIDEO_ID_HERE

# TL;DR

Today I will demonstrate how to:

  • build a FaunaDB database using Netlify add-ons, and
  • access the database using Fauna Shell.

# Executive summary

A common way, although not the only way, to store data is to use a database. The database management system we are going to use is called FaunaDB. It differs from most other database systems in that it is a document-based, low-latency system that supports GraphQL, is 100% ACID transaction compliant, and comes with its' own language called FQL (Fauna Query Language). FaunaDB includes many other features but these, I believe, are the top 5 characteristics of this system.

# Prerequisites

The following technologies are needed for this dev guide:

  • VS Code,
  • a Netlify account,
  • a FaunaDB account,
  • the Netlify CLI tool ($ npm i -g netlify-cli),
  • the Fauna Shell tool ($ npm i -g fauna-shell), and
  • the login-code project (currently made up of a framework and routes).

# Citations

The following references were used during the writing of this dev guide:

# Build a FaunaDB database

Follow these steps to build a FaunaDB database using Netlify add-ons:

  • Create a GitHub account, if required.
  • Create a Netlify account, if required.
  • Create a FaunaDB account, if required.
  • Install the Netlify CLI tool ($ npm i -g netlify-cli), if required.
  • Install the Fauna Shell tool ($ npm i -g fauna-shell), if required.
  • Create the login-code project, if required.
  • Open a terminal, or use the terminal window in VS Code (CTRL + ~).
  • Navigate to the root directory of the login-code project.
  • Connect Netlify CLI to your Netlify account:
$ netlify login

A browser tab will ask you to authorize Netlify CLI so it can access your Netlify account. Once authorized, these settings are stored on your local system in the ~/.netlify/confilg.json file.

  • Link the GitHub project to Netlify:
$ netlify link

Choose to "Use current git" settings. Netlify CLI will ask your Netlify account to go "Looking for sites connected to 'https://github.com/johndoe/login-code'..." Once the local project and the remote project are "Linked", a file called .netlify/state.json is added to the local project.

  • Add the FaunaDB add-on for Netlify CLI:
$ netlify addons:create fauna

Authenticate the add-on by logging in to your FaunaDB account:

$ netlify addons:auth fauna

NOTE: You will be prompted to name a database and import it into your FaunaDB account.

  • Name the database "login-code-db".

# Access your FaunaDB database

Follow these steps to access the recently-created FaunaDB database using Fauna Shell.

  • Configure a connection to Fauna Cloud:
$ fauna cloud-login

NOTE: Endpoints are saved to the ~/.fauna-shell file.

  • List your databases:
$ fauna list-databases
  • Use Fauna Shell to access the "login-code-db" database:
$ fauna shell login-code-db
  • Type .exit to close Fauna Shell.

# Revoke your connections

Follow these steps to remove the Netlify CLI connection to your Netlify account:

  • Login to Netlify.
  • Click your name logo at the top-right of the screen, and select "User settings" from the popup dialog.
  • Click the "Applications" link in the left, sidebar menu.
  • Scroll down to the "Authorized applications" panel at the bottom of the page.

This is where you have the "Options" to "Revoke access" from the applications (e.g. Netlify CLI, FaunaDB, etc.) that you authorized to access your Netlify account.

After revoking access to the Netlify account:

  • navigate to the ~/.netlify/config.json file on your local system and delete it because those settings are no longer valid,
  • navigate to the ~/.fauna-shell file on your local system and delete it because those settings are also no longer valid, and
  • navigate to the ~/.netlify/state.json file in your project and delete it because those settings are no longer valid.

# Review

In this dev guide, we:

  • built a FaunaDB database using Netlify add-ons, and
  • accessed the database using Fauna Shell.

# Next

In the next dev guide, we are going to prepare the FaunaDB database for use by the project.