Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Demo/Demo Query DDB3.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$AccountName = '<acc>'
$Key = '<DDBKey>'
$Database = '<db>'
$Collection = '<coll>'
$id = '622046c70210ae5c13dee9e3bc2f0ff2'

$Query = @"
{
"query": "SELECT * FROM $collection r WHERE r.id = @id",
"parameters": [
{ "name": "@id", "value": $id }
]
}
"@

$temp = New-DocDBQuery -DBName $Database -accountName $AccountName -key $key -collection $Collection -JSONQuery $Query -EnableCrossPartitionQuery
$temp

15 changes: 15 additions & 0 deletions Demo/Demo Query DDB4.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$AccountName = '<acc>'
$Key = '<DDBKey>'
$Database = '<db>'
$Collection = '<coll>'
$partitionKey = '622046c70210ae5c13dee9e3bc2f0ff2'

$Query = @"
{
"query": "SELECT * FROM $collection"
}
"@

$temp = New-DocDBQuery -DBName $Database -accountName $AccountName -key $key -collection $Collection -JSONQuery $Query -partitionKey $partitionKey
$temp

42 changes: 26 additions & 16 deletions DocumentDB.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function Get-DocDBKey([System.String]$Verb = '',[System.String]$ResourceId = '',
[System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') {
[System.String]$ResourceType = '',[System.String]$Date = '',[System.String]$masterKey = '') {
Add-Type -AssemblyName System.Web
$keyBytes = [System.Convert]::FromBase64String($masterKey)
$text = @($Verb.ToLowerInvariant() + "`n" + $ResourceType.ToLowerInvariant() + "`n" + $ResourceId + "`n" + $Date.ToLowerInvariant() + "`n" + "" + "`n")
Expand Down Expand Up @@ -34,21 +34,29 @@ function Get-DocDBCollections([string]$DBName, [string]$accountName, [string]$ke

function New-DocDBHeader([string]$action = "get",[string]$resType, [string]$resourceId, [String]$key) {
$apiDate = Get-UTCDate
$auth = Get-DocDBKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $Key
$auth = Get-DocDBKey -Verb $action -ResourceType $resType -ResourceId $resourceId -Date $apiDate -masterKey $key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("x-ms-date", $apiDate)
$headers.Add("Authorization", $auth)
$headers.Add("x-ms-version", '2015-12-16')
$headers.Add("x-ms-version", '2016-07-11')
return $headers
}

#Post json query
function New-DocDBQuery([switch]$NoClean, [string]$JSONQuery, [string]$DBName, [string]$collection, [string]$accountName, [string]$key){
function New-DocDBQuery([switch]$NoClean, [string]$JSONQuery, [string]$DBName, [string]$collection, [string]$accountName, [string]$key, [switch]$EnableCrossPartitionQuery, [string]$partitionKey){
Write-Host Executing Query $JSONQuery
$BaseUri = "https://" + $accountName + ".documents.azure.com"
$collName = "dbs/"+$DBName+"/colls/" + $collection
$DBName = "dbs/" + $databaseName
$headers = New-DocDBHeader -action Post -resType docs -resourceId $collName -key $key
$headers.Add("x-ms-documentdb-is-upsert", "true")
$headers.Add("x-ms-documentdb-isquery", "true")

if ($enableCrossPartitionQuery) {
$headers.Add("x-ms-documentdb-query-enablecrosspartition", "true")
}
elseif (![string]::IsNullOrEmpty($partitionKey)) {
$headers.Add("x-ms-documentdb-partitionkey", "[$partitionKey]")
}
$uri = $BaseUri + "/" + $collName + "/docs"

Write-host ("Calling " + $uri)
Expand All @@ -67,7 +75,9 @@ function New-DocDBQuery([switch]$NoClean, [string]$JSONQuery, [string]$DBName, [
}
catch
{
Throw $_.Exception.Message
$resp = (New-Object System.IO.StreamReader $_.Exception.Response.GetResponseStream()).ReadToEnd()
write-host $resp
Throw $_.Exception.ToString()
break
}
if($NoClean)
Expand All @@ -82,16 +92,16 @@ function New-DocDBQuery([switch]$NoClean, [string]$JSONQuery, [string]$DBName, [

#Post Document
function Set-DocDBDocument{
[CmdletBinding(DefaultParameterSetName='JSON')]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'JSON')]
[string]$JSONdocument,
[string]$DBName,
[string]$collection,
[string]$accountName,
[string]$key,
[Parameter(Mandatory = $true, ParameterSetName = 'PSO')]
[PSCustomObject]$PSdocument
[CmdletBinding(DefaultParameterSetName='JSON')]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'JSON')]
[string]$JSONdocument,
[string]$DBName,
[string]$collection,
[string]$accountName,
[string]$key,
[Parameter(Mandatory = $true, ParameterSetName = 'PSO')]
[PSCustomObject]$PSdocument
)
if($PSdocument)
{
Expand Down