Skip to content

Commit

Permalink
Feat: AutoContinue + Get-CosmosConnection
Browse files Browse the repository at this point in the history
Added AutoContinue to Invoke-CosmosQuery
Added Get-CosmosConnection
  • Loading branch information
jformacek committed Oct 15, 2023
1 parent 96c04ae commit ba5e53e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Commands/Public/Connect-Cosmos.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function Connect-Cosmos
RetryCount = $RetryCount
Session = @{}
CollectResponseHeaders = $CollectResponseHeaders
RequiredScopes = @("https://$accountName`.documents.azure.com/.default")
RequiredScopes = @("https://$accountName`.documents.azure.com/.default") #we keep scopes separately to override any default scopes set on existing factory passed
AuthFactory = $null
}

Expand Down
21 changes: 21 additions & 0 deletions Commands/Public/Get-CosmosConnection.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Get-CosmosConnection
{
<#
.SYNOPSIS
Returns most recently created Cosmos connection object
.DESCRIPTION
Returns most recently created cosmos connection object that is cached inside the module.
Useful when you do not want to keep connection object in variable and reach for it only when needed
.OUTPUTS
Connection configuration object.
#>
param ()

process
{
$script:Configuration
}
}
61 changes: 37 additions & 24 deletions Commands/Public/Invoke-CosmosQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ function Invoke-CosmosQuery
#Continuation token. Used to ask for next page of results
$ContinuationToken,

[switch]
#when response contains continuation token, returns the reesponse and automatically sends new request with continuation token
#this simnlifies getting all data from query for large datasets
$AutoContinue,

[Parameter()]
[PSTypeName('CosmosLite.Connection')]
#Connection configuration object
Expand All @@ -82,34 +87,42 @@ function Invoke-CosmosQuery

process
{
$rq = Get-CosmosRequest `
-PartitionKey $partitionKey `
-Type Query `
-MaxItems $MaxItems `
-Continuation $ContinuationToken `
-Context $Context `
-Collection $Collection

$QueryDefinition = @{
query = $Query
}
if($null -ne $QueryParameters)
do
{
$QueryDefinition['parameters']=@()
foreach($key in $QueryParameters.Keys)
$rq = Get-CosmosRequest `
-PartitionKey $partitionKey `
-Type Query `
-MaxItems $MaxItems `
-Continuation $ContinuationToken `
-Context $Context `
-Collection $Collection

$QueryDefinition = @{
query = $Query
}
if($null -ne $QueryParameters)
{
$QueryDefinition['parameters']+=@{
name=$key
value=$QueryParameters[$key]
$QueryDefinition['parameters']=@()
foreach($key in $QueryParameters.Keys)
{
$QueryDefinition['parameters']+=@{
name=$key
value=$QueryParameters[$key]
}
}
}
}
$rq.Method = [System.Net.Http.HttpMethod]::Post
$uri = "$url"
$rq.Uri = New-Object System.Uri($uri)
$rq.Payload = ($QueryDefinition | ConvertTo-Json -Depth 99 -Compress)
$rq.ContentType = 'application/query+json'
$rq.Method = [System.Net.Http.HttpMethod]::Post
$uri = "$url"
$rq.Uri = New-Object System.Uri($uri)
$rq.Payload = ($QueryDefinition | ConvertTo-Json -Depth 99 -Compress)
$rq.ContentType = 'application/query+json'

ProcessRequestBatchInternal -Batch (SendRequestInternal -rq $rq -Context $Context) -Context $Context
$response = ProcessRequestBatchInternal -Batch (SendRequestInternal -rq $rq -Context $Context) -Context $Context
$response
#auto-continue if requested
if(-not $AutoContinue) {break;}
if([string]::IsNullOrEmpty($response.Continuation)) {break;}
$ContinuationToken = $response.Continuation
}while($true)
}
}
Binary file modified Module/CosmosLite/CosmosLite.psd1
Binary file not shown.
86 changes: 60 additions & 26 deletions Module/CosmosLite/CosmosLite.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function Connect-Cosmos
RetryCount = $RetryCount
Session = @{}
CollectResponseHeaders = $CollectResponseHeaders
RequiredScopes = @("https://$accountName`.documents.azure.com/.default")
RequiredScopes = @("https://$accountName`.documents.azure.com/.default") #we keep scopes separately to override any default scopes set on existing factory passed
AuthFactory = $null
}

Expand Down Expand Up @@ -270,6 +270,27 @@ function Get-CosmosAccessToken
Get-AadToken -Factory $context.AuthFactory -Scopes $context.RequiredScopes
}
}
function Get-CosmosConnection
{
<#
.SYNOPSIS
Returns most recently created Cosmos connection object
.DESCRIPTION
Returns most recently created cosmos connection object that is cached inside the module.
Useful when you do not want to keep connection object in variable and reach for it only when needed
.OUTPUTS
Connection configuration object.
#>
param ()

process
{
$script:Configuration
}
}
function Get-CosmosDocument
{
<#
Expand Down Expand Up @@ -432,6 +453,11 @@ function Invoke-CosmosQuery
#Continuation token. Used to ask for next page of results
$ContinuationToken,

[switch]
#when response contains continuation token, returns the reesponse and automatically sends new request with continuation token
#this simnlifies getting all data from query for large datasets
$AutoContinue,

[Parameter()]
[PSTypeName('CosmosLite.Connection')]
#Connection configuration object
Expand All @@ -446,35 +472,43 @@ function Invoke-CosmosQuery

process
{
$rq = Get-CosmosRequest `
-PartitionKey $partitionKey `
-Type Query `
-MaxItems $MaxItems `
-Continuation $ContinuationToken `
-Context $Context `
-Collection $Collection

$QueryDefinition = @{
query = $Query
}
if($null -ne $QueryParameters)
do
{
$QueryDefinition['parameters']=@()
foreach($key in $QueryParameters.Keys)
$rq = Get-CosmosRequest `
-PartitionKey $partitionKey `
-Type Query `
-MaxItems $MaxItems `
-Continuation $ContinuationToken `
-Context $Context `
-Collection $Collection

$QueryDefinition = @{
query = $Query
}
if($null -ne $QueryParameters)
{
$QueryDefinition['parameters']+=@{
name=$key
value=$QueryParameters[$key]
$QueryDefinition['parameters']=@()
foreach($key in $QueryParameters.Keys)
{
$QueryDefinition['parameters']+=@{
name=$key
value=$QueryParameters[$key]
}
}
}
}
$rq.Method = [System.Net.Http.HttpMethod]::Post
$uri = "$url"
$rq.Uri = New-Object System.Uri($uri)
$rq.Payload = ($QueryDefinition | ConvertTo-Json -Depth 99 -Compress)
$rq.ContentType = 'application/query+json'

ProcessRequestBatchInternal -Batch (SendRequestInternal -rq $rq -Context $Context) -Context $Context
$rq.Method = [System.Net.Http.HttpMethod]::Post
$uri = "$url"
$rq.Uri = New-Object System.Uri($uri)
$rq.Payload = ($QueryDefinition | ConvertTo-Json -Depth 99 -Compress)
$rq.ContentType = 'application/query+json'

$response = ProcessRequestBatchInternal -Batch (SendRequestInternal -rq $rq -Context $Context) -Context $Context
$response
#auto-continue if requested
if(-not $AutoContinue) {break;}
if([string]::IsNullOrEmpty($response.Continuation)) {break;}
$ContinuationToken = $response.Continuation
}while($true)
}
}
function Invoke-CosmosStoredProcedure
Expand Down

0 comments on commit ba5e53e

Please sign in to comment.