Mermaid Diagram Generation
What Is Mermaid?
Mermaid is a text-based diagram language. Simple syntax, real diagrams -- flowcharts, sequence diagrams, class diagrams, you name it. Paired with AI, you can visualize code architecture fast.
Common Diagram Types
1. Entity Relationship Diagrams
Great for database design and data models:
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE-ITEM : contains
PRODUCT ||--o{ LINE-ITEM : includes
CUSTOMER {
string name
string email
}
ORDER {
int orderNumber
date created
}
2. Flow Charts
Great for process and logic flows:
flowchart TD
A[Start] --> B{Decision}
B -->|Yes| C[Process]
B -->|No| D[End]
C --> E{Another Decision}
E -->|Option 1| F[Action 1]
E -->|Option 2| G[Action 2]
F --> D
G --> D
3. Sequence Diagrams
Great for interaction flows:
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: HTTP Request
Server->>Database: Query
Database-->>Server: Result
Server-->>Client: JSON Response
4. Class Diagrams
Great for object-oriented design:
classDiagram
class Animal {
+name: string
+age: int
+makeSound()
}
class Dog {
+breed: string
+bark()
}
class Cat {
+color: string
+meow()
}
Animal <|-- Dog
Animal <|-- Cat
Process for Generating Diagrams
- Analyze source files (SQL, code, docs)
- Extract entities and relationships
- Generate appropriate diagram type
- Include relevant properties/methods
- Validate syntax with mermaid compiler
- Save to specified location
Validation
npx -p @mermaid-js/mermaid-cli mmdc -i <input>.md -o test.md
Best Practices
- Keep diagrams focused and readable
- Use consistent naming conventions
- Group related entities
- Add labels to relationships
- Include cardinality where relevant
- Use subgraphs for organization
- Add styling for clarity
Having AI Generate Diagrams
In Cursor or Claude Code, you can prompt like this:
Analyze all model files under src/models/
and generate a Mermaid ER diagram showing entity relationships.
AI will:
- Read model files
- Analyze fields and relationships
- Generate Mermaid syntax
- Output renderable diagram code
Using Diagrams in Docs
Most Markdown renderers (GitHub, GitLab, Notion, etc.) support Mermaid. Just specify mermaid as the code block language:
```mermaid
flowchart LR
A --> B --> C
```
Advanced Styling
flowchart TD
subgraph Frontend
A[React App]
B[State Management]
end
subgraph Backend
C[API Server]
D[Database]
end
A --> C
B --> A
C --> D
style A fill:#f9f,stroke:#333
style C fill:#bbf,stroke:#333
Next Steps
Check out Task Implementation Methodology for systematic planning and execution.
📚 Related resources
❓ Common questions
Open a question to review the practical answer.
Which diagram types does Mermaid support and when do I use each?
The chapter highlights 4: (1) ER diagrams (`erDiagram`) — database schemas and model relationships; (2) flowcharts (`flowchart TD`) — business flows and decision trees; (3) sequence diagrams (`sequenceDiagram`) — frontend / API / DB interactions; (4) class diagrams (`classDiagram`) — OO design and inheritance. Mermaid also supports gantt / state / pie etc., but these 4 cover ~80% of daily use.
How do I validate Mermaid syntax locally before committing?
Mermaid CLI: `npx -p @mermaid-js/mermaid-cli mmdc -i <input>.md -o test.md` — passes means syntax is fine, fails prints the exact line. If you do not want to install it, paste into mermaid.live for browser rendering. GitHub, GitLab, and Notion all render Mermaid in markdown, so PR previews work too.
How do I get AI to auto-generate the project's ER diagram?
Hand the agent (Cursor / Claude Code) a goal prompt: "Analyse all model files under `src/models/`, generate a Mermaid ER diagram showing the entity relationships." It reads the files, extracts fields and foreign keys, and emits `erDiagram` syntax. Cap it at 5-10 core models — beyond 20 the diagram becomes unreadable.
When should I use Mermaid subgraphs?
When you have more than 10 nodes, or there is a clear boundary (frontend/backend, microservice split). Wrap React App + State Management into `subgraph Frontend`, API Server + Database into `subgraph Backend`, then connect cross-boundary with `A --> C`. Grouping makes the graph 3x easier to scan than a flat tangle.
Mermaid renders ugly by default — how do I style it?
Use the `style` keyword on individual nodes: `style A fill:#f9f,stroke:#333`, handy for highlighting entry or error nodes. Switch global theme with `%%{init: {'theme':'dark'}}%%` on line 1 — base/dark/forest/neutral are built in. For heavy styling, export SVG and finish in Figma — Mermaid is not a design tool.