module Fauna::Query

Helpers modeling the FaunaDB Query language.

Helpers are usually used via a concise DSL notation. A DSL block may be used directly with Fauna::Client

client.query { create(class_('spells'), { data: { name: 'Magic Missile' } }) }

To build and return an query expression to execute later, use ::expr

Fauna::Query.expr { create(class_('spells'), { data: { name: 'Magic Missile' } }) }

Or, you may directly use the helper methods:

Fauna::Query.create(Fauna::Query.class_('spells'), { data: { name: 'Magic Missile' } })

Public Class Methods

expr(&block) click to toggle source

Build a query expression.

Allows for unscoped calls to Fauna::Query methods within the provided block. The block should return the constructed query expression.

Example: Fauna::Query.expr { add(1, 2, subtract(3, 2)) }

# File lib/fauna/query.rb, line 32
def self.expr(&block)
  return nil if block.nil?

  dsl = QueryDSLContext.new

  Expr.wrap DSLContext.eval_dsl(dsl, &block)
end

Authentication Functions

↑ top

Public Instance Methods

has_identity() click to toggle source

A #has_identity function

Reference: FaunaDB Authentication

# File lib/fauna/query.rb, line 499
def has_identity
  Expr.new has_identity: nil
end
identify(ref, password) click to toggle source

An identify function

Reference: FaunaDB Authentication

# File lib/fauna/query.rb, line 483
def identify(ref, password)
  Expr.new identify: Expr.wrap(ref), password: Expr.wrap(password)
end
identity() click to toggle source

An identity function

Reference: FaunaDB Authentication

# File lib/fauna/query.rb, line 491
def identity
  Expr.new identity: nil
end
login(ref, params) click to toggle source

A login function

Reference: FaunaDB Authentication

# File lib/fauna/query.rb, line 467
def login(ref, params)
  Expr.new login: Expr.wrap(ref), params: Expr.wrap(params)
end
logout(all_tokens) click to toggle source

A logout function

Reference: FaunaDB Authentication

# File lib/fauna/query.rb, line 475
def logout(all_tokens)
  Expr.new logout: Expr.wrap(all_tokens)
end

Basic forms

↑ top

Public Instance Methods

at(timestamp, expr) click to toggle source

An at expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 89
def at(timestamp, expr)
  Expr.new at: Expr.wrap(timestamp), expr: Expr.wrap(expr)
end
call(name, *args) click to toggle source

A call expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 197
def call(name, *args)
  Expr.new call: Expr.wrap(name), arguments: Expr.wrap_varargs(args)
end
do_(*expressions) click to toggle source

A do expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 141
def do_(*expressions)
  Expr.new do: Expr.wrap_varargs(expressions)
end
if_(condition, then_, else_) click to toggle source

An if expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 133
def if_(condition, then_, else_)
  Expr.new if: Expr.wrap(condition), then: Expr.wrap(then_), else: Expr.wrap(else_)
end
lambda(&block) click to toggle source

A lambda expression

Reference: FaunaDB Basic Forms

This form generates var objects for you, and is called like:

Query.lambda do |a|
  Query.add a, a
end
# Produces: {lambda: :a, expr: {add: [{var: :a}, {var: :a}]}}

Query functions requiring lambdas can be passed blocks without explicitly calling lambda.

You can also use lambda_expr and var directly.

block

Takes one or more var expressions and uses them to construct an expression. If this takes more than one argument, the lambda destructures an array argument. (To destructure single-element arrays use lambda_expr.)

# File lib/fauna/query.rb, line 164
def lambda(&block)
  dsl = Query::QueryDSLContext.new
  vars =
    block.parameters.map do |kind, name|
      fail ArgumentError, 'Splat parameters are not supported in lambda expressions.' if kind == :rest
      name
    end

  case vars.length
  when 0
    fail ArgumentError, 'Block must take at least 1 argument.'
  when 1
    # When there's only 1 parameter, don't use an array pattern.
    lambda_expr vars[0], DSLContext.eval_dsl(dsl, var(vars[0]), &block)
  else
    lambda_expr vars, DSLContext.eval_dsl(dsl, *(vars.map { |v| var(v) }), &block)
  end
end
lambda_expr(var, expr) click to toggle source

A raw lambda expression

Reference: FaunaDB Basic Forms

See also lambda.

# File lib/fauna/query.rb, line 189
def lambda_expr(var, expr)
  Expr.new lambda: Expr.wrap(var), expr: Expr.wrap(expr)
end
let(vars, expr = nil, &block) click to toggle source

A let expression

Only one of expr or block should be provided.

Block example: Fauna.query { let(x: 2) { add(1, x) } }.

