Skip to content

Commit 00f6623

Browse files
billyjacobsonryanmats
authored andcommitted
cDPE Storage - Firestore Requests (GoogleCloudPlatform#740)
* Samples: -Array membership -Start at snapshot query -Updating sample data Tests README Composer updates * Use new command to run tests * Fixing snapshot tests * Removing LA from snapshot test because it gets deleted in batch write * Using Google LLC * Add the update doc array sample
1 parent a30b723 commit 00f6623

File tree

8 files changed

+282
-35
lines changed

8 files changed

+282
-35
lines changed

firestore/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ To run the Cloud Firestore Samples:
8383
add-doc-data-types Set document data with different data types.
8484
add-doc-data-with-auto-id Add document data with an auto-generated ID.
8585
add-subcollection Add a subcollection by creating a new document.
86+
array-membership Create queries using an an array-contains where clause.
8687
batch-write Batch write.
8788
chained-query Create a query with chained clauses.
8889
collection-ref Get a collection reference.
@@ -123,8 +124,10 @@ To run the Cloud Firestore Samples:
123124
set-requires-id Set document data with a given document ID.
124125
simple-queries Create queries using single where clauses.
125126
start-at-field-query-cursor Define field start point for a query.
127+
start-at-snapshot-query-cursor Define snapshot start point for a query.
126128
subcollection-ref Get a reference to a subcollection document.
127129
update-doc Update a document.
130+
update-doc-array Update a document array field.
128131
update-nested-fields Update fields in nested data.
129132
update-server-timestamp Update field with server timestamp.
130133
where-order-by-limit-query Combine where with order by and limit in a query.

firestore/composer.json

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,55 @@
99
},
1010
"autoload": {
1111
"files": [
12-
"src/initialize.php",
13-
"src/initialize_project_id.php",
1412
"src/add_data.php",
15-
"src/get_all.php",
16-
"src/set_document.php",
13+
"src/add_doc_data_after_auto_id.php",
1714
"src/add_doc_data_types.php",
18-
"src/set_requires_id.php",
1915
"src/add_doc_data_with_auto_id.php",
20-
"src/add_doc_data_after_auto_id.php",
21-
"src/list_subcollections.php",
22-
"src/query_create_examples.php",
23-
"src/create_query_state.php",
24-
"src/create_query_capital.php",
25-
"src/simple_queries.php",
16+
"src/array_membership.php",
17+
"src/batch_write.php",
2618
"src/chained_query.php",
19+
"src/collection_ref.php",
2720
"src/composite_index_chained_query.php",
28-
"src/range_query.php",
29-
"src/invalid_range_query.php",
21+
"src/create_query_capital.php",
22+
"src/create_query_state.php",
23+
"src/delete_collection.php",
3024
"src/delete_doc.php",
3125
"src/delete_field.php",
32-
"src/delete_collection.php",
33-
"src/retrieve_create_examples.php",
26+
"src/document_path_ref.php",
27+
"src/document_ref.php",
28+
"src/end_at_field_query_cursor.php",
29+
"src/get_all.php",
30+
"src/get_all_docs.php",
3431
"src/get_document.php",
3532
"src/get_multiple_docs.php",
36-
"src/get_all_docs.php",
37-
"src/order_by_name_limit_query.php",
33+
"src/initialize.php",
34+
"src/initialize_project_id.php",
35+
"src/invalid_range_order_by_query.php",
36+
"src/invalid_range_query.php",
37+
"src/list_subcollections.php",
38+
"src/multiple_cursor_conditions.php",
3839
"src/order_by_name_desc_limit_query.php",
40+
"src/order_by_name_limit_query.php",
3941
"src/order_by_state_and_population_query.php",
40-
"src/where_order_by_limit_query.php",
42+
"src/paginated_query_cursor.php",
43+
"src/query_create_examples.php",
4144
"src/range_order_by_query.php",
42-
"src/invalid_range_order_by_query.php",
43-
"src/document_ref.php",
44-
"src/collection_ref.php",
45-
"src/document_path_ref.php",
45+
"src/range_query.php",
46+
"src/retrieve_create_examples.php",
47+
"src/return_info_transaction.php",
48+
"src/run_simple_transaction.php",
49+
"src/set_document.php",
50+
"src/set_document_merge.php",
51+
"src/set_requires_id.php",
52+
"src/simple_queries.php",
53+
"src/start_at_field_query_cursor.php",
54+
"src/start_at_snapshot_query_cursor.php",
4655
"src/subcollection_ref.php",
4756
"src/update_doc.php",
48-
"src/set_document_merge.php",
57+
"src/update_doc_array.php",
4958
"src/update_nested_fields.php",
5059
"src/update_server_timestamp.php",
51-
"src/run_simple_transaction.php",
52-
"src/return_info_transaction.php",
53-
"src/batch_write.php",
54-
"src/start_at_field_query_cursor.php",
55-
"src/end_at_field_query_cursor.php",
56-
"src/paginated_query_cursor.php",
57-
"src/multiple_cursor_conditions.php"
60+
"src/where_order_by_limit_query.php"
5861
]
5962
}
6063
}

