GORM CLI

GORM CLI Overview

GORM CLI reads the interfaces and structs in your project, turns raw SQL comments into typed query methods, and generates helpers for common model operations. Everything compiles to plain Go that works with gorm.io/gorm.

What you get

  • Generics-first output with an opt-out flag (--typed=false).
  • Typed query methods generated from SQL comments on interfaces.
  • Field helpers from models for predicates, updates, and associations.
  • Plain Go code with no runtime wrappers.

Where to look next

Two code paths are produced in the same package. Use configuration to choose which interfaces and models are included.

Quick start

  1. Install the CLI.

    go install gorm.io/cli/gorm@latest
  2. Define interfaces and models.

    // examples/models/user.go
    type User struct {
    gorm.Model
    Name string
    Age int
    Pets []Pet `gorm:"many2many:user_pets"`
    }

    type Pet struct {
    gorm.Model
    Name string
    }

    // examples/query.go
    type Query[T any] interface {
    // SELECT * FROM @@table WHERE id=@id
    GetByID(id int) (T, error)
    }
  3. Generate code.

    # Generics output (default)
    gorm gen -i ./examples -o ./generated

    # Relaxed typing
    gorm gen -i ./examples -o ./generated --typed=false
  4. Call the generated APIs.

     // Typed raw SQL
    u, err := generated.Query[User](db).GetByID(ctx, 123)

    // Field helpers
    users, _ := gorm.G[User](db).
    Where(generated.User.Age.Gt(18)).
    Find(ctx)

    // Association helpers
    gorm.G[User](db).
    Set(
    generated.User.Name.Set("alice"),
    generated.User.Pets.Create(generated.Pet.Name.Set("fido")),
    ).
    Create(ctx)

Repository: github.com/go-gorm/cli

Configuration

genconfig.Config lets you set output paths, choose the interfaces or models to include, and map custom helpers.

Platinum Sponsors

Become a Sponsor!

Platinum Sponsors

Become a Sponsor!