voormedia / rails-erd

Generate Entity-Relationship Diagrams for Rails applications
http://voormedia.github.io/rails-erd/
MIT License
4k stars 367 forks source link

add Mermaid support #405

Closed ahmedash95 closed 1 year ago

ahmedash95 commented 1 year ago

Regarding https://github.com/voormedia/rails-erd/issues/226. This PR adds support for generating diagrams using Mermaid.

here is an example for Event Forms sample schema.

classDiagram
    direction RL
    class `ActiveRecord::InternalMetadata`
    `ActiveRecord::InternalMetadata` : +string value
    class `ActiveRecord::SchemaMigration`
    class `Event`
    `Event` : +boolean active
    `Event` : +string costs
    `Event` : +text description
    `Event` : +string duration
    `Event` : +text introduction
    `Event` : +text report
    `Event` : +string speaker
    `Event` : +string target_audience
    `Event` : +string title
    `Event` : +string tutors
    class `EventDate`
    `EventDate` : +string date
    `EventDate` : +text description
    `EventDate` : +date expiry_date
    `EventDate` : +string location
    class `Form`
    `Form` : +string name
    class `FormField`
    `FormField` : +string field_type
    `FormField` : +string label
    `FormField` : +boolean mandatory
    `FormField` : +string name
    class `FormFieldValue`
    `FormFieldValue` : +string key
    `FormFieldValue` : +string value
    class `Group`
    `Group` : +boolean active
    `Group` : +text description
    `Group` : +text email_message
    `Group` : +string email_receiver
    `Group` : +string email_subject
    `Group` : +string title
    `Group` : +string url_slug
    class `Organization`
    `Organization` : +string domain
    `Organization` : +text email_message
    `Organization` : +string email_receiver
    `Organization` : +string email_subject
    `Organization` : +string name
    `Organization` : +string phone
    `Organization` : +string signup_title
    `Organization` : +string subdomain
    `Organization` : +string website
    class `Signup`
    `Signup` : +boolean confirmed
    `Signup` : +string email
    `Signup` : +text serialized_fields
    class `Stylesheet`
    `Stylesheet` : +text content
    `Stylesheet` : +string name
    `Group` --> `Event`
    `Event` --> `EventDate`
    `EventDate` --> `Signup`
    `Organization` --> `Form`
    `Form` --> `Group`
    `Form` --> `FormField`
    `FormField` --> `FormFieldValue`
    `Organization` --> `Group`
    `Stylesheet` --> `Group`
    `Group` ..> `EventDate`
    `Organization` --> `Stylesheet`
    `Organization` ..> `Event`

Mermaid generator can be configured by passing generator=mermaid to the erd command or specifying the generator in configuration file to be mermaid. and it generates .mmd file.

rake erd generator=mermaid
krames commented 1 year ago

I looked at the code climate error and I think given what the method is doing it is easier to read this way rather than breaking it up into individual methods. I will defer to your judgement though @ahmedash95.

johnpaulashenfelter commented 1 year ago

I would heartily endorse this. Worked great for me and avoids installing the what, two dozen dependencies that graphviz wants through homebrew.

Added to my :development group

gem 'rails-erd', git: 'git@github.com:ahmedash95/rails-erd.git', branch: 'support-mermaid'

and then ran it as asked.

johnpaulashenfelter commented 1 year ago

As an aside, I had been trying to do this with mermerd, a go app, but this implmentation works way better for my usecase since on MySQL i could not get it to generate the schema I wanted.

Its also worthwhile that this works without having to connect to the db in the first place, so maybe they are complementary? In any case 👍 would merge again 😄

johnpaulashenfelter commented 1 year ago

:bump what can I do to help get this merged -- ❤️ using it.

ahmedash95 commented 1 year ago

Hey @johnpaulashenfelter, glad to see how useful it is. let's ping the maintainers to check this and hopefully get it merged

ahmedash95 commented 1 year ago

PING @kerrizor, can you check this PR, please?

kerrizor commented 1 year ago

Thanks, as always, for your patience with my being slow on updates and responses here! This is a great contribution, and I'm happy to merge. I've wanted to add mermaid support for quite some time, and this is a great first step; I especially appreciated making it an option, while maintaining graphviz as the default for now. I suspect that the direction to go for a next major release will be to flip this and make 🧜‍♀️ the default generator, but this gives us the ability to exercise this implementation and uncover any deeper problems before proceeding. Well done.