firestore/firestore.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@
252252
})
253253
);
254254

255+
// Array Membership command
256+
$application->add((new Command('array-membership'))
257+
->setDefinition($inputDefinition)
258+
->setDescription('Create queries using an array-contains where clause.')
259+
->setHelp(<<<EOF
260+
The <info>%command.name%</info> command creates queries using an array-contains where clause using the Google Cloud Firestore API.
261+
262+
<info>php %command.full_name%</info>
263+
264+
EOF
265+
)
266+
->setCode(function ($input, $output) {
267+
$projectId = $input->getArgument('project');
268+
array_membership($projectId);
269+
})
270+
);
271+
255272
// Chained Query command
256273
$application->add((new Command('chained-query'))
257274
->setDefinition($inputDefinition)
@@ -672,6 +689,23 @@
672689
})
673690
);
674691

692+
// Update Document Array command
693+
$application->add((new Command('update-doc-array'))
694+
->setDefinition($inputDefinition)
695+
->setDescription('Update a document array field.')
696+
->setHelp(<<<EOF
697+
The <info>%command.name%</info> command updates a document array field using the Google Cloud Firestore API.
698+
699+
<info>php %command.full_name%</info>
700+
701+
EOF
702+
)
703+
->setCode(function ($input, $output) {
704+
$projectId = $input->getArgument('project');
705+
update_doc_array($projectId);
706+
})
707+
);
708+
675709
// Set Document Merge command
676710
$application->add((new Command('set-document-merge'))
677711
->setDefinition($inputDefinition)
@@ -808,6 +842,23 @@
808842
})
809843
);
810844

845+
// Start At Snapshot Query Cursor command
846+
$application->add((new Command('start-at-snapshot-query-cursor'))
847+
->setDefinition($inputDefinition)
848+
->setDescription('Define snapshot start point for a query.')
849+
->setHelp(<<<EOF
850+
The <info>%command.name%</info> command defines a snapshot start point for a query using the Google Cloud Firestore API.
851+
852+
<info>php %command.full_name%</info>
853+
854+
EOF
855+
)
856+
->setCode(function ($input, $output) {
857+
$projectId = $input->getArgument('project');
858+
start_at_snapshot_query_cursor($projectId);
859+
})
860+
);
861+
811862
// Paginated Query Cursor command
812863
$application->add((new Command('paginated-query-cursor'))
813864
->setDefinition($inputDefinition)

firestore/src/array_membership.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
use Google\Cloud\Firestore\FirestoreClient;
27+
28+
/**
29+
* Create queries using an array-contains where clause.
30+
* ```
31+
* array_membership('your-project-id');
32+
* ```
33+
*/
34+
function array_membership($projectId)
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
$citiesRef = $db->collection('cities');
41+
# [START fs_array_membership]
42+
$containsQuery = $citiesRef->where('regions', 'array-contains', 'west_coast');
43+
# [END fs_array_membership]
44+
foreach ($containsQuery->documents() as $document) {
45+
printf('Document %s returned by query regions array-contains west_coast' . PHP_EOL, $document->id());
46+
}
47+
}

