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: 7 Imported by: 0

README

DBML Reader

Reads Database Markup Language (DBML) files and extracts database schema information.

Overview

The DBML Reader parses .dbml files that define database schemas using the DBML syntax (used by dbdiagram.io) and converts them into RelSpec's internal database model representation.

Features

  • Parses DBML syntax
  • Extracts tables, columns, and relationships
  • Supports DBML-specific features:
    • Table groups and notes
    • Enum definitions
    • Indexes
    • Foreign key relationships

Usage

Basic Example
package main

import (
    "fmt"
    "git.warky.dev/wdevs/relspecgo/pkg/readers"
    "git.warky.dev/wdevs/relspecgo/pkg/readers/dbml"
)

func main() {
    options := &readers.ReaderOptions{
        FilePath: "/path/to/schema.dbml",
    }

    reader := dbml.NewReader(options)
    db, err := reader.ReadDatabase()
    if err != nil {
        panic(err)
    }

    fmt.Printf("Found %d schemas\n", len(db.Schemas))
}
CLI Example
# Read DBML file and convert to JSON
relspec --input dbml --in-file schema.dbml --output json --out-file schema.json

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

Example DBML File

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

  Note: 'Users table'
}

Table posts {
  id bigserial [pk]
  user_id bigint [not null, ref: > users.id]
  title varchar(200) [not null]
  content text

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

Ref: posts.user_id > users.id [delete: cascade]

DBML Features Supported

  • Table definitions with columns
  • Primary keys (pk)
  • Not null constraints (not null)
  • Unique constraints (unique)
  • Default values (default)
  • Inline references (ref)
  • Standalone Ref blocks
  • Indexes and composite indexes
  • Table notes and column notes
  • Enums

Notes

  • DBML is designed for database documentation and diagramming
  • Schema name defaults to public
  • Relationship cardinality is preserved

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader implements the readers.Reader interface for DBML format

func NewReader

func NewReader(options *readers.ReaderOptions) *Reader

NewReader creates a new DBML reader with the given options

func (*Reader) ReadDatabase

func (r *Reader) ReadDatabase() (*models.Database, error)

ReadDatabase reads and parses DBML input, returning a Database model

func (*Reader) ReadSchema

func (r *Reader) ReadSchema() (*models.Schema, error)

ReadSchema reads and parses DBML input, returning a Schema model

func (*Reader) ReadTable

func (r *Reader) ReadTable() (*models.Table, error)

ReadTable reads and parses DBML input, returning a Table model

Jump to

Keyboard shortcuts

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