Skip to content

Commit

Permalink
feat: new cmdlet Find-CouchDBDesignDocument,
Browse files Browse the repository at this point in the history
add WinningRevisionOnly on Request-CouchDBReplication cmdlet
  • Loading branch information
MatteoGuadrini committed Jul 20, 2023
2 parents 9720bdd + 436e506 commit e18b238
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
.ionide

# Sphinx documentation
docs/_build/
docs/build/
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release notes

## 2.6.0
Jul 20, 2023

- Add _Index_ parameter on **Get-CouchDBDesignDocument** cmdlet.
- Add **Find-CouchDBDesignDocument** cmdlet.
- Add _debug_ to **Send-CouchDBRequest** cmdlet.
- Add _WinningRevisionOnly_ parameter on **Request-CouchDBReplication** cmdlet.
- Fix the generation of guid

## 2.5.0
Jun 09, 2022

Expand Down
8 changes: 5 additions & 3 deletions PSCouchDB/PSCouchDB.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

# Version number of this module.
ModuleVersion = '2.5.0'
ModuleVersion = '2.6.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -174,6 +174,7 @@
"Restart-CouchDBServer",
"Search-CouchDBFullText",
"Find-CouchDBDocuments",
"Find-CouchDBDesignDocument",
"Write-CouchDBFullCommit",
"Export-CouchDBDatabase",
"Import-CouchDBDatabase",
Expand All @@ -186,7 +187,7 @@
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = 'CouchDBCachePreference','CouchDBSaveCredentialPreference'
VariablesToExport = @('CouchDBCachePreference','CouchDBSaveCredentialPreference')

# 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 = @(
Expand Down Expand Up @@ -277,6 +278,7 @@
"rps",
"scft",
"fcdoc",
"fcddoc",
"finddoc",
"wcfc",
"ecdb",
Expand Down Expand Up @@ -312,7 +314,7 @@
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = @("CouchDB", "Apache", "curl", "Database", "NoSql", "api", "http", "db")
Tags = @("CouchDB", "Apache", "curl", "Database", "NoSql", "api", "http", "db", "document", "design")

