Skip to content

Commit d9fcf08

Browse files
authored
chore: refactor datastore samples (GoogleCloudPlatform#2033)
1 parent 847060f commit d9fcf08

Some content is hidden

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

54 files changed

+326
-327
lines changed

datastore/api/src/ancestor_query.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create an ancestor query.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function ancestor_query(DatastoreClient $datastore)
28+
function ancestor_query(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_ancestor_query]
3132
$ancestorKey = $datastore->key('TaskList', 'default');
3233
$query = $datastore->query()

datastore/api/src/array_value.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
namespace Google\Cloud\Samples\Datastore;
1919

2020
use Google\Cloud\Datastore\DatastoreClient;
21-
use Google\Cloud\Datastore\Key;
2221

2322
/**
2423
* Create a Datastore entity with some array properties.
2524
*
26-
* @param DatastoreClient $datastore
27-
* @param Key $key
25+
* @param string $keyId
26+
* @param string $namespaceId
2827
*/
29-
function array_value(DatastoreClient $datastore, Key $key)
28+
function array_value(string $keyId, string $namespaceId = null)
3029
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
31+
$key = $datastore->key('Task', $keyId);
3132
// [START datastore_array_value]
3233
$task = $datastore->entity(
3334
$key,

datastore/api/src/array_value_equality.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with equality filters.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function array_value_equality(DatastoreClient $datastore)
28+
function array_value_equality(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_array_value_equality]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/array_value_inequality_range.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with inequality filters.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function array_value_inequality_range(DatastoreClient $datastore)
28+
function array_value_inequality_range(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_array_value_inequality_range]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/ascending_sort.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with ascending sort.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function ascending_sort(DatastoreClient $datastore)
28+
function ascending_sort(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_ascending_sort]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/basic_entity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
/**
2323
* Create a Datastore entity.
2424
*
25-
* @param DatastoreClient $datastore
25+
* @param string $namespaceId
2626
*/
27-
function basic_entity(DatastoreClient $datastore)
27+
function basic_entity(string $namespaceId = null)
2828
{
29+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
2930
// [START datastore_basic_entity]
3031
$task = $datastore->entity('Task', [
3132
'category' => 'Personal',

datastore/api/src/basic_gql_query.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a basic Datastore Gql query.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function basic_gql_query(DatastoreClient $datastore)
28+
function basic_gql_query(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_basic_gql_query]
3132
$gql = <<
3233
SELECT * from Task

datastore/api/src/basic_query.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a basic Datastore query.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function basic_query(DatastoreClient $datastore)
28+
function basic_query(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_basic_query]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/batch_delete.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
namespace Google\Cloud\Samples\Datastore;
1919

2020
use Google\Cloud\Datastore\DatastoreClient;
21-
use Google\Cloud\Datastore\Key;
2221

2322
/**
2423
* Delete multiple Datastore entities with the given keys.
2524
*
26-
* @param DatastoreClient $datastore
27-
* @param array $keys
25+
* @param array $keyIds
26+
* @param string $namespaceId
2827
*/
29-
function batch_delete(DatastoreClient $datastore, array $keys)
28+
function batch_delete(array $keyIds, string $namespaceId = null)
3029
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
31+
$keys = array_map(fn ($keyId) => $datastore->key('Task', $keyId), $keyIds);
3132
// [START datastore_batch_delete]
3233
$result = $datastore->deleteBatch($keys);
3334
// [END datastore_batch_delete]

datastore/api/src/batch_lookup.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
namespace Google\Cloud\Samples\Datastore;
1919

2020
use Google\Cloud\Datastore\DatastoreClient;
21-
use Google\Cloud\Datastore\Key;
2221

2322
/**
2423
* Lookup multiple entities.
2524
*
26-
* @param DatastoreClient $datastore
27-
* @param array $keys
25+
* @param array $keyIds
26+
* @param string $namespaceId
2827
*/
29-
function batch_lookup(DatastoreClient $datastore, array $keys)
28+
function batch_lookup(array $keyIds, string $namespaceId = null)
3029
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
31+
$keys = array_map(fn ($keyId) => $datastore->key('Task', $keyId), $keyIds);
3132
// [START datastore_batch_lookup]
3233
$result = $datastore->lookupBatch($keys);
3334
if (isset($result['found'])) {

datastore/api/src/batch_upsert.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
/**
2424
* Upsert multiple Datastore entities.
2525
*
26-
* @param DatastoreClient $datastore
2726
* @param array $tasks
27+
* @param string $namespaceId
2828
*/
29-
function batch_upsert(DatastoreClient $datastore, array $tasks)
29+
function batch_upsert(array $tasks, string $namespaceId = null)
3030
{
31+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3132
// [START datastore_batch_upsert]
3233
$result = $datastore->upsertBatch($tasks);
3334
// [END datastore_batch_upsert]

datastore/api/src/composite_filter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with a composite filter.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function composite_filter(DatastoreClient $datastore)
28+
function composite_filter(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_composite_filter]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/cursor_paging.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
/**
2525
* Fetch a query cursor.
2626
*
27-
* @param DatastoreClient $datastore
2827
* @param int $pageSize
2928
* @param string $pageCursor
29+
* @param string $namespaceId
3030
*/
31-
function cursor_paging(DatastoreClient $datastore, int $pageSize, string $pageCursor = '')
31+
function cursor_paging(int $pageSize, string $pageCursor = '', string $namespaceId = null)
3232
{
33+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3334
$query = $datastore->query()
3435
->kind('Task')
3536
->limit($pageSize)

datastore/api/src/delete.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
/**
2424
* Delete a Datastore entity with the given key.
2525
*
26-
* @param DatastoreClient $datastore
27-
* @param Key $taskKey
26+
* @param string $namespaceId
27+
* @param string $keyId
2828
*/
29-
function delete(DatastoreClient $datastore, Key $taskKey)
29+
function delete(string $keyId, string $namespaceId = null)
3030
{
31+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
32+
$taskKey = $datastore->key('Task', $keyId);
3133
// [START datastore_delete]
3234
$datastore->delete($taskKey);
3335
// [END datastore_delete]

datastore/api/src/descending_sort.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with descending sort.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function descending_sort(DatastoreClient $datastore)
28+
function descending_sort(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_descending_sort]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/distinct_on.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create a query with distinctOn.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function distinct_on(DatastoreClient $datastore)
28+
function distinct_on(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_distinct_on_query]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/entity_with_parent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create an entity with a parent key.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function entity_with_parent(DatastoreClient $datastore)
28+
function entity_with_parent(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_entity_with_parent]
3132
$parentKey = $datastore->key('TaskList', 'default');
3233
$key = $datastore->key('Task')->ancestorKey($parentKey);

datastore/api/src/equal_and_inequality_range.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
* Create a query with equality filters and inequality range filters on a
2626
* single property.
2727
*
28-
* @param DatastoreClient $datastore
28+
* @param string $namespaceId
2929
*/
30-
function equal_and_inequality_range(DatastoreClient $datastore)
30+
function equal_and_inequality_range(string $namespaceId = null)
3131
{
32+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3233
// [START datastore_equal_and_inequality_range]
3334
$query = $datastore->query()
3435
->kind('Task')

datastore/api/src/eventual_consistent_query.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create and run a query with readConsistency option.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function eventual_consistent_query(DatastoreClient $datastore)
28+
function eventual_consistent_query(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_eventual_consistent_query]
3132
$query = $datastore->query()
3233
->kind('Task')

datastore/api/src/exploding_properties.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create an entity with two array properties.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function exploding_properties(DatastoreClient $datastore)
28+
function exploding_properties(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_exploding_properties]
3132
$task = $datastore->entity(
3233
$datastore->key('Task'),

datastore/api/src/get_or_create.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
/**
2525
* Insert an entity only if there is no entity with the same key.
2626
*
27-
* @param DatastoreClient $datastore
27+
* @param EntityInterface $task
28+
* @param string $namespaceId
2829
*/
29-
function get_or_create(DatastoreClient $datastore, EntityInterface $task)
30+
function get_or_create(EntityInterface $task, string $namespaceId = null)
3031
{
32+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3133
// [START datastore_transactional_get_or_create]
3234
$transaction = $datastore->transaction();
3335
$entity = $transaction->lookup($task->key());

datastore/api/src/get_task_list_entities.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Run a query with an ancestor inside a transaction.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function get_task_list_entities(DatastoreClient $datastore)
28+
function get_task_list_entities(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_transactional_single_entity_group_read_only]
3132
$transaction = $datastore->readOnlyTransaction();
3233
$taskListKey = $datastore->key('TaskList', 'default');

datastore/api/src/incomplete_key.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
/**
2424
* Create an incomplete Datastore key.
2525
*
26-
* @param DatastoreClient $datastore
26+
* @param string $namespaceId
2727
*/
28-
function incomplete_key(DatastoreClient $datastore)
28+
function incomplete_key(string $namespaceId = null)
2929
{
30+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
3031
// [START datastore_incomplete_key]
3132
$taskKey = $datastore->key('Task');
3233
// [END datastore_incomplete_key]

0 commit comments

Comments
 (0)