Skip to Content
DocumentationGuidesCode Generation

Code Generation

The Gofasta CLI includes a powerful code generation system that creates files following the framework’s layered architecture. Every generated file follows consistent conventions and integrates with the existing project structure.

The Generate Command

All generators are accessed through gofasta generate (or the shorthand gofasta g):

gofasta g <generator> <Name> [field:type ...]

The Name argument is automatically converted to the correct casing for each context: PascalCase for Go types, snake_case for file names and database tables, and camelCase for JSON fields.

Available Generators

GeneratorCommandWhat it creates
scaffoldgofasta g scaffoldAll layers for a complete resource
modelgofasta g modelDatabase model + migration
servicegofasta g serviceService interface + implementation
controllergofasta g controllerREST controller
repositorygofasta g repositoryRepository interface + implementation
dtogofasta g dtoRequest/response DTOs
migrationgofasta g migrationEmpty migration pair (up + down)
routegofasta g routeRoute registration file
resolvergofasta g resolverRegenerate GraphQL resolver stubs
providergofasta g providerWire DI provider set
jobgofasta g jobCron job definition
taskgofasta g taskAsync task for the queue
email-templategofasta g email-templateHTML email template

Scaffold: The Full Resource Generator

The scaffold command is the most commonly used generator. It creates every layer of a resource and wires it into the project:

gofasta g scaffold Product name:string price:float description:text active:bool

Generated Files

FileLocationDescription
Modelapp/models/product.model.goGORM model with BaseModelImpl
Up migrationdb/migrations/000006_create_products.up.sqlCREATE TABLE statement
Down migrationdb/migrations/000006_create_products.down.sqlDROP TABLE statement
Repository interfaceapp/repositories/interfaces/product_repository.goCRUD method signatures
Repository implapp/repositories/product.repository.goGORM implementation
Service interfaceapp/services/interfaces/product_service.goBusiness logic signatures
Service implapp/services/product.service.goBusiness logic implementation
DTOsapp/dtos/product.dtos.goCreate/Update request + response types
Controllerapp/rest/controllers/product.controller.goHTTP handlers for CRUD
Routesapp/rest/routes/product.routes.goRoute registrations
Providerapp/di/providers/product.goWire provider set

Patched Files

The scaffold command also modifies existing files to integrate the new resource:

FileWhat changes
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 scaffolding, run gofasta wire to regenerate the Wire DI container, then gofasta migrate up to create the database table.

Field Types

Fields are specified as name:type pairs. The type determines the Go type, SQL column type, and GraphQL type:

TypeGo typeSQL type (Postgres)SQL type (MySQL)GraphQL type
stringstringVARCHAR(255)VARCHAR(255)String
textstringTEXTTEXTString
intintINTEGERINTInt
floatfloat64DECIMAL(10,2)DECIMAL(10,2)Float
boolboolBOOLEANTINYINT(1)Boolean
uuiduuid.UUIDUUIDCHAR(36)ID
timetime.TimeTIMESTAMPDATETIMEDateTime

SQL types are automatically adapted for the database driver configured in config.yaml.

Individual Generators

Model

Generates the model struct and migration files:

gofasta g model Category name:string description:text

Creates:

  • app/models/category.model.go
  • db/migrations/000007_create_categories.up.sql
  • db/migrations/000007_create_categories.down.sql

The model automatically embeds models.BaseModelImpl for standard fields.

Service

Generates the service interface and implementation:

gofasta g service Notification

Creates:

  • app/services/interfaces/notification_service.go
  • app/services/notification.service.go

The service implementation receives a repository through its constructor.

Controller

Generates an HTTP controller:

gofasta g controller Payment

Creates:

  • app/rest/controllers/payment.controller.go

The controller receives a service through its constructor and includes stub CRUD methods.

Repository

Generates the repository interface and GORM implementation:

gofasta g repository Order

Creates:

  • app/repositories/interfaces/order_repository.go
  • app/repositories/order.repository.go

DTO

Generates request and response data transfer objects:

gofasta g dto Invoice amount:float status:string due_date:time

Creates:

  • app/dtos/invoice.dtos.go

Migration

Generates an empty migration pair for custom SQL:

gofasta g migration add_index_to_products

Creates:

  • db/migrations/000008_add_index_to_products.up.sql
  • db/migrations/000008_add_index_to_products.down.sql

Both files are empty — you write the SQL yourself. This is useful for schema changes that are not a simple table creation.

Route

Generates a route registration file:

gofasta g route Subscription

Creates:

  • app/rest/routes/subscription.routes.go

Resolver

Regenerates GraphQL resolver stubs from schema files:

gofasta g resolver

This runs gqlgen to regenerate resolvers. Existing implementations are preserved.

Provider

Generates a Wire dependency injection provider:

gofasta g provider Analytics

Creates:

  • app/di/providers/analytics.go

Job

Generates a cron job definition:

gofasta g job CleanupExpiredTokens

Creates:

  • app/jobs/cleanup_expired_tokens.job.go

Task

Generates an async task for the queue:

gofasta g task SendWelcomeEmail

Creates:

  • app/jobs/send_welcome_email.task.go

Email Template

Generates an HTML email template:

gofasta g email-template order-confirmation

Creates:

  • templates/emails/order-confirmation.html

Customizing Generated Code

All generated code is plain Go — there are no runtime dependencies on the generator. After generation, you own the code and can modify it freely.

Common customizations:

  • Add validation rules to DTOs using binding struct tags
  • Add indexes to models using gorm struct tags
  • Add custom repository methods beyond the standard CRUD
  • Add business logic to services
  • Add custom endpoints to controllers and routes

Workflow Example

A typical workflow for adding a new feature:

# 1. Generate the full resource gofasta g scaffold Order total:float status:string user_id:uuid # 2. Regenerate Wire DI container gofasta wire # 3. Run migrations gofasta migrate up # 4. Customize the generated code # - Add validation to app/dtos/order.dtos.go # - Add business logic to app/services/order.service.go # - Add auth middleware to app/rest/routes/order.routes.go # 5. Test curl -X POST http://localhost:8080/api/v1/orders \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <token>" \ -d '{"total": 99.99, "status": "pending", "user_id": "..."}'

Next Steps

Last updated on