google-cloud-firestore 1.4.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/AUTHENTICATION.md +1 -1
- data/CHANGELOG.md +47 -0
- data/TROUBLESHOOTING.md +2 -8
- data/lib/google-cloud-firestore.rb +14 -21
- data/lib/google/cloud/firestore.rb +12 -23
- data/lib/google/cloud/firestore/client.rb +12 -14
- data/lib/google/cloud/firestore/collection_reference.rb +1 -0
- data/lib/google/cloud/firestore/collection_reference_list.rb +137 -0
- data/lib/google/cloud/firestore/convert.rb +39 -39
- data/lib/google/cloud/firestore/credentials.rb +2 -2
- data/lib/google/cloud/firestore/document_reference.rb +8 -8
- data/lib/google/cloud/firestore/document_reference/list.rb +2 -2
- data/lib/google/cloud/firestore/document_snapshot.rb +4 -4
- data/lib/google/cloud/firestore/query.rb +114 -22
- data/lib/google/cloud/firestore/service.rb +63 -104
- data/lib/google/cloud/firestore/transaction.rb +2 -2
- data/lib/google/cloud/firestore/version.rb +1 -1
- data/lib/google/cloud/firestore/watch/inventory.rb +2 -0
- data/lib/google/cloud/firestore/watch/listener.rb +9 -9
- data/lib/google/cloud/firestore/watch/order.rb +1 -1
- metadata +11 -109
- data/lib/google/cloud/firestore/admin.rb +0 -144
- data/lib/google/cloud/firestore/admin/v1.rb +0 -151
- data/lib/google/cloud/firestore/admin/v1/credentials.rb +0 -44
- data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/field.rb +0 -86
- data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/firestore_admin.rb +0 -161
- data/lib/google/cloud/firestore/admin/v1/doc/google/firestore/admin/v1/index.rb +0 -142
- data/lib/google/cloud/firestore/admin/v1/doc/google/longrunning/operations.rb +0 -51
- data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/empty.rb +0 -29
- data/lib/google/cloud/firestore/admin/v1/doc/google/protobuf/field_mask.rb +0 -222
- data/lib/google/cloud/firestore/admin/v1/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/firestore/admin/v1/firestore_admin_client.rb +0 -902
- data/lib/google/cloud/firestore/admin/v1/firestore_admin_client_config.json +0 -71
- data/lib/google/cloud/firestore/v1.rb +0 -152
- data/lib/google/cloud/firestore/v1/credentials.rb +0 -42
- data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/common.rb +0 -64
- data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/document.rb +0 -136
- data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/firestore.rb +0 -566
- data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/query.rb +0 -227
- data/lib/google/cloud/firestore/v1/doc/google/firestore/v1/write.rb +0 -241
- data/lib/google/cloud/firestore/v1/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/firestore/v1/doc/google/protobuf/empty.rb +0 -29
- data/lib/google/cloud/firestore/v1/doc/google/protobuf/timestamp.rb +0 -113
- data/lib/google/cloud/firestore/v1/doc/google/protobuf/wrappers.rb +0 -26
- data/lib/google/cloud/firestore/v1/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/firestore/v1/firestore_client.rb +0 -1011
- data/lib/google/cloud/firestore/v1/firestore_client_config.json +0 -100
- data/lib/google/cloud/firestore/v1beta1.rb +0 -160
- data/lib/google/cloud/firestore/v1beta1/credentials.rb +0 -42
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/common.rb +0 -64
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/document.rb +0 -136
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/firestore.rb +0 -564
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/query.rb +0 -227
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/write.rb +0 -237
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/empty.rb +0 -29
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/timestamp.rb +0 -113
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/wrappers.rb +0 -26
- data/lib/google/cloud/firestore/v1beta1/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/firestore/v1beta1/firestore_client.rb +0 -1019
- data/lib/google/cloud/firestore/v1beta1/firestore_client_config.json +0 -100
- data/lib/google/firestore/admin/v1/field_pb.rb +0 -32
- data/lib/google/firestore/admin/v1/firestore_admin_pb.rb +0 -84
- data/lib/google/firestore/admin/v1/firestore_admin_services_pb.rb +0 -94
- data/lib/google/firestore/admin/v1/index_pb.rb +0 -58
- data/lib/google/firestore/admin/v1/location_pb.rb +0 -22
- data/lib/google/firestore/admin/v1/operation_pb.rb +0 -90
- data/lib/google/firestore/v1/common_pb.rb +0 -45
- data/lib/google/firestore/v1/document_pb.rb +0 -50
- data/lib/google/firestore/v1/firestore_pb.rb +0 -222
- data/lib/google/firestore/v1/firestore_services_pb.rb +0 -80
- data/lib/google/firestore/v1/query_pb.rb +0 -107
- data/lib/google/firestore/v1/write_pb.rb +0 -80
- data/lib/google/firestore/v1beta1/common_pb.rb +0 -45
- data/lib/google/firestore/v1beta1/document_pb.rb +0 -50
- data/lib/google/firestore/v1beta1/firestore_pb.rb +0 -222
- data/lib/google/firestore/v1beta1/firestore_services_pb.rb +0 -89
- data/lib/google/firestore/v1beta1/query_pb.rb +0 -107
- data/lib/google/firestore/v1beta1/write_pb.rb +0 -79
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1e7097a28daa16ef5b2de8684c5b052b6409b792a4e7f382f878c47745dbf1a
|
4
|
+
data.tar.gz: 35dae74e7a063ad5e456420633c50b60ea0731b2fcafdde0e6ceab25b10bea25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23da24a3a861701b5156d1851be1eba45770565319e86d66e098f3a59156490ec16a7457d52fa55d0c216df653d2e1873a6212b5df3b23c67f576d40d706bd53
|
7
|
+
data.tar.gz: 9aef0214f2cf6a9db195be9b27dbb50f087b8d25be9041419a9d14d018f910aad449140848a0d49aa0223961672361b18e47a1f60b0e6eeeedd3752a88c2ac3a
|
data/AUTHENTICATION.md
CHANGED
@@ -76,7 +76,7 @@ The environment variables that google-cloud-firestore checks for project ID are:
|
|
76
76
|
1. `FIRESTORE_PROJECT`
|
77
77
|
2. `GOOGLE_CLOUD_PROJECT`
|
78
78
|
|
79
|
-
The environment variables that google-cloud-firestore checks for credentials are configured on {Google::Cloud::Firestore::
|
79
|
+
The environment variables that google-cloud-firestore checks for credentials are configured on {Google::Cloud::Firestore::Credentials}:
|
80
80
|
|
81
81
|
1. `FIRESTORE_CREDENTIALS` - Path to JSON file, or JSON contents
|
82
82
|
2. `FIRESTORE_KEYFILE` - Path to JSON file, or JSON contents
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,52 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 2.1.0 / 2020-09-10
|
4
|
+
|
5
|
+
#### Features
|
6
|
+
|
7
|
+
* Add Query#limit_to_last
|
8
|
+
|
9
|
+
### 2.0.0 / 2020-08-06
|
10
|
+
|
11
|
+
This is a major update that removes the "low-level" client interface code, and
|
12
|
+
instead adds the new `google-cloud-firestore-v1` gem as a dependency.
|
13
|
+
The new dependency is a rewritten low-level client, produced by a next-
|
14
|
+
generation client code generator, with improved performance and stability.
|
15
|
+
|
16
|
+
This change should have no effect on the high-level interface that most users
|
17
|
+
will use. The one exception is that the (mostly undocumented) `client_config`
|
18
|
+
argument, for adjusting low-level parameters such as RPC retry settings on
|
19
|
+
client objects, has been removed. If you need to adjust these parameters, use
|
20
|
+
the configuration interface in `google-cloud-firestore-v1`.
|
21
|
+
|
22
|
+
Substantial changes have been made in the low-level interfaces, however. If you
|
23
|
+
are using the low-level classes under the `Google::Cloud::Firestore::V1` module,
|
24
|
+
please review the docs for the new `google-cloud-firestore-v1` gem. In
|
25
|
+
particular:
|
26
|
+
|
27
|
+
* Some classes have been renamed, notably the client class itself.
|
28
|
+
* The client constructor takes a configuration block instead of configuration
|
29
|
+
keyword arguments.
|
30
|
+
* All RPC method arguments are now keyword arguments.
|
31
|
+
|
32
|
+
### 1.4.4 / 2020-05-28
|
33
|
+
|
34
|
+
#### Documentation
|
35
|
+
|
36
|
+
* Fix a few broken links
|
37
|
+
|
38
|
+
### 1.4.3 / 2020-05-21
|
39
|
+
|
40
|
+
#### Bug Fixes
|
41
|
+
|
42
|
+
* Adjusted some default timeout and retry settings
|
43
|
+
|
44
|
+
### 1.4.2 / 2020-05-14
|
45
|
+
|
46
|
+
#### Bug Fixes
|
47
|
+
|
48
|
+
* Fix Ruby 2.7 keyword argument warning
|
49
|
+
|
3
50
|
### 1.4.1 / 2020-04-14
|
4
51
|
|
5
52
|
#### Bug Fixes
|
data/TROUBLESHOOTING.md
CHANGED
@@ -24,14 +24,8 @@ improved, *please* create a new issue on GitHub so we can talk about it.
|
|
24
24
|
|
25
25
|
- [New issue][gh-ruby]
|
26
26
|
|
27
|
-
Or, you can ask questions on the [Google Cloud Platform Slack][slack-ruby]. You
|
28
|
-
can use the "ruby" channel for general Ruby questions, or use the
|
29
|
-
"google-cloud-ruby" channel if you have questions about this gem in particular.
|
30
|
-
|
31
27
|
[so-ruby]: http://stackoverflow.com/questions/tagged/google-cloud-platform+ruby+firestore
|
32
28
|
|
33
|
-
[gh-search-ruby]: https://github.com/
|
34
|
-
|
35
|
-
[gh-ruby]: https://github.com/googlecloudplatform/google-cloud-ruby/issues/new
|
29
|
+
[gh-search-ruby]: https://github.com/googleapis/google-cloud-ruby/issues?q=label%3A%22api%3A+firestore%22
|
36
30
|
|
37
|
-
[
|
31
|
+
[gh-ruby]: https://github.com/googleapis/google-cloud-ruby/issues/new
|
@@ -42,8 +42,6 @@ module Google
|
|
42
42
|
#
|
43
43
|
# * `https://www.googleapis.com/auth/datastore`
|
44
44
|
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
45
|
-
# @param [Hash] client_config A hash of values to override the default
|
46
|
-
# behavior of the API client. Optional.
|
47
45
|
#
|
48
46
|
# @return [Google::Cloud::Firestore::Client]
|
49
47
|
#
|
@@ -60,10 +58,8 @@ module Google
|
|
60
58
|
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
|
61
59
|
# firestore = gcloud.firestore scope: platform_scope
|
62
60
|
#
|
63
|
-
def firestore scope: nil, timeout: nil
|
64
|
-
Google::Cloud.firestore @project, @keyfile,
|
65
|
-
scope: scope, timeout: (timeout || @timeout),
|
66
|
-
client_config: client_config
|
61
|
+
def firestore scope: nil, timeout: nil
|
62
|
+
Google::Cloud.firestore @project, @keyfile, scope: scope, timeout: (timeout || @timeout)
|
67
63
|
end
|
68
64
|
|
69
65
|
##
|
@@ -87,8 +83,6 @@ module Google
|
|
87
83
|
#
|
88
84
|
# * `https://www.googleapis.com/auth/datastore`
|
89
85
|
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
90
|
-
# @param [Hash] client_config A hash of values to override the default
|
91
|
-
# behavior of the API client. Optional.
|
92
86
|
#
|
93
87
|
# @return [Google::Cloud::Firestore::Client]
|
94
88
|
#
|
@@ -97,13 +91,12 @@ module Google
|
|
97
91
|
#
|
98
92
|
# firestore = Google::Cloud.firestore
|
99
93
|
#
|
100
|
-
def self.firestore project_id = nil, credentials = nil, scope: nil,
|
101
|
-
timeout: nil, client_config: nil
|
94
|
+
def self.firestore project_id = nil, credentials = nil, scope: nil, timeout: nil
|
102
95
|
require "google/cloud/firestore"
|
103
|
-
Google::Cloud::Firestore.new project_id:
|
96
|
+
Google::Cloud::Firestore.new project_id: project_id,
|
104
97
|
credentials: credentials,
|
105
|
-
scope:
|
106
|
-
|
98
|
+
scope: scope,
|
99
|
+
timeout: timeout
|
107
100
|
end
|
108
101
|
end
|
109
102
|
end
|
@@ -122,17 +115,17 @@ Google::Cloud.configure.add_config! :firestore do |config|
|
|
122
115
|
default_emulator = Google::Cloud::Config.deferred do
|
123
116
|
ENV["FIRESTORE_EMULATOR_HOST"]
|
124
117
|
end
|
118
|
+
default_scopes = [
|
119
|
+
"https://www.googleapis.com/auth/cloud-platform",
|
120
|
+
"https://www.googleapis.com/auth/datastore"
|
121
|
+
]
|
125
122
|
|
126
123
|
config.add_field! :project_id, default_project, match: String, allow_nil: true
|
127
124
|
config.add_alias! :project, :project_id
|
128
|
-
config.add_field! :credentials, default_creds,
|
129
|
-
match: [String, Hash, Google::Auth::Credentials],
|
130
|
-
allow_nil: true
|
125
|
+
config.add_field! :credentials, default_creds, match: [String, Hash, Google::Auth::Credentials], allow_nil: true
|
131
126
|
config.add_alias! :keyfile, :credentials
|
132
|
-
config.add_field! :scope,
|
127
|
+
config.add_field! :scope, default_scopes, match: [String, Array]
|
133
128
|
config.add_field! :timeout, nil, match: Integer
|
134
|
-
config.add_field! :
|
135
|
-
config.add_field! :
|
136
|
-
match: String, allow_nil: true
|
137
|
-
config.add_field! :endpoint, nil, match: String
|
129
|
+
config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true
|
130
|
+
config.add_field! :endpoint, "firestore.googleapis.com", match: String
|
138
131
|
end
|
@@ -33,7 +33,6 @@ module Google
|
|
33
33
|
#
|
34
34
|
module Firestore
|
35
35
|
# rubocop:disable Metrics/AbcSize
|
36
|
-
# rubocop:disable Metrics/MethodLength
|
37
36
|
|
38
37
|
##
|
39
38
|
# Creates a new object for connecting to the Firestore service.
|
@@ -56,8 +55,6 @@ module Google
|
|
56
55
|
#
|
57
56
|
# * `https://www.googleapis.com/auth/datastore`
|
58
57
|
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
59
|
-
# @param [Hash] client_config A hash of values to override the default
|
60
|
-
# behavior of the API client. Optional.
|
61
58
|
# @param [String] endpoint Override of the endpoint host name. Optional.
|
62
59
|
# If the param is nil, uses the default endpoint.
|
63
60
|
# @param [String] emulator_host Firestore emulator host. Optional.
|
@@ -73,13 +70,17 @@ module Google
|
|
73
70
|
#
|
74
71
|
# firestore = Google::Cloud::Firestore.new
|
75
72
|
#
|
76
|
-
def self.new project_id: nil,
|
77
|
-
|
78
|
-
|
73
|
+
def self.new project_id: nil,
|
74
|
+
credentials: nil,
|
75
|
+
scope: nil,
|
76
|
+
timeout: nil,
|
77
|
+
endpoint: nil,
|
78
|
+
emulator_host: nil,
|
79
|
+
project: nil,
|
80
|
+
keyfile: nil
|
79
81
|
project_id ||= (project || default_project_id)
|
80
82
|
scope ||= configure.scope
|
81
83
|
timeout ||= configure.timeout
|
82
|
-
client_config ||= configure.client_config
|
83
84
|
endpoint ||= configure.endpoint
|
84
85
|
emulator_host ||= configure.emulator_host
|
85
86
|
|
@@ -87,13 +88,8 @@ module Google
|
|
87
88
|
project_id = project_id.to_s
|
88
89
|
raise ArgumentError, "project_id is missing" if project_id.empty?
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
project_id, :this_channel_is_insecure,
|
93
|
-
host: emulator_host, timeout: timeout,
|
94
|
-
client_config: client_config
|
95
|
-
)
|
96
|
-
)
|
91
|
+
service = Firestore::Service.new project_id, :this_channel_is_insecure, host: emulator_host, timeout: timeout
|
92
|
+
return Firestore::Client.new service
|
97
93
|
end
|
98
94
|
|
99
95
|
credentials ||= (keyfile || default_credentials(scope: scope))
|
@@ -107,16 +103,11 @@ module Google
|
|
107
103
|
project_id = project_id.to_s
|
108
104
|
raise ArgumentError, "project_id is missing" if project_id.empty?
|
109
105
|
|
110
|
-
Firestore::
|
111
|
-
|
112
|
-
project_id, credentials,
|
113
|
-
host: endpoint, timeout: timeout, client_config: client_config
|
114
|
-
)
|
115
|
-
)
|
106
|
+
service = Firestore::Service.new project_id, credentials, host: endpoint, timeout: timeout
|
107
|
+
Firestore::Client.new service
|
116
108
|
end
|
117
109
|
|
118
110
|
# rubocop:enable Metrics/AbcSize
|
119
|
-
# rubocop:enable Metrics/MethodLength
|
120
111
|
|
121
112
|
##
|
122
113
|
# Configure the Google Cloud Firestore library.
|
@@ -132,8 +123,6 @@ module Google
|
|
132
123
|
# * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
|
133
124
|
# the set of resources and operations that the connection can access.
|
134
125
|
# * `timeout` - (Integer) Default timeout to use in requests.
|
135
|
-
# * `client_config` - (Hash) A hash of values to override the default
|
136
|
-
# behavior of the API client.
|
137
126
|
# * `endpoint` - (String) Override of the endpoint host name, or `nil`
|
138
127
|
# to use the default endpoint.
|
139
128
|
# * `emulator_host` - (String) Host name of the emulator. Defaults to
|
@@ -82,12 +82,13 @@ module Google
|
|
82
82
|
# @!group Access
|
83
83
|
|
84
84
|
##
|
85
|
-
# Retrieves
|
85
|
+
# Retrieves an enumerator for the root collections.
|
86
86
|
#
|
87
87
|
# @yield [collections] The block for accessing the collections.
|
88
|
-
# @yieldparam [CollectionReference] collection A collection.
|
88
|
+
# @yieldparam [CollectionReference] collection A collection reference object.
|
89
89
|
#
|
90
|
-
# @return [Enumerator<CollectionReference>] collection
|
90
|
+
# @return [Enumerator<CollectionReference>] An enumerator of collection references. If a block is provided,
|
91
|
+
# this is the same enumerator that is accessed through the block.
|
91
92
|
#
|
92
93
|
# @example
|
93
94
|
# require "google/cloud/firestore"
|
@@ -101,11 +102,10 @@ module Google
|
|
101
102
|
#
|
102
103
|
def cols
|
103
104
|
ensure_service!
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
collection_ids.each { |collection_id| yield col collection_id }
|
105
|
+
grpc = service.list_collections "#{path}/documents"
|
106
|
+
cols_enum = CollectionReferenceList.from_grpc(grpc, self, "#{path}/documents").all
|
107
|
+
cols_enum.each { |c| yield c } if block_given?
|
108
|
+
cols_enum
|
109
109
|
end
|
110
110
|
alias collections cols
|
111
111
|
alias list_collections cols
|
@@ -134,8 +134,7 @@ module Google
|
|
134
134
|
raise ArgumentError, "collection_path must refer to a collection."
|
135
135
|
end
|
136
136
|
|
137
|
-
CollectionReference.from_path
|
138
|
-
"#{path}/documents/#{collection_path}", self
|
137
|
+
CollectionReference.from_path "#{path}/documents/#{collection_path}", self
|
139
138
|
end
|
140
139
|
alias collection col
|
141
140
|
|
@@ -167,9 +166,9 @@ module Google
|
|
167
166
|
raise ArgumentError, "Invalid collection_id: '#{collection_id}', " \
|
168
167
|
"must not contain '/'."
|
169
168
|
end
|
170
|
-
query = Google::Firestore::V1::StructuredQuery.new(
|
169
|
+
query = Google::Cloud::Firestore::V1::StructuredQuery.new(
|
171
170
|
from: [
|
172
|
-
Google::Firestore::V1::StructuredQuery::CollectionSelector.new(
|
171
|
+
Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(
|
173
172
|
collection_id: collection_id, all_descendants: true
|
174
173
|
)
|
175
174
|
]
|
@@ -670,8 +669,7 @@ module Google
|
|
670
669
|
# @private
|
671
670
|
def list_documents parent, collection_id, token: nil, max: nil
|
672
671
|
ensure_service!
|
673
|
-
grpc = service.list_documents
|
674
|
-
parent, collection_id, token: token, max: max
|
672
|
+
grpc = service.list_documents parent, collection_id, token: token, max: max
|
675
673
|
DocumentReference::List.from_grpc grpc, self, parent, collection_id
|
676
674
|
end
|
677
675
|
|
@@ -18,6 +18,7 @@ require "google/cloud/firestore/document_reference"
|
|
18
18
|
require "google/cloud/firestore/document_snapshot"
|
19
19
|
require "google/cloud/firestore/query"
|
20
20
|
require "google/cloud/firestore/generate"
|
21
|
+
require "google/cloud/firestore/collection_reference_list"
|
21
22
|
|
22
23
|
module Google
|
23
24
|
module Cloud
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# Copyright 2020 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "delegate"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Firestore
|
21
|
+
##
|
22
|
+
# @private
|
23
|
+
#
|
24
|
+
# An Array delegate for pagination. Private class exposing only an Enumerator to clients.
|
25
|
+
#
|
26
|
+
class CollectionReferenceList < DelegateClass(::Array)
|
27
|
+
##
|
28
|
+
# If not empty, indicates that there are more records that match the request and this value should be passed
|
29
|
+
# to continue.
|
30
|
+
attr_accessor :token
|
31
|
+
|
32
|
+
##
|
33
|
+
# @private Create a new CollectionReference::List with an array of CollectionReference instances.
|
34
|
+
def initialize arr = []
|
35
|
+
super arr
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Whether there is a next page of collection references.
|
40
|
+
#
|
41
|
+
# @return [Boolean]
|
42
|
+
#
|
43
|
+
def next?
|
44
|
+
!token.nil?
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Retrieve the next page of collection references.
|
49
|
+
#
|
50
|
+
# @return [CollectionReference::List] The list of collection references.
|
51
|
+
#
|
52
|
+
def next
|
53
|
+
return nil unless next?
|
54
|
+
ensure_service!
|
55
|
+
grpc = @client.service.list_collections @parent, token: token, max: @max
|
56
|
+
self.class.from_grpc grpc, @client, @parent, max: @max
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Retrieves remaining results by repeatedly invoking {#next} until {#next?} returns `false`. Calls the given
|
61
|
+
# block once for each result, which is passed as the argument to the block.
|
62
|
+
#
|
63
|
+
# An Enumerator is returned if no block is given.
|
64
|
+
#
|
65
|
+
# This method will make repeated API calls until all remaining results are retrieved. (Unlike `#each`, for
|
66
|
+
# example, which merely iterates over the results returned by a single API call.) Use with caution.
|
67
|
+
#
|
68
|
+
# @param [Integer] request_limit The upper limit of API requests to make to load all collection references.
|
69
|
+
# Default is no limit.
|
70
|
+
# @yield [collection_reference] The block for accessing each collection_reference.
|
71
|
+
# @yieldparam [CollectionReference] collection_reference The collection reference object.
|
72
|
+
#
|
73
|
+
# @return [Enumerator]
|
74
|
+
#
|
75
|
+
# @example Iterating each collection reference by passing a block:
|
76
|
+
# require "google/cloud/firestore"
|
77
|
+
#
|
78
|
+
# firestore = Google::Cloud::Firestore.new
|
79
|
+
#
|
80
|
+
# firestore.cols do |collection_reference|
|
81
|
+
# puts collection_reference.collection_id
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# @example Using the enumerator by not passing a block:
|
85
|
+
# require "google/cloud/firestore"
|
86
|
+
#
|
87
|
+
# firestore = Google::Cloud::Firestore.new
|
88
|
+
#
|
89
|
+
# all_collection_ids = firestore.cols.map do |collection_reference|
|
90
|
+
# collection_reference.collection_id
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
def all request_limit: nil
|
94
|
+
request_limit = request_limit.to_i if request_limit
|
95
|
+
unless block_given?
|
96
|
+
return enum_for :all, request_limit: request_limit
|
97
|
+
end
|
98
|
+
results = self
|
99
|
+
loop do
|
100
|
+
results.each { |r| yield r }
|
101
|
+
if request_limit
|
102
|
+
request_limit -= 1
|
103
|
+
break if request_limit < 0
|
104
|
+
end
|
105
|
+
break unless results.next?
|
106
|
+
results = results.next
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
##
|
111
|
+
# @private New CollectionReference::List from a `Google::Cloud::Firestore::V1::ListCollectionIdsResponse`
|
112
|
+
# object.
|
113
|
+
def self.from_grpc grpc, client, parent, max: nil
|
114
|
+
raise ArgumentError, "parent is required" unless parent
|
115
|
+
cols = CollectionReferenceList.new(Array(grpc.collection_ids).map do |collection_id|
|
116
|
+
CollectionReference.from_path "#{parent}/#{collection_id}", client
|
117
|
+
end)
|
118
|
+
token = grpc.next_page_token
|
119
|
+
token = nil if token == ""
|
120
|
+
cols.instance_variable_set :@token, token
|
121
|
+
cols.instance_variable_set :@client, client
|
122
|
+
cols.instance_variable_set :@parent, parent
|
123
|
+
cols.instance_variable_set :@max, max
|
124
|
+
cols
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
|
129
|
+
##
|
130
|
+
# Raise an error unless an active service is available.
|
131
|
+
def ensure_service!
|
132
|
+
raise "Must have active connection" unless @client.service
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|