Setting Up Our Fauna Database
Step 1: Creating Our Fauna Database
The first thing we need to do is create the database for our CuriousCat clone in the Fauna dashboard. If you have not created an account on Fauna before now, create one here: https://dashboard.fauna.com/accounts/register
In the dashboard, click on the NEW DATABASE
button, provide a name for your database then press the SAVE
button.
Step 2: Creating Our Fauna Collections
Next, we need to create the Fauna collections for our project. A collection is similar to SQL tables that contain data with similar characteristics e.g user collection that contain information about users in the database.
We will be creating two (2) collections for our application; one for storing user information and the other for storing questions asked on the platform. To create a collection, click the NEW COLLECTION
button then provide a name for the collection you want to create.
Now that we have created a collection for our users, let’s create another for questions.
Step 3: Creating Our Fauna Indexes
We then need to create the indexes for the collections of our project. A Fauna index allows us to browse through data that is stored in a database collection based on specific attributes.
We will be creating two (2) indexes for our application, one for each of the collections we created earlier (users and questions). To create an index, navigate to the DB Overview
tab on the Fauna sidebar (left side of the screen) then click the NEW INDEX
button.
Now we have created an index for our users, let’s create another for questions.
Step 4: Generate Our Fauna API Key
We will need to create a Fauna API key to connect the database to our CuriousCat clone. To do this, navigate to the security settings on the Fauna sidebar (left side of the screen).
Once you have done this, you will be presented with your API key (hidden here for privacy reasons). The key should be copied as soon as it is generated then stored somewhere easily retrievable.
Step 5: Integrate Fauna into Python
Next, we need to get the Python library for Fauna. It’s available on pip and can be installed with a single line in our terminal.
pip install faunadb
After this is installed, we run the sample code provided in Fauna Python driver docs: https://docs.fauna.com/fauna/current/drivers/python.html
from faunadb import query as q
from faunadb.objects import Ref
from faunadb.client import FaunaClient
client = FaunaClient(secret="your-secret-here")
indexes = client.query(q.paginate(q.indexes()))
print(indexes)
The code above shows how the Fauna Python driver connects to a database with its API key and prints the indexes associated with it. The result from running this code should be similar to the image below.