美文网首页alreadyjs css html
组件分享之后端组件——从 postgres 数据库模式生成DOT

组件分享之后端组件——从 postgres 数据库模式生成DOT

作者: cn華少 | 来源:发表于2022-06-24 21:55 被阅读0次

    组件分享之后端组件——从 postgres 数据库模式生成DOT描述工具dbdot

    背景

    近期正在探索前端、后端、系统端各类常用组件与工具,对其一些常见的组件进行再次整理一下,形成标准化组件专题,后续该专题将包含各类语言中的一些常用组件。欢迎大家进行持续关注。

    组件基本信息

    内容

    本节我们分享一个从 postgres 数据库模式生成DOT描述。工具dbdot
    它是一个二进制文件,可以直接进行安装使用,具体安装包可以在这里进行获取到。

    具体使用如下:

    $ ./dbdot -dbname=pgguide -user=kewluser                                                                                                       [16:33:31]
    digraph  {
    
        node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">products</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>title</TD><TD>character varying</TD></TR> <TR><TD>price</TD><TD>numeric</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>deleted_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>tags</TD><TD>ARRAY</TD></TR>]</TABLE>>,shape=plaintext] n1;
        node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">purchase_items</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>purchase_id</TD><TD>integer</TD></TR> <TR><TD>product_id</TD><TD>integer</TD></TR> <TR><TD>price</TD><TD>numeric</TD></TR> <TR><TD>quantity</TD><TD>integer</TD></TR> <TR><TD>state</TD><TD>character varying</TD></TR>]</TABLE>>,shape=plaintext] n2;
        node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">users</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>email</TD><TD>character varying</TD></TR> <TR><TD>password</TD><TD>character varying</TD></TR> <TR><TD>details</TD><TD>USER-DEFINED</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>deleted_at</TD><TD>timestamp with time zone</TD></TR>]</TABLE>>,shape=plaintext] n3;
        node[label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD colspan="2">purchases</TD></TR>[<TR><TD>id</TD><TD>integer</TD></TR> <TR><TD>created_at</TD><TD>timestamp with time zone</TD></TR> <TR><TD>name</TD><TD>character varying</TD></TR> <TR><TD>address</TD><TD>character varying</TD></TR> <TR><TD>state</TD><TD>character varying</TD></TR> <TR><TD>zipcode</TD><TD>integer</TD></TR> <TR><TD>user_id</TD><TD>integer</TD></TR>]</TABLE>>,shape=plaintext] n4;
        n2->n1;
        n2->n4;
        n4->n3;
    
    }
    

    生成模式图

    ./dbdot -dbname=pgguide -user=kewluser > test.dot && dot -Tpng test.dot -o outfile.png && open outfile.png
    
    image.png

    将表列入白名单并为其生成架构图

    ./dbdot -dbname=pgguide -user=kewluser > test.dot --whitelist=purchase_items,purchases && dot -Tpng test.dot -o outfile-whitelisted.png && open outfile-whitelisted.png
    
    image.png

    具体使用方式可以参考如下:

      -W    ask for password
      -dbname string
            dbname for which you want to generate dot file
      -host string
            database host (default "localhost")
      -port uint
            database port (default 5432)
      -schema string
            schema name (default "public")
      -sslmode
            enable sslmode for postgres db connection
      -user string
            username of postgres db
      -whitelist string
            comma separated list of tables you want to generate dot file for
    
    本文声明:
    88x31.png
    知识共享许可协议
    本作品由 cn華少 采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。

    相关文章

      网友评论

        本文标题:组件分享之后端组件——从 postgres 数据库模式生成DOT

        本文链接:https://www.haomeiwen.com/subject/mzlmvrtx.html