dbml

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

DBML Writer

Generates Database Markup Language (DBML) files from database schema information.

Overview

The DBML Writer converts RelSpec's internal database model representation into DBML syntax, suitable for use with dbdiagram.io and other DBML-compatible tools.

Features

  • Generates DBML syntax
  • Creates table definitions with columns
  • Defines relationships
  • Includes indexes
  • Adds notes and documentation
  • Supports enums

Usage

Basic Example
package main

import (
    "git.warky.dev/wdevs/relspecgo/pkg/models"
    "git.warky.dev/wdevs/relspecgo/pkg/writers"
    "git.warky.dev/wdevs/relspecgo/pkg/writers/dbml"
)

func main() {
    options := &writers.WriterOptions{
        OutputPath: "schema.dbml",
    }

    writer := dbml.NewWriter(options)
    err := writer.WriteDatabase(db)
    if err != nil {
        panic(err)
    }
}
CLI Examples
# Generate DBML from PostgreSQL database
relspec --input pgsql \
  --conn "postgres://localhost/mydb" \
  --output dbml \
  --out-file schema.dbml

# Convert GORM models to DBML
relspec --input gorm --in-file models.go --output dbml --out-file database.dbml

# Convert JSON to DBML for visualization
relspec --input json --in-file schema.json --output dbml --out-file diagram.dbml

Generated DBML Example

Project MyDatabase {
  database_type: 'PostgreSQL'
}

Table users {
  id bigserial [pk, increment]
  username varchar(50) [not null, unique]
  email varchar(100) [not null]
  bio text [null]
  created_at timestamp [not null, default: `now()`]

  Note: 'Users table'

  indexes {
    email [name: 'idx_users_email']
  }
}

Table posts {
  id bigserial [pk, increment]
  user_id bigint [not null]
  title varchar(200) [not null]
  content text [null]
  created_at timestamp [default: `now()`]

  indexes {
    user_id [name: 'idx_posts_user_id']
    (user_id, created_at) [name: 'idx_posts_user_created']
  }
}

Ref: posts.user_id > users.id [delete: cascade, update: no action]

DBML Features

Table Definitions
Table table_name {
  column_name type [attributes]
}
Column Attributes
  • pk - Primary key
  • increment - Auto-increment
  • not null - NOT NULL constraint
  • null - Nullable (explicit)
  • unique - Unique constraint
  • default: value - Default value
  • note: 'text' - Column note
Relationships
Ref: table1.column > table2.column
Ref: table1.column < table2.column
Ref: table1.column - table2.column

Relationship types:

  • > - Many-to-one
  • < - One-to-many
  • - - One-to-one

Relationship actions:

Ref: posts.user_id > users.id [delete: cascade, update: restrict]
Indexes
indexes {
  column_name
  (column1, column2) [name: 'idx_name', unique]
}

Type Mapping

SQL Type DBML Type
bigint bigint
integer int
varchar(n) varchar(n)
text text
boolean boolean
timestamp timestamp
date date
json json
uuid uuid

Notes

  • DBML is designed for database visualization
  • Can be imported into dbdiagram.io
  • Human-readable format
  • Schema names can be included in table names
  • Comments and notes are preserved
  • Ideal for documentation and sharing designs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements the writers.Writer interface for DBML format

func NewWriter

func NewWriter(options *writers.WriterOptions) *Writer

NewWriter creates a new DBML writer with the given options

func (*Writer) WriteDatabase

func (w *Writer) WriteDatabase(db *models.Database) error

WriteDatabase writes a Database model to DBML format

func (*Writer) WriteSchema

func (w *Writer) WriteSchema(schema *models.Schema) error

WriteSchema writes a Schema model to DBML format

func (*Writer) WriteTable

func (w *Writer) WriteTable(table *models.Table) error

WriteTable writes a Table model to DBML format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL