Documentation
¶
Overview ¶
Package dbml provides types and utilities for building and generating Database Markup Language (DBML) schemas programmatically.
DBML is a domain-specific language for defining database structures, commonly used with tools like dbdiagram.io for visualization.
Basic Usage ¶
Create a project, add tables with columns, and generate DBML output:
project := dbml.NewProject("mydb").
WithDatabaseType("PostgreSQL")
users := dbml.NewTable("users").
AddColumn(dbml.NewColumn("id", "bigint").WithPrimaryKey()).
AddColumn(dbml.NewColumn("email", "varchar(255)").WithUnique())
project.AddTable(users)
fmt.Println(project.Generate())
Relationships ¶
Define relationships between tables using inline refs or standalone refs:
// Inline ref on a column
dbml.NewColumn("user_id", "bigint").
WithRef(dbml.ManyToOne, "public", "users", "id")
// Standalone ref with actions
dbml.NewRef(dbml.ManyToOne).
From("public", "posts", "user_id").
To("public", "users", "id").
WithOnDelete(dbml.Cascade)
Package dbml provides a fluent API for building DBML schemas programmatically.
Index ¶
- type Column
- func (c *Column) Generate() string
- func (c *Column) Validate() error
- func (c *Column) WithCheck(constraint string) *Column
- func (c *Column) WithDefault(value string) *Column
- func (c *Column) WithIncrement() *Column
- func (c *Column) WithNote(note string) *Column
- func (c *Column) WithNull() *Column
- func (c *Column) WithPrimaryKey() *Column
- func (c *Column) WithRef(relType RelType, schema, table, column string) *Column
- func (c *Column) WithUnique() *Column
- type ColumnSettings
- type Enum
- type Index
- type IndexColumn
- type InlineRef
- type Project
- func (p *Project) AddEnum(enum *Enum) *Project
- func (p *Project) AddRef(ref *Ref) *Project
- func (p *Project) AddTable(table *Table) *Project
- func (p *Project) AddTableGroup(group *TableGroup) *Project
- func (p *Project) FromJSON(data []byte) error
- func (p *Project) FromYAML(data []byte) error
- func (p *Project) Generate() string
- func (p *Project) ToJSON() ([]byte, error)
- func (p *Project) ToYAML() ([]byte, error)
- func (p *Project) Validate() error
- func (p *Project) WithDatabaseType(dbType string) *Project
- func (p *Project) WithNote(note string) *Project
- type Ref
- func (r *Ref) From(schema, table string, columns ...string) *Ref
- func (r *Ref) Generate() string
- func (r *Ref) To(schema, table string, columns ...string) *Ref
- func (r *Ref) Validate() error
- func (r *Ref) WithColor(color string) *Ref
- func (r *Ref) WithName(name string) *Ref
- func (r *Ref) WithOnDelete(action RefAction) *Ref
- func (r *Ref) WithOnUpdate(action RefAction) *Ref
- type RefAction
- type RefEndpoint
- type RelType
- type Table
- func (t *Table) AddColumn(column *Column) *Table
- func (t *Table) AddIndex(index *Index) *Table
- func (t *Table) Generate() string
- func (t *Table) Validate() error
- func (t *Table) WithAlias(alias string) *Table
- func (t *Table) WithHeaderColor(color string) *Table
- func (t *Table) WithNote(note string) *Table
- func (t *Table) WithSchema(schema string) *Table
- func (t *Table) WithSetting(key, value string) *Table
- type TableGroup
- type TableRef
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct {
Settings *ColumnSettings
Note *string
InlineRef *InlineRef
Name string
Type string
}
Column represents a table column.
func (*Column) WithDefault ¶
WithDefault sets a default value for the column.
func (*Column) WithIncrement ¶
WithIncrement marks the column as auto-incrementing.
func (*Column) WithPrimaryKey ¶
WithPrimaryKey marks the column as a primary key.
func (*Column) WithUnique ¶
WithUnique marks the column as unique.
type ColumnSettings ¶
type ColumnSettings struct {
Default *string
Check *string
PrimaryKey bool
Null bool
Unique bool
Increment bool
}
ColumnSettings represents all column-level settings.
type Enum ¶
Enum represents an enumeration type.
func (*Enum) WithSchema ¶
WithSchema sets the schema for the enum.
type Index ¶
type Index struct {
Type *string
Name *string
Note *string
Columns []IndexColumn
Unique bool
PrimaryKey bool
}
Index represents a table index.
func NewExpressionIndex ¶
NewExpressionIndex creates a new expression-based index.
func (*Index) WithPrimaryKey ¶
WithPrimaryKey marks the index as a primary key.
func (*Index) WithUnique ¶
WithUnique marks the index as unique.
type IndexColumn ¶
type IndexColumn struct {
Name *string // for regular columns
Expression *string // for expression-based indexes like `id*2`
}
IndexColumn represents a column or expression in an index.
type Project ¶
type Project struct {
Name string
DatabaseType *string // "PostgreSQL", "MySQL", etc.
Note *string
Tables map[string]*Table
Enums map[string]*Enum
TableGroups []*TableGroup
Refs []*Ref
}
Project represents the top-level DBML project.
func (*Project) AddTableGroup ¶
func (p *Project) AddTableGroup(group *TableGroup) *Project
AddTableGroup adds a table group to the project.
func (*Project) WithDatabaseType ¶
WithDatabaseType sets the database type for the project.
type Ref ¶
type Ref struct {
Name *string
Left *RefEndpoint
Right *RefEndpoint
OnDelete *RefAction
OnUpdate *RefAction
Color *string
Type RelType
}
Ref represents a relationship between tables.
func (*Ref) WithOnDelete ¶
WithOnDelete sets the ON DELETE action.
func (*Ref) WithOnUpdate ¶
WithOnUpdate sets the ON UPDATE action.
type RefEndpoint ¶
type RefEndpoint struct {
Schema string
Table string
Columns []string // supports composite foreign keys
}
RefEndpoint represents one side of a relationship.
func (*RefEndpoint) Validate ¶
func (e *RefEndpoint) Validate() error
Validate validates a RefEndpoint.
type Table ¶
type Table struct {
Alias *string
Note *string
Settings map[string]string
Schema string
Name string
Columns []*Column
Indexes []*Index
}
Table represents a database table.
func (*Table) WithHeaderColor ¶
WithHeaderColor sets the header color for the table.
func (*Table) WithSchema ¶
WithSchema sets the schema for the table.
func (*Table) WithSetting ¶
WithSetting adds a setting to the table.
type TableGroup ¶
TableGroup represents a logical grouping of tables.
func NewTableGroup ¶
func NewTableGroup(name string) *TableGroup
NewTableGroup creates a new table group.
func (*TableGroup) AddTable ¶
func (tg *TableGroup) AddTable(schema, name string) *TableGroup
AddTable adds a table reference to the group.
func (*TableGroup) Generate ¶
func (tg *TableGroup) Generate() string
Generate generates the DBML syntax for a TableGroup.
func (*TableGroup) Validate ¶
func (g *TableGroup) Validate() error
Validate validates a TableGroup.
type ValidationError ¶
ValidationError represents a validation error.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string