google-cloud-firestore 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e83411cc86b2d8999f2ea7771cc243f032b9119c03885427aa57b007c3695406
4
- data.tar.gz: 2ba1894414f95466c521ac6159dcfeedfb713727049171226a0eaecb2b041a71
3
+ metadata.gz: d1e7097a28daa16ef5b2de8684c5b052b6409b792a4e7f382f878c47745dbf1a
4
+ data.tar.gz: 35dae74e7a063ad5e456420633c50b60ea0731b2fcafdde0e6ceab25b10bea25
5
5
  SHA512:
6
- metadata.gz: ee5186f01ad3ec6adf7508a043793306c622f77b5a4552f91a787d52860e57e8e65c99561d8995cd7dd3a424801ac7e56b36174e6a9139179bed3ac708391966
7
- data.tar.gz: 0c2037885a4d5b12151f346f181de0f632e6c2c399e095899cd9567ad484ac954463c757740c0306639473edf69115a1e8fc37e1914727cbef11d90241aaef9c
6
+ metadata.gz: 23da24a3a861701b5156d1851be1eba45770565319e86d66e098f3a59156490ec16a7457d52fa55d0c216df653d2e1873a6212b5df3b23c67f576d40d706bd53
7
+ data.tar.gz: 9aef0214f2cf6a9db195be9b27dbb50f087b8d25be9041419a9d14d018f910aad449140848a0d49aa0223961672361b18e47a1f60b0e6eeeedd3752a88c2ac3a
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 2.1.0 / 2020-09-10
4
+
5
+ #### Features
6
+
7
+ * Add Query#limit_to_last
8
+
3
9
  ### 2.0.0 / 2020-08-06
4
10
 
5
11
  This is a major update that removes the "low-level" client interface code, and
@@ -62,6 +62,10 @@ module Google
62
62
  # @private The parent path for the query.
63
63
  attr_accessor :parent_path
64
64
 
65
+ ##
66
+ # @private The type for limit queries.
67
+ attr_reader :limit_type
68
+
65
69
  ##
66
70
  # @private The Google::Cloud::Firestore::V1::StructuredQuery object.
67
71
  attr_accessor :query
@@ -118,7 +122,7 @@ module Google
118
122
  new_query.select.fields << field_ref
119
123
  end
120
124
 
121
- Query.start new_query, parent_path, client
125
+ Query.start new_query, parent_path, client, limit_type: limit_type
122
126
  end
123
127
 
124
128
  ##
@@ -154,7 +158,7 @@ module Google
154
158
 
155
159
  new_query.from.last.all_descendants = true
156
160
 
157
- Query.start new_query, parent_path, client
161
+ Query.start new_query, parent_path, client, limit_type: limit_type
158
162
  end
159
163
 
160
164
  ##
@@ -190,7 +194,7 @@ module Google
190
194
 
191
195
  new_query.from.last.all_descendants = false
192
196
 
193
- Query.start new_query, parent_path, client
197
+ Query.start new_query, parent_path, client, limit_type: limit_type
194
198
  end
195
199
 
196
200
  ##
@@ -246,7 +250,7 @@ module Google
246
250
  new_filter = filter field.formatted_string, operator, value
247
251
  add_filters_to_query new_query, new_filter
248
252
 
249
- Query.start new_query, parent_path, client
253
+ Query.start new_query, parent_path, client, limit_type: limit_type
250
254
  end
251
255
 
252
256
  ##
@@ -298,9 +302,8 @@ module Google
298
302
  # end
299
303
  #
300
304
  def order field, direction = :asc
301
- if query_has_cursors?
302
- raise "cannot call order after calling " \
303
- "start_at, start_after, end_before, or end_at"
305
+ if query_has_cursors? || limit_type == :last
306
+ raise "cannot call order after calling limit_to_last, start_at, start_after, end_before, or end_at"
304
307
  end
305
308
 
306
309
  new_query = @query.dup
@@ -315,7 +318,7 @@ module Google
315
318
  direction: order_direction(direction)
316
319
  )
317
320
 
318
- Query.start new_query, parent_path, client
321
+ Query.start new_query, parent_path, client, limit_type: limit_type
319
322
  end
320
323
  alias order_by order
