Add pages for a model
make:crud writes a model with its table, and the pages to list, show,
create, edit and delete its rows, made of your app’s UI components, with
their routes and a test. You get working pages in a minute, and code
you can read and change.
Generate them
go tool anetos make:crud Post title:string body:text published:bool
go run . migrateOpen http://localhost:8080/posts (the header links to it): an empty list with a “New post” button. Save the form empty to see the validation messages, then fill it in.
Each field is name:type, then :optional or :unique if you like:
| Type | Go | Column | Input |
|---|---|---|---|
string | string | VARCHAR(255) | a text box |
text | string | TEXT | a text area |
email | string | VARCHAR(255) | an email box, checked with the email rule |
int | int64 | BIGINT | a number box |
float | float64 | double | a number box with decimals |
bool | bool | boolean | a checkbox |
date | anetos.Date | DATE | a date picker |
Strings, texts, emails and dates are required; :optional lets them be
empty. :unique, for required strings and emails and for dates, adds a
unique index and the unique rule, so the form says “The email has
already been taken.” rather than failing. (An optional string is empty,
not NULL, when left blank, and a blank number is 0: only one row could
have either, so make:crud refuses them; add such an index by hand.)
go tool anetos make:crud Customer name:string email:email:unique notes:text:optional since:dateNumbers get no rule beyond their type: a negative stock is accepted
until you add one (validate:"min:0" on the input’s field). There’s no
decimal type: keep money in an int of cents, which adds up exactly,
rather than a float.
What it writes
For Post:
| File | Holds |
|---|---|
app/models/post.go | Post, with a field per column; anetos generate adds PostCols to models_gen.go |
database/migrations/…_create_posts_table.go | The posts table |
app/handlers/posts.go | Posts: Index (20 a page, newest first), Show, New, Create, Edit, Update, Delete; PostInput, the form, with its validate tags |
views/posts.templ | The list, a post’s page, and the form for new and existing posts. In a react, svelte or vue project: the components frontend/src/pages/Posts/Index, Show and Form (.tsx, .svelte, .vue), and the handlers render them with props |
routes/posts.go | routes.Posts: the seven routes, named posts.index, posts.show… |
locales/en/posts.yaml | The pages’ text: titles, buttons, the fields’ labels, the messages |
posts_test.go | A test that creates, shows, edits and deletes a post through the pages |
It also adds Posts(pages) to routes/web.go (the page group, with
sessions and CSRF protection) and a link to the list in the layout’s
header (views/layout.templ, or the frontend’s src/Layout). It never overwrites a file: if one exists, it writes nothing.
| Route | URL | Does |
|---|---|---|
posts.index | GET /posts | The list, with pages (?page=2) |
posts.new | GET /posts/new | The empty form |
posts.create | POST /posts | Creates the post, then shows it |
posts.show | GET /posts/{id} | The post, with Edit and Delete |
posts.edit | GET /posts/{id}/edit | The filled form |
posts.update | PUT /posts/{id} | Saves the changes |
posts.delete | DELETE /posts/{id} | Deletes the post, then lists the others |
HTML forms send PUT and DELETE with a hidden _method field, which
the project’s web.MethodOverride reads.
Each route is named after its handler (h.Create is posts.create).
Coming from another framework? Rails and Phoenix also call the form
newand saving itcreate, and Phoenix saysdeletewhere Rails saysdestroy. Laravel’s resource routes saycreatefor the form,storefor saving it anddestroyfor deleting. Django’s generic views are classes (CreateView,DeleteView).
Make it yours
The code is plain Anetos code: change it as you would your own.
Only for logged-in users. After
make:auth, move thePosts(pages)call fromroutes/web.goto theloggedIngroup ofroutes/auth.go(Posts(loggedIn)): guests then go to the login page (a guest’s form post gets401: the redirect is for pages). Log a user in at the start ofTestPosts, as its comment shows (make:authwrotefactories.Users), and show the header’s link to logged-in users only, inviews/layout.templ(importinganetos.dev/anetos/authand the app’sapp/models):// illustrative if _, ok := auth.User[*models.User](ctx); ok { @navLink("posts.index", i18n.T(ctx, "posts.title")) }In a react, svelte or vue project, the components read the shared
userprop (usePage().props.user, null for a guest).Guests read, users write. Split
routes/posts.goin two: one function with the list and a post’s page, whichroutes/web.gokeeps calling, and one with the form’s routes, create, edit, update and delete, whichroutes/auth.gocalls withloggedIn. Hide the buttons that change posts from guests as above (Build pages in React shows it for the components).Another column. Add a migration (
go tool anetos make:migration add_slug_to_posts_table), the field toPostandPostInput, a line tofill, and the input topostFieldsin the view.Other text. Change
locales/en/posts.yaml; add a language with its own folder (Translations).Look. The pages call the components of
views/ui(ui.PageHeader,ui.Table,ui.Form,ui.Field…) and have no class names. Change the colors inpublic/static/app.css, or a component’s markup inviews/ui, and every page follows (Style your app).
go test ./... runs the test it wrote; keep it passing as you change
the pages.
In an API project
In a project made with anetos new --stack=api, the same
command writes JSON endpoints instead of pages: the model and its
migration, app/handlers/posts.go, routes/posts.go and
posts_test.go, and Posts(api) at the end of Register in
routes/api.go. There are no views or catalog.
| Route | URL | Answers |
|---|---|---|
api.posts.index | GET /api/v1/posts | A page of posts |
api.posts.create | POST /api/v1/posts | 201 with the post, and its URL in Location |
api.posts.show | GET /api/v1/posts/{id} | The post |
api.posts.update | PUT /api/v1/posts/{id} | The post, every field replaced |
api.posts.delete | DELETE /api/v1/posts/{id} | 204 |
A post is answered as PostResponse, a struct of the handlers’ file
(the ID, the fields, created_at, updated_at), never as the model:
a column you add later shows only once you add it there. Optional dates
are null when empty. The body of POST and PUT is JSON with the
fields’ names, checked with the same rules as the pages’ form: a 422
lists the fields’ errors.
The list is {"data": [...], "current_page": 1, "per_page": 20, "total": 1, "last_page": 1}. Its query:
?page=2, and?per_page=50(20 by default, at most 100).?sort=title, or-titlefor descending:id,created_at,updated_ator a field other than a text. Newest first (-id) by default; another value is a 422. Rows with the same value come newest first. Where an optional date’s empty value (NULL) sorts depends on the database: first ascending in SQLite and MySQL, last in PostgreSQL.- An exact filter per field of type string, email, int, bool or date:
?published=true. An empty value (?title=) doesn’t filter. Texts and floats aren’t filters: search and ranges are yours to add toIndex.
So fields can’t be named page, per_page or sort in an API project.
The endpoints are open to every client. After
make:auth, for clients with a token only:
Move the
Posts(api)call fromroutes/api.gotoroutes/auth.go, in theloggedIngroup:Posts(loggedIn).If tokens should need abilities, require them on the routes, in
routes/posts.go(withanetos.dev/anetos/authimported):// illustrative read := r.With(auth.RequireAbilities("posts:read")) write := r.With(auth.RequireAbilities("posts:write")) read.Get("/posts", web.H(h.Index)).Name("posts.index") write.Post("/posts", web.H(h.Create)).Name("posts.create").Status(http.StatusCreated)posts_test.gonow gets 401: make its test sign up first, withauth_test.go’s helper,app, _ := authRegister(t)in place ofapp := anetostest.New(t, setup).go run . openapi, soopenapi.jsonsays they need a token.
Tutorial: build an API does it step by step, with records that belong to their user.
make:crud updates openapi.json, the API’s description, with the
five operations, PostResponse, PostInput and PagePostResponse
(Describe an API with OpenAPI).
Number and boolean fields are never required: a request without
price saves 0, and a PUT without it sets it back to 0. Add a rule
(validate:"required" refuses 0, so a positive min:1 or a pointer
field may suit better) where a value must be given.
When to write pages by hand
make:crud makes the common case quick: a table, a form, plain
validation. Pages with relations, permissions or htmx start from its
code, or from scratch: the tutorial builds issue
pages by hand, step by step, and the guides cover
forms, validation,
queries and relations.
Next: Add accounts.