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
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
↑ topPublic Instance Methods
A #has_identity function
Reference: FaunaDB Authentication
# File lib/fauna/query.rb, line 499 def has_identity Expr.new has_identity: nil end
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
An identity function
Reference: FaunaDB Authentication
# File lib/fauna/query.rb, line 491 def identity Expr.new identity: nil end
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
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
↑ topPublic Instance Methods
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
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
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
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
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
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
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
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
↑ topPublic Instance Methods
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
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
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
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
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
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
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
↑ topPublic Instance Methods
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
A #new_id function
Reference: FaunaDB Miscellaneous Functions
# File lib/fauna/query.rb, line 571 def new_id Expr.new new_id: nil end
A #next_id function
Reference: FaunaDB Miscellaneous Functions
# File lib/fauna/query.rb, line 561 def next_id Expr.new next_id: nil end
A not function
Reference: FaunaDB Miscellaneous Functions
# File lib/fauna/query.rb, line 795 def not_(boolean) Expr.new not: Expr.wrap(boolean) end
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
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
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
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
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
↑ topPublic Instance Methods
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
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
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
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
↑ topPublic Instance Methods
A difference expression
Reference: FaunaDB Sets
# File lib/fauna/query.rb, line 438 def difference(*sets) Expr.new difference: Expr.wrap_varargs(sets) end
A distinct expression
Reference: FaunaDB Sets
# File lib/fauna/query.rb, line 446 def distinct(set) Expr.new distinct: Expr.wrap(set) end
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
An intersection expression
Reference: FaunaDB Sets
# File lib/fauna/query.rb, line 430 def intersection(*sets) Expr.new intersection: Expr.wrap_varargs(sets) end
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
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
A singleton expression
Reference: FaunaDB Sets
# File lib/fauna/query.rb, line 398 def singleton(ref) Expr.new singleton: Expr.wrap(ref) end
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
↑ topPublic Instance Methods
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
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
↑ topPublic Instance Methods
A date expression
Reference: FaunaDB Time Functions
# File lib/fauna/query.rb, line 551 def date(string) Expr.new date: Expr.wrap(string) end
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
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
↑ topPublic Instance Methods
An abort expression
Reference: FaunaDB Basic Forms
# File lib/fauna/query.rb, line 58 def abort(msg) Expr.new abort: Expr.wrap(msg) end
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
A query expression
Reference: FaunaDB Basic Forms
# File lib/fauna/query.rb, line 79 def query(expr) Expr.new query: Expr.wrap(expr) end
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
↑ topPublic Instance Methods
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
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
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
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
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
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
A delete expression
Reference: FaunaDB Write functions
# File lib/fauna/query.rb, line 332 def delete(ref) Expr.new delete: Expr.wrap(ref) end
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
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
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
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