bun

package
v1.0.30 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

Bun Writer

Generates Go source files with Bun model definitions from database schema information.

Overview

The Bun Writer converts RelSpec's internal database model representation into Go source code with Bun struct definitions, complete with proper tags, relationships, and table configuration.

Features

  • Generates Bun-compatible Go structs
  • Creates proper bun struct tags
  • Adds relationship fields
  • Supports both single-file and multi-file output
  • Maps SQL types to Go types
  • Handles nullable fields with sql.Null* types
  • Generates table aliases

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/bun"
)

func main() {
    options := &writers.WriterOptions{
        OutputPath:  "models.go",
        PackageName: "models",
    }

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

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

# Multi-file output
relspec --input json --in-file schema.json --output bun --out-file models/

Generated Code Example

package models

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

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

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

    // Relationships
    Posts []*Post `bun:"rel:has-many,join:id=user_id" json:"posts,omitempty"`
}

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

    ID      int64          `bun:"id,pk" json:"id"`
    UserID  int64          `bun:"user_id,notnull" json:"user_id"`
    Title   string         `bun:"title,notnull" json:"title"`
    Content sql.NullString `bun:"content" json:"content,omitempty"`

    // Belongs to
    User *User `bun:"rel:belongs-to,join:user_id=id" json:"user,omitempty"`
}

Supported Bun Tags

  • table - Table name and alias
  • column - Column name (auto-derived if not specified)
  • pk - Primary key
  • autoincrement - Auto-increment
  • notnull - NOT NULL constraint
  • unique - Unique constraint
  • default - Default value
  • rel - Relationship definition
  • type - Explicit SQL type

Type Mapping

SQL Type Go Type Nullable Type
bigint int64 sql.NullInt64
integer int sql.NullInt32
varchar, text string sql.NullString
boolean bool sql.NullBool
timestamp time.Time sql.NullTime
numeric float64 sql.NullFloat64

Notes

  • Model names are derived from table names (singularized, PascalCase)
  • Table aliases are auto-generated from table names
  • Multi-file mode: one file per table named sql_{schema}_{table}.go
  • Generated code is auto-formatted
  • JSON tags are automatically added

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeneratePrefix

func GeneratePrefix(tableName string) string

GeneratePrefix generates a 3-letter prefix from a table name Examples: process → PRO, mastertask → MTL, user → USR

func PascalCaseToSnakeCase

func PascalCaseToSnakeCase(s string) string

PascalCaseToSnakeCase converts PascalCase to snake_case Examples: UserID → user_id, HTTPRequest → http_request

func Pluralize

func Pluralize(s string) string

Pluralize converts a singular word to plural Basic implementation with common rules

func Singularize

func Singularize(s string) string

Singularize converts a plural word to singular Basic implementation with common rules

func SnakeCaseToCamelCase

func SnakeCaseToCamelCase(s string) string

SnakeCaseToCamelCase converts snake_case to camelCase Examples: user_id → userID, http_request → httpRequest

func SnakeCaseToPascalCase

func SnakeCaseToPascalCase(s string) string

SnakeCaseToPascalCase converts snake_case to PascalCase Examples: user_id → UserID, http_request → HTTPRequest

Types

type FieldData

type FieldData struct {
	Name    string // Go field name (PascalCase)
	Type    string // Go type
	BunTag  string // Complete bun tag
	JSONTag string // JSON tag
	Comment string // Field comment
}

FieldData represents a single field in a struct

type MethodConfig

type MethodConfig struct {
	GenerateTableName     bool
	GenerateSchemaName    bool
	GenerateTableNameOnly bool
	GenerateGetID         bool
	GenerateGetIDStr      bool
	GenerateSetID         bool
	GenerateUpdateID      bool
	GenerateGetIDName     bool
	GenerateGetPrefix     bool
}

MethodConfig controls which helper methods to generate

func DefaultMethodConfig

func DefaultMethodConfig() *MethodConfig

DefaultMethodConfig returns a MethodConfig with all methods enabled

func LoadMethodConfigFromMetadata

func LoadMethodConfigFromMetadata(metadata map[string]interface{}) *MethodConfig

