Documentation
¶
Overview ¶
Package erd generates Entity Relationship Diagrams from Go domain models.
The package provides two approaches for generating ERD diagrams:
Automatic Generation (via Sentinel) ¶
Use FromSchema to automatically generate diagrams from Go types scanned by the sentinel package:
sentinel.Scan[User]()
diagram := erd.FromSchema("My Domain", sentinel.Schema())
fmt.Println(diagram.ToMermaid())
Manual Construction (via Builder) ¶
Use NewDiagram, NewEntity, NewAttribute, and NewRelationship to construct diagrams programmatically:
diagram := erd.NewDiagram("E-commerce").
AddEntity(erd.NewEntity("Product").
AddAttribute(erd.NewAttribute("ID", "string").WithPrimaryKey())).
AddRelationship(erd.NewRelationship("Cart", "Product", "Items", erd.ManyToMany))
Output Formats ¶
Diagrams can be rendered to multiple formats:
- Diagram.ToMermaid - Mermaid syntax for web rendering
- Diagram.ToDOT - GraphViz DOT format for high-quality output
Struct Tags ¶
When using automatic generation, the erd struct tag controls attribute metadata:
- pk: Primary key
- fk: Foreign key
- uk: Unique key
- note:...: Attribute annotation
Tags can be combined: `erd:"pk,note:Auto-generated UUID"`
Validation ¶
Use Diagram.Validate to check diagram structural validity before rendering.
Package erd provides types and functions for generating Entity Relationship Diagrams.
Index ¶
- type Attribute
- func (a *Attribute) Validate() []ValidationError
- func (a *Attribute) WithForeignKey() *Attribute
- func (a *Attribute) WithKey(keyType KeyType) *Attribute
- func (a *Attribute) WithNote(note string) *Attribute
- func (a *Attribute) WithNullable() *Attribute
- func (a *Attribute) WithPrimaryKey() *Attribute
- func (a *Attribute) WithUnique() *Attribute
- type Cardinality
- type Diagram
- type Entity
- type KeyType
- type Relationship
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attribute ¶
Attribute represents a field/property of an entity.
func NewAttribute ¶
NewAttribute creates a new attribute.
func (*Attribute) Validate ¶
func (a *Attribute) Validate() []ValidationError
Validate checks the attribute for structural validity.
func (*Attribute) WithForeignKey ¶
WithForeignKey marks the attribute as a foreign key.
func (*Attribute) WithNullable ¶
WithNullable marks the attribute as nullable.
func (*Attribute) WithPrimaryKey ¶
WithPrimaryKey marks the attribute as a primary key.
func (*Attribute) WithUnique ¶
WithUnique marks the attribute as unique.
type Cardinality ¶
type Cardinality string
Cardinality represents the type of relationship between entities.
const ( OneToOne Cardinality = "one-to-one" OneToMany Cardinality = "one-to-many" ManyToOne Cardinality = "many-to-one" ManyToMany Cardinality = "many-to-many" )
Cardinality constants.
type Diagram ¶
type Diagram struct {
Title string
Description *string
Entities map[string]*Entity
Relationships []*Relationship
}
Diagram represents an Entity Relationship Diagram.
func FromSchema ¶
FromSchema converts a sentinel schema to an ERD diagram. The schema is typically obtained via sentinel.Schema() after scanning types.
func (*Diagram) AddRelationship ¶
func (d *Diagram) AddRelationship(rel *Relationship) *Diagram
AddRelationship adds a relationship to the diagram.
func (*Diagram) Validate ¶
func (d *Diagram) Validate() []ValidationError
Validate checks the diagram for structural validity.
func (*Diagram) WithDescription ¶
WithDescription sets the description for the diagram.
type Entity ¶
Entity represents a domain model entity (typically a Go struct).
func FromMetadata ¶
FromMetadata converts a single sentinel Metadata to an ERD Entity.
func (*Entity) AddAttribute ¶
AddAttribute adds an attribute to the entity.
func (*Entity) Validate ¶
func (e *Entity) Validate() []ValidationError
Validate checks the entity for structural validity.
func (*Entity) WithPackage ¶
WithPackage sets the package name for the entity.
type Relationship ¶
type Relationship struct {
Label *string
Note *string
From string
To string
Field string
Cardinality Cardinality
}
Relationship represents a relationship between entities.
func NewRelationship ¶
func NewRelationship(from, to, field string, cardinality Cardinality) *Relationship
NewRelationship creates a new relationship.
func (*Relationship) ValidateAgainst ¶
func (r *Relationship) ValidateAgainst(entities map[string]*Entity) []ValidationError
ValidateAgainst checks the relationship references valid entities.
func (*Relationship) WithLabel ¶
func (r *Relationship) WithLabel(label string) *Relationship
WithLabel sets a label for the relationship.
func (*Relationship) WithNote ¶
func (r *Relationship) WithNote(note string) *Relationship
WithNote adds a note to the relationship.
type ValidationError ¶
ValidationError represents a validation error.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error implements the error interface.