Getting started
This Shell getting started guide shows you how to:
-
Define a configuration file endpoint.
-
Display the configuration file.
-
Start
Fauna Shell.
After starting Fauna Shell, experiment with the Shell
commands to gain familiarity with the
CLI.
Log in and define a configuration endpoint
The Dashboard quick start describes how to set up an account, and how to create a database and get the database secret.
If you don’t have endpoints defined in the $HOME/.fauna-shell configuration
file, use the cloud-login command to create a default
configuration endpoint in the fauna-shell configuration file.
-
Create an endpoint, stepping through each of the prompts.
-
These examples use
cloud-exampleas an endpoint name, but you can choose another name. -
The email and password are your Fauna account credentials.
-
Choose the endpoint created for your region group
fauna cloud-login ? Endpoint name cloud-example ? Email address (from https://dashboard.fauna.com/) your.name@yourorg.com ? Password ********* ? Endpoints created. Which endpoint would you like to set as default? (Use arrow keys) Keep 'cloud-us' endpoint as default // use arrow keys to choose endpoint cloud-example-global ❯ cloud-example-us cloud-example-eu ... Configuration updated.This updates the $HOME/.fauna-shell file to include the following endpoints in addition to previously defined endpoints and sets
cloud-example-us: as the default endpoint.[.fauna-shell] default=cloud-example-us ... elided ... [endpoint.cloud-example-global] secret=fnAFQyPwW4ACRSqZe31zFBBPe6fh81CPA3G_esVj [endpoint.cloud-example-us] secret=fnAFQyPwZe31zWpqZ3-ij5Ghso4erhTiU2RXSQaL [endpoint.cloud-example-eu] secret=fnAFQyPwZlAAzh_cxhZe31zKUNvgcNJ6Wj0rEwjY -
-
View your defined endpoints using the
endpoint listcommand:fauna endpoint list Available endpoints: local cloud-eu cloud cloud-us db.fauna.com cloud-example-global * cloud-example-us cloud-example-euThe
cloud-example-usendpoint is the default endpoint.
Display the configuration file
To view the updated configuration file on Linux, macOS, and Unix-like operating systems, enter:
cat $HOME/.fauna-shellOn a Windows operating system, enter:
type %userprofile%\.fauna-shellShell 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_appdatabase: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
Postcollection.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
Postdocument: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
Postcollection: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.fqlThe database name is
my_appand the--fileoption 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!