Expression example: Fauna.query { let({ x: 2 }, add(1, var(:x))) }.

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 103
def let(vars, expr = nil, &block)
  in_ =
    if block.nil?
      expr
    else
      dsl = QueryDSLContext.new
      dslcls = (class << dsl; self; end)

      vars.keys.each do |v|
        dslcls.send(:define_method, v) { var(v) }
      end

      DSLContext.eval_dsl(dsl, &block)
    end

  Expr.new let: Expr.wrap_values(vars), in: Expr.wrap(in_)
end
var(name) click to toggle source

A var expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 125
def var(name)
  Expr.new var: Expr.wrap(name)
end

Collection Functions

↑ top

Public Instance Methods

append(collection, elements) click to toggle source

An append expression

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 264
def append(collection, elements)
  Expr.new append: Expr.wrap(elements), collection: Expr.wrap(collection)
end
drop(number, collection) click to toggle source

A drop expression

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 248
def drop(number, collection)
  Expr.new drop: Expr.wrap(number), collection: Expr.wrap(collection)
end
filter(collection, lambda_expr = nil, &lambda_block) click to toggle source

A filter expression

Only one of lambda_expr or lambda_block should be provided. For example: Fauna.query { filter(collection) { |a| equals a, 1 } }.

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 232
def filter(collection, lambda_expr = nil, &lambda_block)
  Expr.new filter: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
end
foreach(collection, lambda_expr = nil, &lambda_block) click to toggle source

A foreach expression

Only one of lambda_expr or lambda_block should be provided. For example: Fauna.query { foreach(collection) { |a| delete a } }.

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 221
def foreach(collection, lambda_expr = nil, &lambda_block)
  Expr.new foreach: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
end
map(collection, lambda_expr = nil, &lambda_block) click to toggle source

A map expression

Only one of lambda_expr or lambda_block should be provided. For example: Fauna.query { map(collection) { |a| add a, 1 } }.

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 210
def map(collection, lambda_expr = nil, &lambda_block)
  Expr.new map: Expr.wrap(lambda_expr || lambda_block), collection: Expr.wrap(collection)
end
prepend(collection, elements) click to toggle source

A prepend expression

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 256
def prepend(collection, elements)
  Expr.new prepend: Expr.wrap(elements), collection: Expr.wrap(collection)
end
take(number, collection) click to toggle source

A take expression

Reference: FaunaDB Collections

# File lib/fauna/query.rb, line 240
def take(number, collection)
  Expr.new take: Expr.wrap(number), collection: Expr.wrap(collection)
end

Miscellaneous Functions

↑ top

Public Instance Methods

add(*numbers) click to toggle source

An add function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 707
def add(*numbers)
  Expr.new add: Expr.wrap_varargs(numbers)
end
and_(*booleans) click to toggle source

An and function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 779
def and_(*booleans)
  Expr.new and: Expr.wrap_varargs(booleans)
end
class_(name, scope = nil) click to toggle source

A class function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 589
def class_(name, scope = nil)
  return Expr.new class: Expr.wrap(name) if scope.nil?

  Expr.new class: Expr.wrap(name), scope: Expr.wrap(scope)
end
classes(scope = nil) click to toggle source

A classes function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 627
def classes(scope = nil)
  Expr.new classes: Expr.wrap(scope)
end
contains(path, in_) click to toggle source

A contains function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 683
def contains(path, in_)
  Expr.new contains: Expr.wrap(path), in: Expr.wrap(in_)
end
credentials(scope = nil) click to toggle source

A credentials function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 667
def credentials(scope = nil)
  Expr.new credentials: Expr.wrap(scope)
end
database(name, scope = nil) click to toggle source

A database function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 579
def database(name, scope = nil)
  return Expr.new database: Expr.wrap(name) if scope.nil?

  Expr.new database: Expr.wrap(name), scope: Expr.wrap(scope)
end
databases(scope = nil) click to toggle source

A databases function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 619
def databases(scope = nil)
  Expr.new databases: Expr.wrap(scope)
end
divide(*numbers) click to toggle source

A divide function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 731
def divide(*numbers)
  Expr.new divide: Expr.wrap_varargs(numbers)
end
equals(*values) click to toggle source

An equals function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 675
def equals(*values)
  Expr.new equals: Expr.wrap_varargs(values)
end
function(name, scope = nil) click to toggle source

A function function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 609
def function(name, scope = nil)
  return Expr.new function: Expr.wrap(name) if scope.nil?

  Expr.new function: Expr.wrap(name), scope: Expr.wrap(scope)
end
functions(scope = nil) click to toggle source

A functions function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 643
def functions(scope = nil)
  Expr.new functions: Expr.wrap(scope)
end
gt(*values) click to toggle source

A greater than function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 763
def gt(*values)
  Expr.new gt: Expr.wrap_varargs(values)
end
gte(*values) click to toggle source

A greater than or equal function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 771
def gte(*values)
  Expr.new gte: Expr.wrap_varargs(values)