# A URL to the license for this module.
LicenseUri = 'https://github.com/MatteoGuadrini/PSCouchDB/blob/master/LICENSE.md'
Expand Down
24 changes: 23 additions & 1 deletion PSCouchDB/PSCouchDB.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ class PSCouchDBDesignDoc : PSCouchDBDocument {

# Constructor
PSCouchDBDesignDoc () {
$this._id = "_design/$((New-CouchDBUuids -Count 1).uuids[0])"
$this._id = "_design/$((New-Guid).Guid.Replace('-', $null))"
$this.views = New-Object Collections.Generic.List[PSCouchDBView]
$this.doc['_id'] = $this._id
$this.doc.Add('views', @{})
Expand Down Expand Up @@ -1093,6 +1093,7 @@ class PSCouchDBReplication {
hidden [string] $selector
hidden [string] $since_seq
hidden [bool] $use_checkpoints
hidden [bool] $winning_revs_only
hidden [hashtable] $replicator = @{}

# Constuctor
Expand Down Expand Up @@ -1229,6 +1230,11 @@ class PSCouchDBReplication {
$this.replicator.Add('use_checkpoints', $this.use_checkpoints)
}

SetWinningRevisionOnly ([bool]$value) {
$this.winning_revs_only = $value
$this.replicator.Add('winning_revs_only', $this.winning_revs_only)
}

[hashtable] GetDocument () {
return $this.replicator
}
Expand Down Expand Up @@ -1714,17 +1720,22 @@ function Send-CouchDBRequest {
[pscredential] $ProxyCredential
)
# Create PSCouchDBRequest object
Write-Debug -Message "Create PSCouchDBRequest object"
$req = New-Object PSCouchDBRequest
$parameters = @()
# Set cache
Write-Debug -Message "Set cache"
if ($Global:CouchDBCachePreference) {
$req.EnableCache()
}
# Set server
Write-Debug -Message "Set server"
if ($Server) {
Write-Verbose -Message "Set server to $Server"
$req.SetServer($Server)
}
# Set proxy server
Write-Debug -Message "Set proxy server"
if ($ProxyServer) {
if ($ProxyCredential -is [pscredential]) {
Write-Verbose -Message "Set proxy server $ProxyServer with credential"
Expand All @@ -1735,17 +1746,20 @@ function Send-CouchDBRequest {
}
}
# Set protocol
Write-Debug -Message "Set protocol"
if ($Ssl.IsPresent) {
# Set default port
Write-Verbose -Message "Enable SSL on port 6984"
$req.SetSsl()
}
# Set port
Write-Debug -Message "Set port"
if ($Port) {
Write-Verbose -Message "Set port on $Port"
$req.SetPort($Port)
}
# Set method
Write-Debug -Message "Set method"
switch ($Method) {
"HEAD" { $req.SetMethod("HEAD") }
"GET" { $req.SetMethod("GET") }
Expand All @@ -1756,6 +1770,7 @@ function Send-CouchDBRequest {
}
Write-Verbose -Message "Set method to $($req.method)"
# Set database
Write-Debug -Message "Set database"
if ($Database) {
if ($Database -match "\?") {
$arr = $Database -split "\?"
Expand All @@ -1769,6 +1784,7 @@ function Send-CouchDBRequest {
$req.SetDatabase($Database)
}
# Set document
Write-Debug -Message "Set document"
if ($Document) {
if ($Document -match "\?") {
$arr = $Document -split "\?"
Expand All @@ -1782,6 +1798,7 @@ function Send-CouchDBRequest {
$req.SetDocument($Document)
}
# Set attachment
Write-Debug -Message "Set attachment"
if ($Attachment) {
if ('GET','HEAD' -contains $Method) {
Write-Verbose -Message "Set attachment to $Attachment"
Expand All @@ -1796,11 +1813,13 @@ function Send-CouchDBRequest {
}
}
# Set revision
Write-Debug -Message "Set revision"
if ($Revision) {
Write-Verbose -Message "Set revision to $Revision"
$parameters += "rev=$Revision"
}
# Check other params
Write-Debug -Message "Set other params"
if ($Params) {
$parameters += $Params
}
Expand All @@ -1809,6 +1828,7 @@ function Send-CouchDBRequest {
$req.SetParameter($parameters)
}
# Check authorization
Write-Debug -Message "Set authorization"
if ($Authorization -or $Global:CouchDBCredential) {
if ($Global:CouchDBSaveCredentialPreference) {
if (-not($Global:CouchDBCredential)) {
Expand All @@ -1828,11 +1848,13 @@ function Send-CouchDBRequest {
}
}
# Check json data
Write-Debug -Message "Set json data"
if ($Data) {
Write-Verbose -Message "Set json data to: $Data"
$req.SetData($Data)
}
# Get header or request
Write-Debug -Message "Set header or request"
Write-Verbose -Message "Request to CouchDB server: $req"
if ($Method -eq "HEAD") {
$req.GetHeader()
Expand Down
1 change: 1 addition & 0 deletions PSCouchDB/alias/CouchDBalias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ New-Alias -Name "rps" -Value Remove-CouchDBProxy -Option ReadOnly
New-Alias -Name "rcsrv" -Value Restart-CouchDBServer -Option ReadOnly
New-Alias -Name "scft" -Value Search-CouchDBFullText -Option ReadOnly
New-Alias -Name "fcdoc" -Value Find-CouchDBDocuments -Option ReadOnly
New-Alias -Name "fcddoc" -Value Find-CouchDBDesignDocument -Option ReadOnly
New-Alias -Name "wcfc" -Value Write-CouchDBFullCommit -Option ReadOnly
New-Alias -Name "ecdb" -Value Export-CouchDBDatabase -Option ReadOnly
New-Alias -Name "icdb" -Value Import-CouchDBDatabase -Option ReadOnly
Expand Down
119 changes: 119 additions & 0 deletions PSCouchDB/functions/CouchDBdesigndocument.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function Get-CouchDBDesignDocument () {
CouchDB API:
GET /{db}/_design/{ddoc}
HEAD /{db}/_design/{ddoc}
GET /{db}/_design/{ddoc}/_search_info/{index}
.PARAMETER Server
The CouchDB server name. Default is localhost.
.PARAMETER Port
Expand All @@ -175,6 +176,9 @@ function Get-CouchDBDesignDocument () {
The CouchDB database.
.PARAMETER Document
The CouchDB design document.
.PARAMETER Index
Executes a search request against the named index in the specified design document.
WARNING: Search endpoints require a running search plugin connected to each cluster node.
.PARAMETER Info
The CouchDB header of document.
.PARAMETER Authorization
Expand Down Expand Up @@ -205,6 +209,7 @@ function Get-CouchDBDesignDocument () {
[string] $Database,
[Parameter(mandatory = $true, ValueFromPipeline = $true, Position = 1)]
[string] $Document,
[string] $Index,
[switch] $Info,
$Authorization,
[switch] $Ssl,
Expand Down Expand Up @@ -235,10 +240,124 @@ function Get-CouchDBDesignDocument () {
Set-Variable -Name $Variable -Value $exportDdoc -Scope Global
return $null
}
# Check method
if ($Info.IsPresent) { $Method = "HEAD" } else { $Method = "GET" }
# Search index
if ($Index) { $Method = "GET"; $Document += "/_search_info/$Index" }
Send-CouchDBRequest -Server $Server -Port $Port -Method $Method -Database $Database -Document $Document -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
}

function Find-CouchDBDesignDocument {
<#
.SYNOPSIS
Executes a search in the specified design document.
.DESCRIPTION
Executes a search request against the named index in the specified design document.
.NOTES
CouchDB API:
GET /{db}/_design/{ddoc}/_search/{index}
.PARAMETER Server
The CouchDB server name. Default is localhost.
.PARAMETER Port
The CouchDB server port. Default is 5984.
.PARAMETER Database
The CouchDB database.
.PARAMETER Document
The CouchDB design document.
.PARAMETER Index
The index name.
.PARAMETER Bookmark
A bookmark received from a previous search. This parameter enables paging through the results.
.PARAMETER Counts
An array of names of string fields for which counts are requested.
.PARAMETER GroupField
Field by which to group search matches. :query number group_limit: Maximum group count.
.PARAMETER GroupSort
This field defines the order of the groups in a search that uses GroupField. The default sort order is relevance.
.PARAMETER IncludeDocs
Include the full content of the documents in the response.
.PARAMETER IncludeFields
Field names to include in search results. Any fields that are included must be indexed with the store:true option.
.PARAMETER Limit
Limit the number of the returned documents to the specified number. For a grouped search, this parameter limits the number of documents per group.
.PARAMETER Sort
Specifies the sort order of the results. In a grouped search (when group_field is used), this parameter specifies the sort order within a group. The default sort order is relevance.
.PARAMETER Stale
Set to ok to allow the use of an out-of-date index.
.PARAMETER Authorization
The CouchDB authorization form; user and password.
Authorization format like this: user:password
ATTENTION: if the password is not specified, it will be prompted.
.PARAMETER Ssl
Set ssl connection on CouchDB server.
This modify protocol to https and port to 6984.
.PARAMETER Variable
Export into a PSCouchDBDesignDoc variable object.
.PARAMETER ProxyServer
Proxy server through which all non-local calls pass.
Ex. ... -ProxyServer 'http://myproxy.local:8080' ...
.PARAMETER ProxyCredential
Proxy server credential. It must be specified with a PSCredential object.
.EXAMPLE
Find-CouchDBDesignDocument -Database test -Document "space" -Index "planet1" -Limit 25
This example search the named index "planet1" into "space" design document on database "test".
.LINK
https://pscouchdb.readthedocs.io/en/latest/ddoc.html#find-a-design-document
#>
[CmdletBinding()]
param(
[string] $Server,
[int] $Port,
[Parameter(mandatory = $true, Position = 0)]
[string] $Database,
[Parameter(mandatory = $true, ValueFromPipeline = $true, Position = 1)]
[string] $Document,
[Parameter(mandatory = $true, ValueFromPipeline = $true, Position = 2)]
[string] $Index,
[string] $Bookmark,
[array] $Counts,
[hashtable] $GroupField,
[hashtable] $GroupSort,
[switch] $IncludeDocs,
[switch] $IncludeFields,
[int] $Limit,
[string] $Sort,
$Authorization,
[switch] $Ssl,
[string] $Variable,
[string] $ProxyServer,
[pscredential] $ProxyCredential
)
$Params = @()
$Document = "_design/$Document/_search/$Index"
# Check parameters
if ($Bookmark) {
$Params += "bookmark=$($Bookmark | ConvertTo-Json -Compress)"
}
if ($Counts) {
$Params += "counts=$($counts | ConvertTo-Json -Compress)"
}
if ($GroupField) {
$Params += "group_field=$($GroupField | ConvertTo-Json -Compress)"
}
if ($GroupSort) {
$Params += "group_sort=$($GroupSort | ConvertTo-Json -Compress)"
}
if ($IncludeDocs) {
$Params += "include_docs=$($IncludeDocs.IsPresent | ConvertTo-Json -Compress)"
}
if ($IncludeFields) {
$Params += "include_fields=$($IncludeFields.IsPresent | ConvertTo-Json -Compress)"
}
if ($Limit) {
$Params += "limit=$($Limit | ConvertTo-Json -Compress)"
}
if ($Sort) {
$Params += "sort=$($Sort | ConvertTo-Json -Compress)"
}
Send-CouchDBRequest -Server $Server -Port $Port -Method "GET" -Database $Database -Document $Document -Params $Params -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
}

function Get-CouchDBDesignDocumentAttachment () {
<#
.SYNOPSIS
Expand Down
4 changes: 4 additions & 0 deletions PSCouchDB/functions/CouchDBreplication.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ function Request-CouchDBReplication () {
The CouchDB server port. Default is 5984.
.PARAMETER Data
The data in json format or PSCouchDBReplication object.
.PARAMETER WinningRevisionOnly
Replication with this mode discards conflicting revisions, so it could be one way to remove conflicts through replication.
.PARAMETER Authorization
The CouchDB authorization form; user and password.
Authorization format like this: user:password
Expand Down Expand Up @@ -614,6 +616,7 @@ function Request-CouchDBReplication () {
[string] $Server,
[int] $Port,
$Data,
[switch] $WinningRevisionOnly,
$Authorization,
[switch] $Ssl,
[string] $ProxyServer,
Expand All @@ -622,6 +625,7 @@ function Request-CouchDBReplication () {
$Database = "_replicate"
# Check data if PSCouchDBReplication or string
if ($Data -is [PSCouchDBReplication]) {
if ($WinningRevisionOnly.IsPresent) { $Data.SetWinningRevisionOnly($true) }
$Data = $Data.ToJson()
}
Send-CouchDBRequest -Server $Server -Port $Port -Method "POST" -Database $Database -Data $Data -Authorization $Authorization -Ssl:$Ssl -ProxyServer $ProxyServer -ProxyCredential $ProxyCredential
Expand Down
3 changes: 3 additions & 0 deletions docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
.wy-nav-side {
background: #2A425E;
}
.wy-menu-vertical a:hover {
background-color: #4075b1;
}
</style>

{% endblock %}
Loading

0 comments on commit e18b238

Please sign in to comment.