321
324
 
@@ -348,12 +351,13 @@ module Google
348
351
 
349
352
  new_query.offset = num
350
353
 
351
- Query.start new_query, parent_path, client
354
+ Query.start new_query, parent_path, client, limit_type: limit_type
352
355
  end
353
356
 
354
357
  ##
355
- # Limits a query to return a fixed number of results. If the current
356
- # query already has a limit set, this will overwrite it.
358
+ # Limits a query to return only the first matching documents.
359
+ #
360
+ # If the current query already has a limit set, this will overwrite it.
357
361
  #
358
362
  # @param [Integer] num The maximum number of results to return.
359
363
  #
@@ -368,19 +372,83 @@ module Google
368
372
  # cities_col = firestore.col "cities"
369
373
  #
370
374
  # # Create a query
371
- # query = cities_col.offset(10).limit(5)
375
+ # query = cities_col.order(:name, :desc).offset(10).limit(5)
372
376
  #
373
377
  # query.get do |city|
374
378
  # puts "#{city.document_id} has #{city[:population]} residents."
375
379
  # end
376
380
  #
377
381
  def limit num
382
+ if limit_type == :last
383
+ raise "cannot call limit after calling limit_to_last"
384
+ end
385
+
378
386
  new_query = @query.dup
379
387
  new_query ||= StructuredQuery.new
380
388
 
381
389
  new_query.limit = Google::Protobuf::Int32Value.new value: num
382
390
 
383
- Query.start new_query, parent_path, client
391
+ Query.start new_query, parent_path, client, limit_type: :first
392
+ end
393
+
394
+ ##
395
+ # Limits a query to return only the last matching documents.
396
+ #
397
+ # You must specify at least one "order by" clause for limitToLast queries.
398
+ # (See {#order}.)
399
+ #
400
+ # Results for `limit_to_last` queries are only available once all documents
401
+ # are received. Hence, `limit_to_last` queries cannot be streamed using
402
+ # {#listen}.
403
+ #
404
+ # @param [Integer] num The maximum number of results to return.
405
+ #
406
+ # @return [Query] New query with `limit_to_last` called on it.
407
+ #
408
+ # @example
409
+ # require "google/cloud/firestore"
410
+ #
411
+ # firestore = Google::Cloud::Firestore.new
412
+ #
413
+ # # Get a collection reference
414
+ # cities_col = firestore.col "cities"
415
+ #
416
+ # # Create a query
417
+ # query = cities_col.order(:name, :desc).limit_to_last(5)
418
+ #
419
+ # query.get do |city|
420
+ # puts "#{city.document_id} has #{city[:population]} residents."
421
+ # end
422
+ #
423
+ def limit_to_last num
424
+ new_query = @query.dup
425
+
426
+ if new_query.nil? || new_query.order_by.nil? || new_query.order_by.empty?
427
+ raise "specify at least one order clause before calling limit_to_last"
428
+ end
429
+
430
+ if limit_type != :last # Don't reverse order_by more than once.
431
+ # Reverse the order_by directions since we want the last results.
432
+ new_query.order_by.each do |order|
433
+ order.direction = order.direction.to_sym == :DESCENDING ? :ASCENDING : :DESCENDING
434
+ end
435
+
436
+ # Swap the cursors to match the reversed query ordering.
437
+ new_end_at = new_query.start_at.dup
438
+ new_start_at = new_query.end_at.dup
439
+ if new_end_at
440
+ new_end_at.before = !new_end_at.before
441
+ new_query.end_at = new_end_at
442
+ end
443
+ if new_start_at
444
+ new_start_at.before = !new_start_at.before
445
+ new_query.start_at = new_start_at
446
+ end
447
+ end
448
+
449
+ new_query.limit = Google::Protobuf::Int32Value.new value: num
450
+
451
+ Query.start new_query, parent_path, client, limit_type: :last
384
452
  end
385
453
 
386
454
  ##
@@ -477,6 +545,10 @@ module Google
477
545
  def start_at *values
478
546
  raise ArgumentError, "must provide values" if values.empty?
479
547
 
548
+ if limit_type == :last
549
+ raise "cannot call start_at after calling limit_to_last"
550
+ end
551
+
480
552
  new_query = @query.dup
