Git Product home page Git Product logo

typeorm-uml's Introduction

typeorm-uml

oclif Version Downloads/week License

A command line tool to generate UML diagrams for Typeorm projects. It uses plantuml to render diagrams and outputs an URL to a diagram.

Installation

Install this command as a development dependency to your project:

npm i -D typeorm-uml

Usage

Add a new script to your package.json to be able to run it:

{
    "name": "myproject",
    "scripts": {
        "db:diagram": "typeorm-uml ormconfig.json"
    }
}

Then run npm run db:diagram and you will receive an URL to an image with your diagram. You can use this URL to add to your README file or you can download the image and add it to your repository.

Synopsis

USAGE
  $ typeorm-uml [CONFIGNAME]

ARGUMENTS
  CONFIGNAME  [default: ormconfig.json] Path to the Typeorm config file.

OPTIONS
  -D, --direction=(TB|LR)          [default: TB] Arrows directions. TB=top to bottom, LR=left to right.
  -c, --connection=connection      [default: default] The connection name.
  -d, --download=download          The filename where to download the diagram.
  -e, --exclude=exclude            Comma-separated list of entities to exclude from the diagram.
  -f, --format=(png|svg|txt|puml)  [default: png] The diagram file format.
  -i, --include=include            Comma-separated list of entities to include into the diagram.
  --color=pkey=#aaa                Custom colors to use for the diagram.
  --handwritten                    Whether or not to use handwritten mode.
  --monochrome                     Whether or not to use monochrome colors.
  --plantuml-url=plantuml-url      [default: http://www.plantuml.com/plantuml] URL of the plantuml server to use.
  --with-entity-names-only         Whether or not to display only entity names and hide database table names.
  --with-enum-values               Whether or not to show possible values for the enum type field.
  --with-table-names-only          Whether or not to display only database table names and hide entity names.

Defining custom colors

If you want to override colors used in the diagram, you can do it using --color flag. It accepts the key-value pair where key is an element and value is a color. You can use multiple --color flags to override multiple elements. For example:

typeorm-uml path/to/ormconfig.json --color class.ArrowColor=#ff9900 --color class.BorderColor=#ff9900 --color class.BackgroundColor=#efefef --color column=#ddd

You can use pkey, fkey and column colors to override entity column icons, and class.BackgroundColor, class.BorderColor, class.ArrowColor to override enity class styles.

Typescript

If you use .ts entities in your Typeorm config, then run this command with ts-node like this:

ts-node ./node_modules/.bin/typeorm-uml ormconfig.json

PlantUML

Under the hood, this library uses the PlantUML Language to define diagrams and the official plantuml server to draw it.

In most cases, it's fine to use it without a doubt. However, it's not always the case. If you work on a project that has a strict security level and you can't use the public server, then you can set up your own using the official docker image of PlantUML server and use the --plantuml-url flag to let this library know its location.

Run Programmatically

You can also import the TypeormUml class from this package and build UML diagrams on your own. See this small example:

import { EOL } from 'os';
import { join } from 'path';

import { Direction, Flags, Format, TypeormUml } from 'typeorm-uml';

const configPath = join( __dirname, 'path/to/ormconfig.json' );
const flags: Flags = {
    direction: Direction.LR,
    format: Format.SVG,
    handwritten: true,
};

const typeormUml = new TypeormUml();
typeormUml.build( configPath, flags ).then( ( url ) => {
    process.stdout.write( 'Diagram URL: ' + url + EOL );
} );

Please, pay attention that the TypeormUml::build() method also accepts connection instance itself, so you don't need to compose a configuration file if you don't have one in your project. Here is another small example of how it can be used in the typeorm/typescript-example project:

import { EOL } from 'os';
import { join } from 'path';

import { Direction, Flags, Format, TypeormUml } from 'typeorm-uml';
import { createConnection } from 'typeorm';

createConnection().then( async ( connection ) => {
    const flags: Flags = {
        direction: Direction.LR,
        format: Format.SVG,
        handwritten: true,
    };

    const typeormUml = new TypeormUml();
    const url = await typeormUml.build( connection, flags );

    process.stdout.write( 'Diagram URL: ' + url + EOL );
} );

Example

typeorm/typescript-example

typeorm-uml --format=svg --with-table-names-only

typeorm/typescript-example

Contribute

Want to help or have a suggestion? Open a new ticket and we can discuss it or submit a pull request.

License

MIT

typeorm-uml's People

Contributors

cchitsiang avatar dependabot[bot] avatar eugene-manuilov avatar gacktomo avatar maikvv avatar munky69rock avatar prokopsimek avatar raphael-leger avatar reidond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typeorm-uml's Issues

Allow the configuration to be given as a json object

Hi,

Currently, TypeormUml exposes a method build that has the following signature:

public async build( configName: string, flags: Flags )

where configName is the path of the orm config file, usually something called ormconfig.json

However, there are projects where the json orm configuration is not into a json file as it is built during runtime, for instance to allow the use of environment variables into the configuration.

It would be nice if typeorm-url could accept an orm config as json instead of a path to a json file.

The signature could become:

public async build( flags: Flags )

There would be two new flags :

  • jsonConfigPath: string
  • jsonConfig: any

And users of this library would be required to either use jsonConfigPath that is a path to a json config file or jsonConfig that is the raw json object, not stored into any file

Add captions to the diagram icons on `README.md`

since the icon meanings aren't standard, can you please add something like this:

Character Icon Description
- (not used thus you can get rid of this row)
# (?? I'm not sure about this one)
~ unique column
+ primary key column

Plantuml server does not support https

Hi again,

The default plantuml url should probably be http://www.plantuml.com/plantuml (http, not https)

const plantumlUrl = flags['plantuml-url'] || 'https://www.plantuml.com/plantuml';

With the current default, executing new TypeormUml().build(...) produces the following error:

(node:12798) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
    at new ClientRequest (_http_client.js:155:11)
    at request (http.js:50:10)
    at Object.get (http.js:54:15)
    at /myproject/node_modules/typeorm-uml/lib/builder/typeorm-uml.class.js:63:20

To be noted that when accessing http://www.plantuml.com/plantuml or https://www.plantuml.com/plantuml in a browser (chrome at least), it shows up 'Not Secure' in the status bar.

Marks foreign keys

I think it would be nice to have the suffix <<FK>> to all foreign keys columns

return ` ${ prefix }${ columnName }: ${ type.toUpperCase() }${ length }\n`;

You can verify if the column is foreign key by using if (!!column.referencedColumn) (source)

What do you think?

Support changing arrows direction

What about add the --direction option to allow us specify a connection direction?

  --direction                    [default: TB] Arrows directions. TB=top to bottom, LR=left to right.

basically append the string 'left to right direction\n' or 'top to bottom direction\n' after '@startuml\n'

public buildUml( connection: Connection, flags: TypeormUmlCommandFlags ): string {
let uml = '@startuml\n\n';

Error: Unexpected token import

With DEBUG I do not really get anything useful

serge@test-pierre:~/eve/MyProject$ typeorm-uml
  @oclif/config reading core plugin /usr/local/lib/node_modules/typeorm-uml +0ms
  @oclif/config loadJSON /usr/local/lib/node_modules/typeorm-uml/package.json +0ms
  @oclif/config loadJSON /usr/local/lib/node_modules/typeorm-uml/oclif.manifest.json +20ms
(node:28273) Error Plugin: typeorm-uml: files attribute must be specified in /usr/local/lib/node_modules/typeorm-uml/package.json
module: @oclif/[email protected]
plugin: typeorm-uml
root: /usr/local/lib/node_modules/typeorm-uml
See more details with DEBUG=*
Error Plugin: typeorm-uml: files attribute must be specified in /usr/local/lib/node_modules/typeorm-uml/package.json
    at Plugin.warn (/usr/local/lib/node_modules/typeorm-uml/node_modules/@oclif/config/lib/plugin.js:243:19)
    at Plugin.load (/usr/local/lib/node_modules/typeorm-uml/node_modules/@oclif/config/lib/plugin.js:100:18)
    at <anonymous>
module: @oclif/[email protected]
plugin: typeorm-uml
root: /usr/local/lib/node_modules/typeorm-uml
See more details with DEBUG=*
  @oclif/config loadJSON /usr/local/lib/node_modules/typeorm-uml/.oclif.manifest.json +0ms
  @oclif/config reading user plugins pjson /home/serge/.local/share/typeorm-uml/package.json +0ms
  @oclif/config loadJSON /home/serge/.local/share/typeorm-uml/package.json +1ms
  @oclif/config config done +1ms
  typeorm-uml init version: @oclif/[email protected] argv: [] +0ms
Error: Unexpected token import
    at Object.error (/usr/local/lib/node_modules/typeorm-uml/node_modules/@oclif/errors/lib/index.js:22:17)
    at TypeormUmlCommand.error (/usr/local/lib/node_modules/typeorm-uml/node_modules/@oclif/command/lib/command.js:60:23)
    at TypeormUmlCommand.run (/usr/local/lib/node_modules/typeorm-uml/lib/TypeormUmlCommand.js:22:22)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Not able to install, typeorm-uml speacially

I read about typeorm on some stackoverflow post.

I don't know why I am not able to install the package from npm. At same time I can install other packages with ease

Here is sinppet, I am using windows 10.

F:\rushikesh\website\maths163>npm install -D typeorm-uml
npm ERR! Unexpected end of JSON input while parsing near '...s://registry.npmjs.or'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\rushi\AppData\Roaming\npm-cache\_logs\2020-07-17T05_10_27_174Z-debug.log

Here is another command I tried

F:\rushikesh\website\maths163>npm install  typeorm-uml
npm ERR! Unexpected end of JSON input while parsing near '...s://registry.npmjs.or'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\rushi\AppData\Roaming\npm-cache\_logs\2020-07-17T05_10_41_177Z-debug.log```

[Question] Diagram cutoff on the right side

Hi, I generated a uml for my project, there are quite a lot of entities so the right side of the diagram was cut off.

I found a thread in PlantUML's forum where a parameter PLANTUML_LIMIT_SIZE can be set up.

Is it possible to specify this option here?

I'm getting an empty png/puml ?

Hi,

I am trying to use your package to generate my diagram.

since my entities are using *.ts , I am using the following command :

ts-node ./node_modules/typeorm-uml/bin/run ormconfig.json --download=diagram.puml--format=puml
@startuml

!define table(x) class x << (T,#FFAAAA) >>
!define pkey(x) <b>x</b>
hide stereotypes
hide fields
@enduml

which render an empty png

Am I doing something wrong ?

I'm doing this on windows.

Child Entities cause duplication of columns on a table

If you utilize the @ChildEntity(someEnum.VALUE) feature of TypeORM on one or more entities, the diagram it creates for the table is incorrect. You will see the columns duplicated on the table in the diagram.

Parent class:

@Entity('table_name')
@TableInheritance({
  column: { type: 'enum', enum: SomeEnum, name: 'type' },
})
export abstract class TableEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'text', nullable: true })
  name: string | null;
  ...

Child class:

@ChildEntity(SomeEnum.VALUE)
export class ChildEntity extends TableEntity {
...

Customize colors

Hi,

Currently the colors of the diagram are hardcoded, for instance:

uml += '!define pkey(x) <b><color:DarkGoldenRod><&key></color> x</b>\n';
uml += '!define fkey(x) <color:#AAAAAA><&key></color> x\n';
uml += '!define column(x) <color:#EFEFEF><&media-record></color> x\n';

It would be nice to have a set of flags enabling their customization such as:

--primary-key-color #aaaaaa 
--foreign-key-color #aaaaaa
--column-color #aaaaaa
--entity-background-color #aaaaaa
--entity-border-color #aaaaaa
--arrow-color #aaaaaa

OneToOne relation with wrong UML arrows

Good job on the project @eugene-manuilov .

I appreciate you check OneToOne relations. The arrows in diagram behave like ManyToOne relation.

Example:

 @OneToOne(() => StandardName, {
    eager: true,
    onDelete: "SET NULL"
  })
  @JoinColumn()
  standardName!: StandardName;

In UML I have:

image

Actually the arrows should be -|---------

Support for the latest version of typeorm (v0.3.0)

I know that the semver range below covers the v0.3.0 that was just released

typeorm-uml/package.json

Lines 66 to 67 in 583708a

"peerDependencies": {
"typeorm": "^0.2.0"

but I'm not sure if typeorm-uml will keeping working if I upgrade my typeorm (which I cannot do for now)

Please, read this when you have time: https://github.com/typeorm/typeorm/releases/tag/0.3.0 and close this issue if you know that typeorm-urml won't break if we use [email protected]

Improve PlantUML statements when using `--with-table-names-only` option

Hi! When we use the --with-table-names-only flag, since entities names won't be displayed, I think it would be nice to remove them from the output puml diagram's code

Current behavior

Now ... -f puml --with-table-names-only produces lines like table( UserEntity, User ) as User

Expected

To generate lines like table( User ) which will leads to the same image but with less bytes.

Note that this implies changing this following func as well

protected * defineTable(): IterableIterator<string> {

Do you think this is feasible?

SyntaxError: missing ) after argument list

[redacted]\node_modules\.bin\typeorm-uml:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at main ([redacted]\node_modules\ts-node\src\bin.ts:198:14)
    at Object.<anonymous> ([redacted]\node_modules\ts-node\src\bin.ts:288:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

what is going on here? is this my fault? could not figure it out...

Customize plantuml server url

Hi,

Currently the plantuml server url is hardcoded, meaning we can only use the official plantuml server to generate diagrams:

return `http://www.plantuml.com/plantuml/${ format }/${ schema }`;

From a security point-of-view, sending a whole database structure to an external server so that it generates a diagram is not a good practice.

There are ways of hosting a plantuml server (eg. using this docker image)

Once hosted, it can be made available on any custom url - may it be accessible on the world wide web or just within an internal infrastructure.

In order for such a custom server to be supported by this library, it would be nice to be able to specify the plantuml server url as an optional flag.

--plantuml-url: string (default: http://www.plantuml.com/plantuml)

Feel free to take the pull request into consideration

`Cannot find module` error when entities are imported using absolute path

When using absolute path for importing entities, I get the following error:

›   Error: Cannot find module 'src/category/entities/category.entity'
 ›   Require stack:
 ›   - /usr/src/app/src/photo/entities/photo.entity.ts
 ›   - /usr/src/app/node_modules/typeorm/util/DirectoryExportedClassesLoader.js
 ›   - /usr/src/app/node_modules/typeorm/connection/ConnectionMetadataBuilder.js
 ›   - /usr/src/app/node_modules/typeorm/connection/Connection.js
 ›   - /usr/src/app/node_modules/typeorm/connection/ConnectionManager.js
 ›   - /usr/src/app/node_modules/typeorm/index.js
 ›   - /usr/src/app/node_modules/typeorm-uml/lib/TypeormUmlCommand.js
 ›   - /usr/src/app/node_modules/typeorm-uml/bin/run

This is how I am importing category
import { CategoryEntity } from 'src/module-1/entities/category.entity';

I have not found a way to resolve this and the only way these works is using relative paths. Is it possible to have support for using absolute paths when importing entities?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.