Skip to content

Commit 966857b

Browse files
ryanmatsbshaffer
authored andcommitted
Adds Firestore samples (GoogleCloudPlatform#549)
1 parent b2959ad commit 966857b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+6140
-0
lines changed

firestore/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Google Cloud Firestore API Samples
2+
3+
These samples show how to use the [Google Cloud Firestore API][cloud-firestore-api] to store and query data.
4+
5+
[cloud-firestore-api]: http://cloud.google.com/cloud-firestore
6+
7+
## Setup
8+
9+
### Prerequisites
10+
11+
1. Open the [Firebase Console][firebase-console] and create a new project. (You can't use both Cloud Firestore and Cloud Datastore in the same project, which might affect apps using App Engine. Try using Cloud Firestore with a different project if this is the case).
12+
13+
1. In the Database section, click Try Firestore Beta.
14+
15+
1. Click Enable.
16+
17+
[firebase-console]: https://console.firebase.google.com
18+
19+
20+
### Authentication
21+
22+
Authentication is typically done through [Application Default Credentials][adc]
23+
which means you do not have to change the code to authenticate as long as
24+
your environment has credentials. You have a few options for setting up
25+
authentication:
26+
27+
1. When running locally, use the [Google Cloud SDK][google-cloud-sdk]
28+
29+
gcloud auth application-default login
30+
31+
1. When running on App Engine or Compute Engine, credentials are already
32+
set-up. However, you may need to configure your Compute Engine instance
33+
with [additional scopes][additional_scopes].
34+
35+
1. You can create a [Service Account key file][service_account_key_file]. This file can be used to
36+
authenticate to Google Cloud Platform services from any environment. To use
37+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
38+
the path to the key file, for example:
39+
40+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
41+
42+
[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
43+
[additional_scopes]: https://cloud.google.com/compute/docs/authentication#using
44+
[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
45+
46+
## Install Dependencies
47+
48+
1. [Enable the Cloud Firestore API](https://console.cloud.google.com/flows/enableapi?apiid=firestore.googleapis.com).
49+
50+
1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
51+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
52+
(if composer is installed globally).
53+
54+
1. Create a service account at the
55+
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)
56+
57+
1. Download the json key file of the service account.
58+
59+
1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to that file.
60+
61+
## Samples
62+
63+
To run the Cloud Firestore Samples:
64+
65+
$ php firestore.php
66+
Cloud Firestore
67+
68+
Usage:
69+
command [options] [arguments]
70+
71+
Options:
72+
-h, --help Display this help message
73+
-q, --quiet Do not output any message
74+
-V, --version Display this application version
75+
--ansi Force ANSI output
76+
--no-ansi Disable ANSI output
77+
-n, --no-interaction Do not ask any interactive question
78+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
79+
80+
Available commands:
81+
add-data Add data to a document.
82+
add-doc-data-after-auto-id Auto-generate an ID for a document, then add document data.
83+
add-doc-data-types Set document data with different data types.
84+
add-doc-data-with-auto-id Add document data with an auto-generated ID.
85+
batch-write Batch write.
86+
chained-query Create a query with chained clauses.
87+
collection-ref Get a collection reference.
88+
composite-index-chained-query Create a composite index chained query, which combines an equality operator with a range comparison.
89+
create-query-capital Create a query that gets documents where capital=True.
90+
create-query-state Create a query that gets documents where state=CA.
91+
delete-collection Delete a collection.
92+
delete-document Delete a document.
93+
delete-field Delete a field from a document.
94+
delete-test-collections Delete test collections used in these code samples.
95+
document-path-ref Get a document path reference.
96+
document-ref Get a document reference.
97+
end-at-field-query-cursor Define field end point for a query.
98+
get-all-docs Get all documents in a collection.
99+
get-document Get a document.
100+
get-multiple-docs Get multiple documents from a collection.
101+
help Displays help for a command
102+
initialize Initialize Cloud Firestore with default project ID.
103+
initialize-project-id Initialize Cloud Firestore with given project ID.
104+
invalid-range-order-by-query An invalid range with order by query.
105+
invalid-range-query An example of an invalid range query.
106+
list Lists commands
107+
multiple-cursor-conditions Set multiple cursor conditions.
108+
order-by-name-desc-limit-query Create an order by name descending with limit query.
109+
order-by-name-limit-query Create an order by name with limit query.
110+
order-by-state-and-population-query Create an order by state and descending population query.
111+
paginated-query-cursor Paginate using cursor queries.
112+
query-create-examples Create an example collection of documents.
113+
range-order-by-query Create a range with order by query.
114+
range-query Create a query with range clauses.
115+
retrieve-all-documents Retrieve all documents from a collection.
116+
retrieve-create-examples Create an example collection of documents.
117+
return-info-transaction Return information from your transaction.
118+
run-simple-transaction Run a simple transaction.
119+
set-document Set document data.
120+
set-document-merge Set document data by merging it into the existing document.
121+
set-requires-id Set document data with a given document ID.
122+
simple-queries Create queries using single where clauses.
123+
start-at-field-query-cursor Define field start point for a query.
124+
subcollection-ref Get a reference to a subcollection document.
125+
update-doc Update a document.
126+
update-nested-fields Update fields in nested data.
127+
update-server-timestamp Update field with server timestamp.
128+
where-order-by-limit-query Combine where with order by and limit in a query.
129+
130+
## The client library
131+
132+
This sample uses the [Google Cloud Client Library for PHP][google-cloud-php].
133+
You can read the documentation for more details on API usage and use GitHub
134+
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
135+
136+
## Troubleshooting
137+
138+
If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID:
139+
140+
```
141+
[Google\Cloud\Core\Exception\GoogleException]
142+
No project ID was provided, and we were unable to detect a default project ID.
143+
```
144+
145+
If you have not set a timezone you may get an error from php. This can be resolved by:
146+
147+
1. Finding where the php.ini is stored by running `php -i | grep 'Configuration File'`
148+
1. Finding out your timezone from the list on this page: http://php.net/manual/en/timezones.php
149+
1. Editing the php.ini file (or creating one if it doesn't exist)
150+
1. Adding the timezone to the php.ini file e.g., adding the following line: `date.timezone = "America/Los_Angeles"`
151+
152+
[google-cloud-php]: https://googlecloudplatform.github.io/google-cloud-php
153+
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
154+
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
155+
[google-cloud-sdk]: https://cloud.google.com/sdk/

firestore/composer.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"require": {
3+
"google/cloud-firestore": "^0.5",
4+
"symfony/console": "^3.0"
5+
},
6+
"require-dev": {
7+
"phpunit/phpunit": "^4.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Google\\Cloud\\Samples\\Firestore\\": "src/"
12+
},
13+
"files": [
14+
"src/initialize.php",
15+
"src/initialize_project_id.php",
16+
"src/add_data.php",
17+
"src/get_all.php",
18+
"src/set_document.php",
19+
"src/add_doc_data_types.php",
20+
"src/set_requires_id.php",
21+
"src/add_doc_data_with_auto_id.php",
22+
"src/add_doc_data_after_auto_id.php",
23+
"src/query_create_examples.php",
24+
"src/create_query_state.php",
25+
"src/create_query_capital.php",
26+
"src/simple_queries.php",
27+
"src/chained_query.php",
28+
"src/composite_index_chained_query.php",
29+
"src/range_query.php",
30+
"src/invalid_range_query.php",
31+
"src/delete_doc.php",
32+
"src/delete_field.php",
33+
"src/delete_collection.php",
34+
"src/retrieve_create_examples.php",
35+
"src/get_document.php",
36+
"src/get_multiple_docs.php",
37+
"src/get_all_docs.php",
38+
"src/order_by_name_limit_query.php",
39+
"src/order_by_name_desc_limit_query.php",
40+
"src/order_by_state_and_population_query.php",
41+
"src/where_order_by_limit_query.php",
42+
"src/range_order_by_query.php",
43+
"src/invalid_range_order_by_query.php",
44+
"src/document_ref.php",
45+
"src/collection_ref.php",
46+
"src/document_path_ref.php",
47+
"src/subcollection_ref.php",
48+
"src/update_doc.php",
49+
"src/set_document_merge.php",
50+
"src/update_nested_fields.php",
51+
"src/update_server_timestamp.php",
52+
"src/run_simple_transaction.php",
53+
"src/return_info_transaction.php",
54+
"src/batch_write.php",
55+
"src/start_at_field_query_cursor.php",
56+
"src/end_at_field_query_cursor.php",
57+
"src/paginated_query_cursor.php",
58+
"src/multiple_cursor_conditions.php"
59+
]
60+
}
61+
}

0 commit comments

Comments
 (0)