Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fauna-shell

Package Overview
Dependencies
Maintainers
2
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fauna-shell

faunadb shell

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
increased by0.45%
Maintainers
2
Weekly downloads
 
Created
Source

fauna-shell

This tools gives you access to Fauna directly from your CLI.

It also includes a Shell so you can issue queries to Fauna 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 our connection to a Fauna account. (If you don't have an account, you can create a free one here).

Let's run the following command:

$ fauna cloud-login

You will be prompted for your email and password from your Fauna account.

If you would like to use 3rd party identity providers like Github or Netlify, please refer to this guide.

Now that we have an endpoint to connect to we can try to create a database to start interacting with Fauna. See connecting to different endpoints.

This is how you can 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

And then listing your databases:

$ fauna list-databases
listing databases
my_app
my_second_app
my_other_app

You can also delete a particular 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 database my_app:

$ fauna create-key my_app
creating key for database 'my_app' with role 'admin'

created key for database 'my_app' with role 'admin'.
secret: ****************************************

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 (the results may differ from what you see in your database)

$ fauna list-keys
listing keys
Key ID               Database             Role
203269476002562560   my_app               admin
203269731203940864   my_app               admin
203269732275585536   my_app               admin
203269735610057216   test                 admin

And then delete the key with id: 200219702370238976:

$ fauna delete-key 200219702370238976
deleting key 200219702370238976
key 200219702370238976 deleted

See Commands for a list of commands and help on their usage.

Technical Requirements

In order to use Fauna Shell, you will need to meet these system requirements:

Node.js version

  • >= v10.0.0
  • < v12.17.0

Configuration

By default, requests made when using the cloud-login command will hit https://auth-console.fauna-preview.com/login. You can change this behavior by defining the FAUNA_SHELL_LOGIN_URL environment variable in your .env

For example:

FAUNA_SHELL_LOGIN_URL=https://www.mycustomdomain.com/login

Shell

The Fauna Shell lets you issue queries directly to your Fauna database 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 Fauna'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
Connected to http://127.0.0.1:8443
Type Ctrl+D or .exit to exit the shell
my_app>

Once you have the prompt ready, you can start issues queries against your Fauna database. (Note that the results shown here might vary from the ones you see while running the examples).

my_app> Collection.create({ name: "Post" })
{
  name: "Post",
  coll: Collection,
  ts: Time("2023-08-15T16:06:01.120Z"),
  indexes: {},
  constraints: []
}

Let's create an index for our collection Post.


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: []
}

Let's 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 .."
}

We can also insert items in bulk by using iterator functions on arrays.

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 let's try to fetch our post about latte. We need to access it by id like this:

my_app> Post.byId("373143473420763648")
{
  id: "373143473420763648",
  coll: Post,
  ts: Time("2023-08-15T16:16:36.960Z"),
  title: "Deep meanings in a latte"
}

Now let's update our post about our cat, by 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"
  ]
}

And now let's try to 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"
}

Now let's try to delete our post about latte:

my_app> Post.byId("373143473420763648")!.delete()
Post.byId("373143473420763648") /* not found */

If we try to fetch it, we will receive a null document:

