-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCouchDB.psm1
347 lines (270 loc) · 9.82 KB
/
CouchDB.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<#
.Synopsis
Sends a request to a CouchDB database server.
.Description
Sends a request to a CouchDB database server.
#>
function Send-CouchDbRequest {
param(
[string] $method = "GET",
[string] $dbHost = "127.0.0.1",
[int] $port = 5984,
[string] $database = $(throw "Please specify the database name."),
[string] $document,
[string] $rev,
[string] $attachment,
[string] $data,
[switch] $includeDoc
)
if (($attachment -ne $null) -and ($document -eq $null)) {
throw "Cannot accept an attachment name without a document id"
}
# Build the URL
# Don't null-or-empty check the $database parameter. An exception is thrown
# if it's not present. An empty string can be used to retrieve the CouchDB
# version information (GET on http://couchdb:5984/).
$database = $database.Trim().ToLower()
$url = "http://${dbHost}:$port/$database"
$document = $document.Trim()
if (![string]::IsNullOrEmpty($document)) {
$url += "/$document"
}
$attachment = $attachment.Trim()
if (![string]::IsNullOrEmpty($attachment)) {
$url += "/$attachment"
}
# Build the query string
$queryString = @{}
$rev = $rev.Trim()
if (![string]::IsNullOrEmpty($rev)) {
$queryString["rev"] = $rev
}
if ($includeDoc.IsPresent) {
$queryString["include_doc"] = "true"
}
# Add the query string to the URL, if there is anything to add.
if ($queryString.Count -gt 0) {
$url += (Format-QueryString $queryString)
}
$request = [System.Net.WebRequest]::Create("$url")
$request.Method = $method
$request.UserAgent = "Posh-Couch"
# Echo the request to screen for informational purposes.
Write-Host $method $url
if (($method -eq "POST") -and ($data -ne $null)) {
$requestStream = $request.GetRequestStream()
$writeStream = New-Object System.IO.StreamWriter $requestStream
$writeStream.WriteLine($data)
$writeStream.Close()
# Echo the $data to screen for informational purposes.
Write-Host $data
}
# Set up error handling for the CouchDB requests
trap [System.Net.WebException] {
Handle-CouchDBError "$method $url" $_
return
}
# At last! Make the request!
$response = $request.GetResponse()
$responseStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $responseStream
$responseData = $readStream.ReadToEnd()
$readStream.Close()
$response.Close()
# Return the result from CouchDB. This is JSON-formatted.
return $responseData
}
function Handle-CouchDBError {
param(
[string] $request,
[System.Management.Automation.ErrorRecord] $error)
# Write a blank line for whitespacing purposes
Write-Host
if ($error.Exception.Status -eq [System.Net.WebExceptionStatus]::ConnectFailure) {
Write-Host -ForegroundColor Red "CouchDB is not listening on port $port on the server $server."
return
}
if ($error.Exception.Status -eq [System.Net.WebExceptionStatus]::ProtocolError) {
$description = $error.Exception.Message
Write-Host -ForegroundColor Red "CouchDB didn't like the request `"$request`". Here's what it took issue with:`n`t${description}`n"
Write-Host -ForegroundColor Red "Note that CouchDB parameters (database names, attachment names, etc.) must be lower case.`n"
return
}
}
<#
.Synopsis
Serialises an arbitrary hashtable into a query string.
.Description
Serialises an arbitrary hashtable into a query string.
#>
function Format-QueryString {
param([hashtable] $hashtable)
$queryString = "?"
foreach($key in $hashtable.Keys) {
$queryString += [string]::Format("{0}={1}&", $key, $hashtable.$key)
}
return $queryString.TrimEnd("&")
}
<#
.Synopsis
Creates a new CouchDB database.
.Description
Creates a new CouchDB database.
.Parameter Name
The name of the database that you wish to created. This is a required
parameter. CouchDB requires that the database name be entered entirely in
lowercase.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
.Example
# Create a database called "test"
Create-Database -Name "test"
.Example
# Create a database called "test" running on server foo and port 1234
Create-Database -Name "test" -Server "foo" -Port 1234
#>
function New-CouchDbDatabase {
param(
[string] $name = $(throw "Database name is required."),
[string] $server = "127.0.0.1",
[int] $port = 5984
)
Send-CouchDbRequest -method "PUT" -dbHost $server -port $port -database $name
}
<#
.Synopsis
Delete a new CouchDB database.
.Description
Delete a new CouchDB database.
.Parameter Name
The name of the database that you wish to created. This is a required
parameter. CouchDB requires that the database name be entered entirely in
lowercase.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
.Example
# Delete a database called "test"
Remove-CouchDbDatabase -Name "test"
.Example
# Delete a database called "test" running on server foo and port 1234
Remove-CouchDbDatabase -Name "test" -Server "foo" -Port 1234
#>
function Remove-CouchDbDatabase {
param(
[string]$database = $(throw "Datbase name is required."),
[string]$server = "127.0.0.1",
[int]$port = 5984
)
Send-CouchDbRequest -method "DELETE" -dbHost $server -port $port -database $database
}
<#
.Synopsis
Creates a new document in the specified CouchDB database.
.Description
Creates a new document in the specified CouchDB database.
.Parameter Database
The name of the database in which the document should be created.
.Parameter Document
The document to be saved to CouchDB. This must be a valid JSON document.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
#>
function New-CouchDbDocument {
param(
[string] $database = $(throw "Database name is required."),
[string] $server = "127.0.0.1",
[int] $port = 5984,
[string] $document = $(throw "Document is required.")
)
Send-CouchDbRequest -method "POST" -dbHost $server -port $port -database $database -data $document
}
<#
.Synopsis
Retrieves the specified document from the specified CouchDB database.
.Description
Retrieves the specified document from the specified CouchDB database.
.Parameter Database
The name of the database in which the document is stored.
.Parameter Document
The identifier for the document to be retrieved from the specified CouchDB database.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
.Example
# Get the document with ID f42d2e0c5be0a7ab7bdc1cba23fc1d73 from the invoicing database.
Get-CouchDbDocument -document f42d2e0c5be0a7ab7bdc1cba23fc1d73 -database "invoicing"
#>
function Get-CouchDbDocument {
param(
[string] $document = $(throw "Document ID is required."),
[string] $database = $(throw "Database name is required."),
[string] $server = "127.0.01",
[int] $port = 5984
)
Send-CouchDbRequest -dbHost $server -port $port -database $database -document $document -includeDoc
}
<#
.Synopsis
Deletes the specified document from the specified CouchDB database.
.Description
Deletes the specified document from the specified CouchDB database.
.Parameter Document
The identifier for the document to be deleted from the database.
.Parameter Database
The database from which the document is to be deleted.
.Parameter Revision
The revision of the document to be deleted.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
#>
function Remove-CouchDbDocument {
param(
[string] $document = $(throw "Document ID is required."),
[string] $database = $(throw "Database name is required."),
[string] $revision = $(throw "Document revision ID is required."),
[string] $server = "127.0.0.1",
[int] $port = 5984
)
Send-CouchDbRequest -method "DELETE" -dbHost $server -port $port -database $database -document $document -rev $revision
}
<#
.Synopsis
Get all CouchDB databases
.Description
Get a list of all the databases available on the specified CouchDB server.
.Parameter Server
The host name on which CouchDB is running. Defaults to 127.0.0.1
.Parameter Port
The port number on which CouchDB is listening. Defaults to CouchDB's native
port, 5984.
.Example
# Get All CouchDB Databases
Get-CouchDbDatabases
#>
function Get-CouchDbDatabases {
param(
[string]$server = "127.0.0.1",
[int]$port = 5984
)
Send-CouchDbRequest -method "GET" -dbHost $server -port $port -database "_all_dbs"
}
Export-ModuleMember -Function New-CouchDbDatabase
Export-ModuleMember -Function New-CouchDbDocument
Export-ModuleMember -Function Remove-CouchDbDocument
Export-ModuleMember -Function Remove-CouchDbDatabase
Export-ModuleMember -Function Get-CouchDbDocument
Export-ModuleMember -Function Get-CouchDbDatabases