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.

Usage

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

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

Flags

FlagShortDefaultDescription
--skip-migrationfalseSkip generating migration files
--skip-graphqlfalseSkip generating GraphQL resolver
--skip-wirefalseSkip running Wire after generation

Field Types

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

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 without migration files:

gofasta g scaffold Category name:string description:text --skip-migration

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

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 type ProductController struct { service interfaces.ProductService } func NewProductController(service interfaces.ProductService) *ProductController { return &ProductController{service: service} } // Create godoc // @Summary Create a new product // @Tags Products // @Accept json // @Produce json // @Param body body dtos.CreateProductRequest true "Product data" // @Success 201 {object} dtos.ProductResponse // @Router /api/v1/products [post] func (c *ProductController) Create(ctx *gin.Context) { // ... }

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. Regenerate Swagger docs: gofasta swagger
  4. Test the endpoints with curl or the Swagger UI
Last updated on