CRUD Walkthrough
This guide covers a full Create/Read/Update/Delete flow using Kashvi generators and runtime commands.
1. Scaffold a resource
Generate all CRUD files:
kashvi make:crud PostYou can also use flags:
kashvi make:crud Post --authorize --cache--authorize: route snippet printed by CLI includes an auth middleware placeholder.--cache: generated controller includes cache placeholders inIndexandShow.
2. Generated files
For Post, Kashvi creates:
app/models/post.goapp/controllers/post_controller.goapp/services/post_service.godatabase/migrations/<timestamp>_create_posts_table.godatabase/seeders/post_seeder.gotestdata/post_scenarios.json
3. Register routes
The generator prints lines to paste into your route setup. Typical wiring:
package routes
import (
"github.com/your-org/your-app/app/controllers"
appctx "github.com/shashiranjanraj/kashvi/pkg/ctx"
"github.com/shashiranjanraj/kashvi/pkg/router"
)
func RegisterAPI(r *router.Router) {
api := r.Group("/api")
ctrl := controllers.NewPostController()
api.Get("/posts", "posts.index", appctx.Wrap(ctrl.Index))
api.Post("/posts", "posts.store", appctx.Wrap(ctrl.Store))
api.Get("/posts/{id}", "posts.show", appctx.Wrap(ctrl.Show))
api.Put("/posts/{id}", "posts.update", appctx.Wrap(ctrl.Update))
api.Delete("/posts/{id}", "posts.destroy", appctx.Wrap(ctrl.Destroy))
}Ensure the route function is attached in main.go:
app.New().
Routes(routes.RegisterAPI).
Run()4. Implement migration
The generated migration registers automatically, but Up and Down are placeholders. Fill them:
func (m *M_20260301010101_create_posts_table) Up(db *gorm.DB) error {
type Post struct {
gorm.Model
Title string
Body string
}
return db.AutoMigrate(&Post{})
}
func (m *M_20260301010101_create_posts_table) Down(db *gorm.DB) error {
return db.Migrator().DropTable("posts")
}Run migration:
kashvi migrateInspect migration state:
kashvi migrate:status5. Run and test endpoints
Start server:
kashvi serveCreate
curl -X POST http://localhost:8080/api/posts \
-H 'Content-Type: application/json' \
-d '{}'List
curl http://localhost:8080/api/postsShow
curl http://localhost:8080/api/posts/1Update
curl -X PUT http://localhost:8080/api/posts/1 \
-H 'Content-Type: application/json' \
-d '{}'Delete
curl -X DELETE http://localhost:8080/api/posts/1 -iThe generated Destroy handler returns 204 No Content.
6. Use generated test scenarios
kashvi make:crud creates testdata/post_scenarios.json. You can feed these scenarios into your pkg/testkit test runner and keep them as executable API documentation.
If you used --authorize, scenario entries include an Authorization header placeholder (Bearer dummy-jwt-token).
7. Next improvements
- Replace empty request structs in controller methods with typed DTOs + validation tags.
- Move DB logic into
app/services/post_service.goand keep controllers thin. - Add middleware (Auth, rate-limit, role checks) per route group.
- Add pagination in
Indexusingpkg/ormpagination helpers. - Add queue jobs for side effects (notifications, emails, analytics writes).