Skip to content

Commit

Permalink
Work on types, completers, scripts, help
Browse files Browse the repository at this point in the history
  • Loading branch information
nightroman committed Nov 9, 2019
1 parent d4bba6d commit e95e731
Show file tree
Hide file tree
Showing 17 changed files with 898 additions and 390 deletions.
6 changes: 2 additions & 4 deletions .build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ task Package Markdown, ?UpdateScript, {
$ModuleRoot\*

Copy-Item -Destination z\tools\$ModuleName\Scripts `
.\Scripts\Get-MongoFile.ps1,
.\Scripts\Update-MongoFiles.ps1,
.\Scripts\Mdbc.ArgumentCompleters.ps1
.\Scripts\Mdbc.ArgumentCompleters.ps1,
.\Scripts\Update-MongoFiles.ps1
}

# Synopsis: Make NuGet package.
Expand Down Expand Up @@ -241,7 +240,6 @@ task TestHelpSynopsis {
task TestHelp Help, TestHelpExample, TestHelpSynopsis

$UpdateScriptInputs = @(
'Get-MongoFile.ps1'
'Mdbc.ArgumentCompleters.ps1'
'Update-MongoFiles.ps1'
)
Expand Down
70 changes: 46 additions & 24 deletions Module/en-US/Mdbc.dll-Help.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,17 @@ documents use an empty filter, e.g. @{}.

$UpdateParameter = @'
Specifies the update expression.
The argument is either JSON or similar dictionary.
Use dictionary arrays to define aggregate expressions.
The argument is JSON, similar dictionary, or array for aggregate expressions.
'@

$AsParameter = @'
Specifies the output document type. The argument is either the required type or
a shortcut enum value for special types.
the special type shortcut.
A type specifies the output type literally. Type properties must match the
A type specifies the output type literally. Type properties should match the
document fields or the custom type serialization must be registered.
Shortcuts are either enum values or strings:
Special type shortcut strings or enum values:
Default
Default output, Mdbc.Dictionary with underlying BsonDocument.
Expand Down Expand Up @@ -287,9 +286,7 @@ Cmdlets with the parameter Collection use it as the default value.
}
test = {
$collections = @(. $args[0])
#! MMAPv1: files, system.indexes; WiredTiger: files
if ($collections.Count -lt 1) { throw }
if ($collections -notcontains 'files') { throw }
if (!$collections -or $collections[0].GetType().Name -ne 'String') { throw }
}
}
)
Expand Down Expand Up @@ -682,42 +679,53 @@ Merge-Helps $ADatabase @{
command = 'Invoke-MdbcCommand'
synopsis = 'Invokes database commands.'
description = @'
This cmdlet is normally used in order to invoke commands not covered by the
module and driver. See MongoDB manuals for available commands and syntax.
This cmdlet is useful in order to invoke commands not covered by the module.
See MongoDB for available commands and syntax.
'@
parameters = @{
Command = @{
required = $true
description = @'
Specifies the command to be invoked.
The argument is either JSON or similar dictionary, for example hashtable:
The argument is JSON, ordered dictionary, Mdbc.Dictionary, one item hashtable.
JSON:
Invoke-MdbcCommand '{create: "test", capped: true, size: 10485760}'
Ordered dictionary:
Invoke-MdbcCommand @{create='test'; capped=$true; size=1kb; max=5 }
Invoke-MdbcCommand ([ordered]@{create='test'; capped=$true; size=10mb })
If the element order in a command is important then hashtables may not work.
Use JSON strings, ordered tables [ordered]@{...}, or Mdbc.Dictionary instead:
Mdbc.Dictionary, ordered by definition:
$c = New-MdbcData
$c.create = 'test'
$c.capped = $true
$c.size = 1kb
$c.max = 5
$c.size = 10mb
Invoke-MdbcCommand $c
'@
}
As = $AsParameter
}
outputs = @{
type = '[Mdbc.Dictionary]'
description = 'The response document wrapped by Mdbc.Dictionary.'
}
outputs = @(
@{
type = '[Mdbc.Dictionary]'
description = 'The response document wrapped by Mdbc.Dictionary.'
}
@{
type = '[object]'
description = 'Other object type depending on As.'
}
)
links = @(
@{ text = 'MongoDB'; URI = 'http://www.mongodb.org' }
)
examples = @(
@{
code = {
# Invoke the command `serverStatus`
# Get the server status, one item hashtable is fine

Connect-Mdbc . test
Invoke-MdbcCommand @{serverStatus = 1}
}
Expand All @@ -726,6 +734,20 @@ Use JSON strings, ordered tables [ordered]@{...}, or Mdbc.Dictionary instead:
if ($response.host -ne $env:COMPUTERNAME) {throw}
}
}
@{
code = {
# Connect the database `test`, get statistics for the collection
# `test.test`. Mind [ordered], otherwise the command may fail:
# "`scale` is unknown command".

Connect-Mdbc . test
Invoke-MdbcCommand ([ordered]@{collStats = "test"; scale = 1mb})
}
test = {
# just run, used to fail without [ordered]
. $args[0]
}
}
@{
code = {
# Connect the database `test` and invoke the command with
Expand Down Expand Up @@ -757,14 +779,14 @@ Use JSON strings, ordered tables [ordered]@{...}, or Mdbc.Dictionary instead:
$null = Invoke-MdbcCommand $c

# add 10 documents
1..10 | .{process{ @{_id = $_} }} | Add-MdbcData
foreach($_ in 1..10) {Add-MdbcData @{_id = $_}}

# get 5 documents
Get-MdbcData
}
test = {
$data = . $args[0]
if ($data.Count -ne 5) {throw}
$r = . $args[0]
if ($r.Count -ne 5) {throw}
}
}
)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ Get-MdbcData -Count

## Next Steps

[HowTo.test.ps1]: https://github.com/nightroman/Mdbc/blob/master/Tests/HowTo.test.ps1

Read cmdlet help topics and take a look at examples for some basic use cases.

Use *Scripts\Mdbc.ArgumentCompleters.ps1* for database and collection name
completion. Other scripts are toys but may be useful. See *Tests* in the
repository for more technical examples.
Use *Scripts/Mdbc.ArgumentCompleters.ps1* for database and collection name completion and property completion.
*Scripts/Update-MongoFiles.ps1* is a toy for making test data but may be useful for tracking file changes.
See also [HowTo.test.ps1] sample tasks and other tests for more technical examples.

Mdbc cmdlets are designed for rather basic tasks.
For advanced tasks use the driver API directly.
Expand Down
17 changes: 17 additions & 0 deletions Release-Notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Mdbc Release Notes

## v6.0.1

**Mdbc.Dictionary and Mdbc.Collection**

- Add/Set `byte[]` should map to `BsonBinaryData`, not `BsonArray`.
- Override `Equals` and `GetHashCode` similar to wrapped types.
- Map `decimal` <-> `BsonDecimal128`.

**/Scripts**

- `Mdbc.ArgumentCompleters.ps1` -- add name completers for `Get-MdbcDatabase`,
`Remove-MdbcDatabase`, `Get-MdbcCollection`, `Remove-MdbcCollection`,
`Rename-MdbcCollection`.
- Rework `Update-MongoFiles.ps1` -- save less data, use shorter lower case
field names, add some comments.
- Remove `Get-MongoFile.ps1` as not quite useful or significant.

## v6.0.0

Many changes including breaking in order to adopt v2 driver API, improve
Expand Down
51 changes: 0 additions & 51 deletions Scripts/Get-MongoFile.ps1

This file was deleted.

56 changes: 45 additions & 11 deletions Scripts/Mdbc.ArgumentCompleters.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

<#
.Synopsis
Argument completers for Mdbc commands.
.Description
The script registers Mdbc completers for command parameters:
The script adds completers for:
Connect-Mdbc
-DatabaseName ..
Expand All @@ -15,19 +14,26 @@
Export-MdbcData
-Property .., ..
Completers can be used with:
Get-MdbcDatabase
Get-MdbcCollection
Remove-MdbcDatabase
Remove-MdbcCollection
Rename-MdbcCollection
[-Name] ..
* PowerShell v5 native Register-ArgumentCompleter
Simply invoke Mdbc.ArgumentCompleters.ps1, e.g. in a profile.
How to use:
* TabExpansionPlusPlus https://github.com/lzybkr/TabExpansionPlusPlus
Put Mdbc.ArgumentCompleters.ps1 to TabExpansionPlusPlus module directory in
order to be loaded automatically. Or invoke it after importing the module,
e.g. in a profile.
* PowerShell v5 native
Invoke Mdbc.ArgumentCompleters.ps1, e.g. in a profile.
* TabExpansion2.ps1 https://www.powershellgallery.com/packages/TabExpansion2
Put Mdbc.ArgumentCompleters.ps1 to the path in order to be loaded on the
first completion. Or invoke after TabExpansion2.ps1, e.g. in a profile.
Put Mdbc.ArgumentCompleters.ps1 to the path.
Or invoke after TabExpansion2.ps1, e.g. in a profile.
* TabExpansionPlusPlus https://github.com/lzybkr/TabExpansionPlusPlus
Put Mdbc.ArgumentCompleters.ps1 to TabExpansionPlusPlus directory.
Or invoke after importing the module, e.g. in a profile.
NOTE: Not all completers work in TabExpansionPlusPlus.
#>

Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName DatabaseName -ScriptBlock {
Expand All @@ -40,6 +46,20 @@ Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName DatabaseName
}}
}

Register-ArgumentCompleter -CommandName Get-MdbcDatabase, Remove-MdbcDatabase -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)

if (!($myClient = $boundParameters['Client'])) { $myClient = $Client }

@(
foreach($_ in Get-MdbcDatabase -Client $myClient) {
$_.DatabaseNamespace.DatabaseName
}
) -like "$wordToComplete*" | .{process{
New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_
}}
}

Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName CollectionName -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)

Expand All @@ -51,6 +71,20 @@ Register-ArgumentCompleter -CommandName Connect-Mdbc -ParameterName CollectionNa
}}
}

Register-ArgumentCompleter -CommandName Get-MdbcCollection, Remove-MdbcCollection, Rename-MdbcCollection -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $boundParameters)

if (!($myDatabase = $boundParameters['Database'])) { $myDatabase = $Database }

@(
foreach($_ in Get-MdbcCollection -Database $myDatabase) {
$_.CollectionNamespace.CollectionName
}
) -like "$wordToComplete*" | .{process{
New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_
}}
}

Register-ArgumentCompleter -CommandName Add-MdbcData, New-MdbcData, Export-MdbcData -ParameterName Property -ScriptBlock {
$private:commandName, $private:parameterName, $private:wordToComplete, $private:commandAst, $private:boundParameters = $args

Expand Down
Loading

0 comments on commit e95e731

Please sign in to comment.