I am developing an application using Azure Cosmos DB for Spring.
I have a structure with model classes and a ReactiveCosmosRepository, which I use to do queries.
I normally annotate my queries in my repository class:
@Repository
public interface ArchivedDocumentRepository extends ReactiveCosmosRepository<ArchivedDocument, String> {
@Query("SELECT * FROM c)
Flux<ArchivedDocument> findAllArchivedDocuments();
@Query("SELECT * FROM c where c.document_id = @documentId")
Mono<ArchivedDocument> findArchivedDocument(@Param("documentId") String documentId);
}
But now I need to create the SQL using some logic, and cannot annotate it like this. How can I do this?
I have also used the Azure Cosmos SDK directly, where you do like this:
client = new CosmosClientBuilder().endpoint("SOMEURL")
.key("SOMEKEY")
.preferredRegions(Collections.singletonList("SOMELOCATION"))
.consistencyLevel(ConsistencyLevel.EVENTUAL).buildClient();
database = client.getDatabase("DBNAME");
String containerName = "CONTAINERNAME";
CosmosContainer container = database.getContainer(containerName);
String sql = "SELECT * FROM c";
CosmosPagedIterable<DocumentMetadata> filteredDocumentMetadata = container.queryItems(sql, new CosmosQueryRequestOptions(), DocumentMetadata.class);
...
}
If I combine my existing repository code with similar code to this in my service, I can retrieve the data I like and build queries how I like, but it seems a bit unnecessary to e.g. instantiate "client" object, when I already have a connection?
Any advices? How can I combine both the SDK directly with the Spring layer to be able to create queries based on logic instead?