Class: Google::Cloud::Firestore::Query
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::Query
- Defined in:
- lib/google/cloud/firestore/query.rb
Overview
Query
Represents a query to the Firestore API.
Instances of this class are immutable. All methods that refine the query return new instances.
Direct Known Subclasses
Instance Method Summary collapse
-
#end_at(*values) ⇒ Query
Ends query results at a set of field values.
-
#end_before(*values) ⇒ Query
Ends query results before a set of field values.
-
#get {|documents| ... } ⇒ Enumerator<DocumentSnapshot>
(also: #run)
Retrieves document snapshots for the query.
-
#limit(num) ⇒ Query
Limits a query to return a fixed number of results.
-
#listen {|callback| ... } ⇒ QueryListener
(also: #on_snapshot)
Listen to this query for changes.
-
#offset(num) ⇒ Query
Skips to an offset in a query.
-
#order(field, direction = :asc) ⇒ Query
(also: #order_by)
Specifies an "order by" clause on a field.
-
#select(*fields) ⇒ Query
Restricts documents matching the query to return only data for the provided fields.
-
#start_after(*values) ⇒ Query
Starts query results after a set of field values.
-
#start_at(*values) ⇒ Query
Starts query results at a set of field values.
-
#where(field, operator, value) ⇒ Query
Filters the query on a field.
Instance Method Details
#end_at(*values) ⇒ Query
Ends query results at a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
include the document specified by values
.
If the current query already has specified end_before
or
end_at
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
789 790 791 792 793 794 795 796 797 798 799 800 |
# File 'lib/google/cloud/firestore/query.rb', line 789 def end_at *values raise ArgumentError, "must provide values" if values.empty? new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = false new_query.end_at = cursor Query.start new_query, parent_path, client end |
#end_before(*values) ⇒ Query
Ends query results before a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
not include the document specified by values
.
If the current query already has specified end_before
or
end_at
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
685 686 687 688 689 690 691 692 693 694 695 696 |
# File 'lib/google/cloud/firestore/query.rb', line 685 def end_before *values raise ArgumentError, "must provide values" if values.empty? new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = true new_query.end_at = cursor Query.start new_query, parent_path, client end |
#get {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: run
Retrieves document snapshots for the query.
825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/google/cloud/firestore/query.rb', line 825 def get ensure_service! return enum_for(:get) unless block_given? results = service.run_query parent_path, @query results.each do |result| next if result.document.nil? yield DocumentSnapshot.from_query_result(result, client) end end |
#limit(num) ⇒ Query
Limits a query to return a fixed number of results. If the current query already has a limit set, this will overwrite it.
377 378 379 380 381 382 383 384 |
# File 'lib/google/cloud/firestore/query.rb', line 377 def limit num new_query = @query.dup new_query ||= StructuredQuery.new new_query.limit = Google::Protobuf::Int32Value.new(value: num) Query.start new_query, parent_path, client end |
#listen {|callback| ... } ⇒ QueryListener Also known as: on_snapshot
Listen to this query for changes.
862 863 864 865 866 867 868 |
# File 'lib/google/cloud/firestore/query.rb', line 862 def listen &callback raise ArgumentError, "callback required" if callback.nil? ensure_service! QueryListener.new(self, &callback).start end |
#offset(num) ⇒ Query
Skips to an offset in a query. If the current query already has specified an offset, this will overwrite it.
345 346 347 348 349 350 351 352 |
# File 'lib/google/cloud/firestore/query.rb', line 345 def offset num new_query = @query.dup new_query ||= StructuredQuery.new new_query.offset = num Query.start new_query, parent_path, client end |
#order(field, direction = :asc) ⇒ Query Also known as: order_by
Specifies an "order by" clause on a field.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/google/cloud/firestore/query.rb', line 300 def order field, direction = :asc if query_has_cursors? raise "cannot call order after calling " \ "start_at, start_after, end_before, or end_at" end new_query = @query.dup new_query ||= StructuredQuery.new field = FieldPath.parse field unless field.is_a? FieldPath new_query.order_by << StructuredQuery::Order.new( field: StructuredQuery::FieldReference.new( field_path: field.formatted_string ), direction: order_direction(direction) ) Query.start new_query, parent_path, client end |
#select(*fields) ⇒ Query
Restricts documents matching the query to return only data for the provided fields.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/google/cloud/firestore/query.rb', line 104 def select *fields new_query = @query.dup new_query ||= StructuredQuery.new fields = Array(fields).flatten.compact fields = [FieldPath.document_id] if fields.empty? field_refs = fields.flatten.compact.map do |field| field = FieldPath.parse field unless field.is_a? FieldPath StructuredQuery::FieldReference.new \ field_path: field.formatted_string end new_query.select = StructuredQuery::Projection.new field_refs.each do |field_ref| new_query.select.fields << field_ref end Query.start new_query, parent_path, client end |
#start_after(*values) ⇒ Query
Starts query results after a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
not include the document specified by values
.
If the current query already has specified start_at
or
start_after
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/google/cloud/firestore/query.rb', line 581 def start_after *values raise ArgumentError, "must provide values" if values.empty? new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = false new_query.start_at = cursor Query.start new_query, parent_path, client end |
#start_at(*values) ⇒ Query
Starts query results at a set of field values. The field values can be
specified explicitly as arguments, or can be specified implicitly by
providing a DocumentSnapshot object instead. The result set will
include the document specified by values
.
If the current query already has specified start_at
or
start_after
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/google/cloud/firestore/query.rb', line 477 def start_at *values raise ArgumentError, "must provide values" if values.empty? new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = true new_query.start_at = cursor Query.start new_query, parent_path, client end |
#where(field, operator, value) ⇒ Query
Filters the query on a field.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/google/cloud/firestore/query.rb', line 235 def where field, operator, value if query_has_cursors? raise "cannot call where after calling " \ "start_at, start_after, end_before, or end_at" end new_query = @query.dup new_query ||= StructuredQuery.new field = FieldPath.parse field unless field.is_a? FieldPath new_filter = filter field.formatted_string, operator, value add_filters_to_query new_query, new_filter Query.start new_query, parent_path, client end |