# `EctoContext`
[🔗](https://github.com/exfoundry/ecto_context/blob/v0.1.0/lib/ecto_context.ex#L1)

Generates standard data access functions for Ecto-backed contexts at compile time.

The `ecto_context` declaration is the table of contents for a context module.
Every generated function threads a `scope` through, forcing explicit authorization
decisions at every call site.

## Usage

    import EctoContext

    ecto_context schema: Article, scope: &__MODULE__.scope/2 do
      list()
      get!()
      get_by!()
      create()
      update()
      delete()
      change()
    end

    def scope(query, %Scope{admin: true}), do: query
    def scope(query, %Scope{user_id: uid}), do: where(query, user_id: ^uid)

    def permission(_action, _record, %Scope{admin: true}), do: true
    def permission(_, _, _), do: false

## Generated functions

| Function     | Signature                                             | Opts                                              |
|--------------|-------------------------------------------------------|----------------------------------------------------|
| `list`       | `list(scope, opts \\ [])`                           | :preload, :order_by, :limit, :select, :query       |
| `list_for`   | `list_for(scope, assoc_atom, parent_id, opts \\ [])` | :preload, :order_by, :limit, :select, :query      |
| `list_by`    | `list_by(scope, clauses, opts \\ [])`               | :preload, :order_by, :limit, :select, :query       |
| `get`        | `get(scope, id, opts \\ [])`                        | :preload                                           |
| `get!`       | `get!(scope, id, opts \\ [])`                       | :preload, :query                                   |
| `get_by`     | `get_by(scope, clauses, opts \\ [])`                | :preload                                           |
| `get_by!`    | `get_by!(scope, clauses, opts \\ [])`               | :preload, :query                                   |
| `create`     | `create(scope, attrs, opts \\ [])`                  | :changeset (default: `:changeset`)                 |
| `create_for` | `create_for(scope, assoc_atom, parent_id, attrs, opts \\ [])` | :changeset (default: `:changeset`)        |
| `update`     | `update(scope, record, attrs, opts \\ [])`          | :changeset (default: `:changeset`)                 |
| `delete`     | `delete(scope, record)`                               | —                                                  |
| `change`     | `change(scope, record, attrs \\ %{}, opts \\ [])` | :changeset (default: `:changeset`)                |
| `count`      | `count(scope, opts \\ [])`                          | :query                                             |
| `paginate`   | `paginate(scope, opts \\ [])`                       | :page, :per_page, :order_by, :preload, :query      |
| `subscribe`  | `subscribe(scope)`                                    | —                                                  |
| `broadcast`  | `broadcast(scope, message)`                           | —                                                  |

# `default_topic_key`

```elixir
@spec default_topic_key(term()) :: term()
```

Default topic key function for `subscribe/1` and `broadcast/2`.
Returns `scope.user.id`. Override via `topic_key:` in `ecto_context`.

# `ecto_context`
*macro* 

Declares the generated functions for an Ecto-backed context module.

Accepts a keyword list of context-level options (`:schema`, `:scope`, `:repo`, etc.)
and a `do` block containing function declarations like `list()`, `get!()`, `create()`.

See the module documentation for the full list of supported functions and options.

# `resolve_settings`

```elixir
@spec resolve_settings(keyword()) :: keyword()
```

Resolves the effective settings for a context declaration.

Merges three layers (lowest to highest priority):
guessed defaults from Mix/app config, library config from
`config :ecto_context, :defaults`, and the declaration-level opts.

---

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