Documentation
¶
Index ¶
- type Author
- type AuthorLeaderboardRow
- type DBTX
- type ListPostsParams
- type Org
- type Post
- type PostViewsByStatusRow
- type Queries
- func (q *Queries) AuthorLeaderboard(ctx context.Context) ([]AuthorLeaderboardRow, error)
- func (q *Queries) GetAuthor(ctx context.Context, id string) (Author, error)
- func (q *Queries) ListPosts(ctx context.Context, arg ListPostsParams) ([]Post, error)
- func (q *Queries) PostViewsByStatus(ctx context.Context, orgID string) ([]PostViewsByStatusRow, error)
- func (q *Queries) WithTx(tx pgx.Tx) *Queries
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Author ¶
type Author struct {
ID string
OrgID string
Email string
Name string
PasswordHash string
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
}
type AuthorLeaderboardRow ¶
type ListPostsParams ¶ added in v0.6.0
type Org ¶
type Org struct {
ID string
Name string
Slug string
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
}
A tenant. Every other table is scoped to one.
type Post ¶
type Post struct {
ID string
OrgID string
AuthorID string
Title string
Body string
Status string
ViewCount int64
PublishedAt pgtype.Timestamptz
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
DeletedAt pgtype.Timestamptz
}
A blog post.
type PostViewsByStatusRow ¶
type Queries ¶
type Queries struct {
// contains filtered or unexported fields
}
func (*Queries) AuthorLeaderboard ¶
func (q *Queries) AuthorLeaderboard(ctx context.Context) ([]AuthorLeaderboardRow, error)
These are deliberately the queries sqlb is bad at.
sqlb's non-goal is static analytical SQL: a window function or a recursive CTE goes through sqlb.Raw, which is an escape hatch rather than a feature (ADR-0009). sqlc types both at compile time, which is its whole guarantee. So they live here, and the filterable list endpoints live in sqlb. A window function: rank authors within their org by published posts. Nothing about this benefits from being expressible as a runtime filter.
func (*Queries) GetAuthor ¶
A plain lookup, included only so the generated Author struct exists for the adoption test to point sqlb.Describe at.
func (*Queries) ListPosts ¶ added in v0.6.0
The exception, and the one query here that is NOT what sqlc is good at.
This is stage 1 of docs/refactoring-from-sqlc.md: a filterable list endpoint written the only way static SQL can express one. Every optional filter becomes an arm that is always sent and usually means nothing, which is the documented sqlc workaround rather than a strawman — see comparisons.md.
It is here to be replaced, not to be imitated. stage1.go calls it, stage2.go through stage4.go do the same job, and refactor_test.go holds all four to the same answers.
What the shape costs, beyond reading badly:
- Three predicates reach Postgres on every request, each guarded by a NULL check the planner has to see through. Stage 2 sends only what was asked.
- The sort is baked in. `ORDER BY published_at DESC` cannot become `ORDER BY view_count ASC` without a second copy of the whole query, so a column the UI can sort by is a query, and n columns times two directions is 2n of them.
- Nothing here says which columns a *client* may filter on. That decision lives in whatever handler assembles these parameters, so it is reviewable only by reading that handler — where ADR-0006 puts it in the schema.
func (*Queries) PostViewsByStatus ¶
func (q *Queries) PostViewsByStatus(ctx context.Context, orgID string) ([]PostViewsByStatusRow, error)
A grouped aggregate over one org. sqlb can express this with Collect, but the result shape is not the table shape, so the typed guarantee is worth more here than the runtime filterability sqlb trades it for.