my_app> Post.byId("373143473420763648")
Post.byId("373143473420763648") /* 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://example.com"
Endpoint Key: ****************************************
Endpoint Alias [example.com]: example_alias

The Endpoint Alias should be a name that helps you remember the purpose of this endpoint.

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 Fauna if the --endpoint flag is not set.

Endpoints can be listed with the list-endpoints command like this:

$ fauna list-endpoints
localhost
cloud *
cluster-us-east

There 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=secret
graphqlHost=127.0.0.1
graphqlPort=8084


[cloud]
domain=db.fauna.com
scheme=https
secret=FAUNA_SECRET_KEY
graphqlHost=graphql.fauna.com
graphqlPort=443

[cluster-us-east]
domain=cluster-us-east.example.com
port=443
scheme=https
secret=OTHER_FAUNA_SECRET
graphqlHost=cluster-us-east.example.com
graphqlPort=443

Connecting to local endpoints

If you are running Fauna locally using our Docker images, you may need to configure the Shell to work with local endpoints so you can interact with the databases running in the Docker containers.

Once you've installed the Shell and logged in, you can configure this by doing the following:

  1. Run fauna list-endpoints to see all your endpoints. If you haven't added any yet, you should just see the cloud endpoint that was added when you went through the login flow.

  2. By default, the Fauna Docker image serves data via port 8443 (check your Docker logs to confirm the port number). To add this, run the following:

fauna add-endpoint http://localhost:8443 # Doesn't work with HTTPS
  1. When prompted, provide the endpoint key and then give it a name (ex. localhost)

  2. Now, you can interact with your local database through the Fauna Shell by running the command below:

fauna shell --endpoint localhost

Overriding Connection Parameters

Most commands support the following options. You can specify them if you want to connect to a local instance of Fauna.

OPTIONS
  --domain=domain      [default: db.fauna.com] Fauna server domain
  --port=port          [default: 443] Connection port
  --scheme=https|http  [default: https] Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    [default: 80] Connection timeout in milliseconds
  --endpoint=alias     Overrides the default endpoint set in ~/.fauna-shell
  --graphqlHost=domain [default: graphql.fauna.com] The Fauna GraphQL API host
  --graphqlPort=port   [default: 443] The Fauna GraphQL API port to connect to

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 --graphqlHost=127.0.0.1 --graphqlPort=443

Options provided via the CLI will override the values set in the .fauna-shell config file.

For example you can start a shell to a different endpoint from the one set in .fauna-shell:

$ fauna shell my_app --endpoint=endpoint_alias

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.

Executing queries from a file

You can also tell the shell to execute a list of queries that you have stored in a file. For example, you can have a file that creates a collection called setup.fql:

Collection.create({
  name: "Post",
  indexes: {
    byTitle: {
      terms: [{ field: ".title" }]
    }
  }
})

Once the collection is created, you can execute queries against it in another .fql file:

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
  })
})

You can tell Fauna Shell to execute all those queries for you by running the following command:

$ fauna eval my_app --file=./setup.fql
$ fauna eval my_app --file=./queries.fql

Where my_app is the name of your database, and ./queries.fql is the path to the file where you saved the queries. If my_app is left out it will execute the queries file on the default fauna shell endpoint.

List of Commands

fauna add-endpoint ENDPOINT

Adds a connection endpoint for Fauna.

USAGE
  $ fauna add-endpoint ENDPOINT

ARGUMENTS
  ENDPOINT  Fauna server endpoint

DESCRIPTION
  Adds a connection endpoint for Fauna.

EXAMPLE
  $ fauna add-endpoint https://db.fauna.com:443
  $ fauna add-endpoint http://localhost:8443/ --alias localhost --key secret

See code: src/commands/add-endpoint.js

fauna autocomplete [SHELL]

display autocomplete installation instructions

USAGE
  $ fauna autocomplete [SHELL]

ARGUMENTS
  SHELL  shell type

OPTIONS
  -r, --refresh-cache  Refresh cache (ignores displaying instructions)

EXAMPLES
  $ fauna autocomplete
  $ fauna autocomplete bash
  $ fauna autocomplete zsh
  $ fauna autocomplete --refresh-cache

See code: @oclif/plugin-autocomplete

fauna cloud-login

Adds a Fauna endpoint.

USAGE
  $ fauna cloud-login

DESCRIPTION
  Adds a Fauna endpoint.

EXAMPLE
  $ fauna cloud-login

See code: src/commands/cloud-login.js

fauna create-database DBNAME

Creates a database

USAGE
  $ fauna create-database DBNAME

ARGUMENTS
  DBNAME  database name

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Creates a database

EXAMPLE
  $ fauna 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    (admin|server|server-readonly|client) key user role

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Creates a key for the specified database

EXAMPLE
  $ fauna 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  Fauna server endpoint alias

DESCRIPTION
  Sets an endpoint as the default one

EXAMPLE
  $ fauna 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

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Deletes a database

EXAMPLE
  $ fauna delete-database dbname

See code: src/commands/delete-database.js

fauna delete-endpoint ENDPOINT_ALIAS

Deletes a connection endpoint.

USAGE
  $ fauna delete-endpoint ENDPOINT_ALIAS

ARGUMENTS
  ENDPOINT_ALIAS  Fauna server endpoint alias

DESCRIPTION
  Deletes a connection endpoint.

EXAMPLE
  $ fauna delete-endpoint endpoint_alias

See code: src/commands/delete-endpoint.js

fauna delete-key KEYNAME

Deletes a key

USAGE
  $ fauna delete-key KEYNAME

ARGUMENTS
  KEYNAME  key name

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Deletes a key

EXAMPLE
  $ fauna 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 child databases in the current database

USAGE
  $ fauna list-databases

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Lists child databases in the current database

EXAMPLE
  $ fauna list-databases

See code: src/commands/list-databases.js

fauna list-endpoints

Lists connection endpoints.

USAGE
  $ fauna list-endpoints

DESCRIPTION
  Lists connection endpoints.

EXAMPLE
  $ fauna list-endpoints

See code: src/commands/list-endpoints.js

fauna list-keys

List keys in the current database or in its child databases

USAGE
  $ fauna list-keys

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  List keys in the current database or in its child databases

EXAMPLE
  $ fauna list-keys

See code: src/commands/list-keys.js

fauna run-queries DBNAME

Runs the queries found on the file passed to the command.

USAGE
  $ fauna run-queries DBNAME

ARGUMENTS
  DBNAME  database name

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --file=file          File where to read queries from
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds

DESCRIPTION
  Runs the queries found on the file passed to the command.

EXAMPLE
  $ fauna run-queries dbname --file=/path/to/queries.fql

See code: src/commands/run-queries.js

fauna shell [DBNAME]

Starts an interactive shell.

USAGE
  $ fauna shell [DBNAME]

ARGUMENTS
  DBNAME  database name

OPTIONS
  --domain=domain      Fauna server domain
  --endpoint=endpoint  Fauna server endpoint
  --port=port          Connection port
  --scheme=https|http  Connection scheme
  --secret=secret      Fauna secret key
  --timeout=timeout    Connection timeout in milliseconds
  --version=4|10       [default: 10] FQL version to use

DESCRIPTION
  Starts an interactive shell.

EXAMPLE
  $ fauna shell dbname

See code: src/commands/shell.js

fauna eval [DBNAME] [QUERY]

Evaluates a fauna query

USAGE
  $ fauna eval [DBNAME] [QUERY]

ARGUMENTS
  QUERY  FQL query to execute
  DBNAME Database name

OPTIONS
  --domain=domain                  Fauna server domain
  --endpoint=endpoint              Fauna server endpoint
  --file=file                      File where to read queries from
  --format=json|shell|json-tagged  [default: shell if tty, json if no tty] Output format
  --output=output                  File to write output to
  --port=port                      Connection port
  --scheme=https|http              Connection scheme
  --secret=secret                  Fauna secret key
  --stdin                          Read file input from stdin. Writes to stdout by default
  --timeout=timeout                Connection timeout in milliseconds
  --version=4|10                   [default: 10] FQL version to use

DESCRIPTION
  Runs the specified query. Can read from stdin, file or command line.
  Outputs to either stdout or file.
  Output format can be specified.

EXAMPLES
  $ fauna eval "Collection.all()"
  $ fauna eval nestedDbName "Collection.all()"
  $ fauna eval --file=/path/to/queries.fql
  $ echo "1 + 1" | fauna eval
  $ fauna eval "2 + 3" --output=/tmp/result"
  $ fauna eval "2 + 3" --format=json --output=/tmp/result"

See code: src/commands/eval.js

fauna import

Import data to Fauna

USAGE
  $ fauna import --path [DATA]

OPTIONS
  --allow-short-rows       Allows rows which are shorter than the number of headers
  --append                 Allows appending documents to a non-empty collection
  --collection=collection  Collection name. When not specified, the collection name is the filename when --path is file
  --db=db                  Child database name; imported documents are stored in this database
  --domain=domain          Fauna server domain
  --endpoint=endpoint      Fauna server endpoint
  --path=path              (required) Path to .csv/.json file, or path to folder containing .csv/.json files
  --port=port              Connection port
  --scheme=https|http      Connection scheme
  --secret=secret          Fauna secret key
  --timeout=timeout        Connection timeout in milliseconds

  --type=type              Column type casting, converts the column value to a Fauna type.
                           Format: <column>::<type>
                           <column>: the name of the column to cast values
                           <type>: one of 'number', 'bool', or 'date'.

EXAMPLES
  $ fauna import --path ./collection_name.csv
  $ fauna import --append --path ./collection.csv
  $ fauna import --db=sampleDB --collection=SampleCollection --path ./datafile.csv
  $ fauna import --db=sampleDB --path ./dump
  $ fauna import --type=header_name::date --type=hdr2::number --type=hdrX::bool --path ./collection.csv

See code: src/commands/import.js

fauna upload-graphql-schema graphqlFilePath

Upload GraphQL schema

USAGE
  $ fauna upload-graphql-schema GRAPHQLFILEPATH

ARGUMENTS
  GRAPHQLFILEPATH  Path to GraphQL schema

OPTIONS
  --domain=domain            Fauna server domain
  --endpoint=endpoint        Fauna server endpoint
  --graphqlHost=graphqlHost  The Fauna GraphQL API host
  --graphqlPort=port         GraphQL port
  --mode=merge|override      [default: merge] Upload mode
  --port=port                Connection port
  --scheme=https|http        Connection scheme
  --secret=secret            Fauna secret key
  --timeout=timeout          Connection timeout in milliseconds

EXAMPLES
  $ fauna upload-graphql-schema ./schema.gql
  $ fauna upload-graphql-schema ./schema.gql --mode override

See code: src/commands/upload-graphql-schema.js

fauna import --path FILE_PATH

Import data to dana

USAGE
  $ fauna import --path FILE_PATH


OPTIONS
  --allow-short-rows       Allows rows which are shorter than the number of headers
  --append                 Allows appending documents to a non-empty collection
  --collection=collection  Collection name. When not specified, the collection name is the filename when --path is file
  --db=db                  Child database name; imported documents are stored in this database
  --domain=domain          Fauna server domain
  --endpoint=endpoint      Fauna server endpoint
  --path=path              (required) Path to .csv/.json file, or path to folder containing .csv/.json files
  --port=port              Connection port
  --scheme=https|http      Connection scheme
  --secret=secret          Fauna secret key
  --timeout=timeout        Connection timeout in milliseconds

  --type=type              Column type casting, converts the column value to a Fauna type.
                           Format: <column>::<type>
                           <column>: the name of the column to cast values
                           <type>: one of 'number', 'bool', or 'date'.

EXAMPLES
  $ fauna import --path ./samplefile.csv
  $ fauna import --append --path ./samplefile.csv
  $ fauna import --db=sampleDB --collection=Samplecollection --path ./samplefile.csv
  $ fauna import --db=sampleDB --path ./dump
  $ fauna import --type=header_name::date --type=hdr2::number --type=hdrX::bool --path ./samplefile.csv

See code: src/commands/upload-graphql-schema.js

Development

All above commands starts with fauna, but you are able to run them this way after installation of the fauna-shell package.
During development, you might want to test your changes without installing the package every single time.
To do so, you can run commands like this:

# don't forget to install dependencies for your fauna-shell project
npm install

# run a command you need
./bin/run cloud-login
./bin/run import

Keywords

FAQs

Package last updated on 21 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc