Skip to content

SeaDude/mmerd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎨 mmerd - Mermaid ERD Generator

Automatically generate beautiful Entity Relationship Diagrams from your SQL files!

Version License PRs Welcome

✨ Features

  • πŸš€ Instant ERD Generation - Run mmerd in any directory with SQL files
  • πŸ” Smart Relationship Detection - Automatically finds foreign key relationships across multiple SQL files
  • πŸ“Š High-Resolution Output - Generates crisp, scalable diagrams (PNG and SVG)
  • 🎯 Zero Configuration - Works out of the box with sensible defaults
  • πŸ“ Multi-File Support - Perfect for projects that organize tables in separate SQL files
  • 🎨 Mermaid Powered - Leverages Mermaid.js for beautiful, customizable diagrams

πŸ“Έ Example Output

Running mmerd on a blog database with separate SQL files for users, posts, comments, and tags:

erDiagram
    users ||--o{ posts : "writes"
    users ||--o{ comments : "authors"
    posts ||--o{ comments : "has"
    posts ||--o{ post_tags : "tagged"
    tags ||--o{ post_tags : "applied"
Loading

πŸš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/yourusername/mmerd.git
cd mmerd

# Run the installer
./install.sh

The installer will:

  • βœ… Check for Node.js and npm (required prerequisites)
  • βœ… Install mermaid-cli if not present
  • βœ… Install mmerd and companion tools to ~/.local/bin
  • βœ… Add ~/.local/bin to your PATH if needed

Basic Usage

# Navigate to a directory with SQL files
cd /path/to/your/database/schemas

# Generate ERD
mmerd

# Output:
# βœ“ Created: database_erd.mermaid
# βœ“ Created: database_erd.png
# πŸ–ΌοΈ Opens the diagram automatically!

πŸ› οΈ Available Commands

Command Description Usage
mmerd Generate ERD from SQL files in current directory mmerd
mmviz Convert .mermaid file to high-res PNG mmviz diagram.mermaid
mmsvg Convert .mermaid file to scalable SVG mmsvg diagram.mermaid
mmview Quick view any image file mmview output.png

πŸ“š How It Works

  1. Scans all .sql files in the current directory
  2. Parses CREATE TABLE statements to extract table structure
  3. Detects relationships through:
    • Inline REFERENCES clauses
    • FOREIGN KEY constraints
    • ALTER TABLE ADD FOREIGN KEY statements
  4. Generates a Mermaid ERD with proper relationships
  5. Renders to high-resolution PNG (4x scale for clarity)
  6. Opens the diagram in your default image viewer

🎯 Use Cases

Perfect for:

  • πŸ“– Documentation - Auto-generate database diagrams for your docs
  • πŸ‘€ Code Reviews - Visualize schema changes in PRs
  • πŸŽ“ Learning - Understand database structures quickly
  • πŸ—οΈ Design - Validate relationships before implementation
  • πŸ” Debugging - Spot missing or incorrect foreign keys

πŸ“ Project Structure

mmerd/
β”œβ”€β”€ bin/
β”‚   β”œβ”€β”€ mmerd         # Main ERD generator
β”‚   β”œβ”€β”€ mmviz         # Mermaid to PNG converter
β”‚   └── mmsvg         # Mermaid to SVG converter
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ users.sql     # Sample schema files
β”‚   β”œβ”€β”€ posts.sql
β”‚   β”œβ”€β”€ comments.sql
β”‚   └── tags.sql
β”œβ”€β”€ install.sh        # Installation script
β”œβ”€β”€ uninstall.sh      # Uninstallation script
└── README.md         # You are here!

πŸ”§ Prerequisites

  • Node.js (v14 or higher)
  • npm (comes with Node.js)

Don't have Node.js? Install it from:

  • Ubuntu/Debian: sudo apt-get install nodejs npm
  • macOS: brew install node
  • Windows/Others: nodejs.org

πŸ“ SQL File Requirements

mmerd works with standard SQL CREATE TABLE syntax:

-- Basic table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL
);

-- Table with foreign key
CREATE TABLE posts (
    id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
    title VARCHAR(200) NOT NULL,
    content TEXT
);

Supported:

  • βœ… CREATE TABLE statements
  • βœ… Inline REFERENCES clauses
  • βœ… FOREIGN KEY constraints
  • βœ… ALTER TABLE ADD FOREIGN KEY
  • βœ… Multiple files with cross-file relationships

🎨 Customization

Output Location

By default, mmerd creates:

  • database_erd.mermaid - Mermaid source file
  • database_erd.png - Rendered diagram

Manual Rendering

You can edit the .mermaid file and re-render:

# Edit the mermaid file
nano database_erd.mermaid

# Re-render to PNG
mmviz database_erd.mermaid

# Or render to SVG for infinite scaling
mmsvg database_erd.mermaid

πŸ—‘οΈ Uninstallation

cd mmerd
./uninstall.sh

This will:

  • Remove mmerd scripts from ~/.local/bin
  • Optionally uninstall mermaid-cli
  • Keep your Node.js installation intact

🀝 Contributing

Contributions are welcome! Feel free to:

  • πŸ› Report bugs
  • πŸ’‘ Suggest features
  • πŸ”§ Submit PRs

πŸ“„ License

MIT License - feel free to use in your projects!

πŸ™ Acknowledgments

πŸ’‘ Tips & Tricks

Generate ERD for Specific Tables

# Create a temporary directory
mkdir temp_erd
# Copy only the SQL files you want
cp users.sql posts.sql temp_erd/
cd temp_erd
mmerd

Version Control Your ERDs

# Add to your project
mmerd
git add database_erd.mermaid
git commit -m "Update database ERD"

CI/CD Integration

# GitHub Actions example
- name: Generate ERD
  run: |
    npm install -g @mermaid-js/mermaid-cli
    mmerd
    mkdir -p docs/diagrams
    mv database_erd.png docs/diagrams/

πŸ› Troubleshooting

mmerd: command not found

  • Run source ~/.bashrc (or source ~/.zshrc for zsh)
  • Ensure ~/.local/bin is in your PATH

No SQL files found

  • Ensure you're in a directory with .sql files
  • Check file extensions are .sql (not .SQL or .sql.txt)

Diagram not opening automatically

  • Linux: Install xdg-utils
  • macOS: Should work automatically
  • Windows: WSL may need additional configuration

Made with ❀️ for developers who love clean database documentation

About

Automatically generate Entity Relationship Diagrams from SQL files

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages