Creating Data Models
Gojang provides an automated generator (task addmodel / go run ./gojang/cmd/addmodel) that scaffolds Ent schema, handlers, routes, templates and admin registration. This guide summarizes the manual and automated approaches.
Automated Model Generation
Interactive:
task addmodel
# or
go run ./gojang/cmd/addmodel
Non-interactive example:
go run ./gojang/cmd/addmodel \
--model Product \
--icon "📦" \
--fields "name:string:required,description:text,price:float:required,stock:int"
The tool supports --dry-run, examples, and reserved-keyword validation.
Manual Steps (overview)
- Define the Ent schema in
gojang/models/schema/ - Generate Ent code:
cd gojang/models && go generate ./...
- Create form structs in
gojang/views/forms/ - Create handlers in
gojang/http/handlers/ - Create routes in
gojang/http/routes/and register them ingojang/cmd/web/main.go - Create templates in
gojang/views/templates/<model>/ - Register model with the admin panel in
gojang/admin/models.go
Notes
- The app auto-migrates on startup, but you can also create manual SQL migrations if desired.
- See the code samples in the repository for full handler, route, and template examples (the docs in the repo contain step-by-step code snippets).
Checklist
- Define schema
- go generate
- Create form struct
- Create handler & routes
- Create templates
- Register with admin
- Test CRUD and admin pages