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
| Flag | Default | Description |
|---|---|---|
--graphql | false | Also 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. |
--gql | false | Alias for --graphql |
Field Types
| Type | Go Type | SQL Type (Postgres) | GraphQL Type |
|---|---|---|---|
string | string | VARCHAR(255) | String |
text | string | TEXT | String |
int | int | INTEGER | Int |
float | float64 | DECIMAL(10,2) | Float |
bool | bool | BOOLEAN | Boolean |
uuid | uuid.UUID | UUID | ID |
time | time.Time | TIMESTAMP | DateTime |
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:floatGenerate a BlogPost with multiple fields:
gofasta g scaffold BlogPost title:string body:text published:bool author_id:uuid published_at:timeGenerate a resource with GraphQL support:
gofasta g scaffold Category name:string description:text --graphqlWhat It Generates
Running gofasta g scaffold Product name:string price:float creates 11 files:
| File | Description |
|---|---|
app/models/product.model.go | GORM database model |
db/migrations/000006_create_products.up.sql | Create table migration |
db/migrations/000006_create_products.down.sql | Drop table migration |
app/repositories/interfaces/product_repository.go | Repository interface |
app/repositories/product.repository.go | Repository implementation |
app/services/interfaces/product_service.go | Service interface |
app/services/product.service.go | Service implementation |
app/dtos/product.dtos.go | Request/response DTOs |
app/di/providers/product.go | Wire DI provider set |
app/rest/controllers/product.controller.go | REST controller with CRUD handlers |
app/rest/routes/product.routes.go | Route definitions |
It also patches 4 existing files:
| File | Change |
|---|---|
app/di/container.go | Adds ProductService and ProductController fields |
app/di/wire.go | Adds ProductSet to the Wire build |
app/rest/routes/index.routes.go | Registers Product routes |
cmd/serve.go | Wires 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.goto 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:
- Run the migration:
gofasta migrate up - Regenerate Wire (if not done automatically):
gofasta wire - Test the endpoints with curl or your preferred API client
Related
- gofasta g model — generate only the model
- gofasta g controller — generate only the controller
- gofasta g migration — generate only migration files
- gofasta migrate — run the generated migrations
- Quick Start