Connect Fauna to an app
This tutorial is the third and final part of a series that shows you how to quickly set up and run a Fauna database using the Fauna Dashboard. For an overview of the series, see Get started with the Fauna Dashboard.
In the previous tutorial, you created a collection and added documents to it using an FQL query. In this tutorial, you’ll create a basic app that connects to your database using a Fauna client driver. The drivers let you pass FQL queries to Fauna using an FQL template string.
Create an authentication secret
Fauna supports several types of
authentication secrets. Create a
key, which is one type of secret, with the
built-in server-readonly
role:
-
In the Dashboard’s Explorer page, select your demo database.
-
In the database’s Keys tab, click Create Key.
-
Choose a Role of server (read-only).
-
Click Save.
-
Copy the Key Secret.
Set the FAUNA_SECRET environment variable
Set the FAUNA_SECRET
environment variable to your key’s secret. Fauna’s
client drivers can access the secret from this variable.
export FAUNA_SECRET=<KEY_SECRET>
Set up a project
Create a directory for the app and install the Fauna client driver for your preferred programming language.
mkdir app
cd app
npm install fauna
mkdir app
cd app
pip install fauna
mkdir app
cd app
go mod init app
go get github.com/fauna/fauna-go/v3
mkdir app
cd app
dotnet new console
dotnet add package Fauna
Initialize a Gradle project:
mkdir example
cd example
gradle init \
--type java-application \
--dsl groovy \
--test-framework junit \
--project-name app \
--package app \
--java-version 17 \
--no-incubating \
--no-split-project
In the example
directory, edit example/app/build.gradle
and add the
JVM driver as a dependency:
dependencies {
...
implementation "com.fauna:fauna-jvm:1.0.0"
...
}
Create a basic app
In the app
directory, create an app.mjs
file and add the following code:
In the app
directory, create an app.py
file and add the following code:
In the app
directory, create an app.go
file and add the following code:
In the app
directory, edit the Program.cs
file and replace the code with the
following:
In the example
directory, edit the
example/app/src/main/java/app/App.java
file and replace the code with the
following:
Run the app
Run the script from the app
directory.
node app.mjs
The script prints a list of review data from your database in the terminal:
[
{
title: 'Great NYC-style pizza',
rating: 4,
comment: null,
customer: { email: 'alice.appleseed@example.com' },
product: { name: 'pizza' }
},
{
title: 'Delicious limes!',
rating: 5,
comment: 'Incredibly ripe and juicy.',
customer: { email: 'alice.appleseed@example.com' },
product: { name: 'limes' }
}
]
Run the script from the app
directory.
python app.py
The script prints a list of review data from your database in the terminal:
{
'title': 'Great NYC-style pizza',
'rating': 4,
'comment': None,
'customer': {
'email': 'alice.appleseed@example.com'
},
'product': {
'name': 'pizza'
}
}
{
'title': 'Delicious limes!',
'rating': 5,
'comment': 'Incredibly ripe and juicy.',
'customer': {
'email': 'alice.appleseed@example.com'
},
'product': {
'name': 'limes'
}
}
Run the script from the app
directory.
go run app.go
The script prints a list of review data from your database in the terminal:
{
"comment": null,
"customer": {
"email": "alice.appleseed@example.com"
},
"product": {
"name": "pizza"
},
"rating": 4,
"title": "Great NYC-style pizza"
}
{
"comment": "Incredibly ripe and juicy.",
"customer": {
"email": "alice.appleseed@example.com"
},
"product": {
"name": "limes"
},
"rating": 5,
"title": "Delicious limes!"
}
Run the script from the app
directory. The script prints a list of review data
from your database in the terminal.
dotnet run
Title: Great NYC-style pizza
Rating: 4
Comment:
Customer: alice.appleseed@example.com
Product: pizza
--------
Title: Delicious limes!
Rating: 5
Comment: Incredibly ripe and juicy.
Customer: alice.appleseed@example.com
Product: limes
--------
Run the script from the example
directory.
./gradlew run
The script prints a list of review data from your database in the terminal:
Title: Great NYC-style pizza
Rating: 4
Comment: null
Customer: alice.appleseed@example.com
Product: pizza
--------
Title: Delicious limes!
Rating: 5
Comment: Incredibly ripe and juicy.
Customer: alice.appleseed@example.com
Product: limes
--------
After: Optional.empty