481
553
  new_query ||= StructuredQuery.new
482
554
 
@@ -484,7 +556,7 @@ module Google
484
556
  cursor.before = true
485
557
  new_query.start_at = cursor
486
558
 
487
- Query.start new_query, parent_path, client
559
+ Query.start new_query, parent_path, client, limit_type: limit_type
488
560
  end
489
561
 
490
562
  ##
@@ -581,6 +653,11 @@ module Google
581
653
  def start_after *values
582
654
  raise ArgumentError, "must provide values" if values.empty?
583
655
 
656
+ if limit_type == :last
657
+ raise "cannot call start_after after calling limit_to_last"
658
+ end
659
+
660
+
584
661
  new_query = @query.dup
585
662
  new_query ||= StructuredQuery.new
586
663
 
@@ -588,7 +665,7 @@ module Google
588
665
  cursor.before = false
589
666
  new_query.start_at = cursor
590
667
 
591
- Query.start new_query, parent_path, client
668
+ Query.start new_query, parent_path, client, limit_type: limit_type
592
669
  end
593
670
 
594
671
  ##
@@ -685,6 +762,11 @@ module Google
685
762
  def end_before *values
686
763
  raise ArgumentError, "must provide values" if values.empty?
687
764
 
765
+ if limit_type == :last
766
+ raise "cannot call end_before after calling limit_to_last"
767
+ end
768
+
769
+
688
770
  new_query = @query.dup
689
771
  new_query ||= StructuredQuery.new
690
772
 
@@ -692,7 +774,7 @@ module Google
692
774
  cursor.before = true
693
775
  new_query.end_at = cursor
694
776
 
695
- Query.start new_query, parent_path, client
777
+ Query.start new_query, parent_path, client, limit_type: limit_type
696
778
  end
697
779
 
698
780
  ##
@@ -789,6 +871,11 @@ module Google
789
871
  def end_at *values
790
872
  raise ArgumentError, "must provide values" if values.empty?
791
873
 
874
+ if limit_type == :last
875
+ raise "cannot call end_at after calling limit_to_last"
876
+ end
877
+
878
+
792
879
  new_query = @query.dup
793
880
  new_query ||= StructuredQuery.new
794
881
 
@@ -796,7 +883,7 @@ module Google
796
883
  cursor.before = false
797
884
  new_query.end_at = cursor
798
885
 
799
- Query.start new_query, parent_path, client
886
+ Query.start new_query, parent_path, client, limit_type: limit_type
800
887
  end
801
888
 
802
889
  ##
@@ -828,6 +915,10 @@ module Google
828
915
  return enum_for :get unless block_given?
829
916
 
830
917
  results = service.run_query parent_path, @query
918
+
919
+ # Reverse the results for Query#limit_to_last queries since that method reversed the order_by directions.
920
+ results = results.to_a.reverse if limit_type == :last
921
+
831
922
  results.each do |result|
832
923
  next if result.document.nil?
833
924
  yield DocumentSnapshot.from_query_result result, client
@@ -870,11 +961,12 @@ module Google
870
961
 
871
962
  ##
872
963
  # @private Start a new Query.
873
- def self.start query, parent_path, client
964
+ def self.start query, parent_path, client, limit_type: nil
874
965
  query ||= StructuredQuery.new
875
966
  Query.new.tap do |q|
876
967
  q.instance_variable_set :@query, query
877
968
  q.instance_variable_set :@parent_path, parent_path
969
+ q.instance_variable_set :@limit_type, limit_type
878
970
  q.instance_variable_set :@client, client
879
971
  end
880
972
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Firestore
19
- VERSION = "2.0.0".freeze
19
+ VERSION = "2.1.0".freeze
20
20
  end
21
21
  end
22
22
  end
@@ -127,6 +127,8 @@ module Google
127
127
  end
128
128
 
129
129
  def current_docs
130
+ # Reverse the results for Query#limit_to_last queries since that method reversed the order_by directions.
131
+ return @tree.keys.reverse if @query&.limit_type == :last
130
132
  @tree.keys
131
133
  end
132
134
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-firestore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-06 00:00:00.000000000 Z
11
+ date: 2020-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-core