-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cosmos DB: Adds Patch Support (#2161)
## Why make this change? Today, in cosmos DB, we have ability to `update` the item which is actually `replace` the item. Adding new operation i.e. `patch` where customer would have ability to "patch" an item. `PATCH` would be available only for Cosmos DB. ## What is this change? Before going further, it is highly recommended to go through below docs: 1. How patch works in Cosmos DB https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-operations 2. Limitations: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-modes 3. How it works with SDK: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update-getting-started?tabs=dotnet#prerequisites To Summarize, here is a simple example of patch: ``` List<PatchOperation> operations = new () { PatchOperation.Add("/color", "silver"), PatchOperation.Remove("/used"), PatchOperation.Increment("/price", 50.00), PatchOperation.Add("/tags/-", "featured-bikes") }; ItemResponse<Product> response = await container.PatchItemAsync<Product>( id: "e379aea5-63f5-4623-9a9b-4cd9b33b91d5", partitionKey: new PartitionKey("road-bikes"), patchOperations: operations ); ``` You need to generate `PatchOpertation` which need below information: a) Operation type i.e. Set, Add, Remove etc b) Attribute Path where operation needs to be applied i.e "/color", "/used" in above example c) New value **What DAB supports?** 1. We decided to support only `Set` operation, as it solves the purpose to update an item. It means, you cannot perform any specific operations like remove, move etc. 2. There is no special handling for an array. 3. If the target path specifies an element that doesn't exist, it's added. 4. If the target path specifies an element that already exists, its value is replaced. **Changes as part of this PR:** 1. Generate `patch` operation for given entities ![image](https://github.com/Azure/data-api-builder/assets/6362382/4501b73b-1b09-4a82-bc3b-dae9352ca2e7) 5. Generate `PatchPlanetInput` without _id_ field as using patch operation you can not update an `Id` ![image](https://github.com/Azure/data-api-builder/assets/6362382/400b19ad-779d-4282-9b70-d73793dc4fa5) 6. Implement patch operation a) It translates the passed item into "patchoperation" by traversing the item. b) Checks if number of patch operations are less than 10 or more than 10 (as cosmsodb supports at max 10 patch operations at a time) c) If it is less than or equal to 10, it fires patch call with patch operations d) If it is greater than 10, then it creates a transaction batch of patch call, with 10 patch operations in each patch call. (_RU exhaustive but functionally it works_) **Pictorial Overview of the implementation** ```mermaid flowchart TD User[fa:fa-user User]-->| patch operation |DAB subgraph DAB[DAB] Authorization[Authorization]-->Patch subgraph Patch[Patch Operation] PatchOperation[Generate Patch 'Set' Operations with passed item] -->CheckCount{Number of Patch Operation > 10} CheckCount --> |No| SDKOperation[SDK Patch call] CheckCount --> |Yes| TransactionBatch[Create a batch of patch calls with max 10 patch operations ]-->SDKBatchOperation[SDK ExecuteBatch call] end end ``` ## How was this tested? - [ ] Integration Tests - [ ] Unit Tests ## Sample Request(s) - Example REST and/or GraphQL request to demonstrate modifications - Example of CLI usage to demonstrate modifications
- Loading branch information
1 parent
d2aa5aa
commit e076d87
Showing
12 changed files
with
738 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.