end
index(name, scope = nil) click to toggle source

An index function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 599
def index(name, scope = nil)
  return Expr.new index: Expr.wrap(name) if scope.nil?

  Expr.new index: Expr.wrap(name), scope: Expr.wrap(scope)
end
indexes(scope = nil) click to toggle source

An indexes function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 635
def indexes(scope = nil)
  Expr.new indexes: Expr.wrap(scope)
end
keys(scope = nil) click to toggle source

A keys function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 659
def keys(scope = nil)
  Expr.new keys: Expr.wrap(scope)
end
lt(*values) click to toggle source

A less than function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 747
def lt(*values)
  Expr.new lt: Expr.wrap_varargs(values)
end
lte(*values) click to toggle source

A less than or equal function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 755
def lte(*values)
  Expr.new lte: Expr.wrap_varargs(values)
end
modulo(*numbers) click to toggle source

A modulo function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 739
def modulo(*numbers)
  Expr.new modulo: Expr.wrap_varargs(numbers)
end
multiply(*numbers) click to toggle source

A multiply function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 715
def multiply(*numbers)
  Expr.new multiply: Expr.wrap_varargs(numbers)
end
new_id() click to toggle source

A #new_id function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 571
def new_id
  Expr.new new_id: nil
end
next_id() click to toggle source

A #next_id function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 561
def next_id
  Expr.new next_id: nil
end
not_(boolean) click to toggle source

A not function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 795
def not_(boolean)
  Expr.new not: Expr.wrap(boolean)
end
or_(*booleans) click to toggle source

An or function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 787
def or_(*booleans)
  Expr.new or: Expr.wrap_varargs(booleans)
end
select(path, from, params = {}) click to toggle source

A select function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 691
def select(path, from, params = {})
  Expr.new Expr.wrap_values(params).merge(select: Expr.wrap(path), from: Expr.wrap(from))
end
select_all(path, from) click to toggle source

A #select_all function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 699
def select_all(path, from)
  Expr.new select_all: Expr.wrap(path), from: Expr.wrap(from)
end
subtract(*numbers) click to toggle source

A subtract function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 723
def subtract(*numbers)
  Expr.new subtract: Expr.wrap_varargs(numbers)
end
tokens(scope = nil) click to toggle source

A tokens function

Reference: FaunaDB Miscellaneous Functions

# File lib/fauna/query.rb, line 651
def tokens(scope = nil)
  Expr.new tokens: Expr.wrap(scope)
end

Read Functions

↑ top

Public Instance Methods

exists(ref, params = {}) click to toggle source

An exists expression

Reference: FaunaDB Read functions

# File lib/fauna/query.rb, line 298
def exists(ref, params = {})
  Expr.new Expr.wrap_values(params).merge(exists: Expr.wrap(ref))
end
get(ref, params = {}) click to toggle source

A get expression

Reference: FaunaDB Read functions

# File lib/fauna/query.rb, line 274
def get(ref, params = {})
  Expr.new Expr.wrap_values(params).merge(get: Expr.wrap(ref))
end
key_from_secret(secret) click to toggle source

A #key_from_secret expression

Reference: FaunaDB Read functions

# File lib/fauna/query.rb, line 282
def key_from_secret(secret)
  Expr.new key_from_secret: Expr.wrap(secret)
end
paginate(set, params = {}) click to toggle source

A paginate expression

Reference: FaunaDB Read functions

# File lib/fauna/query.rb, line 290
def paginate(set, params = {})
  Expr.new Expr.wrap_values(params).merge(paginate: Expr.wrap(set))
end

Set Functions

↑ top

Public Instance Methods

difference(*sets) click to toggle source

A difference expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 438
def difference(*sets)
  Expr.new difference: Expr.wrap_varargs(sets)
end
distinct(set) click to toggle source

A distinct expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 446
def distinct(set)
  Expr.new distinct: Expr.wrap(set)
end
events(ref_set) click to toggle source

An events expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 406
def events(ref_set)
  Expr.new events: Expr.wrap(ref_set)
end
intersection(*sets) click to toggle source

An intersection expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 430
def intersection(*sets)
  Expr.new intersection: Expr.wrap_varargs(sets)
end
join(source, target_expr = nil, &target_block) click to toggle source

A join expression

Only one of target_expr or target_block should be provided. For example: Fauna.query { join(source) { |x| match some_index, x } }.

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 457
def join(source, target_expr = nil, &target_block)
  Expr.new join: Expr.wrap(source), with: Expr.wrap(target_expr || target_block)
end
match(index, *terms) click to toggle source

A match expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 414
def match(index, *terms)
  Expr.new match: Expr.wrap(index), terms: Expr.wrap_varargs(terms)
end
singleton(ref) click to toggle source

