bun

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

Bun Reader

Reads Go source files containing Bun model definitions and extracts database schema information.

Overview

The Bun Reader parses Go source code files that define Bun models (structs with bun struct tags) and converts them into RelSpec's internal database model representation.

Features

  • Parses Bun struct tags to extract column definitions
  • Extracts table names from bun:"table:tablename" tags
  • Identifies primary keys, foreign keys, and indexes
  • Supports relationship detection
  • Handles both single files and directories

Usage

Basic Example
package main

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

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

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

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

# Convert Bun models to GORM
relspec --input bun --in-file models.go --output gorm --out-file gorm_models.go

Supported Bun Tags

The reader recognizes the following Bun struct tags:

  • table - Table name
  • column - Column name
  • type - SQL data type
  • pk - Primary key
  • notnull - NOT NULL constraint
  • autoincrement - Auto-increment column
  • default - Default value
  • unique - Unique constraint
  • rel - Relationship definition

Example Bun Model

package models

import (
    "time"
    "github.com/uptrace/bun"
)

type User struct {
    bun.BaseModel `bun:"table:users,alias:u"`

    ID        int64     `bun:"id,pk,autoincrement"`
    Username  string    `bun:"username,notnull,unique"`
    Email     string    `bun:"email,notnull"`
    CreatedAt time.Time `bun:"created_at,notnull,default:now()"`

    Posts []*Post `bun:"rel:has-many,join:id=user_id"`
}

type Post struct {
    bun.BaseModel `bun:"table:posts,alias:p"`

    ID      int64  `bun:"id,pk"`
    UserID  int64  `bun:"user_id,notnull"`
    Title   string `bun:"title,notnull"`
    Content string `bun:"content"`

    User *User `bun:"rel:belongs-to,join:user_id=id"`
}

Notes

  • Test files (ending in _test.go) are automatically excluded
  • The bun.BaseModel embedded struct is automatically recognized
  • Schema defaults to public if not specified

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JoinInfo

type JoinInfo struct {
	ForeignKey    string // Column in the related table
	ReferencedKey string // Column in the current table
}

JoinInfo holds parsed join information

type Reader

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

Reader implements the readers.Reader interface for Bun Go model files

func NewReader

func NewReader(options *readers.ReaderOptions) *Reader

NewReader creates a new Bun reader with the given options

func (*Reader) ReadDatabase

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

ReadDatabase reads Bun Go model files and returns a Database model

func (*Reader) ReadSchema

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

ReadSchema reads Bun Go model files and returns a Schema model

func (*Reader) ReadTable

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

ReadTable reads a Bun Go model file and returns a Table model

Jump to

Keyboard shortcuts

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