01-basic-entity
One User entity, written out in every field type and every query type. Start here.
- Every field type:
String, Int, Int64, Float, Bool, Byte, Time, JSON
- Field options:
Unique(), Optional(), Immutable(), Default(), DefaultFunc()
Validate() with your own Go function from the logic package
- Field level
Contracts(): a password that clients write but never read, timestamps they read but never write
- Ready made queries:
DefaultCRUD(), CreateBulk(), ListAll(), DeleteAll()
CreateBulk().Upsert("email"): re-importing a row overwrites it instead of failing on the unique email
- Queries by field:
GetBy("email"), ListBy("is_active"), and Name() to rename one
- Filters:
filter.Range() and filter.Search()
- Indexes: multi column
index.Asc() / index.Desc(), mixed sort order, Unique(), Name()
- The same schema on three dialects: sqlite, postgresql, mysql
Entity
ent/schema/user.go is the same file in all three
dialect folders. Only sqlc.yaml and the generated SQL differ.
Fields() lists one field per type, so you can see what each one becomes in
schema.sql and in schema.proto. Two fields are worth a look:
password is SQLC() plus PROTO().WriteOnly(). It is a column, and clients
can send it, but it is not in any response message.
created_at and updated_at are PROTO().ReadOnly(). The server sets them
with DefaultFunc(time.Now), so they are not in create or update requests.
api_key shows DefaultFunc on bytes. The value is generated in Go, not by the
database.
Dialects
| Folder |
Database |
Notes |
sqlite |
file on disk |
nothing to start, fastest way to try it |
postgresql |
docker |
make run starts it and waits |
mysql |
docker |
make run starts it and waits |
Compare the three ent/contract/sqlc/schema.sql files to see how types map. For
example field.Float becomes REAL on sqlite, DOUBLE PRECISION on postgresql
and DOUBLE on mysql.
Run
cd sqlite
make run # generates types, bundles the frontend, serves on :8080
For postgresql and mysql, make down stops the database and drops its data.