Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use cosmos client directly along with spring data repository as I could not find few operations like partial updates in spring data cosmos #32390

Closed
arunim29 opened this issue Dec 1, 2022 · 11 comments
Assignees
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Milestone

Comments

@arunim29
Copy link

arunim29 commented Dec 1, 2022

Query/Question
I am trying to use spring data cosmos in my project. But some operations which I need I could not find in spring data cosmos. For eg., partial updates, do only an insert (not upsert), do only an update (not upsert).

So, I was thinking of using cosmosclient of cosmos sdk directly. Is there a way to use cosmosclient along with spring data cosmos such that both use the same cosmosclient instance?

Why is this not a Bug or a feature Request?
This is not a bug/feature request. I want to use the convenience of spring data for GET queries along with flexibility cosmosclient provides for some other operations.

Setup (please complete the following information if applicable):

Library: (com.azure:azure-spring-data-cosmos:3.30.3)]

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Dec 1, 2022
@arunim29 arunim29 changed the title Query How to use cosmos client directly along with spring data repository as I could not find few operations like partial updates in spring data cosmos Dec 1, 2022
@chenrujun chenrujun added the azure-spring-cosmos Spring cosmos related issues. label Dec 2, 2022
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 2, 2022
@chenrujun chenrujun added Client This issue points to a problem in the data-plane of the library. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. azure-spring All azure-spring related issues labels Dec 2, 2022
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 2, 2022
@chenrujun
Copy link

Hi, @arunim29

Thanks for reaching out.
CosmosAsyncClient is a bean. You can get the bean same way as you get the XxxRepository.

For example:

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

    private XxxRepository repository;
    private CosmosAsyncClient client;

    public SampleApplication(XxxRepository repository, CosmosAsyncClient client){
        this.repository= repository;
        this.client= client;
    }
}

@chenrujun chenrujun assigned chenrujun and unassigned kushagraThapar Dec 2, 2022
@chenrujun chenrujun moved this to Todo in Spring Cloud Azure Dec 2, 2022
@chenrujun chenrujun added this to the 2023-01 milestone Dec 2, 2022
@kushagraThapar
Copy link
Member

@arunim29 what @chenrujun mentioned is absolutely correct. However, I would like to know your use case of Patch API in Spring and how / why do you plan to use this feature. Since we are planning on developing Patch API support in spring-data-cosmos SDK, it would really help us to know how our customers plan to use this feature.

@TheovanKraay / @trande4884 - please take this discussion forward, thanks!

@kushagraThapar
Copy link
Member

@arunim29 - looks like you are already involved in the discussion here, but this is the issue that we are tracking for support of patch operations - #21433

@chenrujun chenrujun moved this from Todo to Need Author Feedback in Spring Cloud Azure Dec 6, 2022
@stliu stliu moved this to Done in Spring Cloud Azure Dec 10, 2022
@chenrujun chenrujun added the needs-author-feedback Workflow: More information is needed from author to address the issue. label Dec 13, 2022
@TheovanKraay
Copy link
Member

TheovanKraay commented Dec 15, 2022

@arunim29 + @chenrujun we plan to expose patch something like the below. Let us know if this is not what you expect. Feel free to make suggestions or ask questions here.

CosmosPatchOperations patchOperations = CosmosPatchOperations
    .create()
    .replace("/size", 5)
    .add("/color", "blue");

User patchedUser = userRepository.save(user,patchOperations,null);

@arunim29
Copy link
Author

arunim29 commented Dec 29, 2022

@arunim29 what @chenrujun mentioned is absolutely correct. However, I would like to know your use case of Patch API in Spring and how / why do you plan to use this feature. Since we are planning on developing Patch API support in spring-data-cosmos SDK, it would really help us to know how our customers plan to use this feature.

@TheovanKraay / @trande4884 - please take this discussion forward, thanks!

@kushagraThapar I was on vacation. Hence, could not reply. So, my usecase is pretty straight-forward. I have a bunch of fields in my document but in one of the heavily used apis of my service, I only need to update one field. Hence, I do not want to first fetch the document and then update it. It will be a waste of resources.

@ghost ghost added needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team and removed needs-author-feedback Workflow: More information is needed from author to address the issue. labels Dec 29, 2022
@arunim29
Copy link
Author

@arunim29 + @chenrujun we plan to expose patch something like the below. Let us know if this is not what you expect. Feel free to make suggestions or ask questions here.

CosmosPatchOperations patchOperations = CosmosPatchOperations
    .create()
    .replace("/size", 5)
    .add("/color", "blue");

User patchedUser = userRepository.save(user,patchOperations,null);

@TheovanKraay what is the user in userRepository.save(user,patchOperations,null) ? In patched operations, we are not expected to pass any existing object. Also, what is this null param?

@arunim29
Copy link
Author

arunim29 commented Dec 29, 2022

@TheovanKraay I had a look at ur PR for this feature and understand now that null represents CosmosPatchItemRequestOptions. This is fine.

BUT I have a concern in this method signature. Here in the userRepository.save(user,patchOperations,null) , my assumption is that you are expecting caller to create a User object with just id and pk set right? If that is the case, what if the user has added jackson annotations in this POJO which prevent non-nulls values for some fields?

Will u just get id and pk and ignore rest of the POJO?

@TheovanKraay
Copy link
Member

@TheovanKraay I had a look at ur PR for this feature and understand now that null represents CosmosPatchItemRequestOptions. This is fine.

BUT I have a concern in this method signature. Here in the userRepository.save(user,patchOperations,null) , my assumption is that you are expecting caller to create a User object with just id and pk set right? If that is the case, what if the user has added jackson annotations in this POJO which prevent non-nulls values for some fields?

Will u just get id and pk and ignore rest of the POJO?

Yes that’s correct. Caller may pass the whole object if it’s available, or they can set only id and pk if they prefer. Either way only id and pk will be used from the object passed. Only the values defined in patchOperations are the ones that will actually be patched.

@TheovanKraay
Copy link
Member

@arunim29 in light of this discussion, we're making changes to avoid the confusion you ran into. Caller will now pass id + partition key of the entity to be patched, along with it's domainType, and the intended patchOperations:

CosmosPatchOperations patchOperations = CosmosPatchOperations
    .create()
    .replace("/size", 5)
    .add("/color", "blue");

User patchedUser = userRepository.save(user.getId(), new PartitionKey(user.getLastName()), User.class, patchOperations);

@chenrujun chenrujun assigned TheovanKraay and unassigned chenrujun Jan 11, 2023
@TheovanKraay
Copy link
Member

See PR for patch: #32630

@kushagraThapar
Copy link
Member

This has been implemented, closing it.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
Archived in project
Development

No branches or pull requests

4 participants