Exemple #1
0
    def on_snapshot(self, callback):
        """Monitor the documents in this collection.

        This starts a watch on this collection using a background thread. The
        provided callback is run on the snapshot of the documents.

        Args:
            callback(~.firestore.collection.CollectionSnapshot): a callback
                to run when a change occurs.

        Example:
            from google.cloud import firestore

            db = firestore.Client()
            collection_ref = db.collection(u'users')

            def on_snapshot(collection_snapshot):
                for doc in collection_snapshot.documents:
                    print(u'{} => {}'.format(doc.id, doc.to_dict()))

            # Watch this collection
            collection_watch = collection_ref.on_snapshot(on_snapshot)

            # Terminate this watch
            collection_watch.unsubscribe()
        """
        return Watch.for_query(
            query_mod.Query(self),
            callback,
            document.DocumentSnapshot,
            document.DocumentReference,
        )
Exemple #2
0
    def get(self, transaction=None):
        """Read the documents in this collection.

        This sends a ``RunQuery`` RPC and then returns an iterator which
        consumes each document returned in the stream of ``RunQueryResponse``
        messages.

        .. note::

           The underlying stream of responses will time out after
           the ``max_rpc_timeout_millis`` value set in the GAPIC
           client configuration for the ``RunQuery`` API.  Snapshots
           not consumed from the iterator before that point will be lost.

        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).

        Args:
            transaction (Optional[~.firestore_v1beta1.transaction.\
                Transaction]): An existing transaction that the query will
                run in.

        Yields:
            ~.firestore_v1beta1.document.DocumentSnapshot: The next
            document that fulfills the query.
        """
        query = query_mod.Query(self)
        return query.get(transaction=transaction)
Exemple #3
0
    def limit(self, count):
        """Create a limited query with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.limit` for
        more information on this method.

        Args:
            count (int): Maximum number of documents to return that match
                the query.

        Returns:
            ~.firestore_v1beta1.query.Query: A limited query.
        """
        query = query_mod.Query(self)
        return query.limit(count)
Exemple #4
0
    def offset(self, num_to_skip):
        """Skip to an offset in a query with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.offset` for
        more information on this method.

        Args:
            num_to_skip (int): The number of results to skip at the beginning
                of query results. (Must be non-negative.)

        Returns:
            ~.firestore_v1beta1.query.Query: An offset query.
        """
        query = query_mod.Query(self)
        return query.offset(num_to_skip)
Exemple #5
0
    def select(self, field_paths):
        """Create a "select" query with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.select` for
        more information on this method.

        Args:
            field_paths (Iterable[str, ...]): An iterable of field paths
                (``.``-delimited list of field names) to use as a projection
                of document fields in the query results.

        Returns:
            ~.firestore_v1beta1.query.Query: A "projected" query.
        """
        query = query_mod.Query(self)
        return query.select(field_paths)
Exemple #6
0
    def end_at(self, document_fields):
        """End query at a cursor with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.end_at` for
        more information on this method.

        Args:
            document_fields (Union[~.firestore_v1beta1.\
                document.DocumentSnapshot, dict, list, tuple]): a document
                snapshot or a dictionary/list/tuple of fields representing a
                query results cursor. A cursor is a collection of values that
                represent a position in a query result set.

        Returns:
            ~.firestore_v1beta1.query.Query: A query with cursor.
        """
        query = query_mod.Query(self)
        return query.end_at(document_fields)
Exemple #7
0
    def start_after(self, document_fields):
        """Start query after a cursor with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.start_after` for
        more information on this method.

        Args:
            document_fields (Union[~.firestore_v1beta1.\
                document.DocumentSnapshot, dict]): Either a document snapshot
                or a dictionary of fields representing a query results
                cursor. A cursor is a collection of values that represent a
                position in a query result set.

        Returns:
            ~.firestore_v1beta1.query.Query: A query with cursor.
        """
        query = query_mod.Query(self)
        return query.start_after(document_fields)
Exemple #8
0
    def order_by(self, field_path, **kwargs):
        """Create an "order by" query with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.order_by` for
        more information on this method.

        Args:
            field_path (str): A field path (``.``-delimited list of
                field names) on which to order the query results.
            kwargs (Dict[str, Any]): The keyword arguments to pass along
                to the query. The only supported keyword is ``direction``,
                see :meth:`~.firestore_v1beta1.query.Query.order_by` for
                more information.

        Returns:
            ~.firestore_v1beta1.query.Query: An "order by" query.
        """
        query = query_mod.Query(self)
        return query.order_by(field_path, **kwargs)
    def get(self, transaction=None):
        """Read the documents in this collection.

        This sends a ``RunQuery`` RPC and then consumes each document
        returned in the stream of ``RunQueryResponse`` messages.

        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).

        Args:
            transaction (Optional[~.firestore_v1beta1.transaction.\
                Transaction]): An existing transaction that the query will
                run in.

        Yields:
            ~.firestore_v1beta1.document.DocumentSnapshot: The next
            document that fulfills the query.
        """
        query = query_mod.Query(self)
        return query.get(transaction=transaction)
Exemple #10
0
    def where(self, field_path, op_string, value):
        """Create a "where" query with this collection as parent.

        See
        :meth:`~.firestore_v1beta1.query.Query.where` for
        more information on this method.

        Args:
            field_path (str): A field path (``.``-delimited list of
                field names) for the field to filter on.
            op_string (str): A comparison operation in the form of a string.
                Acceptable values are ``<``, ``<=``, ``==``, ``>=``
                and ``>``.
            value (Any): The value to compare the field against in the filter.
                If ``value`` is :data:`None` or a NaN, then ``==`` is the only
                allowed operation.

        Returns:
            ~.firestore_v1beta1.query.Query: A filtered query.
        """
        query = query_mod.Query(self)
        return query.where(field_path, op_string, value)