Cloud Datastore V1 Client - Class DatastoreClient (1.18.1)

Reference documentation and code samples for the Cloud Datastore V1 Client class DatastoreClient.

Service Description: Each RPC normalizes the partition IDs of the keys in its input entities, and always returns entities with keys with normalized partition IDs.

This applies to all keys and entities, including those in values, except keys with both an empty path and an empty or unset partition ID. Normalization of input keys sets the project ID (if not already set) to the project ID from the request.

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:

$datastoreClient = new Google\Cloud\Datastore\V1\DatastoreClient();
try {
    $projectId = 'project_id';
    $keys = [];
    $response = $datastoreClient->allocateIds($projectId, $keys);
} finally {
    $datastoreClient->close();
}

Methods

__construct

Constructor.

Parameters
NameDescription
options array

Optional. Options for configuring the service API wrapper.

↳ apiEndpoint string

The address of the API remote host. May optionally include the port, formatted as "

↳ credentials string|array|FetchAuthTokenInterface|CredentialsWrapper

The credentials to be used by the client to authorize API calls. This option accepts either a path to a credentials file, or a decoded credentials file as a PHP array. Advanced usage: In addition, this option can also accept a pre-constructed Google\Auth\FetchAuthTokenInterface object or Google\ApiCore\CredentialsWrapper object. Note that when one of these objects are provided, any settings in $credentialsConfig will be ignored.

↳ credentialsConfig array

Options used to configure credentials, including auth token caching, for the client. For a full list of supporting configuration options, see Google\ApiCore\CredentialsWrapper::build() .

↳ disableRetries bool

Determines whether or not retries defined by the client configuration should be disabled. Defaults to false.

↳ clientConfig string|array

Client method configuration, including retry settings. This option can be either a path to a JSON file, or a PHP array containing the decoded JSON data. By default this settings points to the default client config file, which is provided in the resources folder.

↳ transport string|TransportInterface

The transport used for executing network requests. May be either the string rest or grpc. Defaults to grpc if gRPC support is detected on the system. Advanced usage: Additionally, it is possible to pass in an already instantiated Google\ApiCore\Transport\TransportInterface object. Note that when this object is provided, any settings in $transportConfig, and any $apiEndpoint setting, will be ignored.

↳ transportConfig array

Configuration options that will be used to construct the transport. Options for each supported transport type should be passed in a key for that transport. For example: $transportConfig = [ 'grpc' => [...], 'rest' => [...], ]; See the Google\ApiCore\Transport\GrpcTransport::build() and Google\ApiCore\Transport\RestTransport::build() methods for the supported options.

↳ clientCertSource callable

A callable which returns the client cert as a string. This can be used to provide a certificate and private key to the transport layer for mTLS.

allocateIds

Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

keys array<Google\Cloud\Datastore\V1\Key>

Required. A list of keys with incomplete key paths for which to allocate IDs. No key may be reserved/read-only.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\AllocateIdsResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\AllocateIdsResponse;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\Key;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function allocate_ids_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Prepare any non-scalar elements to be passed along with the request.
    $keys = [new Key()];

    // Call the API and handle any network failures.
    try {
        /** @var AllocateIdsResponse $response */
        $response = $datastoreClient->allocateIds($projectId, $keys);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    allocate_ids_sample($projectId);
}

beginTransaction

Begins a new transaction.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ transactionOptions TransactionOptions

Options for a new transaction.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\BeginTransactionResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\BeginTransactionResponse;
use Google\Cloud\Datastore\V1\DatastoreClient;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function begin_transaction_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Call the API and handle any network failures.
    try {
        /** @var BeginTransactionResponse $response */
        $response = $datastoreClient->beginTransaction($projectId);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    begin_transaction_sample($projectId);
}

commit

Commits a transaction, optionally creating, deleting or modifying some entities.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

mode int

The type of commit to perform. Defaults to TRANSACTIONAL. For allowed values, use constants defined on {@see \Google\Cloud\Datastore\V1\CommitRequest\Mode}

mutations array<Google\Cloud\Datastore\V1\Mutation>

The mutations to perform.

When mode is TRANSACTIONAL, mutations affecting a single entity are applied in order. The following sequences of mutations affecting a single entity are not permitted in a single Commit request:

  • insert followed by insert
  • update followed by insert
  • upsert followed by insert
  • delete followed by update

When mode is NON_TRANSACTIONAL, no two mutations may affect a single entity.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ transaction string

The identifier of the transaction associated with the commit. A transaction identifier is returned by a call to Datastore.BeginTransaction.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\CommitResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\CommitRequest\Mode;
use Google\Cloud\Datastore\V1\CommitResponse;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\Mutation;

/**
 * @param string $projectId The ID of the project against which to make the request.
 * @param int    $mode      The type of commit to perform. Defaults to `TRANSACTIONAL`.
 */
function commit_sample(string $projectId, int $mode): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Prepare any non-scalar elements to be passed along with the request.
    $mutations = [new Mutation()];

    // Call the API and handle any network failures.
    try {
        /** @var CommitResponse $response */
        $response = $datastoreClient->commit($projectId, $mode, $mutations);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';
    $mode = Mode::MODE_UNSPECIFIED;

    commit_sample($projectId, $mode);
}

