Class FirestoreTemplate

java.lang.Object
com.google.cloud.spring.data.firestore.FirestoreTemplate
All Implemented Interfaces:
FirestoreReactiveOperations

public class FirestoreTemplate extends Object implements FirestoreReactiveOperations
An implementation of FirestoreReactiveOperations.
Since:
1.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Constant representing the special property to use when querying by a document ID.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FirestoreTemplate(com.google.firestore.v1.FirestoreGrpc.FirestoreStub firestoreStub, String parent, FirestoreClassMapper classMapper, FirestoreMappingContext mappingContext)
    Constructor for FirestoreTemplate.
  • Method Summary

    Modifier and Type
    Method
    Description
    buildResourceName(FirestorePersistentEntity<?> persistentEntity, String resource)
    Builds the full reference path to a resource such as a document or a sub-collection.
    <T> reactor.core.publisher.Mono<Long>
    count(Class<T> entityClass)
    Count entities of the given domain.
    <T> reactor.core.publisher.Mono<Long>
    count(Class<T> entityClass, com.google.firestore.v1.StructuredQuery.Builder queryBuilder)
    Count entities of the given domain corresponding to the predicates given in the query builder.
    <T> reactor.core.publisher.Mono<Void>
    delete(org.reactivestreams.Publisher<T> entityPublisher)
    Delete entities provided by publisher.
    <T> reactor.core.publisher.Mono<Long>
    deleteAll(Class<T> clazz)
    Delete all entities of a given domain type.
    reactor.core.publisher.Mono<Void>
    deleteById(org.reactivestreams.Publisher<String> idPublisher, Class<?> entityClass)
    Delete entities of a given domain type using ids published by producer.
    <T> reactor.core.publisher.Flux<T>
    execute(com.google.firestore.v1.StructuredQuery.Builder builder, Class<T> entityType)
    Executes a query represented as query builder and returns results of the given domain type.
    <T> reactor.core.publisher.Mono<Boolean>
    existsById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
    Test if the entity of the given domain type with a given id exists.
    <T> reactor.core.publisher.Flux<T>
    findAll(Class<T> clazz)
    Get all the entities of the given domain type.
    <T> reactor.core.publisher.Flux<T>
    findAllById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
    Get an entity of the given domain type by id.
    <T> reactor.core.publisher.Mono<T>
    findById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
    Get an entity of the given domain type by id.
     
     
    int
     
     
    <T> reactor.core.publisher.Mono<T>
    save(T entity)
    Saves an instance of an object to Cloud Firestore.
    <T> reactor.core.publisher.Flux<T>
    saveAll(org.reactivestreams.Publisher<T> instances)
    Saves multiple objects to Cloud Firestore.
    void
    setWriteBufferSize(int bufferWriteSize)
    Sets how many entities to include in an insert/update/delete buffered operation.
    void
    Sets the Duration for how long to wait for the entity buffer to fill before sending the buffered entities to Firestore for insert/update/delete operations.
    withParent(String id, Class<?> entityClass)
    Creates FirestoreReactiveOperations object with a specified parent document.
    withParent(T parent)
    Creates FirestoreReactiveOperations object with a specified parent document.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NAME_FIELD

      public static final String NAME_FIELD
      Constant representing the special property to use when querying by a document ID.
  • Constructor Details

    • FirestoreTemplate

      public FirestoreTemplate(com.google.firestore.v1.FirestoreGrpc.FirestoreStub firestoreStub, String parent, FirestoreClassMapper classMapper, FirestoreMappingContext mappingContext)
      Constructor for FirestoreTemplate.
      Parameters:
      firestoreStub - Firestore gRPC stub
      parent - the parent resource. For example: projects/{project_id}/databases/{database_id}/documents
      classMapper - a FirestoreClassMapper used for conversion
      mappingContext - Mapping Context
  • Method Details

    • setWriteBufferTimeout

      public void setWriteBufferTimeout(Duration bufferTimeout)
      Sets the Duration for how long to wait for the entity buffer to fill before sending the buffered entities to Firestore for insert/update/delete operations.
      Parameters:
      bufferTimeout - duration to wait for entity buffer to fill before sending to Firestore. (default = 500ms)
    • getWriteBufferTimeout

      public Duration getWriteBufferTimeout()
    • setWriteBufferSize

      public void setWriteBufferSize(int bufferWriteSize)
      Sets how many entities to include in an insert/update/delete buffered operation.

      The maximum buffer size is 500. In most cases users should leave this at the maximum value.

      Parameters:
      bufferWriteSize - the entity buffer size for buffered operations (default = 500)
    • getWriteBufferSize

      public int getWriteBufferSize()
    • existsById

      public <T> reactor.core.publisher.Mono<Boolean> existsById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
      Description copied from interface: FirestoreReactiveOperations
      Test if the entity of the given domain type with a given id exists. Uses the first emitted element to perform the query.
      Specified by:
      existsById in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      idPublisher - publisher that provides an id.
      entityClass - the domain type of the entity.
      Returns:
      Mono emitting true if an entity with the given id exists, false otherwise.
    • findById

      public <T> reactor.core.publisher.Mono<T> findById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
      Description copied from interface: FirestoreReactiveOperations
      Get an entity of the given domain type by id. Uses the first emitted element to perform the query.
      Specified by:
      findById in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      idPublisher - publisher that provides an id.
      entityClass - the domain type of the entity.
      Returns:
      Mono emitting the found entity.
    • findAllById

      public <T> reactor.core.publisher.Flux<T> findAllById(org.reactivestreams.Publisher<String> idPublisher, Class<T> entityClass)
      Description copied from interface: FirestoreReactiveOperations
      Get an entity of the given domain type by id.
      Specified by:
      findAllById in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      idPublisher - publisher that provides ids.
      entityClass - the domain type of the entity.
      Returns:
      Flux emitting the found entities.
    • save

      public <T> reactor.core.publisher.Mono<T> save(T entity)
      Description copied from interface: FirestoreReactiveOperations
      Saves an instance of an object to Cloud Firestore. Behaves as an insert only (not update).
      Specified by:
      save in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type of the object to save.
      Parameters:
      entity - the instance to save.
      Returns:
      Mono emitting the saved entity.
    • saveAll

      public <T> reactor.core.publisher.Flux<T> saveAll(org.reactivestreams.Publisher<T> instances)
      Saves multiple objects to Cloud Firestore. Not atomic. Behaves as insert or update.

      The buffer size and buffer timeout settings for saveAll(org.reactivestreams.Publisher<T>) can be modified by calling setWriteBufferSize(int) and setWriteBufferTimeout(java.time.Duration).

      Specified by:
      saveAll in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type of the objects to save.
      Parameters:
      instances - the objects to save.
      Returns:
      Flux emitting the saved entities.
    • findAll

      public <T> reactor.core.publisher.Flux<T> findAll(Class<T> clazz)
      Description copied from interface: FirestoreReactiveOperations
      Get all the entities of the given domain type.
      Specified by:
      findAll in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      clazz - the domain type to get.
      Returns:
      Mono emitting the found entities.
    • count

      public <T> reactor.core.publisher.Mono<Long> count(Class<T> entityClass)
      Description copied from interface: FirestoreReactiveOperations
      Count entities of the given domain. Note that Firestore doesn't support "count" operation natively, so id query will be executed and all ids will be retrieved so they could be counted.
      Specified by:
      count in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      entityClass - the domain type of entities.
      Returns:
      Mono emitting the number of entities.
    • count

      public <T> reactor.core.publisher.Mono<Long> count(Class<T> entityClass, com.google.firestore.v1.StructuredQuery.Builder queryBuilder)
      Description copied from interface: FirestoreReactiveOperations
      Count entities of the given domain corresponding to the predicates given in the query builder. Note that Firestore doesn't support "count" operation natively, so id query will be executed and all ids will be retrieved so they could be counted.
      Specified by:
      count in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      entityClass - the domain type of entities.
      queryBuilder - the query builder that contains predicates; note that id projection and the collection name will be set during execution
      Returns:
      Mono emitting the number of entities.
    • deleteAll

      public <T> reactor.core.publisher.Mono<Long> deleteAll(Class<T> clazz)
      Delete all entities of a given domain type.

      The buffer size and buffer timeout settings for saveAll(org.reactivestreams.Publisher<T>) can be modified by calling setWriteBufferSize(int) and setWriteBufferTimeout(java.time.Duration).

      Specified by:
      deleteAll in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      clazz - the domain type to delete from Cloud Datastore.
      Returns:
      Mono emitting the number of deleted entities.
    • delete

      public <T> reactor.core.publisher.Mono<Void> delete(org.reactivestreams.Publisher<T> entityPublisher)
      Delete entities provided by publisher.

      The buffer size and buffer timeout settings for saveAll(org.reactivestreams.Publisher<T>) can be modified by calling setWriteBufferSize(int) and setWriteBufferTimeout(java.time.Duration).

      Specified by:
      delete in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      entityPublisher - publisher that provides entities to be removed.
      Returns:
      Mono signaling when operation has completed.
    • deleteById

      public reactor.core.publisher.Mono<Void> deleteById(org.reactivestreams.Publisher<String> idPublisher, Class<?> entityClass)
      Delete entities of a given domain type using ids published by producer.

      The buffer size and buffer timeout settings for saveAll(org.reactivestreams.Publisher<T>) can be modified by calling setWriteBufferSize(int) and setWriteBufferTimeout(java.time.Duration).

      Specified by:
      deleteById in interface FirestoreReactiveOperations
      Parameters:
      idPublisher - publisher that provides ids of entities to be removed.
      entityClass - the domain type of entities.
      Returns:
      Mono signaling when operation has completed.
    • execute

      public <T> reactor.core.publisher.Flux<T> execute(com.google.firestore.v1.StructuredQuery.Builder builder, Class<T> entityType)
      Description copied from interface: FirestoreReactiveOperations
      Executes a query represented as query builder and returns results of the given domain type.
      Specified by:
      execute in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the domain type.
      Parameters:
      builder - the query builder.
      entityType - the domain type of entities.
      Returns:
      Flux emitting the found entities.
    • withParent

      public FirestoreReactiveOperations withParent(String id, Class<?> entityClass)
      Description copied from interface: FirestoreReactiveOperations
      Creates FirestoreReactiveOperations object with a specified parent document. All operations and queries will be scoped to that parent document's subcollections. By default, FirestoreReactiveOperations uses the root document as the parent.
      Specified by:
      withParent in interface FirestoreReactiveOperations
      Parameters:
      id - the id of the Document entity
      entityClass - the class of the Document entity
      Returns:
      template with a given parent.
    • withParent

      public <T> FirestoreReactiveOperations withParent(T parent)
      Description copied from interface: FirestoreReactiveOperations
      Creates FirestoreReactiveOperations object with a specified parent document. The parent doesn't have to exist in Firestore, but should have a non-empty id field. All operations and queries will be scoped to that parent's subcollections. By default, FirestoreReactiveOperations uses the root document as the parent.
      Specified by:
      withParent in interface FirestoreReactiveOperations
      Type Parameters:
      T - the type param of the parent.
      Parameters:
      parent - the query builder.
      Returns:
      template with a given parent.
    • buildResourceName

      public String buildResourceName(FirestorePersistentEntity<?> persistentEntity, String resource)
      Description copied from interface: FirestoreReactiveOperations
      Builds the full reference path to a resource such as a document or a sub-collection. This method uses the root collection as the parent (projects/{project_id}/databases/{database_id}/documents/{collection_name}/)
      Specified by:
      buildResourceName in interface FirestoreReactiveOperations
      Parameters:
      persistentEntity - a Firestore persistent entity
      resource - the resource name or path to the resource.
      Returns:
      a String representing the reference path.
    • getMappingContext

      public FirestoreMappingContext getMappingContext()
    • getClassMapper

      public FirestoreClassMapper getClassMapper()