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
ConstructorsConstructorDescriptionCosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter) InitializationCosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler) InitializationCosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter) InitializationCosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, org.springframework.data.auditing.IsNewAwareAuditingHandler cosmosAuditingHandler) Initialization -
Method Summary
Modifier and TypeMethodDescription<T> longCount<T> longcount(CosmosQuery query, String containerName) CountlongCountcom.azure.cosmos.models.CosmosContainerPropertiescreateContainerIfNotExists(CosmosEntityInformation<?, ?> information) 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.voidDelete the DocumentQuery, delete all the items in the given container.voiddeleteById(String containerName, Object id, com.azure.cosmos.models.PartitionKey partitionKey) Deletes the item by id and partition key.voiddeleteContainer(String containerName) Delete container<S extends T,T>
voiddeleteEntities(CosmosEntityInformation<T, ?> information, Iterable<S> entities) Deletes the entities using bulk<T> voiddeleteEntity(String containerName, T entity) Deletes the entity<T> Booleanexists(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> Find the DocumentQuery, find all the items specified by domain type in the given container.<T> Iterable<T> Find the DocumentQuery, find all the items specified by domain type.<T> Iterable<T> 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> TFinds item by id<T> TFinds item by id<T> TFinds item by id<T,ID> Iterable <T> Find by idsgetContainerName(Class<?> domainType) To get container name by domainTypegetContainerNameOverride(String containerName) Check if an overridden version of containerName is present, and if so, return itcom.azure.cosmos.models.CosmosContainerPropertiesgetContainerProperties(String containerName) Get properties for specified containerTo get converter<T> TInserts item into the given container<T> TInserts item into the given container<T> Tinsert(T objectToSave, com.azure.cosmos.models.PartitionKey partitionKey) Inserts item<S extends T,T>
Iterable<S> insertAll(CosmosEntityInformation<T, ?> information, Iterable<S> entities) Insert all items with bulk.<T> org.springframework.data.domain.Page<T> paginationQuery(CosmosQuery query, Class<T> domainType, String containerName) Pagination query<T> Tpatch(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> Tpatch(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 CosmosPatchItemRequestOptionscom.azure.cosmos.models.CosmosContainerPropertiesreplaceContainerProperties(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> 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 queryvoidsetApplicationContext(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 querylongSum<T> voidUpserts an item into container with partition key<T> voidupsert(T object) Upserts an item with partition key<T> TupsertAndReturnEntity(String containerName, T object) Upserts an item and return item properties
-
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 nulldatabaseName- must not be nullcosmosConfig- must not be nullmappingCosmosConverter- must not be nullcosmosAuditingHandler- can be null
-
CosmosTemplate
public CosmosTemplate(com.azure.cosmos.CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter) Initialization- Parameters:
client- must not be nulldatabaseName- must not be nullcosmosConfig- must not be nullmappingCosmosConverter- 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 nullcosmosConfig- must not be nullmappingCosmosConverter- must not be nullcosmosAuditingHandler- can be null
-
CosmosTemplate
public CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter) Initialization- Parameters:
cosmosFactory- must not be nullcosmosConfig- must not be nullmappingCosmosConverter- 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:
setApplicationContextin interfaceorg.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:
insertin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
objectToSave- must not be nullpartitionKey- must not be null- Returns:
- the inserted item
-
insert
Inserts item into the given container- Specified by:
insertin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
containerName- must not be nullobjectToSave- 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:
insertin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
containerName- must not be nullobjectToSave- must not be nullpartitionKey- must not be null- Returns:
- the inserted item
-
insertAll
public <S extends T,T> Iterable<S> insertAll(CosmosEntityInformation<T, ?> information, Iterable<S> entities) Insert all items with bulk.- Specified by:
insertAllin interfaceCosmosOperations- Type Parameters:
S- type class of domain typeT- type class of domain type- Parameters:
information- the CosmosEntityInformationentities- the Iterable entities to be inserted- Returns:
- Flux of result
-
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:
patchin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
id- must not be nullpartitionKey- must not be nulldomainType- must not be nullpatchOperations- 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:
patchin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
id- must not be nullpartitionKey- must not be nulldomainType- must not be nullpatchOperations- must not be nulloptions- Optional CosmosPatchItemRequestOptions, e.g. options.setFilterPredicate("FROM products p WHERE p.used = false");- Returns:
- the patched item
-
findById
Finds item by id- Specified by:
findByIdin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
id- must not be nulldomainType- 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:CosmosOperationsFinds item by id- Specified by:
findByIdin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
id- must not be nulldomainType- must not be nullpartitionKey- must not be null- Returns:
- found item
-
findById
Finds item by id- Specified by:
findByIdin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
containerName- must not be nullid- must not be nulldomainType- must not be null- Returns:
- found item
-
upsert
public <T> void upsert(T object) Upserts an item with partition key- Specified by:
upsertin interfaceCosmosOperations- Type Parameters:
T- type of upsert object- Parameters:
object- upsert object
-
upsert
Upserts an item into container with partition key- Specified by:
upsertin interfaceCosmosOperations- Type Parameters:
T- type of upsert object- Parameters:
containerName- the container nameobject- upsert object
-
upsertAndReturnEntity
Upserts an item and return item properties- Specified by:
upsertAndReturnEntityin interfaceCosmosOperations- Type Parameters:
T- type of upsert object- Parameters:
containerName- the container nameobject- upsert object- Returns:
- upsert object entity
-
findAll
Find the DocumentQuery, find all the items specified by domain type.- Specified by:
findAllin interfaceCosmosOperations- Type Parameters:
T- class type of domain- Parameters:
domainType- the domain type- Returns:
- found results in a List
-
findAll
Find the DocumentQuery, find all the items specified by domain type in the given container.- Specified by:
findAllin interfaceCosmosOperations- Type Parameters:
T- class type of domain- Parameters:
containerName- the container namedomainType- 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:CosmosOperationsFind the DocumentQuery, find all the items specified by domain type in the given container.- Specified by:
findAllin interfaceCosmosOperations- Type Parameters:
T- class type of domain- Parameters:
partitionKey- the partition keydomainType- the domain type- Returns:
- results in an Iterable
-
deleteAll
Delete the DocumentQuery, delete all the items in the given container.- Specified by:
deleteAllin interfaceCosmosOperations- Parameters:
containerName- Container name of databasedomainType- the domain type
-
deleteContainer
Description copied from interface:CosmosOperationsDelete container- Specified by:
deleteContainerin interfaceCosmosOperations- Parameters:
containerName- the container name
-
getContainerName
Description copied from interface:CosmosOperationsTo get container name by domainType- Specified by:
getContainerNamein interfaceCosmosOperations- Parameters:
domainType- class type- Returns:
- String
-
getContainerNameOverride
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:CosmosOperationsCreates container if not exists- Specified by:
createContainerIfNotExistsin interfaceCosmosOperations- Parameters:
information- CosmosEntityInformation- Returns:
- CosmosContainerProperties
-
getContainerProperties
public com.azure.cosmos.models.CosmosContainerProperties getContainerProperties(String containerName) Description copied from interface:CosmosOperationsGet properties for specified container- Specified by:
getContainerPropertiesin interfaceCosmosOperations- 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:CosmosOperationsReplace container properties for the specified container- Specified by:
replaceContainerPropertiesin interfaceCosmosOperations- Parameters:
containerName- Stringproperties- 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:
deleteByIdin interfaceCosmosOperations- Parameters:
containerName- Container name of databaseid- item idpartitionKey- the partition key
-
deleteEntity
Deletes the entity- Specified by:
deleteEntityin interfaceCosmosOperations- Type Parameters:
T- type class of domain type- Parameters:
containerName- the container nameentity- the entity object
-
deleteEntities
public <S extends T,T> void deleteEntities(CosmosEntityInformation<T, ?> information, Iterable<S> entities) Deletes the entities using bulk- Specified by:
deleteEntitiesin interfaceCosmosOperations- Type Parameters:
S- type class of domain typeT- type class of domain type- Parameters:
information- the CosmosEntityInformationentities- the Iterable entities to be inserted
-
findByIds
Description copied from interface:CosmosOperationsFind by ids- Specified by:
findByIdsin interfaceCosmosOperations- Type Parameters:
T- type of domainTypeID- type of ID- Parameters:
ids- iterable of idsdomainType- type classcontainerName- 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:
findin interfaceCosmosOperations- Type Parameters:
T- class of domainType- Parameters:
query- The representation for query method.domainType- Class of domaincontainerName- 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:
existsin interfaceCosmosOperations- Type Parameters:
T- class of domainType- Parameters:
query- The representation for query method.domainType- Class of domaincontainerName- 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. Uses bulk if possible.- Specified by:
deletein interfaceCosmosOperations- Type Parameters:
T- class of domainType- Parameters:
query- The representation for query method.domainType- Class of domaincontainerName- 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:CosmosOperationsFind all items in a given container with partition key- Specified by:
findAllin interfaceCosmosOperations- Type Parameters:
T- type of domainType- Parameters:
pageable- Pageable objectdomainType- the domainTypecontainerName- 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:CosmosOperationsRun the query.- Specified by:
runPaginationQueryin interfaceCosmosOperations- Type Parameters:
T- the type parameter- Parameters:
querySpec- the query specpageable- the pageabledomainType- the domain typereturnType- 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:CosmosOperationsPagination query- Specified by:
paginationQueryin interfaceCosmosOperations- Type Parameters:
T- type class of domainType- Parameters:
query- the document querydomainType- type classcontainerName- 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:CosmosOperationsSlice query- Specified by:
sliceQueryin interfaceCosmosOperations- Type Parameters:
T- type class of domainType- Parameters:
query- the document querydomainType- type classcontainerName- 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:CosmosOperationsRun custom SQL query- Specified by:
runSliceQueryin interfaceCosmosOperations- Type Parameters:
T- the type parameter- Parameters:
querySpec- the query specpageable- the pageabledomainType- the domain typereturnType- the return type- Returns:
- the Page
-
count
Description copied from interface:CosmosOperationsCount- Specified by:
countin interfaceCosmosOperations- Parameters:
containerName- the container name- Returns:
- count result
-
count
Description copied from interface:CosmosOperationsCount- Specified by:
countin interfaceCosmosOperations- Type Parameters:
T- type class of domainType- Parameters:
query- the document querycontainerName- the container name- Returns:
- count result
-
count
Description copied from interface:CosmosOperationsCount- Specified by:
countin interfaceCosmosOperations- Type Parameters:
T- type class of domainType- Parameters:
querySpec- the document query speccontainerName- the container name- Returns:
- count result
-
sum
Description copied from interface:CosmosOperationsSum- Specified by:
sumin interfaceCosmosOperations- Parameters:
querySpec- the document query speccontainerName- the container name- Returns:
- sum result
-
getConverter
Description copied from interface:CosmosOperationsTo get converter- Specified by:
getConverterin interfaceCosmosOperations- Returns:
- MappingCosmosConverter
-
runQuery
public <T> Iterable<T> runQuery(com.azure.cosmos.models.SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType) Description copied from interface:CosmosOperationsRun the query.- Specified by:
runQueryin interfaceCosmosOperations- Type Parameters:
T- the type parameter- Parameters:
querySpec- the query specdomainType- the domain typereturnType- 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:CosmosOperationsRun the query.- Specified by:
runQueryin interfaceCosmosOperations- Type Parameters:
T- the type parameter- Parameters:
querySpec- the query specsort- the sort orderdomainType- the domain typereturnType- the return type- Returns:
- the Iterable
-