Skip to content

Commit d9fcf08

Browse files
authored
chore: refactor datastore samples (#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 = <<<EOF
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<Key> $keys
25+
* @param array<string> $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<Key> $keys
25+
* @param array<string> $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'])) {

0 commit comments

Comments
 (0)