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 the need of install additional libraries.
You can install it via npm like this:
$ npm install -g fauna-shell
Usage
The tool allows you to do things like creating, deleting and listings databases.
First lets create a file with our FaunaDB key so the shell has access to our account. If you don't have an account, you can create a free account here.
Run the following command by replacing YOUR_FAUNADB_KEY
with the actual key from your FaunaDB Cloud account.
$ echo "secret=YOUR_FAUNADB_KEY" >> ~/.fauna-shell
The fauna
tool will read our key from that file and then use it to authenticate against the FaunaDB Cloud.
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> query(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> query(
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> query(
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> query(
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> query(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> query(
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> query(
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> query(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> query(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 your local FaunaDB instance
All the 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
You can also save them in the .fauna-shell
configuration file like this:
domain=127.0.0.1
port=8443
scheme=http
timeout=42
secret=YOUR_FAUNA_SECRET_KEY
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 create-database DBNAME
Creates a database
USAGE
$ fauna create-database DBNAME
ARGUMENTS
DBNAME database name
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
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
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
DESCRIPTION
Creates a key for the specified database
EXAMPLE
$ fauna-shell create-key dbname admin
See code: src/commands/create-key.js
fauna delete-database DBNAME
Deletes a database
USAGE
$ fauna delete-database DBNAME
ARGUMENTS
DBNAME database name
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
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
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
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
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
DESCRIPTION
Lists top level databases
EXAMPLE
$ fauna-shell list-databases
See code: src/commands/list-databases.js
fauna list-keys
Lists top level keys
USAGE
$ fauna list-keys
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
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
OPTIONS
--domain=domain FaunaDB server domain
--port=port Connection port
--scheme=https|http Connection scheme
--secret=secret FaunaDB secret key
--timeout=timeout Connection timeout in milliseconds
DESCRIPTION
Starts a FaunaDB shell
EXAMPLE
$ fauna-shell dbname
See code: src/commands/shell.js