Getting started
This getting started guide shows you how to:
-
Create an authentication key that allows you to use fauna-shell to interact with a database.
-
Define a configuration file endpoint.
-
Display the configuration file.
-
Start
Fauna Shell
.
After starting Fauna Shell
, experiment with the available
commands to gain familiarity with Fauna Shell
.
Create a key to access your database
-
Log in to your Fauna account using your email and password or sign up for an account if you don’t already have one.
-
Choose the Explorer menu item.
-
Hover over the database name you want and click the key icon.
-
Click the CREATE-KEY button.
-
Choose Admin or Server Role and enter an optional Key Name.
-
Click the SAVE button.
-
Copy the KEY’S SECRET, which is needed to configure an endpoint.
This is the only time Fauna displays the secret field, which is equivalent to a password. Fauna can’t recover a secret that is discarded or lost. Copy and save the secret to a password manager or other safe location.
You can now access and query your database using fauna-shell.
Define a configuration endpoint
Use the cloud-login
command to create a default
configuration endpoint in the fauna-shell configuration file.
-
Open a terminal window on your local host.
-
Run the
cloud-login
command and enter Return to add acloud
definition to your configuration file.:fauna cloud-login ? The endpoint alias prefix (to combine with a region): cloud
The cloud configuration defaults to the
db.fauna.com
endpoint. -
At the prompt for an authentication method, enter Return to choose
Secret
as the authentication method:? How do you prefer to authenticate? Email and Password ❯ Secret
-
At the prompt, paste the key that you created in the preceding step and enter Return.
-
In response to the region prompt, choose a region and enter Return.
Select a region (Use arrow keys) ❯ Classic Europe (EU) United States (US)
-
Enter Return to accept the default,
Y
, and acceptcloud
as the default endpoint:? Would you like endpoint 'cloud' to be default? (Y/n) Y
Display the configuration file
To view the updated configuration file on Linux, macOS, and Unix-like operating systems, enter:
cat $HOME/.fauna-shell
On a Windows operating system, enter:
type %userprofile%\.fauna-shell
Common CLI commands
-
Log in to your account and run the following command:
fauna cloud-login
You’re prompted for your email and password.
-
Now that you have an endpoint to connect to try to create a database to start interacting with Fauna.
This is how you create a database called
my_app
:fauna create-database my_app creating database my_app created database my_app To start a shell with your new database, run: fauna shell my_app Or, to create an application key for your database, run: fauna create-key my_app
-
Start a shell in your
my_app
database and create a collection namedUsers
:fauna shell my_app Starting shell for database my_app Connected to reference:http///127.0.0.1:8443 Type Ctrl+D or .exit to exit the shell my_app> Collection.create({ name: "Users" }) { name: "Users", coll: Collection, ts: Time("2023-10-03T02:40:37.060Z"), indexes: {}, constraints: [] } my_app>
-
List your databases:
fauna list-databases listing databases my_app my_second_app my_other_app
-
Delete a database:
fauna delete-database my_other_app deleting database 'my_other_app' database 'my_other_app' deleted
-
You can also create, list, and delete keys.
This is how you create a key for the
my_app
database:fauna create-key my_app creating key for database 'my_app' with role 'admin' created key for database 'my_app' with role 'admin'. secret: fnAFPULk2WAAQY9t4x0tduzuz85gC-suDbTnl7um To access 'my_app' with this key, create a client using the driver library for your language of choice using the above secret.
-
This is how to list keys:
fauna list-keys listing keys Key ID Database Role 203269476002562560 my_app admin 203269731203940864 my_app admin 203269732275585536 my_app admin 203269735610057216 test admin
-
Now, delete the key with id
200219702370238976
:fauna delete-key 200219702370238976 deleting key 200219702370238976 key 200219702370238976 deleted
Shell session example
The fauna-shell allows you to query your Fauna database using FQL without installing other libraries.
-
Create a database and invoke the Shell to start working with the Fauna data model.
fauna create-database my_app
-
Start the shell for the
my_app
database:fauna shell my_app Starting shell for database my_app Connected to reference:http///127.0.0.1:8443 Type Ctrl+D or .exit to exit the shell my_app>
-
After the prompt displays, you can start issuing queries against your database:
my_app> Collection.create({ name: "Post" }) { name: "Post", coll: Collection, ts: Time("2023-08-15T16:06:01.120Z"), indexes: {}, constraints: [] }
Note that your results might differ from this example.
-
Create an index for the
Post
collection.my_app> Post.definition.update({ indexes: { byTitle: { terms: [{ field: ".title" }] } } }) { name: "Post", coll: Collection, ts: Time("2023-08-15T16:07:10.800Z"), indexes: { byTitle: { terms: [ { field: ".title" } ], queryable: true, status: "complete" } }, constraints: [] }
-
Insert a new
Post
document:my_app> Post.create({ title: "What I had for breakfast .." }) { id: "373143369066480128", coll: Post, ts: Time("2023-08-15T16:14:57.440Z"), title: "What I had for breakfast .." }
-
You can insert items in bulk by using array iterator functions:
my_app> ["My cat and other marvels", "Pondering during a commute", "Deep meanings in a latte"].map(title => Post.create({ title: title })) [ { id: "373143473418666496", coll: Post, ts: Time("2023-08-15T16:16:36.960Z"), title: "My cat and other marvels" }, { id: "373143473419715072", coll: Post, ts: Time("2023-08-15T16:16:36.960Z"), title: "Pondering during a commute" }, { id: "373143473420763648", coll: Post, ts: Time("2023-08-15T16:16:36.960Z"), title: "Deep meanings in a latte" } ]
-
Now, fetch the post about latte accessing the collection by id:
my_app> Post.byId("373143473420763648") { id: "373143473420763648", coll: Post, ts: Time("2023-08-15T16:16:36.960Z"), title: "Deep meanings in a latte" }
-
Update the post about the cat, adding some tags:
my_app> Post.byId("373143473420763648")!.update({ tags: ["cute", "pet"] }) { id: "373143473420763648", coll: Post, ts: Time("2023-08-15T16:17:41Z"), title: "Deep meanings in a latte", tags: [ "cute", "pet" ] }
-
Change the content of that post:
my_app> Post.byId("373143473418666496")!.replace({ title: "My dog and other marvels" }) { id: "373143473418666496", coll: Post, ts: Time("2023-08-15T16:18:32.680Z"), title: "My dog and other marvels" }
-
Delete the post about latte:
my_app> Post.byId("373143473420763648")!.delete() Post.byId("373143473420763648") /* not found */
-
A null document is returned when you try to fetch the deleted document:
my_app> Post.byId("373143473420763648") Post.byId("373143473420763648") /* not found */
-
Finally, exit the shell by pressing
Ctrl+D
.
Query using a file
You can use the shell to run a list of queries stored in a file as shown in this example.
-
Create a setup.fql file that has an FQL expression that creates a collection named
Post
:Collection.create({ name: "Post", indexes: { byTitle: { terms: [{ field: ".title" }] } } })
-
Create a queries.fql file that has an FQL expression that creates a document in the
Post
collection:Post.create({ title: "What I had for breakfast .." }) [ "My cat and other marvels", "Pondering during a commute", "Deep meanings in a latte", ].map(title => { Post.create({ title: title }) })
-
Submit the queries with the following commands:
fauna eval my_app --file=./setup.fql $ fauna eval my_app --file=./queries.fql
The database name is
my_app
and the--file
option is the path and file name of the saved the queries. If the database name is omitted, the queries are run on the default fauna shell endpoint.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!