我刚刚发布了另一个重大更新到Psgraph。此版本包括新关键字,并帮助解锁GraphViz的更多功能。这些功能将使您更容易构建实体和数据模型图。

示例数据模型图显示产品和订单表

指数

发行说明

2.1.17 20180217
* add Record command
* add Row command
* add Entity command
* add Show-PSGraph command
* add key name case correction
* throws error when there are Graphviz parse errors

记录

记录是单个节点中的一列值。

Graph {
    Record -Name Table1 -Rows @(
        'Row1'
        'Row2'
        'Row3'
    )
} | Show-PSGraph

这将生成一个如下所示的节点:

单节点记录对象

Under the covers, this is a node object. The command takes care of all the attributes and HTML label formating for you. Because this is a Node, you can created edges to it like you would any other node.

Graph {
    Record -Name Table1 -Rows @(
        'Row1'
        'Row2'
        'Row3'
    )

    Node Other
    Edge Other -To Table1
}

I offer a lot of flexible ways to work with the 记录 command. Here is the minimal DSL style syntax:

Graph {
    Record Table1 @(
        'Row1'
        'Row2'
        'Row3'
    )
}

只要知道第二个默认参数是数组,打开了许多选项。

$list = @(
    'Row1'
    'Row2'
    'Row3'
)

Record Table1 $list

$list | Record -Name Table2

-ScriptBlock.

Having an array for the second parameter of a PowerShell DSL is not that common. So I also added support for using a scriptblock. It works the same as the array in many cases.

Graph {
    Record Table1 {
        'Row1'
        'Row2'
        'Row3'
    }
}

We will make better use of that scriptblock when I introduce the command.

行命令与记录一起使用以制作更丰富的对象。

Graph {
    Record Table1 {
        Row 'Row1'
        Row 'Row2'
        Row 'Row3'
    }
}

If you take a close look at the command, it has 3 parameters.

排  -Label 'MY row' -Name 'Port1' -EncodeHTML

The -Label is the text that you see in the record. The -Lable is the default parameter when no parameter is specified. The label supports simple HTML.

排  'My Row' -Name 'Port1'
Row -Label 'First: <B>Kevin</B>'

Because the label is rendered as HTML, I added -EncodeHTML for when you data includes characters like <>& that can mess with the HTML syntax.

排  -Label 'Mom & Dad' -EncodeHTML

-姓名

您可以命名为您名称节点的行。通过给出一个名称,我们可以用边缘定位它。

Graph {
    Record Table1 {
        Row 'Row1' -Name Row1
        Row 'Row2' -Name Row2
        Row 'Row3' -Name Row3
    }

    Record Table2 {
        Row 'Row1' -Name Row1
        Row 'Row2' -Name Row2
        Row 'Row3' -Name Row3
    }

    Edge Table1:Row1 -to Table2:Row1
    Edge Table1:Row3 -to Table2:Row2
} | Show-PSGraph

两个节点与行有交叉边缘

如果标签是一个没有空格或符号的简单单词,则该行将使用它作为默认行名称。如果您开始将自定义HTML注入行,则不会有默认的行名称。

GraphViz文档将这些行名称为节点上的端口。

实体

The 实体 command takes an object and maps it into a 记录. This turned out to be a common pattern in how I was trying to use the 记录.

$object = [PSCustomObject]@{
    First = 'Kevin'
    Last = 'Marquette'
    Age = 37
}

Graph {
    Entity $object
} | Show-PSGraph

显示pscustomobject的实体

I provide 3 different views of the object with the -Show parameter. Here are the possible options.

  • Name - Property name
  • TypeName - 名称和值类型
  • Value - Name and value

以下是显示值的相同对象。

Graph {
    Entity $object -Name 'Person' -Show Value
} | Show-PSGraph

显示对象值的实体

实体将使用属性名称自动命名每行。这将允许您直接绘制边缘。我在本文结束时有一个更复杂的例子,它在行动中显示了这一点。

-姓名

如果您有一小部分要放在图形上的对象,请确保给每个自定义名称。

$servers = Import-CSV .\myservers.csv

Graph {
    $servers | ForEach-Object {
        Entity $PSItem -Name $PSItem.ComputerName
    }
}

-财产

The -财产 parameter allows for easy filtering of the properties that you want to display.

实体 $Server -Property ComputerName, CPU, Memory, IP, Location

展示

The Export-PSGraph command has a parameter called -ShowGraph that will show the graph after generating it. This release added 展示 that does the same thing with one command.

Graph {
    Node test
} | Show-PSGraph

把它拉在一起

I opened the article with a table diagram showing the 实体 command in action. I create 4 hahstables that represent database tables. I provide sample data so the types can be auto-discovered. I add all 4 of them to the graph as an 实体 with a -姓名. Then I create edges between the properties that would be the primary and foreign key relationships.

$product = [ordered]@{
    ProductName = 'Sandbox'
    ProductID = 'P4576'
    CategoryID = 'C728'
    Description = 'Tractor tire with sand'
}

$Category = [ordered]@{
    CategoryID = 'C728'
    CategoryName = 'Backyard'
}

$OrderDetail = [ordered]@{
    OrderID = 'O3294'
    ProductID = 'P4576'
    UnitPrice = 280.00
    Quantity = 1
}

$Order = [ordered]@{
    OrderID = 'O3294'
    CustomerID = 'C1034'
    Address = '123 Street, Irvine CA'
}

Graph @{rankdir='LR'} {

    Entity $Product -Name Product
    Entity $Category -Name Category
    Entity $OrderDetail -Name OrderDetail
    Entity $Order -Name Order

    Edge Product:CategoryID -to Category:CategoryID
    Edge OrderDetail:OrderID -to Order:OrderID
    Edge OrderDetail:ProductID -to Product:ProductID

} | Show-PSGraph

示例数据模型图显示产品和订单表

Here is that same diagram with -Show Value specified for each 实体:

相同的示例数据模型图,显示值而不是类型

我从中拉了那个数据模型 斯科特Guthrie's LINQ到SQL(第2部分 - 定义我们的数据模型类) 文章是因为它是一个如此伟大的示例模型。

结束语

For people coming from Graphviz, the 记录 is not a true record object as defined by the DOT language specification. I am using the HTML markup options to create this node. The base commands will continue to align closely with the Graphviz DOT language. As I add new commands, they will start to abstract away the underlying complexity.

我尚未在这些新对象上提供大量定制。目的和风格现在有点僵硬。我还没有决定揭露和实施样式选项的最佳方式。

I have already found the use of 记录 to be useful in my graphs. I should have added it a long time ago. If you find any bugs or unexpected behavior, feel free to open an issue on the GitHub.项目页面.

2.1.17版本已经存在于Psgallery上。

Find-Module PSGraph | Install-Module