# `PgRest.SQL.Format`
[🔗](https://github.com/agoodway/pgrest/blob/v0.2.0/lib/pg_rest/sql/format.ex#L1)

The SQL expression of a response `body` column, by media type: PostgREST's `asJsonF`,
`asJsonSingleF`, `addNullsToSnip` and `asCsvF`.

The expression aggregates the `_postgrest_t` rows of the statement's final select, and
`shape` says how:

  * `:rows` - a JSON array of the rows
  * `:single_composite` - the first row as one object, or `null` (the object media type, or a
    function returning one row)
  * `:scalar_set` - a JSON array of the `pgrst_scalar` column
  * `:scalar` - the first `pgrst_scalar` value, or `null`

With `nulls_stripped?` the JSON aggregate is wrapped in `json_strip_nulls(...)`. CSV is the
same for every shape: a header, a newline, then each row's text form without its enclosing
parentheses, one per line. The rows' text is Postgres' record output (its own quoting), which
is what PostgREST sends. The header is unquoted column names, from `header`:

  * `:first_row` (default) - the keys of the first row of `pgrst_source`, as PostgREST's
    `asCsvF`: empty when there is no row
  * `{:expr, sql}` - a SQL text expression for the names, whatever the rows (a call's or a
    write's body columns). The caller binds the names; they are request text (aliases) and
    never spliced

## Differences from PostgREST

A call's or write's CSV header lists exactly the columns the body carries, empty results
included. PostgREST v14.18 also adds unselected filter and key columns to the header:
`GET /rpc/getallprojects?select=name&id=eq.1` answers header `id,name` over a one-column body.
It also sorts the names and names the column behind an alias. We treat that as a bug and do
not copy it.

# `shape`

```elixir
@type shape() :: :rows | :single_composite | :scalar | :scalar_set
```

# `body`

```elixir
@spec body(PgRest.MediaType.t(), shape(), :first_row | {:expr, iodata()}) :: iodata()
```

The `body` expression (text) for `media` and `shape`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
