From f946c185dbbf5a17012be1695c046a9e1eb0dde8 Mon Sep 17 00:00:00 2001 From: Matteo Guadrini Date: Thu, 7 Jun 2018 17:40:05 +0200 Subject: [PATCH] Add new cmdlet Get-CouchDBIndex, New-CouchDBIndex, Remove-CouchDBIndex --- PSCouchDB/PSCouchDB.psd1 | 310 ++++++++++++++++++++------------------- PSCouchDB/PSCouchDB.psm1 | 75 ++++++++++ 2 files changed, 232 insertions(+), 153 deletions(-) diff --git a/PSCouchDB/PSCouchDB.psd1 b/PSCouchDB/PSCouchDB.psd1 index 8a45345..9c8e57f 100644 --- a/PSCouchDB/PSCouchDB.psd1 +++ b/PSCouchDB/PSCouchDB.psd1 @@ -8,156 +8,160 @@ @{ -# Script module or binary module file associated with this manifest. -RootModule = 'PSCouchDB.psm1' - -# Version number of this module. -ModuleVersion = '1.4.0' - -# Supported PSEditions -# CompatiblePSEditions = @() - -# ID used to uniquely identify this module -GUID = 'b4de3821-e760-497d-a0ed-c9af0dc041dd' - -# Author of this module -Author = 'Matteo Guadrini ' - -# Company or vendor of this module -#CompanyName = '' - -# Copyright statement for this module -Copyright = '(c) Matteo Guadrini. All rights reserved.' - -# Description of the functionality provided by this module -Description = 'Powershell module for CouchDB v2.0' - -# Minimum version of the PowerShell engine required by this module -# PowerShellVersion = '' - -# Name of the PowerShell host required by this module -# PowerShellHostName = '' - -# Minimum version of the PowerShell host required by this module -# PowerShellHostVersion = '' - -# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# DotNetFrameworkVersion = '' - -# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# CLRVersion = '' - -# Processor architecture (None, X86, Amd64) required by this module -# ProcessorArchitecture = '' - -# Modules that must be imported into the global environment prior to importing this module -# RequiredModules = @() - -# Assemblies that must be loaded prior to importing this module -# RequiredAssemblies = @() - -# Script files (.ps1) that are run in the caller's environment prior to importing this module. -# ScriptsToProcess = @() - -# Type files (.ps1xml) to be loaded when importing this module -# TypesToProcess = @() - -# Format files (.ps1xml) to be loaded when importing this module -# FormatsToProcess = @() - -# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess -# NestedModules = @() - -# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = @("Get-CouchDBDatabase", - "Get-CouchDBDatabaseChanges", - "Get-CouchDBDocument", - "Get-CouchDBAttachment", - "Get-CouchDBUser", - "Get-CouchDBAdmin", - "Get-CouchDBConfiguration", - "Get-CouchDBNode", - "Get-CouchDBReplication", - "Get-CouchDBReplicationScheduler", - "Get-CouchDBActiveTask", - "Measure-CouchDBStatistics", - "Enable-CouchDBCluster", - "Add-CouchDBNode", - "Compress-CouchDBDatabase", - "Clear-CouchDBView", - "Set-CouchDBDocument", - "Set-CouchDBAttachment", - "Set-CouchDBUser", - "Set-CouchDBAdmin", - "Set-CouchDBConfiguration", - "Set-CouchDBReplication", - "Grant-CouchDBDatabasePermission", - "Revoke-CouchDBDatabasePermission", - "New-CouchDBDatabase", - "New-CouchDBDocument", - "New-CouchDBAttachment", - "New-CouchDBUser", - "New-CouchDBAdmin", - "New-CouchDBReplication", - "New-CouchDBUuids", - "Remove-CouchDBDatabase", - "Remove-CouchDBDocument", - "Remove-CouchDBAttachment", - "Remove-CouchDBUser", - "Remove-CouchDBAdmin", - "Remove-CouchDBNode", - "Remove-CouchDBReplication", - "Restart-CouchDBServer", - "Find-CouchDBDocuments" - ) - -# Cmdlet s to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = @() - -# Variables to export from this module -#VariablesToExport = '*' - -# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @() - -# DSC resources to export from this module -# DscResourcesToExport = @() - -# List of all modules packaged with this module -# ModuleList = @() - -# List of all files packaged with this module -# FileList = @() - -# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. -PrivateData = @{ - - PSData = @{ - - # Tags applied to this module. These help with module discovery in online galleries. - Tags = @("CouchDB","Apache","curl") - - # A URL to the license for this module. - # LicenseUri = '' - - # A URL to the main website for this project. - ProjectUri = 'https://matteoguadrini.github.io/PSCouchDB' - - # A URL to an icon representing this module. - # IconUri = '' - - # ReleaseNotes of this module - # ReleaseNotes = '' - - } # End of PSData hashtable - -} # End of PrivateData hashtable - -# HelpInfo URI of this module -# HelpInfoURI = '' - -# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. -# DefaultCommandPrefix = '' - -} - + # Script module or binary module file associated with this manifest. + RootModule = 'PSCouchDB.psm1' + + # Version number of this module. + ModuleVersion = '1.5.0' + + # Supported PSEditions + # CompatiblePSEditions = @() + + # ID used to uniquely identify this module + GUID = 'b4de3821-e760-497d-a0ed-c9af0dc041dd' + + # Author of this module + Author = 'Matteo Guadrini ' + + # Company or vendor of this module + #CompanyName = '' + + # Copyright statement for this module + Copyright = '(c) Matteo Guadrini. All rights reserved.' + + # Description of the functionality provided by this module + Description = 'Powershell module for CouchDB v2.0' + + # Minimum version of the PowerShell engine required by this module + # PowerShellVersion = '' + + # Name of the PowerShell host required by this module + # PowerShellHostName = '' + + # Minimum version of the PowerShell host required by this module + # PowerShellHostVersion = '' + + # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # DotNetFrameworkVersion = '' + + # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. + # CLRVersion = '' + + # Processor architecture (None, X86, Amd64) required by this module + # ProcessorArchitecture = '' + + # Modules that must be imported into the global environment prior to importing this module + # RequiredModules = @() + + # Assemblies that must be loaded prior to importing this module + # RequiredAssemblies = @() + + # Script files (.ps1) that are run in the caller's environment prior to importing this module. + # ScriptsToProcess = @() + + # Type files (.ps1xml) to be loaded when importing this module + # TypesToProcess = @() + + # Format files (.ps1xml) to be loaded when importing this module + # FormatsToProcess = @() + + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess + # NestedModules = @() + + # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. + FunctionsToExport = @("Get-CouchDBDatabase", + "Get-CouchDBDatabaseChanges", + "Get-CouchDBDocument", + "Get-CouchDBAttachment", + "Get-CouchDBUser", + "Get-CouchDBAdmin", + "Get-CouchDBConfiguration", + "Get-CouchDBNode", + "Get-CouchDBReplication", + "Get-CouchDBReplicationScheduler", + "Get-CouchDBActiveTask", + "Get-CouchDBIndex", + "Measure-CouchDBStatistics", + "Enable-CouchDBCluster", + "Add-CouchDBNode", + "Compress-CouchDBDatabase", + "Clear-CouchDBView", + "Set-CouchDBDocument", + "Set-CouchDBAttachment", + "Set-CouchDBUser", + "Set-CouchDBAdmin", + "Set-CouchDBConfiguration", + "Set-CouchDBReplication", + "Grant-CouchDBDatabasePermission", + "Revoke-CouchDBDatabasePermission", + "New-CouchDBDatabase", + "New-CouchDBDocument", + "New-CouchDBAttachment", + "New-CouchDBUser", + "New-CouchDBAdmin", + "New-CouchDBReplication", + "New-CouchDBUuids", + "New-CouchDBIndex", + "Remove-CouchDBDatabase", + "Remove-CouchDBDocument", + "Remove-CouchDBAttachment", + "Remove-CouchDBUser", + "Remove-CouchDBAdmin", + "Remove-CouchDBNode", + "Remove-CouchDBReplication", + "Remove-CouchDBIndex", + "Restart-CouchDBServer", + "Find-CouchDBDocuments" + ) + + # Cmdlet s to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. + CmdletsToExport = @() + + # Variables to export from this module + #VariablesToExport = '*' + + # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. + AliasesToExport = @() + + # DSC resources to export from this module + # DscResourcesToExport = @() + + # List of all modules packaged with this module + # ModuleList = @() + + # List of all files packaged with this module + # FileList = @() + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = @("CouchDB","Apache","curl") + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + ProjectUri = 'https://matteoguadrini.github.io/PSCouchDB' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + } # End of PSData hashtable + + } # End of PrivateData hashtable + + # HelpInfo URI of this module + # HelpInfoURI = '' + + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = '' + + } + + \ No newline at end of file diff --git a/PSCouchDB/PSCouchDB.psm1 b/PSCouchDB/PSCouchDB.psm1 index 3f29448..c9409ff 100644 --- a/PSCouchDB/PSCouchDB.psm1 +++ b/PSCouchDB/PSCouchDB.psm1 @@ -348,6 +348,26 @@ function Get-CouchDBActiveTask () { Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Authorization $Authorization } +function Get-CouchDBIndex () { + <# + .SYNOPSIS + Get indexes of database. + .DESCRIPTION + Get indexes of CouchDB database. + .EXAMPLE + Get-CouchDBIndex -Database test -Authorization "admin:password" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = $(throw "Please specify the database name."), + [string] $Authorization + ) + $Document = '_index' + Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Authorization $Authorization +} + function Measure-CouchDBStatistics () { <# .SYNOPSIS @@ -907,6 +927,35 @@ function New-CouchDBReplication () { Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization } +function New-CouchDBIndex () { + <# + .SYNOPSIS + Create a new index of database. + .DESCRIPTION + Create a new index of CouchDB database. + .EXAMPLE + New-CouchDBIndex -Database test -Name test-index -Fields name,surname -Authorization "admin:password" + #> + [CmdletBinding()] + param( + [string] $Server, + [int] $Port, + [string] $Database = $(throw "Please specify the database name."), + [string] $Name = $(throw "Please specify the name of index document."), + [array] $Fields = $(throw "Please specify at least one fields."), + [string] $Authorization + ) + $Document = '_index' + $index = @{ 'index' = @{}; 'type' = 'json' } + $index.name = "$Name" + $index.index.fields = @() + foreach ($Field in $Fields) { + $index.index.fields += $Field + } + $Data = $index | ConvertTo-Json -Depth 3 -Compress + Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Document $Document -Data $Data -Authorization $Authorization +} + function New-CouchDBUuids () { <# .SYNOPSIS @@ -1141,6 +1190,32 @@ function Remove-CouchDBReplication () { } } +function Remove-CouchDBIndex () { + <# + .SYNOPSIS + Remove a index of a database. + .DESCRIPTION + Remove a index of CouchDB database. + .EXAMPLE + $ddoc = Get-CouchDBIndex -Database test -Authorization "admin:password" + Remove-CouchDBIndex -Database test -DesignDoc $ddoc.indexes.ddoc[1] -Name $ddoc.indexes.name[1] -Authorization "admin:password" + #> + [CmdletBinding(SupportsShouldProcess = $true)] + param( + [string] $Server, + [int] $Port, + [string] $Database = $(throw "Please specify the database name."), + [string] $DesignDoc = $(throw "Please specify the designdoc id for index."), + [string] $Name = $(throw "Please specify the name of index."), + [string] $Authorization, + [switch]$Force + ) + $Document = "_index/$DesignDoc/json/$Name" + if ($Force -or $PSCmdlet.ShouldContinue("Do you wish remove index $DesignDoc ?","Remove index $DesignDoc on database $Database")) { + Send-CouchDBRequest -Server $Server -Port $Port -Method "DELETE" -Database $Database -Document $Document -Authorization $Authorization + } +} + function Restart-CouchDBServer () { <# .SYNOPSIS