Client#

Client for interacting with the Google Cloud Firestore API.

This is the base from which all interactions with the API occur.

In the hierarchy of API concepts

class google.cloud.firestore_v1beta1.client.Client(project=None, credentials=None, database='(default)')[source]#

Bases: google.cloud.client.ClientWithProject

Client for interacting with Google Cloud Firestore API.

Note

Since the Cloud Firestore API requires the gRPC transport, no _http argument is accepted by this class.

Parameters:
  • project (Optional[str]) – The project which the client acts on behalf of. If not passed, falls back to the default inferred from the environment.
  • credentials (Optional[Credentials]) – The OAuth2 Credentials to use for this client. If not passed, falls back to the default inferred from the environment.
  • database (Optional[str]) – The database name that the client targets. For now, DEFAULT_DATABASE (the default value) is the only valid database.
SCOPE = ('https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/datastore')#

The scopes required for authenticating with the Firestore service.

batch()[source]#

Get a batch instance from this client.

Returns:A “write” batch to be used for accumulating document changes and sending the changes all at once.
Return type:WriteBatch
collection(*collection_path)[source]#

Get a reference to a collection.

For a top-level collection:

>>> client.collection('top')

For a sub-collection:

>>> client.collection('mydocs/doc/subcol')
>>> # is the same as
>>> client.collection('mydocs', 'doc', 'subcol')

Sub-collections can be nested deeper in a similar fashion.

Parameters:collection_path (Tuple[str, ..]) –

Can either be

  • A single /-delimited path to a collection
  • A tuple of collection path segments
Returns:A reference to a collection in the Firestore database.
Return type:CollectionReference
document(*document_path)[source]#

Get a reference to a document in a collection.

For a top-level document:

>>> client.document('collek/shun')
>>> # is the same as
>>> client.document('collek', 'shun')

For a document in a sub-collection:

>>> client.document('mydocs/doc/subcol/child')
>>> # is the same as
>>> client.document('mydocs', 'doc', 'subcol', 'child')

Documents in sub-collections can be nested deeper in a similar fashion.

Parameters:document_path (Tuple[str, ..]) –

Can either be

  • A single /-delimited path to a document
  • A tuple of document path segments
Returns:A reference to a document in a collection.
Return type:DocumentReference
static field_path(*field_names)[source]#

Create a field path from a list of nested field names.

A field path is a .-delimited concatenation of the field names. It is used to represent a nested field. For example, in the data

data = {
   'aa': {
       'bb': {
           'cc': 10,
       },
   },
}

the field path 'aa.bb.cc' represents the data stored in data['aa']['bb']['cc'].

Parameters:field_names (Tuple[str, ..]) – The list of field names.
Returns:The .-delimited field path.
Return type:str
get_all(references, field_paths=None, transaction=None)[source]#

Retrieve a batch of documents.

Note

Documents returned by this method are not guaranteed to be returned in the same order that they are given in references.

Note

If multiple references refer to the same document, the server will only return one result.

See field_path() for more information on field paths.

If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed).

Parameters:
  • references (List[DocumentReference, ..]) – Iterable of document references to be retrieved.
  • field_paths (Optional[Iterable[str, ..]]) – An iterable of field paths (.-delimited list of field names) to use as a projection of document fields in the returned results. If no value is provided, all fields will be returned.
  • transaction (Optional[ Transaction]) – An existing transaction that these references will be retrieved in.
Yields:

.DocumentSnapshot – The next document snapshot that fulfills the query, or None if the document does not exist.

transaction(**kwargs)[source]#

Get a transaction that uses this client.

See Transaction for more information on transactions and the constructor arguments.

Parameters:kwargs (Dict[str, Any]) – The keyword arguments (other than client) to pass along to the Transaction constructor.
Returns:A transaction attached to this client.
Return type:Transaction
static write_option(**kwargs)[source]#

Create a write option for write operations.

Write operations include set(), update() and delete().

One of the following keyword arguments must be provided:

  • last_update_time (google.protobuf.timestamp_pb2.               Timestamp): A timestamp. When set, the target document must
    exist and have been last updated at that time. Protobuf update_time timestamps are typically returned from methods that perform write operations as part of a “write result” protobuf or directly.
  • exists (bool): Indicates if the document being modified
    should already exist.

Providing no argument would make the option have no effect (so it is not allowed). Providing multiple would be an apparent contradiction, since last_update_time assumes that the document was updated (it can’t have been updated if it doesn’t exist) and exists indicate that it is unknown if the document exists or not.

Parameters:kwargs (Dict[str, Any]) – The keyword arguments described above.
Raises:TypeError – If anything other than exactly one argument is provided by the caller.
google.cloud.firestore_v1beta1.client.DEFAULT_DATABASE = '(default)'#

str – The default database used in a Client.

class google.cloud.firestore_v1beta1.client.ExistsOption(exists)[source]#

Bases: google.cloud.firestore_v1beta1.client.WriteOption

Option used to assert existence on a write operation.

This will typically be created by write_option().

Parameters:exists (bool) – Indicates if the document being modified should already exist.
modify_write(write_pb, **unused_kwargs)[source]#

Modify a Write protobuf based on the state of this write option.

If:

  • exists=True, adds a precondition that requires existence
  • exists=False, adds a precondition that requires non-existence
Parameters:
  • write_pb (google.cloud.firestore_v1beta1.types.Write) – A Write protobuf instance to be modified with a precondition determined by the state of this option.
  • unused_kwargs (Dict[str, Any]) – Keyword arguments accepted by other subclasses that are unused here.
class google.cloud.firestore_v1beta1.client.LastUpdateOption(last_update_time)[source]#

Bases: google.cloud.firestore_v1beta1.client.WriteOption

Option used to assert a “last update” condition on a write operation.

This will typically be created by write_option().

Parameters:last_update_time (google.protobuf.timestamp_pb2.Timestamp) – A timestamp. When set, the target document must exist and have been last updated at that time. Protobuf update_time timestamps are typically returned from methods that perform write operations as part of a “write result” protobuf or directly.
modify_write(write_pb, **unused_kwargs)[source]#

Modify a Write protobuf based on the state of this write option.

The last_update_time is added to write_pb as an “update time” precondition. When set, the target document must exist and have been last updated at that time.

Parameters:
  • write_pb (google.cloud.firestore_v1beta1.types.Write) – A Write protobuf instance to be modified with a precondition determined by the state of this option.
  • unused_kwargs (Dict[str, Any]) – Keyword arguments accepted by other subclasses that are unused here.
class google.cloud.firestore_v1beta1.client.WriteOption[source]#

Bases: object

Option used to assert a condition on a write operation.

modify_write(write_pb, no_create_msg=None)[source]#

Modify a Write protobuf based on the state of this write option.

This is a virtual method intended to be implemented by subclasses.

Parameters:
  • write_pb (google.cloud.firestore_v1beta1.types.Write) – A Write protobuf instance to be modified with a precondition determined by the state of this option.
  • no_create_msg (Optional[str]) – A message to use to indicate that a create operation is not allowed.
Raises:

NotImplementedError – Always, this method is virtual.