Skip to Content
DocumentationCLI Referencegofasta generatescaffold

gofasta g scaffold

Generates a full CRUD resource spanning all layers of the application. This is the most powerful generator command — it creates 11 new files and patches 4 existing files to wire everything together. After running this command and applying migrations, you have a fully working REST API for your resource.

Aliases: s

Usage

gofasta g scaffold <Name> [field:type ...] [flags]

gofasta generate scaffold also works. The g shorthand is equivalent to generate.

Flags

FlagDefaultDescription
--graphqlfalseAlso generate GraphQL schema and wire up the resolver for this resource. This is separate from the --graphql flag on gofasta new, which enables GraphQL for the entire project. Your project must have been created with gofasta new --graphql for this flag to work.
--gqlfalseAlias for --graphql

Field Types

TypeGo TypeSQL Type (Postgres)GraphQL Type
stringstringVARCHAR(255)String
textstringTEXTString
intintINTEGERInt
floatfloat64DECIMAL(10,2)Float
boolboolBOOLEANBoolean
uuiduuid.UUIDUUIDID
timetime.TimeTIMESTAMPDateTime

The datetime alias can be used in place of time. SQL types are generated per-driver (postgres, mysql, sqlite, sqlserver, clickhouse) based on the driver configured in config.yaml.

Examples

Generate a Product resource with name and price fields:

gofasta g scaffold Product name:string price:float

Generate a BlogPost with multiple fields:

gofasta g scaffold BlogPost title:string body:text published:bool author_id:uuid published_at:time

Generate a resource with GraphQL support:

gofasta g scaffold Category name:string description:text --graphql

What It Generates

Running gofasta g scaffold Product name:string price:float creates 11 files:

FileDescription
app/models/product.model.goGORM database model
db/migrations/000006_create_products.up.sqlCreate table migration
db/migrations/000006_create_products.down.sqlDrop table migration
app/repositories/interfaces/product_repository.goRepository interface
app/repositories/product.repository.goRepository implementation
app/services/interfaces/product_service.goService interface
app/services/product.service.goService implementation
app/dtos/product.dtos.goRequest/response DTOs
app/di/providers/product.goWire DI provider set
app/rest/controllers/product.controller.goREST controller with CRUD handlers
app/rest/routes/product.routes.goRoute definitions

It also patches 4 existing files:

FileChange
app/di/container.goAdds ProductService and ProductController fields
app/di/wire.goAdds ProductSet to the Wire build
app/rest/routes/index.routes.goRegisters Product routes
cmd/serve.goWires ProductController into the route config

After generation, Wire regeneration is run automatically.

With --graphql, the scaffold additionally:

  • Generates a GraphQL schema file (.gql)
  • Patches resolver.go to add the service dependency
  • Runs gqlgen generate

Generated Code Examples

Model

// app/models/product.model.go package models import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) type Product struct { ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"` Name string `gorm:"type:varchar(255);not null" json:"name"` Price float64 `gorm:"type:decimal(10,2);not null" json:"price"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"` }

Controller (excerpt)

// app/rest/controllers/product.controller.go package controllers import ( "net/http" "myapp/app/dtos" svcInterfaces "myapp/app/services/interfaces" "myapp/pkg/httputil" ) type ProductController struct { service svcInterfaces.ProductService } func NewProductController(service svcInterfaces.ProductService) *ProductController { return &ProductController{service: service} } // Create handles POST /api/v1/products func (c *ProductController) Create(w http.ResponseWriter, r *http.Request) error { var req dtos.CreateProductRequest // parse and validate request body // ... result, err := c.service.Create(req) if err != nil { return err } return httputil.WriteJSON(w, http.StatusCreated, result) }

Routes (excerpt)

// app/rest/routes/product.routes.go package routes import ( "github.com/go-chi/chi/v5" "myapp/app/rest/controllers" "myapp/pkg/httputil" ) func RegisterProductRoutes(r chi.Router, controller *controllers.ProductController) { r.Post("/products", httputil.Handle(controller.Create)) r.Get("/products", httputil.Handle(controller.FindAll)) r.Get("/products/{id}", httputil.Handle(controller.FindByID)) r.Put("/products/{id}", httputil.Handle(controller.Update)) r.Delete("/products/{id}", httputil.Handle(controller.Delete)) }

After Running

After generating a scaffold, complete these steps:

  1. Run the migration: gofasta migrate up
  2. Regenerate Wire (if not done automatically): gofasta wire
  3. Test the endpoints with curl or your preferred API client
Last updated on