LoadMethodConfigFromMetadata loads method configuration from metadata map

type ModelData

type ModelData struct {
	Name            string
	TableName       string // schema.table format
	SchemaName      string
	TableNameOnly   string // just table name without schema
	Comment         string
	Fields          []*FieldData
	Config          *MethodConfig
	PrimaryKeyField string // Name of the primary key field
	PrimaryKeyIsSQL bool   // Whether PK uses SQL type (needs .Int64() call)
	IDColumnName    string // Name of the ID column in database
	Prefix          string // 3-letter prefix
}

ModelData represents a single model/struct in the template

func NewModelData

func NewModelData(table *models.Table, schema string, typeMapper *TypeMapper, flattenSchema bool) *ModelData

NewModelData creates a new ModelData from a models.Table

func (*ModelData) AddRelationshipField

func (md *ModelData) AddRelationshipField(field *FieldData)

AddRelationshipField adds a relationship field to the model

type TemplateData

type TemplateData struct {
	PackageName string
	Imports     []string
	Models      []*ModelData
	Config      *MethodConfig
}

TemplateData represents the data passed to the template for code generation

func NewTemplateData

func NewTemplateData(packageName string, config *MethodConfig) *TemplateData

NewTemplateData creates a new TemplateData with the given package name and config

func (*TemplateData) AddImport

func (td *TemplateData) AddImport(importPath string)

AddImport adds an import to the template data (deduplicates automatically)

func (*TemplateData) AddModel

func (td *TemplateData) AddModel(model *ModelData)

AddModel adds a model to the template data

func (*TemplateData) FinalizeImports

func (td *TemplateData) FinalizeImports()

FinalizeImports sorts and organizes imports

type Templates

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

Templates holds the parsed templates

func NewTemplates

func NewTemplates() (*Templates, error)

NewTemplates creates and parses the templates

func (*Templates) GenerateCode

func (t *Templates) GenerateCode(data *TemplateData) (string, error)

GenerateCode executes the template with the given data

type TypeMapper

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

TypeMapper handles type conversions between SQL and Go types for Bun

func NewTypeMapper

func NewTypeMapper() *TypeMapper

NewTypeMapper creates a new TypeMapper with default settings

func (*TypeMapper) BuildBunTag

func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) string

BuildBunTag generates a complete Bun tag string for a column Bun format: bun:"column_name,type:type_name,pk,default:value"

func (*TypeMapper) BuildRelationshipTag

func (tm *TypeMapper) BuildRelationshipTag(constraint *models.Constraint, relType string) string

BuildRelationshipTag generates Bun tag for relationship fields Bun format: bun:"rel:has-one,join:local_column=foreign_column"

func (*TypeMapper) GetBunImport

func (tm *TypeMapper) GetBunImport() string

GetBunImport returns the import path for Bun

func (*TypeMapper) GetSQLTypesImport

func (tm *TypeMapper) GetSQLTypesImport() string

GetSQLTypesImport returns the import path for sql_types (ResolveSpec common)

func (*TypeMapper) NeedsFmtImport

func (tm *TypeMapper) NeedsFmtImport(generateGetIDStr bool) bool

NeedsFmtImport checks if we need fmt import (for GetIDStr method)

func (*TypeMapper) NeedsTimeImport

func (tm *TypeMapper) NeedsTimeImport(goType string) bool

NeedsTimeImport checks if the Go type requires time package import

func (*TypeMapper) SQLTypeToGoType

func (tm *TypeMapper) SQLTypeToGoType(sqlType string, notNull bool) string

SQLTypeToGoType converts a SQL type to its Go equivalent Uses ResolveSpec common package types (all are nullable by default in Bun)

type Writer

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

Writer implements the writers.Writer interface for Bun models

func NewWriter

func NewWriter(options *writers.WriterOptions) *Writer

NewWriter creates a new Bun writer with the given options

func (*Writer) WriteDatabase

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

WriteDatabase writes a complete database as Bun models

func (*Writer) WriteSchema

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

WriteSchema writes a schema as Bun models

func (*Writer) WriteTable

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

WriteTable writes a single table as a Bun model

Jump to

Keyboard shortcuts

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