google-cloud-firestore 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +8 -0
- data/LICENSE +201 -0
- data/README.md +30 -0
- data/lib/google-cloud-firestore.rb +106 -0
- data/lib/google/cloud/firestore.rb +514 -0
- data/lib/google/cloud/firestore/batch.rb +462 -0
- data/lib/google/cloud/firestore/client.rb +449 -0
- data/lib/google/cloud/firestore/collection_reference.rb +249 -0
- data/lib/google/cloud/firestore/commit_response.rb +145 -0
- data/lib/google/cloud/firestore/convert.rb +561 -0
- data/lib/google/cloud/firestore/credentials.rb +35 -0
- data/lib/google/cloud/firestore/document_reference.rb +468 -0
- data/lib/google/cloud/firestore/document_snapshot.rb +324 -0
- data/lib/google/cloud/firestore/field_path.rb +216 -0
- data/lib/google/cloud/firestore/field_value.rb +113 -0
- data/lib/google/cloud/firestore/generate.rb +35 -0
- data/lib/google/cloud/firestore/query.rb +651 -0
- data/lib/google/cloud/firestore/service.rb +176 -0
- data/lib/google/cloud/firestore/transaction.rb +726 -0
- data/lib/google/cloud/firestore/v1beta1.rb +121 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/common.rb +63 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/document.rb +134 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/firestore.rb +584 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/query.rb +215 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/firestore/v1beta1/write.rb +167 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/any.rb +124 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/timestamp.rb +106 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/protobuf/wrappers.rb +89 -0
- data/lib/google/cloud/firestore/v1beta1/doc/google/rpc/status.rb +83 -0
- data/lib/google/cloud/firestore/v1beta1/doc/overview.rb +53 -0
- data/lib/google/cloud/firestore/v1beta1/firestore_client.rb +974 -0
- data/lib/google/cloud/firestore/v1beta1/firestore_client_config.json +100 -0
- data/lib/google/cloud/firestore/version.rb +22 -0
- data/lib/google/firestore/v1beta1/common_pb.rb +44 -0
- data/lib/google/firestore/v1beta1/document_pb.rb +49 -0
- data/lib/google/firestore/v1beta1/firestore_pb.rb +219 -0
- data/lib/google/firestore/v1beta1/firestore_services_pb.rb +87 -0
- data/lib/google/firestore/v1beta1/query_pb.rb +103 -0
- data/lib/google/firestore/v1beta1/write_pb.rb +73 -0
- metadata +251 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b003b01ba72dbe743a7874f6978b886d395bea33a1cca2c13995b2e1054c5dc
|
4
|
+
data.tar.gz: bdb2c649d040de24cb63a80bb6de03ce427288b0de2cf4311c19702a751a40ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: edb498706f4a6ca151c28bbe7d45c5e62b2e86566879cfd394b2b7443b3d29db33f1bdeb53ab10c183a083a0a583e7069176b68ac136ec1ee700e200f1875b63
|
7
|
+
data.tar.gz: 9cd66ddae9bc997f0d87616cef791a48e6a89e87304c29d79eb4b97997dae1294ff820dd1a57011e4a34ac5b782a77db44f1bcc6dd2ef733acb7fafcafb61813
|
data/.yardopts
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
https://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Ruby Client for Google Cloud Firestore API ([Beta](https://github.com/GoogleCloudPlatform/google-cloud-ruby#versioning))
|
2
|
+
|
3
|
+
[Google Cloud Firestore API][Product Documentation]:
|
4
|
+
|
5
|
+
- [Client Library Documentation][]
|
6
|
+
- [Product Documentation][]
|
7
|
+
|
8
|
+
## Quick Start
|
9
|
+
In order to use this library, you first need to go through the following
|
10
|
+
steps:
|
11
|
+
|
12
|
+
1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project)
|
13
|
+
2. [Enable the Google Cloud Firestore API.](https://console.cloud.google.com/apis/api/firestore)
|
14
|
+
3. [Setup Authentication.](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud/master/guides/authentication)
|
15
|
+
|
16
|
+
### Installation
|
17
|
+
```
|
18
|
+
$ gem install google-cloud-firestore
|
19
|
+
```
|
20
|
+
|
21
|
+
### Next Steps
|
22
|
+
- Read the [Client Library Documentation][] for Google Cloud Firestore API
|
23
|
+
to see other available methods on the client.
|
24
|
+
- Read the [Google Cloud Firestore API Product documentation][Product Documentation]
|
25
|
+
to learn more about the product and see How-to Guides.
|
26
|
+
- View this [repository's main README](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/README.md)
|
27
|
+
to see the full list of Cloud APIs that we cover.
|
28
|
+
|
29
|
+
[Client Library Documentation]: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-firestore/latest/google/firestore/v1beta1
|
30
|
+
[Product Documentation]: https://cloud.google.com/firestore
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# Copyright 2017 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
|
+
# This file is here to be autorequired by bundler, so that the .firestore and
|
17
|
+
# #firestore methods can be available, but the library and all dependencies
|
18
|
+
# won't be loaded until required and used.
|
19
|
+
|
20
|
+
|
21
|
+
gem "google-cloud-core"
|
22
|
+
require "google/cloud"
|
23
|
+
|
24
|
+
module Google
|
25
|
+
module Cloud
|
26
|
+
##
|
27
|
+
# Creates a new object for connecting to the Firestore service.
|
28
|
+
# Each call creates a new connection.
|
29
|
+
#
|
30
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
31
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
32
|
+
#
|
33
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
34
|
+
# set of resources and operations that the connection can access. See
|
35
|
+
# [Using OAuth 2.0 to Access Google
|
36
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
37
|
+
#
|
38
|
+
# The default scope is:
|
39
|
+
#
|
40
|
+
# * `https://www.googleapis.com/auth/datastore`
|
41
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
42
|
+
# @param [Hash] client_config A hash of values to override the default
|
43
|
+
# behavior of the API client. Optional.
|
44
|
+
#
|
45
|
+
# @return [Google::Cloud::Firestore::Client]
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# require "google/cloud"
|
49
|
+
#
|
50
|
+
# gcloud = Google::Cloud.new
|
51
|
+
# firestore = gcloud.firestore
|
52
|
+
#
|
53
|
+
# @example The default scope can be overridden with the `scope` option:
|
54
|
+
# require "google/cloud"
|
55
|
+
#
|
56
|
+
# gcloud = Google::Cloud.new
|
57
|
+
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
|
58
|
+
# firestore = gcloud.firestore scope: platform_scope
|
59
|
+
#
|
60
|
+
def firestore scope: nil, timeout: nil, client_config: nil
|
61
|
+
Google::Cloud.firestore @project, @keyfile,
|
62
|
+
scope: scope, timeout: (timeout || @timeout),
|
63
|
+
client_config: client_config
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Creates a new object for connecting to the Firestore service.
|
68
|
+
# Each call creates a new connection.
|
69
|
+
#
|
70
|
+
# For more information on connecting to Google Cloud see the [Authentication
|
71
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
72
|
+
#
|
73
|
+
# @param [String] project_id Identifier for a Firestore project. If not
|
74
|
+
# present, the default project for the credentials is used.
|
75
|
+
# @param [String, Hash, Google::Auth::Credentials] credentials The path to
|
76
|
+
# the keyfile as a String, the contents of the keyfile as a Hash, or a
|
77
|
+
# Google::Auth::Credentials object. (See {Datastore::Credentials})
|
78
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
|
79
|
+
# set of resources and operations that the connection can access. See
|
80
|
+
# [Using OAuth 2.0 to Access Google
|
81
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
82
|
+
#
|
83
|
+
# The default scope is:
|
84
|
+
#
|
85
|
+
# * `https://www.googleapis.com/auth/datastore`
|
86
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
87
|
+
# @param [Hash] client_config A hash of values to override the default
|
88
|
+
# behavior of the API client. Optional.
|
89
|
+
#
|
90
|
+
# @return [Google::Cloud::Firestore::Client]
|
91
|
+
#
|
92
|
+
# @example
|
93
|
+
# require "google/cloud"
|
94
|
+
#
|
95
|
+
# firestore = Google::Cloud.firestore
|
96
|
+
#
|
97
|
+
def self.firestore project_id = nil, credentials = nil, scope: nil,
|
98
|
+
timeout: nil, client_config: nil
|
99
|
+
require "google/cloud/firestore"
|
100
|
+
Google::Cloud::Firestore.new project_id: project_id,
|
101
|
+
credentials: credentials,
|
102
|
+
scope: scope, timeout: timeout,
|
103
|
+
client_config: client_config
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,514 @@
|
|
1
|
+
# Copyright 2017 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 "google-cloud-firestore"
|
17
|
+
require "google/cloud/firestore/client"
|
18
|
+
|
19
|
+
module Google
|
20
|
+
module Cloud
|
21
|
+
##
|
22
|
+
# # Cloud Firestore
|
23
|
+
#
|
24
|
+
# Cloud Firestore is a NoSQL document database built for automatic scaling,
|
25
|
+
# high performance, and ease of application development. While the Cloud
|
26
|
+
# Firestore interface has many of the same features as traditional
|
27
|
+
# databases, as a NoSQL database it differs from them in the way it
|
28
|
+
# describes relationships between data objects.
|
29
|
+
#
|
30
|
+
# For more information about Cloud Firestore, read the [Cloud
|
31
|
+
# Firestore Documentation](https://cloud.google.com/firestore/docs/).
|
32
|
+
#
|
33
|
+
# The goal of google-cloud is to provide an API that is comfortable to
|
34
|
+
# Rubyists. Authentication is handled by {Google::Cloud#firestore}. You can
|
35
|
+
# provide the project and credential information to connect to the Cloud
|
36
|
+
# Firestore service, or if you are running on Google Compute Engine this
|
37
|
+
# configuration is taken care of for you. You can read more about the
|
38
|
+
# options for connecting in the [Authentication
|
39
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
40
|
+
#
|
41
|
+
# ## Adding data
|
42
|
+
#
|
43
|
+
# Cloud Firestore stores data in Documents, which are stored in Collections.
|
44
|
+
# Cloud Firestore creates collections and documents implicitly the first
|
45
|
+
# time you add data to the document. (For more information, see [Adding Data
|
46
|
+
# to Cloud Firestore](https://cloud.google.com/firestore/docs/manage-data/add-data).
|
47
|
+
#
|
48
|
+
# To create or overwrite a single document, use {Firestore::Client#doc} to
|
49
|
+
# obtain a document reference. (This does not create a document in Cloud
|
50
|
+
# Firestore.) Then, call {Firestore::DocumentReference#set} to create the
|
51
|
+
# document or overwrite an existing document:
|
52
|
+
#
|
53
|
+
# ```ruby
|
54
|
+
# require "google/cloud/firestore"
|
55
|
+
#
|
56
|
+
# firestore = Google::Cloud::Firestore.new
|
57
|
+
#
|
58
|
+
# # Get a document reference
|
59
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
60
|
+
#
|
61
|
+
# nyc_ref.set({ name: "New York City" }) # Document created
|
62
|
+
# ```
|
63
|
+
#
|
64
|
+
# When you use this combination of `doc` and `set` to create a new document,
|
65
|
+
# you must specify an ID for the document. (In the example above, the ID is
|
66
|
+
# "NYC".) However, if you do not have a meaningful ID for the document, you
|
67
|
+
# may omit the ID from a call to {Firestore::CollectionReference#doc}, and
|
68
|
+
# Cloud Firestore will auto-generate an ID for you.
|
69
|
+
#
|
70
|
+
# ```ruby
|
71
|
+
# require "google/cloud/firestore"
|
72
|
+
#
|
73
|
+
# firestore = Google::Cloud::Firestore.new
|
74
|
+
#
|
75
|
+
# # Get a collection reference
|
76
|
+
# cities_col = firestore.col "cities"
|
77
|
+
#
|
78
|
+
# # Get a document reference with data
|
79
|
+
# random_ref = cities_col.doc
|
80
|
+
# random_ref.set({ name: "New York City" })
|
81
|
+
#
|
82
|
+
# # The document ID is randomly generated
|
83
|
+
# random_ref.document_id #=> "RANDOMID123XYZ"
|
84
|
+
# ```
|
85
|
+
#
|
86
|
+
# You can perform both of the operations shown above, auto-generating
|
87
|
+
# an ID and creating the document, in a single call to
|
88
|
+
# {Firestore::CollectionReference#add}.
|
89
|
+
#
|
90
|
+
# ```ruby
|
91
|
+
# require "google/cloud/firestore"
|
92
|
+
#
|
93
|
+
# firestore = Google::Cloud::Firestore.new
|
94
|
+
#
|
95
|
+
# # Get a collection reference
|
96
|
+
# cities_col = firestore.col "cities"
|
97
|
+
#
|
98
|
+
# # Get a document reference with data
|
99
|
+
# random_ref = cities_col.add({ name: "New York City" })
|
100
|
+
#
|
101
|
+
# # The document ID is randomly generated
|
102
|
+
# random_ref.document_id #=> "RANDOMID123XYZ"
|
103
|
+
# ```
|
104
|
+
#
|
105
|
+
# You can also use `add` to create an empty document:
|
106
|
+
#
|
107
|
+
# ```ruby
|
108
|
+
# require "google/cloud/firestore"
|
109
|
+
#
|
110
|
+
# firestore = Google::Cloud::Firestore.new
|
111
|
+
#
|
112
|
+
# # Get a collection reference
|
113
|
+
# cities_col = firestore.col "cities"
|
114
|
+
#
|
115
|
+
# # Create a document without data
|
116
|
+
# random_ref = cities_col.add
|
117
|
+
#
|
118
|
+
# # The document ID is randomly generated
|
119
|
+
# random_ref.document_id #=> "RANDOMID123XYZ"
|
120
|
+
# ```
|
121
|
+
#
|
122
|
+
# ## Retrieving collection references
|
123
|
+
#
|
124
|
+
# Collections are simply named containers for documents. A collection
|
125
|
+
# contains documents and nothing else. It can't directly contain raw fields
|
126
|
+
# with values, and it can't contain other collections. You do not need to
|
127
|
+
# "create" or "delete" collections. After you create the first document in a
|
128
|
+
# collection, the collection exists. If you delete all of the documents in a
|
129
|
+
# collection, it no longer exists. (For more information, see [Cloud
|
130
|
+
# Firestore Data Model](https://cloud.google.com/firestore/docs/data-model).
|
131
|
+
#
|
132
|
+
# Use {Firestore::Client#cols} to list the root-level collections:
|
133
|
+
#
|
134
|
+
# ```ruby
|
135
|
+
# require "google/cloud/firestore"
|
136
|
+
#
|
137
|
+
# firestore = Google::Cloud::Firestore.new
|
138
|
+
#
|
139
|
+
# # Get the root collections
|
140
|
+
# firestore.cols.each do |col|
|
141
|
+
# puts col.collection_id
|
142
|
+
# end
|
143
|
+
# ```
|
144
|
+
#
|
145
|
+
# Retrieving a reference to a single root-level collection is similar:
|
146
|
+
#
|
147
|
+
# ```ruby
|
148
|
+
# require "google/cloud/firestore"
|
149
|
+
#
|
150
|
+
# firestore = Google::Cloud::Firestore.new
|
151
|
+
#
|
152
|
+
# # Get the cities collection
|
153
|
+
# cities_col = firestore.col "cities"
|
154
|
+
# ```
|
155
|
+
#
|
156
|
+
# To list the collections in a document, first get the document reference,
|
157
|
+
# then use {Firestore::DocumentReference#cols}:
|
158
|
+
#
|
159
|
+
# ```ruby
|
160
|
+
# require "google/cloud/firestore"
|
161
|
+
#
|
162
|
+
# firestore = Google::Cloud::Firestore.new
|
163
|
+
#
|
164
|
+
# # Get a document reference
|
165
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
166
|
+
#
|
167
|
+
# nyc_ref.cols.each do |col|
|
168
|
+
# puts col.collection_id
|
169
|
+
# end
|
170
|
+
# ```
|
171
|
+
#
|
172
|
+
# Again, retrieving a reference to a single collection is similar::
|
173
|
+
#
|
174
|
+
# ```ruby
|
175
|
+
# require "google/cloud/firestore"
|
176
|
+
#
|
177
|
+
# firestore = Google::Cloud::Firestore.new
|
178
|
+
#
|
179
|
+
# # Get a document reference
|
180
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
181
|
+
#
|
182
|
+
# # Get precincts sub-collection
|
183
|
+
# precincts_col = nyc_ref.col "precincts"
|
184
|
+
# ```
|
185
|
+
#
|
186
|
+
# ## Reading data
|
187
|
+
#
|
188
|
+
# You can retrieve a snapshot of the data in a single document with
|
189
|
+
# {Firestore::DocumentReference#get}, which returns an instance of
|
190
|
+
# {Firestore::DocumentSnapshot}:
|
191
|
+
#
|
192
|
+
# ```ruby
|
193
|
+
# require "google/cloud/firestore"
|
194
|
+
#
|
195
|
+
# firestore = Google::Cloud::Firestore.new
|
196
|
+
#
|
197
|
+
# # Get a document reference
|
198
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
199
|
+
#
|
200
|
+
# nyc_snap = nyc_ref.get
|
201
|
+
# nyc_snap[:population] #=> 1000000
|
202
|
+
# ```
|
203
|
+
# In the example above, {Firestore::DocumentSnapshot#[]} is used to access a
|
204
|
+
# top-level field. To access nested fields, use {Firestore::FieldPath}:
|
205
|
+
#
|
206
|
+
# ```ruby
|
207
|
+
# require "google/cloud/firestore"
|
208
|
+
#
|
209
|
+
# firestore = Google::Cloud::Firestore.new
|
210
|
+
#
|
211
|
+
# user_snap = firestore.doc("users/frank").get
|
212
|
+
#
|
213
|
+
# nested_field_path = Google::Cloud::Firestore::FieldPath.new(
|
214
|
+
# :favorites, :food
|
215
|
+
# )
|
216
|
+
# user_snap.get(nested_field_path) #=> "Pizza"
|
217
|
+
# ```
|
218
|
+
#
|
219
|
+
# Or, use {Firestore::Client#get_all} to retrieve a list of document
|
220
|
+
# snapshots (data):
|
221
|
+
#
|
222
|
+
# ```ruby
|
223
|
+
# require "google/cloud/firestore"
|
224
|
+
#
|
225
|
+
# firestore = Google::Cloud::Firestore.new
|
226
|
+
#
|
227
|
+
# # Get and print city documents
|
228
|
+
# cities = ["cities/NYC", "cities/SF", "cities/LA"]
|
229
|
+
# firestore.get_all(cities).each do |city|
|
230
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
231
|
+
# end
|
232
|
+
# ```
|
233
|
+
#
|
234
|
+
# To retrieve all of the document snapshots in a collection, use
|
235
|
+
# {Firestore::CollectionReference#get}:
|
236
|
+
#
|
237
|
+
# ```ruby
|
238
|
+
# require "google/cloud/firestore"
|
239
|
+
#
|
240
|
+
# firestore = Google::Cloud::Firestore.new
|
241
|
+
#
|
242
|
+
# # Get a collection reference
|
243
|
+
# cities_col = firestore.col "cities"
|
244
|
+
#
|
245
|
+
# # Get and print all city documents
|
246
|
+
# cities_col.get do |city|
|
247
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
248
|
+
# end
|
249
|
+
# ```
|
250
|
+
#
|
251
|
+
# The example above is actually a simple query without filters. Let's look
|
252
|
+
# at some other queries for Cloud Firestore.
|
253
|
+
#
|
254
|
+
# ## Querying data
|
255
|
+
#
|
256
|
+
# Use {Firestore::Query#where} to filter queries on a field:
|
257
|
+
#
|
258
|
+
# ```ruby
|
259
|
+
# require "google/cloud/firestore"
|
260
|
+
#
|
261
|
+
# firestore = Google::Cloud::Firestore.new
|
262
|
+
#
|
263
|
+
# # Get a collection reference
|
264
|
+
# cities_col = firestore.col "cities"
|
265
|
+
#
|
266
|
+
# # Create a query
|
267
|
+
# query = cities_col.where(:population, :>=, 1000000)
|
268
|
+
#
|
269
|
+
# query.get do |city|
|
270
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
271
|
+
# end
|
272
|
+
# ```
|
273
|
+
#
|
274
|
+
# You can order the query results with {Firestore::Query#order}:
|
275
|
+
#
|
276
|
+
# ```ruby
|
277
|
+
# require "google/cloud/firestore"
|
278
|
+
#
|
279
|
+
# firestore = Google::Cloud::Firestore.new
|
280
|
+
#
|
281
|
+
# # Get a collection reference
|
282
|
+
# cities_col = firestore.col "cities"
|
283
|
+
#
|
284
|
+
# # Create a query
|
285
|
+
# query = cities_col.order(:name, :desc)
|
286
|
+
#
|
287
|
+
# query.get do |city|
|
288
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
289
|
+
# end
|
290
|
+
# ```
|
291
|
+
#
|
292
|
+
# Query methods may be chained, as in this example using
|
293
|
+
# {Firestore::Query#limit} and {Firestore::Query#offset} to perform
|
294
|
+
# pagination:
|
295
|
+
#
|
296
|
+
# ```ruby
|
297
|
+
# require "google/cloud/firestore"
|
298
|
+
#
|
299
|
+
# firestore = Google::Cloud::Firestore.new
|
300
|
+
#
|
301
|
+
# # Get a collection reference
|
302
|
+
# cities_col = firestore.col "cities"
|
303
|
+
#
|
304
|
+
# # Create a query
|
305
|
+
# query = cities_col.limit(5).offset(10)
|
306
|
+
#
|
307
|
+
# query.get do |city|
|
308
|
+
# puts "#{city.document_id} has #{city[:population]} residents."
|
309
|
+
# end
|
310
|
+
# ```
|
311
|
+
#
|
312
|
+
# See [Managing Indexes in Cloud
|
313
|
+
# Firestore](https://cloud.google.com/firestore/docs/query-data/indexing) to
|
314
|
+
# ensure the best performance for your queries.
|
315
|
+
#
|
316
|
+
# ## Updating data
|
317
|
+
#
|
318
|
+
# You can use {Firestore::DocumentReference#set} to completely overwrite an
|
319
|
+
# existing document:
|
320
|
+
#
|
321
|
+
# ```ruby
|
322
|
+
# require "google/cloud/firestore"
|
323
|
+
#
|
324
|
+
# firestore = Google::Cloud::Firestore.new
|
325
|
+
#
|
326
|
+
# # Get a document reference
|
327
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
328
|
+
#
|
329
|
+
# nyc_ref.set({ name: "New York City" })
|
330
|
+
# ```
|
331
|
+
#
|
332
|
+
# Or, to selectively update only the fields appearing in your `data`
|
333
|
+
# argument, set the `merge` option to `true`:
|
334
|
+
#
|
335
|
+
# ```ruby
|
336
|
+
# require "google/cloud/firestore"
|
337
|
+
#
|
338
|
+
# firestore = Google::Cloud::Firestore.new
|
339
|
+
#
|
340
|
+
# # Get a document reference
|
341
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
342
|
+
#
|
343
|
+
# nyc_ref.set({ name: "New York City" }, merge: true)
|
344
|
+
# ```
|
345
|
+
#
|
346
|
+
# Use {Firestore::DocumentReference#update} to directly update a
|
347
|
+
# deeply-nested field with a {Firestore::FieldPath}:
|
348
|
+
#
|
349
|
+
# ```ruby
|
350
|
+
# require "google/cloud/firestore"
|
351
|
+
#
|
352
|
+
# firestore = Google::Cloud::Firestore.new
|
353
|
+
#
|
354
|
+
# user_ref = firestore.doc "users/frank"
|
355
|
+
#
|
356
|
+
# nested_field_path = Google::Cloud::Firestore::FieldPath.new(
|
357
|
+
# :favorites, :food
|
358
|
+
# )
|
359
|
+
# user_ref.update({ nested_field_path: "Pasta" })
|
360
|
+
# ```
|
361
|
+
#
|
362
|
+
# ## Using transactions and batched writes
|
363
|
+
#
|
364
|
+
# Cloud Firestore supports atomic operations for reading and writing data.
|
365
|
+
# In a set of atomic operations, either all of the operations succeed, or
|
366
|
+
# none of them are applied. There are two types of atomic operations in
|
367
|
+
# Cloud Firestore: A transaction is a set of read and write operations on
|
368
|
+
# one or more documents, while a batched write is a set of only write
|
369
|
+
# operations on one or more documents. (For more information, see
|
370
|
+
# [Transactions and Batched Writes](https://cloud.google.com/firestore/docs/manage-data/transactions).
|
371
|
+
#
|
372
|
+
# ### Transactions
|
373
|
+
#
|
374
|
+
# A transaction consists of any number of read operations followed by any
|
375
|
+
# number of write operations. (Read operations must always come before write
|
376
|
+
# operations.) In the case of a concurrent update by another client, Cloud
|
377
|
+
# Firestore runs the entire transaction again. Therefore, transaction blocks
|
378
|
+
# should be idempotent and should not not directly modify application state.
|
379
|
+
#
|
380
|
+
# ```ruby
|
381
|
+
# require "google/cloud/firestore"
|
382
|
+
#
|
383
|
+
# firestore = Google::Cloud::Firestore.new
|
384
|
+
#
|
385
|
+
# city = firestore.col("cities").doc("SF")
|
386
|
+
# city.set({ name: "San Francisco",
|
387
|
+
# state: "CA",
|
388
|
+
# country: "USA",
|
389
|
+
# capital: false,
|
390
|
+
# population: 860000 })
|
391
|
+
#
|
392
|
+
# firestore.transaction do |tx|
|
393
|
+
# new_population = tx.get(city).data[:population] + 1
|
394
|
+
# tx.update(city, { population: new_population })
|
395
|
+
# end
|
396
|
+
# ```
|
397
|
+
#
|
398
|
+
# ### Batched writes
|
399
|
+
#
|
400
|
+
# If you do not need to read any documents in your operation set, you can
|
401
|
+
# execute multiple write operations as a single batch. A batch of writes
|
402
|
+
# completes atomically and can write to multiple documents. Batched writes
|
403
|
+
# are also useful for migrating large data sets to Cloud Firestore.
|
404
|
+
#
|
405
|
+
# ```ruby
|
406
|
+
# require "google/cloud/firestore"
|
407
|
+
#
|
408
|
+
# firestore = Google::Cloud::Firestore.new
|
409
|
+
#
|
410
|
+
# firestore.batch do |b|
|
411
|
+
# # Set the data for NYC
|
412
|
+
# b.set("cities/NYC", { name: "New York City" })
|
413
|
+
#
|
414
|
+
# # Update the population for SF
|
415
|
+
# b.update("cities/SF", { population: 1000000 })
|
416
|
+
#
|
417
|
+
# # Delete LA
|
418
|
+
# b.delete("cities/LA")
|
419
|
+
# end
|
420
|
+
# ```
|
421
|
+
#
|
422
|
+
# ## Deleting data
|
423
|
+
#
|
424
|
+
# Use {Firestore::DocumentReference#delete} to delete a document from Cloud
|
425
|
+
# Firestore:
|
426
|
+
#
|
427
|
+
# ```ruby
|
428
|
+
# require "google/cloud/firestore"
|
429
|
+
#
|
430
|
+
# firestore = Google::Cloud::Firestore.new
|
431
|
+
#
|
432
|
+
# # Get a document reference
|
433
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
434
|
+
#
|
435
|
+
# nyc_ref.delete
|
436
|
+
# ```
|
437
|
+
#
|
438
|
+
# To delete specific fields from a document, use the
|
439
|
+
# {Firestore::Client.field_delete} method when you update a document:
|
440
|
+
#
|
441
|
+
# ```ruby
|
442
|
+
# require "google/cloud/firestore"
|
443
|
+
#
|
444
|
+
# firestore = Google::Cloud::Firestore.new
|
445
|
+
#
|
446
|
+
# # Get a document reference
|
447
|
+
# nyc_ref = firestore.doc "cities/NYC"
|
448
|
+
#
|
449
|
+
# nyc_ref.update({ name: "New York City",
|
450
|
+
# trash: firestore.field_delete })
|
451
|
+
# ```
|
452
|
+
#
|
453
|
+
# To delete an entire collection or sub-collection in Cloud Firestore,
|
454
|
+
# retrieve all the documents within the collection or sub-collection and
|
455
|
+
# delete them. If you have larger collections, you may want to delete the
|
456
|
+
# documents in smaller batches to avoid out-of-memory errors. Repeat the
|
457
|
+
# process until you've deleted the entire collection or sub-collection.
|
458
|
+
#
|
459
|
+
module Firestore
|
460
|
+
##
|
461
|
+
# Creates a new object for connecting to the Firestore service.
|
462
|
+
# Each call creates a new connection.
|
463
|
+
#
|
464
|
+
# For more information on connecting to Google Cloud see the
|
465
|
+
# [Authentication
|
466
|
+
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
|
467
|
+
#
|
468
|
+
# @param [String] project_id Identifier for a Firestore project. If not
|
469
|
+
# present, the default project for the credentials is used.
|
470
|
+
# @param [String, Hash, Google::Auth::Credentials] credentials The path to
|
471
|
+
# the keyfile as a String, the contents of the keyfile as a Hash, or a
|
472
|
+
# Google::Auth::Credentials object. (See {Datastore::Credentials})
|
473
|
+
# @param [String, Array<String>] scope The OAuth 2.0 scopes controlling
|
474
|
+
# the set of resources and operations that the connection can access.
|
475
|
+
# See [Using OAuth 2.0 to Access Google
|
476
|
+
# APIs](https://developers.google.com/identity/protocols/OAuth2).
|
477
|
+
#
|
478
|
+
# The default scope is:
|
479
|
+
#
|
480
|
+
# * `https://www.googleapis.com/auth/datastore`
|
481
|
+
# @param [Integer] timeout Default timeout to use in requests. Optional.
|
482
|
+
# @param [Hash] client_config A hash of values to override the default
|
483
|
+
# behavior of the API client. Optional.
|
484
|
+
# @param [String] project Alias for the `project_id` argument. Deprecated.
|
485
|
+
# @param [String] keyfile Alias for the `credentials` argument.
|
486
|
+
# Deprecated.
|
487
|
+
#
|
488
|
+
# @return [Google::Cloud::Firestore::Client]
|
489
|
+
#
|
490
|
+
# @example
|
491
|
+
# require "google/cloud/firestore"
|
492
|
+
#
|
493
|
+
# firestore = Google::Cloud::Firestore.new
|
494
|
+
#
|
495
|
+
def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
|
496
|
+
client_config: nil, project: nil, keyfile: nil
|
497
|
+
project_id ||= (project || Firestore::Service.default_project_id)
|
498
|
+
project_id = project_id.to_s # Always cast to a string
|
499
|
+
fail ArgumentError, "project_id is missing" if project_id.empty?
|
500
|
+
|
501
|
+
credentials ||= keyfile
|
502
|
+
credentials ||= Firestore::Credentials.default(scope: scope)
|
503
|
+
unless credentials.is_a? Google::Auth::Credentials
|
504
|
+
credentials = Firestore::Credentials.new credentials, scope: scope
|
505
|
+
end
|
506
|
+
|
507
|
+
Firestore::Client.new \
|
508
|
+
Firestore::Service.new \
|
509
|
+
project_id, credentials,
|
510
|
+
timeout: timeout, client_config: client_config
|
511
|
+
end
|
512
|
+
end
|
513
|
+
end
|
514
|
+
end
|