RubyRuby driver

This section describes Fauna’s open source Ruby driver, which provides the resources required to interact with FaunaDB.

Install

The FaunaDB Ruby driver is distributed as a Ruby Gem.

gem install fauna

If you use Bundler, add it to your application’s Gemfile:

gem 'fauna'

And then execute:

bundle

Compatibility

This driver is compatible with the following Ruby versions:

  • MRI 1.9.3

  • MRI 2.2.3

  • JRuby 1.7.19

Usage

First, require the Gem:

require 'fauna'

Create a Client

All API requests pass through a Fauna::Client. Creating a client requires either an admin key, server key, client key, or a token.

server_key = 'ls8AkXLdakAAAALPAJFy3LvQAAGwDRAS_Prjy6O8VQBfQAlZzwAA'

Now we can make a database-level client:

$fauna = Fauna::Client.new(secret: server_key)

You can optionally configure an observer on the client. To ease debugging, we provide a simple logging observer at Fauna::ClientLogger.logger, which you can configure as such:

require 'logger'
logger = Logger.new(STDERR)
observer = Fauna::ClientLogger.logger { |log| logger.debug(log) }

$fauna = Fauna::Client.new(
  secret: server_key,
  observer: observer)

Execute queries

# Create a class
$fauna.query { create ref('classes'), name: 'users' }

# Create an instance of the class
taran = $fauna.query do
  create ref('classes/users'), data: { email: 'taran@example.com' }
end

# Update the instance
taran = $fauna.query do
  update taran[:ref], data: {
    name: 'Taran',
    profession: 'Pigkeeper'
  }
end

# Page through a set
pigkeepers = Fauna::Query.expr { match(ref('indexes/users_by_profession'), 'Pigkeeper') }
oracles = Fauna::Query.expr { match(ref('indexes/users_by_profession'), 'Oracle') }

$fauna.query { paginate(union(pigkeepers, oracles)) }

# Delete the user
$fauna.query { delete user[:ref] }

Next steps

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!