-
Notifications
You must be signed in to change notification settings - Fork 45
Add New-CosmosDbTransactionalBatch command to support transactional batch operations
#517
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
Open
ArmaanMcleod
wants to merge
28
commits into
PlagueHO:main
Choose a base branch
from
ArmaanMcleod:transactional-batch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b8776d5
Add New-CosmosDbTransactionalBatch command
ArmaanMcleod 9245c55
Add output type to command
ArmaanMcleod fac1112
Add docs
ArmaanMcleod 0115b60
Set type on batch operations
ArmaanMcleod f69d7a6
Add unit tests
ArmaanMcleod 8034788
Add integration tests
ArmaanMcleod 0c9d117
Updated change log
ArmaanMcleod f4575a4
Add output type and parameter type to Set-CosmosDbTransactionalBatchO…
ArmaanMcleod 8d55068
Fix formatting for Project name base name in test
ArmaanMcleod c2eb2e7
Add format XML
ArmaanMcleod e17fd11
Add supports should process for transactional batch
ArmaanMcleod dbccd92
Remove unnecessary integration tests
ArmaanMcleod c65843e
Test JSON contents in ReturnJson test
ArmaanMcleod ac445e0
move should process to beginning of cmdlet
ArmaanMcleod b670c35
Add should process tests and wrapped around web request block
ArmaanMcleod 75e0e06
Add Copilot suggestions
ArmaanMcleod 0c6e117
Update source/Public/transactionalbatch/New-CosmosDbTransactionalBatc…
ArmaanMcleod 0dd03b0
Fix typo with headers
ArmaanMcleod a553eb3
Merge branch 'main' into transactional-batch
ArmaanMcleod 0b14be8
Use booleans for headers in mocks instead of strings
ArmaanMcleod dc8f0db
Add more unit tests
ArmaanMcleod 7593626
Apply suggestions from code review
ArmaanMcleod a04fb27
Replace IsAtomic boolean flag with NoAtomic switch
ArmaanMcleod b7b2bbe
Remove related links from docs
ArmaanMcleod e287b7c
Simplified formats and types to be more readable with important prope…
ArmaanMcleod 3459ee0
Add SupportsShouldProcess to Set-CosmosDbTransactionalBatchOperationType
ArmaanMcleod ab1c15d
Fix markdown linter errors
ArmaanMcleod af580a2
Fix markdown line length error in CHANGELOG
ArmaanMcleod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| --- | ||
| external help file: CosmosDB-help.xml | ||
| Module Name: CosmosDB | ||
| online version: | ||
| schema: 2.0.0 | ||
| --- | ||
|
|
||
| # New-CosmosDbTransactionalBatch | ||
|
|
||
| ## SYNOPSIS | ||
|
|
||
| Execute a transactional batch operation against a collection in a Cosmos DB database. | ||
|
|
||
| ## SYNTAX | ||
|
|
||
| ```powershell | ||
| New-CosmosDbTransactionalBatch -Context <Context> -PartitionKey <String> -CollectionId <String> | ||
| -Documents <Object[]> [-OperationType <String>] [-IsAtomic <Boolean>] [-ReturnJson <switch>] [<CommonParameters>] | ||
| ``` | ||
|
|
||
| ## DESCRIPTION | ||
|
|
||
| This cmdlet will execute a transactional batch operation against a collection in a Cosmos DB database. All operations in the batch will target documents within the same partition key. If IsAtomic is true (default), either all operations succeed or all operations are rolled back. | ||
|
|
||
| ## EXAMPLES | ||
|
|
||
| ### Example 1: Create multiple documents atomically | ||
|
|
||
| ```powershell | ||
| PS C:\> $documents = @( | ||
| @{ id = 'doc1'; name = 'Alice'; customerId = 'test' }, | ||
| @{ id = 'doc2'; name = 'Bob'; customerId = 'test' } | ||
| ) | ||
| PS C:\> New-CosmosDbTransactionalBatch -Context $context -PartitionKey 'test' -CollectionId 'Customers' -Documents $documents -OperationType 'Create' | ||
| ``` | ||
|
|
||
| ### Example 2: Upsert documents with atomic behavior disabled | ||
|
|
||
| ```powershell | ||
| PS C:\> $documents = @( | ||
| @{ id = 'doc1'; name = 'Alice Updated'; customerId = 'test' }, | ||
| @{ id = 'doc2'; name = 'Bob Updated'; customerId = 'test' } | ||
| ) | ||
| PS C:\> New-CosmosDbTransactionalBatch -Context $context -PartitionKey 'test' -CollectionId 'Customers' -Documents $documents -OperationType 'Upsert' -IsAtomic $false | ||
| ``` | ||
|
|
||
| ### Example 3: Return raw JSON response | ||
|
|
||
| ```powershell | ||
| PS C:\> $result = New-CosmosDbTransactionalBatch -Context $context -PartitionKey 'test' -CollectionId 'Customers' -Documents $documents -ReturnJson | ||
| PS C:\> $result | ConvertFrom-Json | ||
| ``` | ||
|
|
||
| ## PARAMETERS | ||
|
|
||
| ### -Context | ||
|
|
||
| This is an object containing the context information of the Cosmos DB database that will be deleted. It should be created by `New-CosmosDbContext`. | ||
|
|
||
| ```yaml | ||
| Type: Context | ||
| Parameter Sets: (All) | ||
| Aliases: Connection | ||
|
|
||
| Required: True | ||
| Position: Named | ||
| Default value: None | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -PartitionKey | ||
| This is the partition key value for all documents in the batch. All documents must belong to the same partition. | ||
| ```yaml | ||
| Type: String | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
|
|
||
| Required: True | ||
| Position: Named | ||
| Default value: None | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -CollectionId | ||
| This is the Id of the collection to execute the batch operation against. | ||
| ```yaml | ||
| Type: String | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
|
|
||
| Required: True | ||
| Position: Named | ||
| Default value: None | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -Documents | ||
| An array of documents to include in the batch operation. Each document will be processed using the specified OperationType. | ||
| ```yaml | ||
| Type: Object[] | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
|
|
||
| Required: True | ||
| Position: Named | ||
| Default value: None | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -OperationType | ||
| The type of operation to perform on each document in the batch. Valid values are: Create, Upsert, Read, Replace, Delete. | ||
| ```yaml | ||
| Type: String | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
| Accepted values: Create, Upsert, Read, Replace, Delete | ||
|
|
||
| Required: False | ||
| Position: Named | ||
| Default value: Create | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -IsAtomic | ||
| Determines whether the batch operation should be atomic. If true (default), either all operations succeed or all are rolled back. If false, individual operations can succeed or fail independently. | ||
| ```yaml | ||
| Type: Boolean | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
|
|
||
| Required: False | ||
| Position: Named | ||
| Default value: True | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### -ReturnJson | ||
| Return the raw JSON response from Cosmos DB instead of parsed objects. | ||
| ```yaml | ||
| Type: SwitchParameter | ||
| Parameter Sets: (All) | ||
| Aliases: | ||
|
|
||
| Required: False | ||
| Position: Named | ||
| Default value: False | ||
| Accept pipeline input: False | ||
| Accept wildcard characters: False | ||
| ``` | ||
| ### CommonParameters | ||
| This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). | ||
| ## INPUTS | ||
| ### None | ||
| ## OUTPUTS | ||
| ### System.Object[] | ||
| Returns an array of batch operation results, one for each document in the batch. | ||
| ## NOTES | ||
| - All documents in a batch must belong to the same partition key | ||
| - Maximum of 100 operations per batch | ||
| - Each operation result includes statusCode, requestCharge, eTag, and resourceBody properties | ||
| - When IsAtomic is true, any operation failure will roll back the entire batch | ||
| ## RELATED LINKS | ||
| [New-CosmosDbDocument](New-CosmosDbDocument.md) | ||
| [New-CosmosDbContext](New-CosmosDbContext.md) | ||
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
source/Private/transactionalbatch/Set-CosmosDbTransactionalBatchOperationType.ps1
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| function Set-CosmosDbTransactionalBatchOperationType | ||
ArmaanMcleod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [CmdletBinding()] | ||
| [OutputType([System.Object[]])] | ||
| param | ||
| ( | ||
| [Parameter(Mandatory = $true)] | ||
| [System.Object[]] | ||
| $BatchOperations | ||
| ) | ||
|
|
||
| foreach ($item in $BatchOperations) | ||
| { | ||
| $item.PSObject.TypeNames.Insert(0, 'CosmosDB.TransactionalBatchOperation') | ||
| } | ||
|
|
||
| return $BatchOperations | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
source/Public/transactionalbatch/New-CosmosDbTransactionalBatch.ps1
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| function New-CosmosDbTransactionalBatch | ||
ArmaanMcleod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| [CmdletBinding(SupportsShouldProcess = $true)] | ||
ArmaanMcleod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| [OutputType([System.Object[]])] | ||
| param ( | ||
| [Parameter(Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
ArmaanMcleod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [CosmosDb.Context] | ||
| $Context, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.String] | ||
| $PartitionKey, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [ValidateScript({ Assert-CosmosDbCollectionIdValid -Id $_ -ArgumentName 'CollectionId' })] | ||
| [System.String] | ||
| $CollectionId, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.Object[]] | ||
| $Documents, | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [ValidateSet('Create', 'Upsert', 'Read', 'Replace', 'Delete')] | ||
| [System.String] | ||
| $OperationType = 'Create', | ||
|
|
||
| [Parameter(Mandatory = $false)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.Boolean] | ||
| $IsAtomic = $true, | ||
ArmaanMcleod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [Parameter()] | ||
| [System.Management.Automation.SwitchParameter] | ||
| $ReturnJson | ||
| ) | ||
|
|
||
| $operationDescription = $LocalizedData.ShouldExecuteTransactionalBatch -f $Documents.Count, $OperationType.ToLower(), $CollectionId, $PartitionKey | ||
|
|
||
| if (-not $PSCmdlet.ShouldProcess('Azure', $operationDescription)) | ||
| { | ||
| return | ||
| } | ||
|
|
||
| $operations = $Documents | ForEach-Object { | ||
| @{ | ||
| operationType = $OperationType | ||
| resourceBody = $_ | ||
| } | ||
| } | ||
|
|
||
| $batchBody = $operations | ConvertTo-Json -Depth 100 -Compress -AsArray | ||
|
|
||
| $resourcePath = 'colls/{0}/docs' -f $CollectionId | ||
|
|
||
| $headers = @{ | ||
| 'x-ms-cosmos-is-batch-request' = $IsAtomic | ||
| 'x-ms-cosmos-batch-atomic' = $true | ||
ArmaanMcleod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'x-ms-documentdb-partitionkey' = "[`"$PartitionKey`"]" | ||
| } | ||
|
|
||
| $result = Invoke-CosmosDbRequest ` | ||
| -Context $Context ` | ||
| -Method 'Post' ` | ||
| -ResourceType 'docs' ` | ||
| -ResourcePath $resourcePath ` | ||
| -Body $batchBody ` | ||
| -Headers $headers ` | ||
| -ApiVersion '2018-12-31' | ||
|
|
||
| if ($ReturnJson.IsPresent) | ||
| { | ||
| return $result.Content | ||
| } | ||
|
|
||
| try | ||
| { | ||
| $batchOperations = $result.Content | ConvertFrom-Json | ||
| return (Set-CosmosDbTransactionalBatchOperationType -BatchOperations $batchOperations) | ||
| } | ||
| catch | ||
| { | ||
| New-CosmosDbInvalidOperationException -Message ($LocalizedData.ErrorConvertingDocumentJsonToObject) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.