A Go package for building and generating DBML (Database Markup Language) programmatically.
Define database schemas with Go's type safety and generate DBML for visualisation tools like dbdiagram.io:
project := dbml.NewProject("ecommerce").
WithDatabaseType("PostgreSQL")
project.AddTable(
dbml.NewTable("orders").
AddColumn(dbml.NewColumn("id", "bigint").WithPrimaryKey()).
AddColumn(dbml.NewColumn("user_id", "bigint").
WithRef(dbml.ManyToOne, "public", "users", "id")),
)
fmt.Println(project.Generate())Schemas become testable, version-controlled, and composable.
go get github.com/zoobz-io/dbmlRequires Go 1.24 or higher.
package main
import (
"fmt"
"github.com/zoobz-io/dbml"
)
func main() {
project := dbml.NewProject("my_database").
WithDatabaseType("PostgreSQL")
users := dbml.NewTable("users").
AddColumn(
dbml.NewColumn("id", "bigint").
WithPrimaryKey().
WithIncrement(),
).
AddColumn(
dbml.NewColumn("email", "varchar(255)").
WithUnique(),
).
AddColumn(
dbml.NewColumn("created_at", "timestamp").
WithDefault("now()"),
)
project.AddTable(users)
if err := project.Validate(); err != nil {
panic(err)
}
fmt.Println(project.Generate())
}users := dbml.NewTable("users").
AddColumn(dbml.NewColumn("id", "bigint").WithPrimaryKey())
posts := dbml.NewTable("posts").
AddColumn(dbml.NewColumn("id", "bigint").WithPrimaryKey()).
AddColumn(
dbml.NewColumn("user_id", "bigint").
WithRef(dbml.ManyToOne, "public", "users", "id"),
)
project.AddTable(users).AddTable(posts)ref := dbml.NewRef(dbml.ManyToOne).
From("public", "posts", "user_id").
To("public", "users", "id").
WithOnDelete(dbml.Cascade).
WithOnUpdate(dbml.Restrict)
project.AddRef(ref)// Simple index
table.AddIndex(dbml.NewIndex("email"))
// Composite index
table.AddIndex(
dbml.NewIndex("user_id", "created_at").
WithName("idx_user_created").
WithUnique(),
)
// Expression-based index
table.AddIndex(
dbml.NewExpressionIndex("date(created_at)").
WithType("btree"),
)status := dbml.NewEnum("order_status",
"pending", "processing", "shipped", "delivered").
WithNote("Order status values")
project.AddEnum(status)group := dbml.NewTableGroup("User Management").
AddTable("public", "users").
AddTable("public", "roles").
AddTable("public", "permissions")
project.AddTableGroup(group)| Feature | Description |
|---|---|
| Type-safe schemas | Catch errors at compile time, not when generating output |
| Fluent builder API | Chainable methods make schema construction readable |
| Built-in validation | Validate schemas before generation to catch structural issues |
| Serialisation | Export schemas to JSON or YAML for storage and interchange |
| Minimal dependencies | Only requires gopkg.in/yaml.v3 |
- Schema as code — Version control, test, and compose database schemas like any other Go code
- Visualisation ready — Generate DBML for tools like dbdiagram.io directly from your definitions
- Single source of truth — Define once, generate documentation and diagrams from the same source
- Project - Top-level container for database schema
- Table - Database table definition
- Column - Table column with type and constraints
- Index - Single, composite, or expression-based indexes
- Ref - Relationships between tables
- Enum - Enumeration types
- TableGroup - Logical grouping of tables
const (
OneToMany RelType = "<" // One-to-many
ManyToOne RelType = ">" // Many-to-one
OneToOne RelType = "-" // One-to-one
ManyToMany RelType = "<>" // Many-to-many
)const (
Cascade RefAction = "cascade"
Restrict RefAction = "restrict"
SetNull RefAction = "set null"
SetDefault RefAction = "set default"
NoAction RefAction = "no action"
)NewProject(name string) *ProjectWithDatabaseType(dbType string) *ProjectWithNote(note string) *ProjectAddTable(table *Table) *ProjectAddEnum(enum *Enum) *ProjectAddRef(ref *Ref) *ProjectAddTableGroup(group *TableGroup) *ProjectValidate() errorGenerate() string
NewTable(name string) *TableWithSchema(schema string) *TableWithAlias(alias string) *TableWithNote(note string) *TableWithHeaderColor(color string) *TableAddColumn(column *Column) *TableAddIndex(index *Index) *Table
NewColumn(name, colType string) *ColumnWithPrimaryKey() *ColumnWithNull() *ColumnWithUnique() *ColumnWithIncrement() *ColumnWithDefault(value string) *ColumnWithCheck(constraint string) *ColumnWithNote(note string) *ColumnWithRef(relType RelType, schema, table, column string) *Column
NewIndex(columns ...string) *IndexNewExpressionIndex(expressions ...string) *IndexWithType(indexType string) *IndexWithName(name string) *IndexWithUnique() *IndexWithPrimaryKey() *IndexWithNote(note string) *Index
NewRef(relType RelType) *RefWithName(name string) *RefFrom(schema, table string, columns ...string) *RefTo(schema, table string, columns ...string) *RefWithOnDelete(action RefAction) *RefWithOnUpdate(action RefAction) *RefWithColor(color string) *Ref
Contributions are welcome. See CONTRIBUTING.md for development setup and guidelines.
MIT