View on GitHub

PSCouchDB

PSCouchDB is a powershell module then makes it possible to work with CouchDB in powershell host or ise, without a third-party tool like curl

download .ZIPdownload .TGZ

The complete and powerful powershell module for CouchDB v2.X

Powershell meet CouchDB

PSCouchDB

Installation and simple usage

  1. Download and install CouchDB following the docs.
  2. Download and install latest PSCouchDB module by copying it under %Windir%\System32\WindowsPowerShell\v1.0\Modules for all users or under %UserProfile%\Documents\WindowsPowerShell\Modules for the current user or install through PowershellGallery.

    ATTENTION: This module is not signed. Before import or execute cmdlet on this module, see about_signing session. To skip this part and continue, run Set-ExecutionPolicy -ExecutionPolicy Unrestricted

  3. Now let’s start by creating an admin user, run this cmdlet:
    $password = "password" | ConvertTo-SecureString -AsPlainText -Force
    New-CouchDBAdmin -Userid admin -Password $password
    

    ATTENTION: Authentication for read and write no required by default, but required if you create custom user, like session “Grant permission” on this document. For more information see permission on docs permission page

  4. Now, configure database mode, in single node (cluster of one single node) or cluster, run this cmdlet:
    # Single node cluster
    Enable-CouchDBCluster -SingleNode -Authorization "admin:password"
    # Cluster (with three node)
    Enable-CouchDBCluster -Authorization "admin:password"
    

    For more details, see the docs.

  5. Now, open powershell and create a first personal database:
    New-CouchDBDatabase -Database test -Authorization "admin:password"
    

    where admin is a newly created user and your password.

  6. Create a sample document for test database:
    $Data = '{
     "name": "Arthur",
     "planet": "Heart"
    }'
    New-CouchDBDocument -Database test -Document "Hitchhikers" -Data $Data -Authorization "admin:password"
    
  7. Add attachment file in our docuemnt:
    $rev = (Get-CouchDBDocument -Database test -Document "Hitchhikers")._rev
    "Ultimate Question of Life, the Universe and Everything" | Out-File C:\file.txt
    New-CouchDBAttachment -Database test -Document "Hitchhikers" -revision $rev -Attachment C:\file.txt -Authorization "admin:password"
    
  8. Finally, get a document:
    Get-CouchDBAttachment -Database test -Document "Hitchhikers" -Attachment file.txt
    

Grant permission

First, check all the available databases:

Get-CouchDBDatabase

Create a new user in _users database:

$password = "password" | ConvertTo-SecureString -AsPlainText -Force
New-CouchDBUser -Userid admin_test -Password $password -Roles admin -Authorization "admin:password"

Apply the correct permissions to the database:

Grant-CouchDBDatabasePermission -Database test -admin admin_test -AdminRoles admin -Authorization "admin:password"

Test the new permissions by creating a new document:

$Data = '{
	"color": "blue",
	"value": "#0000FF"
}'
New-CouchDBDocument -Database test -Document "blue" -Data $Data -Authorization "admin_test:passw0rd"

For more details, see the docs.

Revoke permission

To revoke all permissions on a database, use this cmdlet:

Revoke-CouchDBDatabasePermission -Database test -Authorization "admin:password"

For more details, see the docs.

Find data on database

To search for data in a specific database, use this cmdlet:

Find-CouchDBDocuments -Database test -Selector "name" -Value "Arthur" -Fields _id,name,planet -Operator eq

Selector is the search field; Value is the value of the selector; Fields they are the fields that return from the research and Operator it is the comparison operator used to compare the selector. For more details, see the docs.

Logical Operators

For logical operators, use native class PSCouchDBQuery. So, run this:

using module PSCouchDB
$myquery = New-Object PSCouchDBQuery
$myquery.AddSelector("answer", 42)
$myquery.AddSelector("name", 'Arthur')

AND

Find if “answer” is 42 and “name” is Arthur

$myquery.AddLogicalOperator('$and')
Find-CouchDBDocuments -Database test -Find $myquery.GetNativeQuery()

OR

Find if “answer” is 42 or “name” is Arthur

$myquery.AddLogicalOperator('$or')
Find-CouchDBDocuments -Database test -Find $myquery.GetNativeQuery()

NOT

Find if “answer” is not 42 and “name” is not Arthur

$myquery.AddLogicalOperator('$not')
Find-CouchDBDocuments -Database test -Find $myquery.GetNativeQuery()

NOR

Find if “answer” is not 42 or “name” is not Arthur

$myquery.AddLogicalOperator('$nor')
Find-CouchDBDocuments -Database test -Find $myquery.GetNativeQuery()

For more details, see the docs.

Complete documentation

For other operation, for more details and for learning all cmdlets and possibilities, see the docs.

Kanban board

If you are curious, if you want to contribute or simply see the features, look at the project’s kanban board here: KANBAN.

Cmdlet help

If you want to have an overview of the module, do this:

help about_pscouchdb

Search for the cmdlets using a keyword pattern and then view the help:

Search-CouchDBHelp -Pattern Database | foreach {Get-Help $_.Name}

To get examples of all the cmdlets of this module, use this command:

Get-Command -Module *PSCouchDB* | foreach {Get-Help $_.Name -Example}

or see docs. For a little demonstration, see here

Donation and Support.