Skip to content

Client Reference

Datastore client connection manager for the Google Cloud Datastore ODM.

This module provides a singleton pattern to ensure that only one instance of the google.cloud.datastore.Client is created per GCP project and database. This prevents connection overhead and memory leaks.

get_client(project=None, database=None)

Retrieve the global Google Cloud Datastore client instance.

If the client for the requested project and database has not been initialized yet, this function will create and cache a new instance. If project or database is None, it automatically infers the defaults from the host environment (e.g., the GOOGLE_CLOUD_PROJECT environment variable).

Parameters:

Name Type Description Default
project str | None

The specific GCP project ID to connect to. Defaults to None.

None
database str | None

The specific Datastore database name to connect to. Defaults to None.

None

Returns:

Type Description
Client

datastore.Client: The active Datastore client connection.

Examples:

Retrieve the default client inferred from the environment:

from google_cloud_datastore_odm.client import get_client

client = get_client()

Retrieve a client for a specific tenant database:

tenant_client = get_client(project="my-project", database="tenant-db")