firestore/src/query_create_examples.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,40 @@ function query_create_examples($projectId)
4444
'state' => 'CA',
4545
'country' => 'USA',
4646
'capital' => false,
47-
'population' => 860000
47+
'population' => 860000,
48+
'regions' => ['west_coast', 'norcal']
4849
]);
4950
$citiesRef->document('LA')->set([
5051
'name' => 'Los Angeles',
5152
'state' => 'CA',
5253
'country' => 'USA',
5354
'capital' => false,
54-
'population' => 3900000
55+
'population' => 3900000,
56+
'regions' => ['west_coast', 'socal']
5557
]);
5658
$citiesRef->document('DC')->set([
5759
'name' => 'Washington D.C.',
5860
'state' => null,
5961
'country' => 'USA',
6062
'capital' => true,
61-
'population' => 680000
63+
'population' => 680000,
64+
'regions' => ['east_coast']
6265
]);
6366
$citiesRef->document('TOK')->set([
6467
'name' => 'Tokyo',
6568
'state' => null,
6669
'country' => 'Japan',
6770
'capital' => true,
68-
'population' => 9000000
71+
'population' => 9000000,
72+
'regions' => ['kanto', 'honshu']
6973
]);
7074
$citiesRef->document('BJ')->set([
7175
'name' => 'Beijing',
7276
'state' => null,
7377
'country' => 'China',
7478
'capital' => true,
75-
'population' => 21500000
79+
'population' => 21500000,
80+
'regions' => ['jingjinji', 'hebei']
7681
]);
7782
printf('Added example cities data to the cities collection.' . PHP_EOL);
7883
# [END fs_query_create_examples]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
use Google\Cloud\Firestore\FirestoreClient;
27+
28+
/**
29+
* Define snapshot start point for a query.
30+
* ```
31+
* start_at_snapshot_query_cursor('your-project-id');
32+
* ```
33+
*/
34+
function start_at_snapshot_query_cursor($projectId)
35+
{
36+
// Create the Cloud Firestore client
37+
$db = new FirestoreClient([
38+
'projectId' => $projectId,
39+
]);
40+
# [START fs_start_at_snapshot_query_cursor]
41+
$citiesRef = $db->collection('cities');
42+
$docRef = $citiesRef->document('SF');
43+
$snapshot = $docRef->snapshot();
44+
45+
$query = $citiesRef
46+
->orderBy('population')
47+
->startAt($snapshot);
48+
# [END fs_start_at_snapshot_query_cursor]
49+
$snapshot = $query->documents();
50+
foreach ($snapshot as $document) {
51+
printf('Document %s returned by start at SF snapshot query cursor.' . PHP_EOL, $document->id());
52+
}
53+
}

firestore/src/update_doc_array.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2018 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/firestore/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Firestore;
25+
26+
use Google\Cloud\Firestore\FieldValue;
27+
use Google\Cloud\Firestore\FirestoreClient;
28+
29+
/**
30+
* Update a document array field.
31+
* ```
32+
* update_doc_array('your-project-id');
33+
* ```
34+
*/
35+
function update_doc_array($projectId)
36+
{
37+
// Create the Cloud Firestore client
38+
$db = new FirestoreClient([
39+
'projectId' => $projectId,
40+
]);
41+
# [START fs_update_doc_array]
42+
$cityRef = $db->collection('cities')->document('DC');
43+
44+
// Atomically add a new region to the "regions" array field.
45+
$cityRef->update([
46+
['path' => 'regions', 'value' => FieldValue::arrayUnion(['greater_virginia'])]
47+
]);
48+
49+
// Atomically remove a region from the "regions" array field.
50+
$cityRef->update([
51+
['path' => 'regions', 'value' => FieldValue::arrayRemove(['east_coast'])]
52+
]);
53+
# [END fs_update_doc_array]
54+
printf('Updated the regions field of the DC document in the cities collection.' . PHP_EOL);
55+
}

0 commit comments

Comments
 (0)