Powershell meet CouchDB
- Download and install CouchDB following the docs.
- 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
- Now let's start by creating an admin user by connecting to Fauxton and going to session "Admin Party!" on left of the menu or run this cmdlet:
$password = "password" | ConvertTo-SecureString -AsPlainText -Force
New-CouchDBAdmin -Userid adminuser -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 wiki permission page
- Before configure database mode, create this system document whit this cmdlet:
New-CouchDBDatabase -Database _users -Authorization "adminuser:password"
New-CouchDBDatabase -Database _replicator -Authorization "adminuser:password"
New-CouchDBDatabase -Database _global_changes -Authorization "adminuser:password"
Now, configure database mode, in single node (cluster of one single node) or cluster, clicking on "Setup" on left of the menu; follow the wizard and complete this procedure or run this cmdlet:
Enable-CouchDBCluster -Authorization "adminuser:password"
- Now, open powershell and create a first personal database:
New-CouchDBDatabase -Database test -Authorization "adminuser:password"
where adminuser is a newly created user and your password.
- Create a sample document for
test
database:
$Data = '{
"color": "red",
"value": "#f00"
}'
New-CouchDBDocument -Database test -Document "red" -Data $Data -Authorization "adminuser:password"
- Add attachment file in our docuemnt:
$rev = (Get-CouchDBDocument -Database test -Document "red")._rev
"Blood is red" | Out-File C:\file.txt
New-CouchDBAttachment -Database test -Document "red" -revision $rev -Attachment C:\file.txt -Authorization "adminuser:password"
- Finally, get a document:
Get-CouchDBAttachment -Database test -Document "red" -Attachment file.txt
First, check all the available databases:
Get-CouchDBDatabase
Create a new user in _users database:
New-CouchDBUser -Userid admin_test -Password "passw0rd" -Roles admin -Authorization "adminuser:password"
Apply the correct permissions to the database:
Grant-CouchDBDatabasePermission -Database test -AdminUser admin_test -AdminRoles admin -Authorization "adminuser: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"
To revoke all permissions on a database, use this cmdlet:
Revoke-CouchDBDocument -Database test
To search for data in a specific database, use this cmdlet:
Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -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.
The operators available, for now, are the following:
- 'lt' The field is less than the argument.
- 'lte' The field is less than or equal to the argument.
- 'eq' The field is equal to the argument.
- 'ne' The field is not equal to the argument.
- 'gte' The field is greater than or equal to the argument.
- 'gt' The field is greater than the to the argument.
- 'exists' Check whether the field exists or not, regardless of its value.
- 'type' Check the document field’s type. Valid values are "null", "boolean", "number", "string", "array", and "object".
- 'in' The document field must exist in the list provided.
- 'nin' The document field not must exist in the list provided.
- 'size' Special condition to match the length of an array field in a document. Non-array fields cannot match this condition.
- 'regex' A regular expression pattern to match against the document field. Only matches when the field is a string value and matches the supplied regular expression. The matching algorithms are based on the Perl Compatible Regular Expression (PCRE) library. For more information about what is implemented, see the see the Erlang Regular Expression
Sort
indicates the field you want to sort ascending data.
Search data with other comparison operator:
Find-CouchDBDocuments -Database test -Selector "answer" -Value 42 -Fields _id,answer -Operator lt
Find-CouchDBDocuments -Database test -Selector "answer" -Value 42 -Fields _id,answer -Operator gt
Find-CouchDBDocuments -Database test -Selector "color" -Value "blue" -Fields _id,color -Operator ne
Find-CouchDBDocuments -Database test -Selector "color" -Value true -Fields _id,color -Operator exists
Find-CouchDBDocuments -Database test -Selector "color" -Value string -Fields _id,color -Operator type
Find-CouchDBDocuments -Database test -Selector "answer" -Value number -Fields _id,answer -Operator type
Find-CouchDBDocuments -Database test -Selector "array" -Value "value1" -Fields _id,array -Operator in
Find-CouchDBDocuments -Database test -Selector "array" -Value "value 2" -Fields _id,array -Operator nin
Find-CouchDBDocuments -Database test -Selector "array" -Value 1 -Fields _id,array -Operator size
Find-CouchDBDocuments -Database test -Selector "color" -Value "^[rR].[dD]" -Fields _id,color -Operator regex
For now, I've used the native powershell logical operators.
((Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -Operator eq).docs -and (Find-CouchDBDocuments -Database test -Selector "color" -Value "blue" -Fields _id,color -Operator eq).docs)
((Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -Operator eq).docs -or (Find-CouchDBDocuments -Database test -Selector "color" -Value "blue" -Fields _id,color -Operator eq).docs)
(-not(Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -Operator eq).docs)
(-not((Find-CouchDBDocuments -Database test -Selector "color" -Value "red" -Fields _id,color -Operator eq).docs -or (Find-CouchDBDocuments -Database test -Selector "color" -Value "blue" -Fields _id,color -Operator eq).docs))
For other operation see the wiki.
To get examples of all the cmdlets of this module, use this command:
Get-Command -Module *PSCouchDB* | foreach {Get-Help $_.Name -Example}
or see wiki page