Files
Hein b4ff4334cc
Some checks failed
CI / Lint (push) Successful in -27m53s
CI / Test (1.24) (push) Successful in -27m31s
CI / Build (push) Successful in -28m13s
CI / Test (1.25) (push) Failing after 1m11s
Integration Tests / Integration Tests (push) Failing after -28m15s
feat(models): 🎉 Add GUID field to various models
* Introduced GUID field to Database, Domain, DomainTable, Schema, Table, View, Sequence, Column, Index, Relationship, Constraint, Enum, and Script models.
* Updated initialization functions to assign new GUIDs using uuid package.
* Enhanced DCTX reader and writer to utilize GUIDs from models where available.
2026-01-04 19:53:17 +02:00
..
2025-12-17 20:44:02 +02:00
2025-12-28 10:34:20 +02:00
2025-12-17 20:44:02 +02:00
2025-12-16 18:10:40 +02:00
2025-12-18 19:15:22 +02:00
2025-12-16 21:43:45 +02:00

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