A singleton expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 398
def singleton(ref)
  Expr.new singleton: Expr.wrap(ref)
end
union(*sets) click to toggle source

A union expression

Reference: FaunaDB Sets

# File lib/fauna/query.rb, line 422
def union(*sets)
  Expr.new union: Expr.wrap_varargs(sets)
end

String Functions

↑ top

Public Instance Methods

casefold(string, normalizer = nil) click to toggle source

A casefold function

Reference: FaunaDB String Functions

# File lib/fauna/query.rb, line 521
def casefold(string, normalizer = nil)
  if normalizer.nil?
    Expr.new casefold: Expr.wrap(string)
  else
    Expr.new casefold: Expr.wrap(string), normalizer: Expr.wrap(normalizer)
  end
end
concat(strings, separator = nil) click to toggle source

A concat function

Reference: FaunaDB String Functions

# File lib/fauna/query.rb, line 509
def concat(strings, separator = nil)
  if separator.nil?
    Expr.new concat: Expr.wrap(strings)
  else
    Expr.new concat: Expr.wrap(strings), separator: Expr.wrap(separator)
  end
end

Time and Date Functions

↑ top

Public Instance Methods

date(string) click to toggle source

A date expression

Reference: FaunaDB Time Functions

# File lib/fauna/query.rb, line 551
def date(string)
  Expr.new date: Expr.wrap(string)
end
epoch(number, unit) click to toggle source

An epoch expression

Reference: FaunaDB Time Functions

# File lib/fauna/query.rb, line 543
def epoch(number, unit)
  Expr.new epoch: Expr.wrap(number), unit: Expr.wrap(unit)
end
time(string) click to toggle source

A time expression

Reference: FaunaDB Time Functions

# File lib/fauna/query.rb, line 535
def time(string)
  Expr.new time: Expr.wrap(string)
end

Values

↑ top

Public Instance Methods

abort(msg) click to toggle source

An abort expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 58
def abort(msg)
  Expr.new abort: Expr.wrap(msg)
end
object(fields) click to toggle source

An object expression

Query expression constructs can also take a regular ruby object, so the following are equivalent:

Fauna.query { { x: 1, y: add(1, 2) } }
Fauna.query { object(x: 1, y: add(1, 2)) }

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 71
def object(fields)
  Expr.new object: Expr.wrap_values(fields)
end
query(expr) click to toggle source

A query expression

Reference: FaunaDB Basic Forms

# File lib/fauna/query.rb, line 79
def query(expr)
  Expr.new query: Expr.wrap(expr)
end
ref(str, id = nil) click to toggle source

Construct a ref value

Reference: FaunaDB Values

# File lib/fauna/query.rb, line 46
def ref(str, id = nil)
  if id.nil?
    Expr.new :@ref => Expr.wrap(str)
  else
    Expr.new ref: Expr.wrap(str), id: Expr.wrap(id)
  end
end

Write Functions

↑ top

Public Instance Methods

create(class_ref, params) click to toggle source

A create expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 308
def create(class_ref, params)
  Expr.new create: Expr.wrap(class_ref), params: Expr.wrap(params)
end
create_class(params) click to toggle source

A create class expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 356
def create_class(params)
  Expr.new create_class: Expr.wrap(params)
end
create_database(params) click to toggle source

A create database expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 372
def create_database(params)
  Expr.new create_database: Expr.wrap(params)
end
create_function(params) click to toggle source

A create function expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 388
def create_function(params)
  Expr.new create_function: Expr.wrap(params)
end
create_index(params) click to toggle source

A create index expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 364
def create_index(params)
  Expr.new create_index: Expr.wrap(params)
end
create_key(params) click to toggle source

A create key expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 380
def create_key(params)
  Expr.new create_key: Expr.wrap(params)
end
delete(ref) click to toggle source

A delete expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 332
def delete(ref)
  Expr.new delete: Expr.wrap(ref)
end
insert(ref, ts, action, params) click to toggle source

An insert expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 340
def insert(ref, ts, action, params)
  Expr.new insert: Expr.wrap(ref), ts: Expr.wrap(ts), action: Expr.wrap(action), params: Expr.wrap(params)
end
remove(ref, ts, action) click to toggle source

A remove expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 348
def remove(ref, ts, action)
  Expr.new remove: Expr.wrap(ref), ts: Expr.wrap(ts), action: Expr.wrap(action)
end
replace(ref, params) click to toggle source

A replace expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 324
def replace(ref, params)
  Expr.new replace: Expr.wrap(ref), params: Expr.wrap(params)
end
update(ref, params) click to toggle source

An update expression

Reference: FaunaDB Write functions

# File lib/fauna/query.rb, line 316
def update(ref, params)
  Expr.new update: Expr.wrap(ref), params: Expr.wrap(params)
end