def _write_pb_for_create(document_path, document_data):
        from google.cloud.firestore_v1.proto import common_pb2
        from google.cloud.firestore_v1.proto import document_pb2
        from google.cloud.firestore_v1.proto import write_pb2
        from google.cloud.firestore_v1 import _helpers

        return write_pb2.Write(
            update=document_pb2.Document(
                name=document_path,
                fields=_helpers.encode_dict(document_data)),
            current_document=common_pb2.Precondition(exists=False),
        )
Ejemplo n.º 2
0
    def get_transform_pb(self, document_path, exists=None):
        def make_array_value(values):
            value_list = [encode_value(element) for element in values]
            return document_pb2.ArrayValue(values=value_list)

        path_field_transforms = (
            [
                (
                    path,
                    write_pb2.DocumentTransform.FieldTransform(
                        field_path=path.to_api_repr(),
                        set_to_server_value=REQUEST_TIME_ENUM,
                    ),
                )
                for path in self.server_timestamps
            ]
            + [
                (
                    path,
                    write_pb2.DocumentTransform.FieldTransform(
                        field_path=path.to_api_repr(),
                        remove_all_from_array=make_array_value(values),
                    ),
                )
                for path, values in self.array_removes.items()
            ]
            + [
                (
                    path,
                    write_pb2.DocumentTransform.FieldTransform(
                        field_path=path.to_api_repr(),
                        append_missing_elements=make_array_value(values),
                    ),
                )
                for path, values in self.array_unions.items()
            ]
        )
        field_transforms = [
            transform for path, transform in sorted(path_field_transforms)
        ]
        transform_pb = write_pb2.Write(
            transform=write_pb2.DocumentTransform(
                document=document_path, field_transforms=field_transforms
            )
        )
        if exists is not None:
            transform_pb.current_document.CopyFrom(
                common_pb2.Precondition(exists=exists)
            )

        return transform_pb
Ejemplo n.º 3
0
    def get_update_pb(self, document_path, exists=None, allow_empty_mask=False):

        if exists is not None:
            current_document = common_pb2.Precondition(exists=exists)
        else:
            current_document = None

        update_pb = write_pb2.Write(
            update=document_pb2.Document(
                name=document_path, fields=encode_dict(self.set_fields)
            ),
            update_mask=self._get_update_mask(allow_empty_mask),
            current_document=current_document,
        )

        return update_pb
Ejemplo n.º 4
0
def pb_for_delete(document_path, option):
    """Make a ``Write`` protobuf for ``delete()`` methods.

    Args:
        document_path (str): A fully-qualified document path.
        option (optional[:class:`~google.cloud.firestore_v1.client.WriteOption`]):
            A write option to make assertions / preconditions on the server
            state of the document before applying changes.

    Returns:
        google.cloud.firestore_v1.types.Write: A
        ``Write`` protobuf instance for the ``delete()``.
    """
    write_pb = write_pb2.Write(delete=document_path)
    if option is not None:
        option.modify_write(write_pb)

    return write_pb