Class CosmosTemplate

java.lang.Object
com.azure.spring.data.cosmos.core.CosmosTemplate
All Implemented Interfaces:
CosmosOperations, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

public class CosmosTemplate extends Object implements CosmosOperations, org.springframework.context.ApplicationContextAware
Template class for cosmos db
  • Constructor Summary

    Constructors
    Constructor
    Description
    CosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)
    Initialization
    CosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler)
    Initialization
    CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)
    Initialization
    CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler)
    Initialization
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> long
    count(com.azure.cosmos.models.SqlQuerySpec querySpec, String containerName)
    Count
    <T> long
    count(CosmosQuery query, String containerName)
    Count
    long
    count(String containerName)
    Count
    com.azure.cosmos.models.CosmosContainerProperties
    Creates container if not exists
    <T> Iterable<T>
    delete(CosmosQuery query, Class<T> domainType, String containerName)
    Delete the DocumentQuery, need to query the domains at first, then delete the item from the result.
    void
    deleteAll(String containerName, Class<?> domainType)
    Delete the DocumentQuery, delete all the items in the given container.
    void
    deleteById(String containerName, Object id, com.azure.cosmos.models.PartitionKey partitionKey)
    Deletes the item by id and partition key.
    void
    deleteContainer(String containerName)
    Delete container
    <T> void
    deleteEntity(String containerName, T entity)
    Deletes the entity
    <T> Boolean
    exists(CosmosQuery query, Class<T> domainType, String containerName)
    Checks if document query items exist
    <T> Iterable<T>
    find(CosmosQuery query, Class<T> domainType, String containerName)
    Finds the document query items
    <T> Iterable<T>
    findAll(com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType)
    Find the DocumentQuery, find all the items specified by domain type in the given container.
    <T> Iterable<T>
    findAll(Class<T> domainType)
    Find the DocumentQuery, find all the items specified by domain type.
    <T> Iterable<T>
    findAll(String containerName, Class<T> domainType)
    Find the DocumentQuery, find all the items specified by domain type in the given container.
    <T> org.springframework.data.domain.Page<T>
    findAll(org.springframework.data.domain.Pageable pageable, Class<T> domainType, String containerName)
    Find all items in a given container with partition key
    <T> T
    findById(Object id, Class<T> domainType)
    Finds item by id
    <T> T
    findById(Object id, Class<T> domainType, com.azure.cosmos.models.PartitionKey partitionKey)
    Finds item by id
    <T> T
    findById(String containerName, Object id, Class<T> domainType)
    Finds item by id
    <T, ID> Iterable<T>
    findByIds(Iterable<ID> ids, Class<T> domainType, String containerName)
    Find by ids
    getContainerName(Class<?> domainType)
    To get container name by domainType
    Check if an overridden version of containerName is present, and if so, return it
    com.azure.cosmos.models.CosmosContainerProperties
    Get properties for specified container
    To get converter
    <T> T
    insert(String containerName, T objectToSave)
    Inserts item into the given container
    <T> T
    insert(String containerName, T objectToSave, com.azure.cosmos.models.PartitionKey partitionKey)
    Inserts item into the given container
    <T> T
    insert(T objectToSave, com.azure.cosmos.models.PartitionKey partitionKey)
    Inserts item
    <T> org.springframework.data.domain.Page<T>
    paginationQuery(CosmosQuery query, Class<T> domainType, String containerName)
    Pagination query
    <T> T
    patch(Object id, com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType, com.azure.cosmos.models.CosmosPatchOperations patchOperations)
    Patches item applies partial update (patch) to an item
    <T> T
    patch(Object id, com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType, com.azure.cosmos.models.CosmosPatchOperations patchOperations, com.azure.cosmos.models.CosmosPatchItemRequestOptions options)
    applies partial update (patch) to an item with CosmosPatchItemRequestOptions
    com.azure.cosmos.models.CosmosContainerProperties
    replaceContainerProperties(String containerName, com.azure.cosmos.models.CosmosContainerProperties properties)
    Replace container properties for the specified container
    <T> org.springframework.data.domain.Page<T>
    runPaginationQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Pageable pageable, Class<?> domainType, Class<T> returnType)
    Run the query.
    <T> Iterable<T>
    runQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType)
    Run the query.
    <T> Iterable<T>
    runQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Sort sort, Class<?> domainType, Class<T> returnType)
    Run the query.
    <T> org.springframework.data.domain.Slice<T>
    runSliceQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Pageable pageable, Class<?> domainType, Class<T> returnType)
    Run custom SQL query
    void
    setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
    Sets the application context
    <T> org.springframework.data.domain.Slice<T>
    sliceQuery(CosmosQuery query, Class<T> domainType, String containerName)
    Slice query
    <T> void
    upsert(String containerName, T object)
    Upserts an item into container with partition key
    <T> void
    upsert(T object)
    Upserts an item with partition key
    <T> T
    upsertAndReturnEntity(String containerName, T object)
    Upserts an item and return item properties

    Methods inherited from class java.lang.Object

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

    • CosmosTemplate

      public CosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler)
      Initialization
      Parameters:
      client - must not be null
      databaseName - must not be null
      cosmosConfig - must not be null
      mappingCosmosConverter - must not be null
      cosmosAuditingHandler - can be null
    • CosmosTemplate

      public CosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)
      Initialization
      Parameters:
      client - must not be null
      databaseName - must not be null
      cosmosConfig - must not be null
      mappingCosmosConverter - must not be null
    • CosmosTemplate

      public CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler)
      Initialization
      Parameters:
      cosmosFactory - must not be null
      cosmosConfig - must not be null
      mappingCosmosConverter - must not be null
      cosmosAuditingHandler - can be null
    • CosmosTemplate

      public CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)
      Initialization
      Parameters:
      cosmosFactory - must not be null
      cosmosConfig - must not be null
      mappingCosmosConverter - must not be null
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Sets the application context
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Parameters:
      applicationContext - must not be null
      Throws:
      org.springframework.beans.BeansException - the bean exception
    • insert

      public <T> T insert(T objectToSave, com.azure.cosmos.models.PartitionKey partitionKey)
      Inserts item
      Specified by:
      insert in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      objectToSave - must not be null
      partitionKey - must not be null
      Returns:
      the inserted item
    • insert

      public <T> T insert(String containerName, T objectToSave)
      Inserts item into the given container
      Specified by:
      insert in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      containerName - must not be null
      objectToSave - must not be null
      Returns:
      the inserted item
    • insert

      public <T> T insert(String containerName, T objectToSave, com.azure.cosmos.models.PartitionKey partitionKey)
      Inserts item into the given container
      Specified by:
      insert in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      containerName - must not be null
      objectToSave - must not be null
      partitionKey - must not be null
      Returns:
      the inserted item
    • patch

      public <T> T patch(Object id, com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType, com.azure.cosmos.models.CosmosPatchOperations patchOperations)
      Patches item applies partial update (patch) to an item
      Specified by:
      patch in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      id - must not be null
      partitionKey - must not be null
      patchOperations - must not be null
      domainType - must not be null
      Returns:
      the patched item
    • patch

      public <T> T patch(Object id, com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType, com.azure.cosmos.models.CosmosPatchOperations patchOperations, com.azure.cosmos.models.CosmosPatchItemRequestOptions options)
      applies partial update (patch) to an item with CosmosPatchItemRequestOptions
      Specified by:
      patch in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      id - must not be null
      partitionKey - must not be null
      domainType - must not be null
      patchOperations - must not be null
      options - Optional CosmosPatchItemRequestOptions, e.g. options.setFilterPredicate("FROM products p WHERE p.used = false");
      Returns:
      the patched item
    • findById

      public <T> T findById(Object id, Class<T> domainType)
      Finds item by id
      Specified by:
      findById in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      id - must not be null
      domainType - must not be null
      Returns:
      found item
    • findById

      public <T> T findById(Object id, Class<T> domainType, com.azure.cosmos.models.PartitionKey partitionKey)
      Description copied from interface: CosmosOperations
      Finds item by id
      Specified by:
      findById in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      id - must not be null
      domainType - must not be null
      partitionKey - must not be null
      Returns:
      found item
    • findById

      public <T> T findById(String containerName, Object id, Class<T> domainType)
      Finds item by id
      Specified by:
      findById in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      containerName - must not be null
      id - must not be null
      domainType - must not be null
      Returns:
      found item
    • upsert

      public <T> void upsert(T object)
      Upserts an item with partition key
      Specified by:
      upsert in interface CosmosOperations
      Type Parameters:
      T - type of upsert object
      Parameters:
      object - upsert object
    • upsert

      public <T> void upsert(String containerName, T object)
      Upserts an item into container with partition key
      Specified by:
      upsert in interface CosmosOperations
      Type Parameters:
      T - type of upsert object
      Parameters:
      containerName - the container name
      object - upsert object
    • upsertAndReturnEntity

      public <T> T upsertAndReturnEntity(String containerName, T object)
      Upserts an item and return item properties
      Specified by:
      upsertAndReturnEntity in interface CosmosOperations
      Type Parameters:
      T - type of upsert object
      Parameters:
      containerName - the container name
      object - upsert object
      Returns:
      upsert object entity
    • findAll

      public <T> Iterable<T> findAll(Class<T> domainType)
      Find the DocumentQuery, find all the items specified by domain type.
      Specified by:
      findAll in interface CosmosOperations
      Type Parameters:
      T - class type of domain
      Parameters:
      domainType - the domain type
      Returns:
      found results in a List
    • findAll

      public <T> Iterable<T> findAll(String containerName, Class<T> domainType)
      Find the DocumentQuery, find all the items specified by domain type in the given container.
      Specified by:
      findAll in interface CosmosOperations
      Type Parameters:
      T - class type of domain
      Parameters:
      containerName - the container name
      domainType - the domain type
      Returns:
      found results in a List
    • findAll

      public <T> Iterable<T> findAll(com.azure.cosmos.models.PartitionKey partitionKey, Class<T> domainType)
      Description copied from interface: CosmosOperations
      Find the DocumentQuery, find all the items specified by domain type in the given container.
      Specified by:
      findAll in interface CosmosOperations
      Type Parameters:
      T - class type of domain
      Parameters:
      partitionKey - the partition key
      domainType - the domain type
      Returns:
      results in an Iterable
    • deleteAll

      public void deleteAll(@NonNull String containerName, @NonNull Class<?> domainType)
      Delete the DocumentQuery, delete all the items in the given container.
      Specified by:
      deleteAll in interface CosmosOperations
      Parameters:
      containerName - Container name of database
      domainType - the domain type
    • deleteContainer

      public void deleteContainer(@NonNull String containerName)
      Description copied from interface: CosmosOperations
      Delete container
      Specified by:
      deleteContainer in interface CosmosOperations
      Parameters:
      containerName - the container name
    • getContainerName

      public String getContainerName(Class<?> domainType)
      Description copied from interface: CosmosOperations
      To get container name by domainType
      Specified by:
      getContainerName in interface CosmosOperations
      Parameters:
      domainType - class type
      Returns:
      String
    • getContainerNameOverride

      public String getContainerNameOverride(String containerName)
      Check if an overridden version of containerName is present, and if so, return it
      Parameters:
      containerName - Container name of database
      Returns:
      containerName
    • createContainerIfNotExists

      public com.azure.cosmos.models.CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation<?,?> information)
      Description copied from interface: CosmosOperations
      Creates container if not exists
      Specified by:
      createContainerIfNotExists in interface CosmosOperations
      Parameters:
      information - CosmosEntityInformation
      Returns:
      CosmosContainerProperties
    • getContainerProperties

      public com.azure.cosmos.models.CosmosContainerProperties getContainerProperties(String containerName)
      Description copied from interface: CosmosOperations
      Get properties for specified container
      Specified by:
      getContainerProperties in interface CosmosOperations
      Parameters:
      containerName - String
      Returns:
      CosmosContainerProperties
    • replaceContainerProperties

      public com.azure.cosmos.models.CosmosContainerProperties replaceContainerProperties(String containerName, com.azure.cosmos.models.CosmosContainerProperties properties)
      Description copied from interface: CosmosOperations
      Replace container properties for the specified container
      Specified by:
      replaceContainerProperties in interface CosmosOperations
      Parameters:
      containerName - String
      properties - CosmosContainerProperties
      Returns:
      CosmosContainerProperties
    • deleteById

      public void deleteById(String containerName, Object id, com.azure.cosmos.models.PartitionKey partitionKey)
      Deletes the item by id and partition key.
      Specified by:
      deleteById in interface CosmosOperations
      Parameters:
      containerName - Container name of database
      id - item id
      partitionKey - the partition key
    • deleteEntity

      public <T> void deleteEntity(String containerName, T entity)
      Deletes the entity
      Specified by:
      deleteEntity in interface CosmosOperations
      Type Parameters:
      T - type class of domain type
      Parameters:
      containerName - the container name
      entity - the entity object
    • findByIds

      public <T, ID> Iterable<T> findByIds(Iterable<ID> ids, Class<T> domainType, String containerName)
      Description copied from interface: CosmosOperations
      Find by ids
      Specified by:
      findByIds in interface CosmosOperations
      Type Parameters:
      T - type of domainType
      ID - type of ID
      Parameters:
      ids - iterable of ids
      domainType - type class
      containerName - the container name
      Returns:
      results in an Iterable
    • find

      public <T> Iterable<T> find(@NonNull CosmosQuery query, @NonNull Class<T> domainType, String containerName)
      Finds the document query items
      Specified by:
      find in interface CosmosOperations
      Type Parameters:
      T - class of domainType
      Parameters:
      query - The representation for query method.
      domainType - Class of domain
      containerName - Container name of database
      Returns:
      All the found items as List.
    • exists

      public <T> Boolean exists(@NonNull CosmosQuery query, @NonNull Class<T> domainType, String containerName)
      Checks if document query items exist
      Specified by:
      exists in interface CosmosOperations
      Type Parameters:
      T - class of domainType
      Parameters:
      query - The representation for query method.
      domainType - Class of domain
      containerName - Container name of database
      Returns:
      if items exist
    • delete

      public <T> Iterable<T> delete(@NonNull CosmosQuery query, @NonNull Class<T> domainType, @NonNull String containerName)
      Delete the DocumentQuery, need to query the domains at first, then delete the item from the result. The cosmos db Sql API do _NOT_ support DELETE query, we cannot add one DeleteQueryGenerator.
      Specified by:
      delete in interface CosmosOperations
      Type Parameters:
      T - class of domainType
      Parameters:
      query - The representation for query method.
      domainType - Class of domain
      containerName - Container name of database
      Returns:
      All the deleted items as List.
    • findAll

      public <T> org.springframework.data.domain.Page<T> findAll(org.springframework.data.domain.Pageable pageable, Class<T> domainType, String containerName)
      Description copied from interface: CosmosOperations
      Find all items in a given container with partition key
      Specified by:
      findAll in interface CosmosOperations
      Type Parameters:
      T - type of domainType
      Parameters:
      pageable - Pageable object
      domainType - the domainType
      containerName - the container name
      Returns:
      results as Page
    • runPaginationQuery

      public <T> org.springframework.data.domain.Page<T> runPaginationQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Pageable pageable, Class<?> domainType, Class<T> returnType)
      Description copied from interface: CosmosOperations
      Run the query.
      Specified by:
      runPaginationQuery in interface CosmosOperations
      Type Parameters:
      T - the type parameter
      Parameters:
      querySpec - the query spec
      pageable - the pageable
      domainType - the domain type
      returnType - the return type
      Returns:
      the Page
    • paginationQuery

      public <T> org.springframework.data.domain.Page<T> paginationQuery(CosmosQuery query, Class<T> domainType, String containerName)
      Description copied from interface: CosmosOperations
      Pagination query
      Specified by:
      paginationQuery in interface CosmosOperations
      Type Parameters:
      T - type class of domainType
      Parameters:
      query - the document query
      domainType - type class
      containerName - the container name
      Returns:
      results as Page
    • sliceQuery

      public <T> org.springframework.data.domain.Slice<T> sliceQuery(CosmosQuery query, Class<T> domainType, String containerName)
      Description copied from interface: CosmosOperations
      Slice query
      Specified by:
      sliceQuery in interface CosmosOperations
      Type Parameters:
      T - type class of domainType
      Parameters:
      query - the document query
      domainType - type class
      containerName - the container name
      Returns:
      results as Slice
    • runSliceQuery

      public <T> org.springframework.data.domain.Slice<T> runSliceQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Pageable pageable, Class<?> domainType, Class<T> returnType)
      Description copied from interface: CosmosOperations
      Run custom SQL query
      Specified by:
      runSliceQuery in interface CosmosOperations
      Type Parameters:
      T - the type parameter
      Parameters:
      querySpec - the query spec
      pageable - the pageable
      domainType - the domain type
      returnType - the return type
      Returns:
      the Page
    • count

      public long count(String containerName)
      Description copied from interface: CosmosOperations
      Count
      Specified by:
      count in interface CosmosOperations
      Parameters:
      containerName - the container name
      Returns:
      count result
    • count

      public <T> long count(CosmosQuery query, String containerName)
      Description copied from interface: CosmosOperations
      Count
      Specified by:
      count in interface CosmosOperations
      Type Parameters:
      T - type class of domainType
      Parameters:
      query - the document query
      containerName - the container name
      Returns:
      count result
    • count

      public <T> long count(com.azure.cosmos.models.SqlQuerySpec querySpec, String containerName)
      Description copied from interface: CosmosOperations
      Count
      Specified by:
      count in interface CosmosOperations
      Type Parameters:
      T - type class of domainType
      Parameters:
      querySpec - the document query spec
      containerName - the container name
      Returns:
      count result
    • getConverter

      public MappingCosmosConverter getConverter()
      Description copied from interface: CosmosOperations
      To get converter
      Specified by:
      getConverter in interface CosmosOperations
      Returns:
      MappingCosmosConverter
    • runQuery

      public <T> Iterable<T> runQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType)
      Description copied from interface: CosmosOperations
      Run the query.
      Specified by:
      runQuery in interface CosmosOperations
      Type Parameters:
      T - the type parameter
      Parameters:
      querySpec - the query spec
      domainType - the domain type
      returnType - the return type
      Returns:
      the Iterable
    • runQuery

      public <T> Iterable<T> runQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, org.springframework.data.domain.Sort sort, Class<?> domainType, Class<T> returnType)
      Description copied from interface: CosmosOperations
      Run the query.
      Specified by:
      runQuery in interface CosmosOperations
      Type Parameters:
      T - the type parameter
      Parameters:
      querySpec - the query spec
      sort - the sort order
      domainType - the domain type
      returnType - the return type
      Returns:
      the Iterable