fauna-shell
This tools gives you access to FaunaDB directly from your CLI.
It also includes a Shell so you can issue queries to FaunaDB without needing to install additional libraries.
You can install it via npm like this:
$ npm install -g fauna-shell
Usage
The fauna-shell allows you to do things like creating, deleting and listings databases.
First lets configure an endpoint to connect to the FaunaDB cloud. (If you don't have an account, you can create a free account here).
Let's run the following command:
$ fauna add-endpoint "https://db.fauna.com"
Endpoint Key: ****************************************
Endpoint Alias [db.fauna.com]: cloud
When you are prompted for your Endpoint Key
enter actual key from your FaunaDB Cloud account.
The Endpoint Alias should be a name that helps you remember the purpose of this endpoint, in this case we use cloud
.
Now that we have an endpoint to connect to we can try to create a database to start playing with FaunaDB.
This is how you can create a database called my_app
:
$ fauna create-database my_app
creating database my_app
{ ref: Ref(id=my_app, class=Ref(id=databases)),
ts: 1527202906492280,
name: 'my_app' }
And then listing your databases:
$ fauna list-databases
listing databases
[ Ref(id=my_app, class=Ref(id=databases)),
Ref(id=my_second_app, class=Ref(id=databases)),
Ref(id=my_other_app, class=Ref(id=databases)) ]
You can also delete a particular database:
$ fauna delete-database my_other_app
deleting database my_other_app
{ ref: Ref(id=my_other_app, class=Ref(id=databases)),
ts: 1527202988832864,
name: 'my_other_app' }
You can also create
, list
, and delete
keys.
This is how you create a key for the database my_app
:
$ fauna create-key my_app
creating key for database my_app with role admin
{ ref: Ref(id=200219648752353792, class=Ref(id=keys)),
ts: 1527203186632830,
database: Ref(id=my_app, class=Ref(id=databases)),
role: 'admin',
secret: 'fnACx1K1sPACABUvNQMZjWNZgsKUVo83btQy0i1x',
hashed_secret: '************************************************************' }
This is how to list keys (the results may differ from what you see in your instance of FaunaDB)
$ fauna list-keys
listing keys
[ Ref(id=200219648752353792, class=Ref(id=keys)),
Ref(id=200219702370238976, class=Ref(id=keys)) ]
And then delete the key with id: 200219702370238976
:
fauna delete-key 200219702370238976
deleting key 200219702370238976
{ ref: Ref(id=200219702370238976, class=Ref(id=keys)),
ts: 1527203237774958,
database: Ref(id=my_second_app, class=Ref(id=databases)),
role: 'admin',
hashed_secret: '************************************************************' }
See Commands for a list of commands and help on their usage.
Shell
The Fauna Shell lets you issue queries directly to your FaunaDB instance without the need for installing additional libraries.
Let's create a database and then we'll jump straight into the Shell to start playing with FaunaDB's data model.
fauna create-database my_app
Our next step is to start the shell for a specific database, in this case my_app
:
fauna shell my_app
starting shell for database my_app
faunadb>
Once you have the prompt ready, you can start issues queries against your FaunaDB instance. (Note that the results shown here might vary from the ones you see while running the examples).
faunadb> CreateClass({ name: "posts" })
faunadb>
{ ref: Ref(id=posts, class=Ref(id=classes)),
ts: 1527204921493935,
history_days: 30,
name: 'posts' }
Let's create an index for our posts.
faunadb> CreateIndex(
{
name: "posts_by_title",
source: Class("posts"),
terms: [{ field: ["data", "title"] }]
})
faunadb>
{ ref: Ref(id=posts_by_title, class=Ref(id=indexes)),
ts: 1527204953090934,
active: false,
partitions: 1,
name: 'posts_by_title',
source: Ref(id=posts, class=Ref(id=classes)),
terms: [ { field: [Array] } ] }
Let's insert a post item:
faunadb> Create(
Class("posts"),
{ data: { title: "What I had for breakfast .." } })
faunadb>
{ ref: Ref(id=200221588659896832, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205036673645,
data: { title: 'What I had for breakfast ..' } }
We can also insert items in bulk by using the Map
function.
faunadb> Map(
[
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
],
Lambda("post_title",
Create(
Class("posts"), { data: { title: Var("post_title") } }
))
)
faunadb>
[ { ref: Ref(id=200221673472919040, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205117556412,
data: { title: 'My cat and other marvels' } },
{ ref: Ref(id=200221673472918016, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205117556412,
data: { title: 'Pondering during a commute' } },
{ ref: Ref(id=200221673471869440, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205117556412,
data: { title: 'Deep meanings in a latte' } } ]
Now let's try to fetch our post about latte. We need to access it by id like this:
faunadb> Get(Ref("classes/posts/200221673471869440"))
faunadb>
{ ref: Ref(id=200221673471869440, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205117556412,
data: { title: 'Deep meanings in a latte' } }
Now let's update our post about our cat, by adding some tags:
faunadb> Update(
Ref("classes/posts/200221673472919040"),
{ data: { tags: ["pet", "cute"] } })
faunadb>
{ ref: Ref(id=200221673472919040, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205328606603,
data: { title: 'My cat and other marvels', tags: [ 'pet', 'cute' ] } }
And now let's try to change the content of that post:
faunadb> Replace(
Ref("classes/posts/200221673472919040"),
{ data: { title: "My dog and other marvels" } })
{ ref: Ref(id=200221673472919040, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205345816901,
data: { title: 'My dog and other marvels' } }
faunadb>
Now let's try to delete our post about latte:
faunadb> Delete(Ref("classes/posts/200221673471869440"))
faunadb>
{ ref: Ref(id=200221673471869440, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1527205117556412,
data: { title: 'Deep meanings in a latte' } }
If we try to fetch it, we will receive an error:
faunadb> Get(Ref("classes/posts/200221673471869440"))
faunadb>
Error: instance not found
Finally you can exit the shell by pressing ctrl+d
.
Command Details
$ fauna COMMAND
running command...
$ fauna (-v|--version|version)
fauna/0.0.1 darwin-x64 node-v8.11.1
$ fauna --help [COMMAND]
USAGE
$ fauna COMMAND
...
Connecting to different endpoints
We can add endpoints by calling the following command add-endpoint
. We will be prompted to enter the authentication key and an alias for the endpoint.
$ fauna add-endpoint "https://db.fauna.com"
Endpoint Key: ****************************************
Endpoint Alias [db.fauna.com]: cloud
If we have defined many endpoints, we could set one of them as the default one with the default-endpoint
command:
$ fauna default-endpoint cloud
The default endpoint will be used by the shell to connect to FaunaDB.
Endpoints can be listed with the list-endpoints
command like this:
$ fauna list-endpoints
localhost
cloud *
cluster-us-east
Theere we see that the cloud
endpoint has a *
next to its name, meaning that it's the current default one.
Finally, endpoints will be saved to a ~/.fauna-shell
file like this:
default=cloud
[localhost]
domain=127.0.0.1
port=8443
scheme=http
secret=the_secret
[cloud]
domain=db.fauna.com
scheme=https
secret=FAUNA_SECRET_KEY
[cluster-us-east]
domain=cluster-us-east.example.com
port=443
scheme=https
secret=OTHER_FAUNA_SECRET
Overriding Connection Parameters
Most commands support the following options. You can specify them if you want to connect to your local FaunaDB instance.
OPTIONS
--domain=domain [default: db.fauna.com] FaunaDB server domain
--port=port [default: 443] Connection port
--scheme=https|http [default: https] Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout [default: 80] Connection timeout in milliseconds
They can be used like this:
fauna create-database testdb --domain=127.0.0.1 port=8443 --scheme=http --secret=YOUR_FAUNA_SECRET_KEY --timeout=42
Options provided via the CLI will override the values set in the .fauna-shell
config file.
Any options that are not specified either via the .fauna-shell
config file or the CLI will be set to the defaults offered by the faunadb-js client.
List of Commands
fauna add-endpoint ENDPOINT
Adds a connection endpoint for FaunaDB
USAGE
$ fauna add-endpoint ENDPOINT
ARGUMENTS
ENDPOINT FaunaDB server endpoint
DESCRIPTION
Adds a connection endpoint for FaunaDB
EXAMPLE
$ fauna-shell add-endpoint https://db.fauna.com:443
See code: src/commands/add-endpoint.js
fauna create-database DBNAME
Creates a database
USAGE
$ fauna create-database DBNAME
ARGUMENTS
DBNAME database name
DESCRIPTION
Creates a database
EXAMPLE
$ fauna-shell create-database dbname
See code: src/commands/create-database.js
fauna create-key DBNAME [ROLE]
Creates a key for the specified database
USAGE
$ fauna create-key DBNAME [ROLE]
ARGUMENTS
DBNAME database name
ROLE [default: admin] key user role
DESCRIPTION
Creates a key for the specified database
EXAMPLE
$ fauna-shell create-key dbname admin
See code: src/commands/create-key.js
fauna default-endpoint ENDPOINT_ALIAS
Sets an endpoint as the default one
USAGE
$ fauna default-endpoint ENDPOINT_ALIAS
ARGUMENTS
ENDPOINT_ALIAS FaunaDB server endpoint
DESCRIPTION
Sets an endpoint as the default one
EXAMPLE
$ fauna-shell default-endpoint endpoint
See code: src/commands/default-endpoint.js
fauna delete-database DBNAME
Deletes a database
USAGE
$ fauna delete-database DBNAME
ARGUMENTS
DBNAME database name
DESCRIPTION
Deletes a database
EXAMPLE
$ fauna-shell delete-database dbname
See code: src/commands/delete-database.js
fauna delete-key KEYNAME
Deletes a key
USAGE
$ fauna delete-key KEYNAME
ARGUMENTS
KEYNAME key name
DESCRIPTION
Deletes a key
EXAMPLE
$ fauna-shell delete-key 123456789012345678
See code: src/commands/delete-key.js
fauna help [COMMAND]
display help for fauna
USAGE
$ fauna help [COMMAND]
ARGUMENTS
COMMAND command to show help for
OPTIONS
--all see all commands in CLI
See code: @oclif/plugin-help
fauna list-databases
Lists top level databases
USAGE
$ fauna list-databases
DESCRIPTION
Lists top level databases
EXAMPLE
$ fauna-shell list-databases
See code: src/commands/list-databases.js
fauna list-endpoints
Lists FaunaDB connection endpoints
USAGE
$ fauna list-endpoints
DESCRIPTION
Lists FaunaDB connection endpoints
EXAMPLE
$ fauna-shell list-endpoints
See code: src/commands/list-endpoints.js
fauna list-keys
Lists top level keys
USAGE
$ fauna list-keys
DESCRIPTION
Lists top level keys
EXAMPLE
$ fauna-shell list-keys
See code: src/commands/list-keys.js
fauna shell DBNAME
Starts a FaunaDB shell
USAGE
$ fauna shell DBNAME
ARGUMENTS
DBNAME database name
DESCRIPTION
Starts a FaunaDB shell
EXAMPLE
$ fauna-shell dbname
See code: src/commands/shell.js