lib/fauna/query.rb in fauna-2.4.0 vs lib/fauna/query.rb in fauna-3.0.0
- old
+ new
@@ -3,21 +3,21 @@
# 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(ref('classes', 'spells'), { data: { name: 'Magic Missile' } }) }
+ # client.query { create(class_('spells'), { data: { name: 'Magic Missile' } }) }
#
# To build and return an query expression to execute later, use Fauna::Query.expr
#
- # Fauna::Query.expr { create(ref('classes', 'spells'), { data: { name: 'Magic Missile' } }) }
+ # Fauna::Query.expr { create(class_('spells'), { data: { name: 'Magic Missile' } }) }
#
# Or, you may directly use the helper methods:
#
- # Fauna::Query.create(Fauna::Query.ref('classes', 'spells'), { data: { name: 'Magic Missile' } })
+ # Fauna::Query.create(Fauna::Query.class_('spells'), { data: { name: 'Magic Missile' } })
module Query
- extend self
+ extend self, Deprecate
class QueryDSLContext < DSLContext # :nodoc:
include Query
end
@@ -43,17 +43,25 @@
# Construct a ref value
#
# Reference: {FaunaDB Values}[https://fauna.com/documentation/queries#values]
def ref(str, id = nil)
if id.nil?
- Ref.new(str)
+ Expr.new :@ref => Expr.wrap(str)
else
Expr.new ref: Expr.wrap(str), id: Expr.wrap(id)
end
end
##
+ # An abort expression
+ #
+ # Reference: {FaunaDB Basic Forms}[https://fauna.com/documentation/queries#basic_forms]
+ 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) } }
@@ -382,10 +390,26 @@
end
# :section: Set Functions
##
+ # A singleton expression
+ #
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
+ def singleton(ref)
+ Expr.new singleton: Expr.wrap(ref)
+ end
+
+ ##
+ # An events expression
+ #
+ # Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
+ def events(ref_set)
+ Expr.new events: Expr.wrap(ref_set)
+ end
+
+ ##
# A match expression
#
# Reference: {FaunaDB Sets}[https://fauna.com/documentation/queries#sets]
def match(index, *terms)
Expr.new match: Expr.wrap(index), terms: Expr.wrap_varargs(terms)
@@ -458,10 +482,26 @@
# Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
def identify(ref, password)
Expr.new identify: Expr.wrap(ref), password: Expr.wrap(password)
end
+ ##
+ # An identity function
+ #
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
+ def identity
+ Expr.new identity: nil
+ end
+
+ ##
+ # A has_identity function
+ #
+ # Reference: {FaunaDB Authentication}[https://fauna.com/documentation/queries#auth_functions]
+ def has_identity
+ Expr.new has_identity: nil
+ end
+
# :section: String Functions
##
# A concat function
#
@@ -476,12 +516,16 @@
##
# A casefold function
#
# Reference: {FaunaDB String Functions}[https://fauna.com/documentation/queries#string_functions]
- def casefold(string)
- Expr.new casefold: Expr.wrap(string)
+ 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
# :section: Time and Date Functions
##
@@ -516,43 +560,117 @@
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
def next_id
Expr.new next_id: nil
end
+ deprecate :next_id, :new_id
+
##
+ # A new_id function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def new_id
+ Expr.new new_id: nil
+ end
+
+ ##
# A database function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
- def database(name)
- Expr.new database: Expr.wrap(name)
+ 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 class function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
- def class_(name)
- Expr.new class: Expr.wrap(name)
+ 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
##
# An index function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
- def index(name)
- Expr.new index: Expr.wrap(name)
+ 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
##
# A function function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
- def function(name)
- Expr.new function: Expr.wrap(name)
+ 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 databases function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def databases(scope = nil)
+ Expr.new databases: Expr.wrap(scope)
+ end
+
+ ##
+ # A classes function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def classes(scope = nil)
+ Expr.new classes: Expr.wrap(scope)
+ end
+
+ ##
+ # An indexes function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def indexes(scope = nil)
+ Expr.new indexes: Expr.wrap(scope)
+ end
+
+ ##
+ # A functions function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def functions(scope = nil)
+ Expr.new functions: Expr.wrap(scope)
+ end
+
+ ##
+ # A tokens function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def tokens(scope = nil)
+ Expr.new tokens: Expr.wrap(scope)
+ end
+
+ ##
+ # A keys function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def keys(scope = nil)
+ Expr.new keys: Expr.wrap(scope)
+ end
+
+ ##
+ # A credentials function
+ #
+ # Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
+ def credentials(scope = nil)
+ Expr.new credentials: Expr.wrap(scope)
+ end
+
+ ##
# An equals function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation#queries-misc_functions]
def equals(*values)
Expr.new equals: Expr.wrap_varargs(values)
@@ -570,9 +688,17 @@
# A select function
#
# Reference: {FaunaDB Miscellaneous Functions}[https://fauna.com/documentation/queries#misc_functions]
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}[https://fauna.com/documentation/queries#misc_functions]
+ def select_all(path, from)
+ Expr.new select_all: Expr.wrap(path), from: Expr.wrap(from)
end
##
# An add function
#