lookup

Looks up entities by key.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

keys array<Google\Cloud\Datastore\V1\Key>

Required. Keys of entities to look up.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ readOptions ReadOptions

The options for this lookup request.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\LookupResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\Key;
use Google\Cloud\Datastore\V1\LookupResponse;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function lookup_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Prepare any non-scalar elements to be passed along with the request.
    $keys = [new Key()];

    // Call the API and handle any network failures.
    try {
        /** @var LookupResponse $response */
        $response = $datastoreClient->lookup($projectId, $keys);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    lookup_sample($projectId);
}

reserveIds

Prevents the supplied keys' IDs from being auto-allocated by Cloud Datastore.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

keys array<Google\Cloud\Datastore\V1\Key>

Required. A list of keys with complete key paths whose numeric IDs should not be auto-allocated.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\ReserveIdsResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\Key;
use Google\Cloud\Datastore\V1\ReserveIdsResponse;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function reserve_ids_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Prepare any non-scalar elements to be passed along with the request.
    $keys = [new Key()];

    // Call the API and handle any network failures.
    try {
        /** @var ReserveIdsResponse $response */
        $response = $datastoreClient->reserveIds($projectId, $keys);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    reserve_ids_sample($projectId);
}

rollback

Rolls back a transaction.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

transaction string

Required. The transaction identifier, returned by a call to Datastore.BeginTransaction.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\RollbackResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\RollbackResponse;

/**
 * @param string $projectId   The ID of the project against which to make the request.
 * @param string $transaction The transaction identifier, returned by a call to
 *                            [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction].
 */
function rollback_sample(string $projectId, string $transaction): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Call the API and handle any network failures.
    try {
        /** @var RollbackResponse $response */
        $response = $datastoreClient->rollback($projectId, $transaction);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';
    $transaction = '...';

    rollback_sample($projectId, $transaction);
}

runAggregationQuery

Runs an aggregation query.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ partitionId PartitionId

Entities are partitioned into subsets, identified by a partition ID. Queries are scoped to a single partition. This partition ID is normalized with the standard default context partition ID.

↳ readOptions ReadOptions

The options for this query.

↳ aggregationQuery AggregationQuery

The query to run.

↳ gqlQuery GqlQuery

The GQL query to run. This query must be an aggregation query.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\RunAggregationQueryResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\RunAggregationQueryResponse;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function run_aggregation_query_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Call the API and handle any network failures.
    try {
        /** @var RunAggregationQueryResponse $response */
        $response = $datastoreClient->runAggregationQuery($projectId);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    run_aggregation_query_sample($projectId);
}

runQuery

Queries for entities.

Parameters
NameDescription
projectId string

Required. The ID of the project against which to make the request.

partitionId Google\Cloud\Datastore\V1\PartitionId

Entities are partitioned into subsets, identified by a partition ID. Queries are scoped to a single partition. This partition ID is normalized with the standard default context partition ID.

optionalArgs array

Optional.

↳ databaseId string

The ID of the database against which to make the request. '(default)' is not allowed; please use empty string '' to refer the default database.

↳ readOptions ReadOptions

The options for this query.

↳ query Query

The query to run.

↳ gqlQuery GqlQuery

The GQL query to run. This query must be a non-aggregation query.

↳ retrySettings RetrySettings|array

Retry settings to use for this call. Can be a Google\ApiCore\RetrySettings object, or an associative array of retry settings parameters. See the documentation on Google\ApiCore\RetrySettings for example usage.

Returns
TypeDescription
Google\Cloud\Datastore\V1\RunQueryResponse
Example
use Google\ApiCore\ApiException;
use Google\Cloud\Datastore\V1\DatastoreClient;
use Google\Cloud\Datastore\V1\PartitionId;
use Google\Cloud\Datastore\V1\RunQueryResponse;

/**
 * @param string $projectId The ID of the project against which to make the request.
 */
function run_query_sample(string $projectId): void
{
    // Create a client.
    $datastoreClient = new DatastoreClient();

    // Prepare any non-scalar elements to be passed along with the request.
    $partitionId = new PartitionId();

    // Call the API and handle any network failures.
    try {
        /** @var RunQueryResponse $response */
        $response = $datastoreClient->runQuery($projectId, $partitionId);
        printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
    } catch (ApiException $ex) {
        printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
    }
}

/**
 * This sample has been automatically generated and should be regarded as a code
 * template only. It will require modifications to work:
 *  - It may require correct/in-range values for request initialization.
 *  - It may require specifying regional endpoints when creating the service client,
 *    please see the apiEndpoint client configuration option for more details.
 */
function callSample(): void
{
    $projectId = '[PROJECT_ID]';

    run_query_sample($projectId);
}

Constants

SERVICE_NAME

Value: 'google.datastore.v1.Datastore'

The name of the service.

SERVICE_ADDRESS

Value: 'datastore.googleapis.com'

The default address of the service.

DEFAULT_SERVICE_PORT

Value: 443

The default port of the service.

CODEGEN_NAME

Value: 'gapic'

The name of the code generator, to be included in the agent header.