--- title: Annotations description: Customize generated Go names, result groups, scan destinations, and enum mappings. --- ## What is an annotation? An annotation supplies metadata for generated Go code. Use `@stitchql(...)` to name a result field, group selected columns, choose a scan destination, or change an enum's Go mapping. Annotations do not appear in the emitted SQL. ## Structure and placement An annotation contains comma-separated `name:` and `type:` options: ```stitchql @stitchql(name: DisplayTitle, type: string) ``` | Option | Meaning | | --- | --- | | `name: DisplayTitle` | Choose an exported Go field or enum type name | | `type: string` | Use an explicit Go type instead of the inferred destination | Names are unquoted Go identifiers and must begin with an uppercase letter. Types use Go syntax. Qualified types require an import in the `.stitchql` file; handwritten local types belong in neighboring `.go` files. Place the annotation above the column, expression, fragment expansion, or enum declaration it describes. The supported options depend on that target: | Location | Allowed options | | --- | --- | | Column or expression in SELECT or RETURNING | `name`, `type`, or both | | Columns expansion in SELECT or RETURNING | `name`, or `name` and `type` together | | Column or nested expansion inside a `columns` declaration | `name` only | | CREATE TYPE AS ENUM declaration | Either `name` or `type`, never both | | Predicates, WHERE/ON clauses, table columns, tables, or fields fragments | No annotations | An annotation applies to the **next complete result entry** or enum declaration. It cannot appear inside an expression or after its target. Supply at least one option; duplicate options, unknown options, and stacked annotations are errors. Options can span lines, and a trailing comma is allowed. ## Examples These examples are independent unless one explicitly reuses an earlier declaration. Unless a schema is shown, they assume `books.id` and `books.title` are NOT NULL, `books.subtitle` is nullable, and `books.author_id` relates to `authors.id`. ### Name a Go result field ```stitchql query ReadTitles() { SELECT @stitchql(name: DisplayTitle) b.title AS title FROM books AS b } ``` This generates: ```go type ReadTitlesResult struct { DisplayTitle string } ``` The emitted SQL contains no annotation (whitespace adjusted for readability): ```sql SELECT b.title AS title FROM books AS b ``` The SQL result label remains `title`. The annotation changes only the Go field name. Use a SQL alias when you want to change the SQL label as well. Computed expressions still need an SQL alias, even when they have a `name` annotation. The annotation does not take the place of `AS ...`. ### Choose a scan destination By default, computed results use conservative nullable Go types. When the SQL guarantees a value, you can choose a plain destination: ```stitchql query ReadDisplayTitle() { SELECT @stitchql(type: string) COALESCE(b.subtitle, b.title) AS display_title FROM books AS b } ``` `DisplayTitle` uses `string`. In this example, the NOT NULL `title` column makes the COALESCE result non-null. Without that guarantee, a nullable destination is appropriate: ```stitchql import "database/sql" query ReadSubtitle() { SELECT @stitchql(type: sql.Null[string]) b.subtitle FROM books AS b } ``` An explicit type is used **exactly as written**. StitchQL does not add a nullable wrapper around it. The annotation does not add an SQL cast, change the database type, replace NULL with a zero value, or alter the query's values. Go checks the destination type, and pgx reports incompatible values or NULL destinations when scanning. Supported Go type expressions include named and imported types, generic instantiations, pointers, slices, arrays, maps, and anonymous structs. Custom destinations can use pgx codecs or implement `sql.Scanner` for SQL types outside the default mappings. ### Group result fields Without an annotation, a columns expansion contributes fields directly to the query's result. Add a name to place its fields in a nested group: ```stitchql columns bookColumns[b books] { b.id, b.title } columns authorColumns[a authors] { a.name, a.nickname } query ReadBooksWithAuthors() { SELECT {bookColumns[b]}, @stitchql(name: Author) {authorColumns[a]} FROM books AS b LEFT JOIN authors AS a ON a.id = b.author_id } ``` The result has the following shape: ```go type ReadBooksWithAuthorsResult struct { ID int64 Title string Author struct { Name sql.Null[string] Nickname sql.Null[string] } } ``` The LEFT JOIN makes both author fields nullable. The group remains an anonymous struct; it does not become a pointer when the joined row is absent. An unmatched row and a matched row whose selected values are all NULL remain indistinguishable. The same fragment can appear in different named groups, such as `Original` and `Sequel` in a self-join. Each group infers nullability for its own alias. Equal field names in different groups are allowed; duplicates inside one group are errors. Nested name-only groups can also be declared inside columns fragments. ### Use a custom group type Define the destination in Go, using `db` tags to map the selected SQL labels: ```go title="store/books.go" package store type BookSummary struct { Key int64 `db:"book_id"` Title string `db:"title"` } ``` ```stitchql columns bookSummary[b books] { b.id AS book_id, b.title } query ReadBookSummary() { SELECT @stitchql(name: Book, type: BookSummary) {bookSummary[b]} FROM books AS b } ``` The generated result contains `Book BookSummary`. A custom group type requires an explicit `name`; a fragment can expand to multiple SQL labels, so there is no single label from which to infer the group's field name. Group mapping is strict and uses exported struct fields and `db` tags. Missing or mismatched fields and non-struct targets produce scan errors. A custom group must account for NULL values introduced by joins. Its type is not automatically changed to match each consumer. Place the type override at the query's SELECT or RETURNING site. A reusable `columns` declaration supports name annotations, but not type overrides. ### Decode a JSON result into a Go type Start with a `jsonb` column that stores book metadata: ```stitchql title="store/books.stitchql" package store schema { CREATE TABLE books ( id bigint PRIMARY KEY, metadata jsonb NOT NULL ); } ``` For example, `metadata` could contain: ```json {"title": "Kindred", "pages": 264} ``` Define a Go destination for that JSON object. Both PostgreSQL `json` and `jsonb` columns use pgx JSON decoding and the Go type's `json` tags: ```go title="store/metadata.go" package store type BookMetadata struct { Title string `json:"title"` Pages int32 `json:"pages"` } ``` ```stitchql query ReadMetadata() { SELECT @stitchql(type: BookMetadata) b.metadata FROM books AS b } ``` The result field is `Metadata BookMetadata`. JSON object mapping uses `json` tags; mapping several SQL columns into a named result group uses `db` tags. Anonymous struct destinations can use the same tags. ### Annotate RETURNING columns Result annotations work the same way in INSERT, UPDATE, and DELETE RETURNING lists: ```stitchql query RenameBook(id int64, title string) { UPDATE books AS b SET title = {title} WHERE b.id = {id} RETURNING @stitchql(name: BookID) b.id, @stitchql(name: DisplayTitle) b.title } ``` The result has `BookID int64` and `DisplayTitle string`. You can also annotate columns expansions in RETURNING to create named or custom result groups. ### Control enum generation An annotation above an enum declaration controls its Go mapping across the package: ```stitchql schema { @stitchql(name: Status) CREATE TYPE book_status AS ENUM ('draft', 'published'); @stitchql(type: string) CREATE TYPE visibility AS ENUM ('public', 'private'); } ``` The first declaration generates `Status`, `StatusDraft`, and `StatusPublished`. The second uses plain strings in inferred results and emits no Go enum type or constants. `type:` can also refer to an existing local or imported Go type. Enum annotations accept **either `name` or `type`**, not both. They retain the SQL type name and label ordering. Nullable and array results still wrap the chosen enum base type; this differs from a result-column type override, which sets the exact destination. A result-column override takes precedence over the enum's package-wide mapping. All declared enums remain included in `RegisterTypes`, including those with type overrides. See [Types and enums](types.md#enums) for connection registration.