# `Backpex.Adapters.Ecto`
[🔗](https://github.com/naymspace/backpex/blob/0.18.0/lib/backpex/adapters/ecto.ex#L1)

The `Backpex.Adapter` to connect your `Backpex.LiveResource` to an `Ecto.Schema`.

## `adapter_config`

* `:repo` (`t:atom/0`) - Required. The `Ecto.Repo` that will be used to perform CRUD operations for the given schema.

* `:schema` (`t:atom/0`) - Required. The `Ecto.Schema` for the resource.

* `:update_changeset` (function of arity 3) - Changeset to use when updating items. Additional metadata is passed as a keyword list via the third parameter:
  - `:assigns` - the assigns
  - `:target` - the name of the `form` target that triggered the changeset call. Default to `nil` if the call was not triggered by a form field. The default value is `&Backpex.Adapters.Ecto.default_changeset/3`.

* `:create_changeset` (function of arity 3) - Changeset to use when creating items. Additional metadata is passed as a keyword list via the third parameter:
  - `:assigns` - the assigns
  - `:target` - the name of the `form` target that triggered the changeset call. Default to `nil` if the call was not triggered by a form field. The default value is `&Backpex.Adapters.Ecto.default_changeset/3`.

* `:item_query` (function of arity 3) - The function that can be used to modify the ecto query. It will be used when resources are being fetched. This
  happens on `index`, `edit` and `show` view. In most cases this function will be used to filter items on `index`
  view based on certain criteria, but it may also be used to join other tables on `edit` or `show` view.

  This function should accept the following parameters:

  - `query` - `Ecto.Query.t()`
  - `live_action` - `atom()`
  - `assigns` - `map()`

  It should return an `Ecto.Queryable`. It is recommended to build your `item_query` on top of the incoming query.
  Otherwise you will likely get binding errors.

  The default value is `&Backpex.Adapters.Ecto.default_item_query/3`.

> ### Work in progress {: .warning}
>
> The `Backpex.Adapters.Ecto` is under heavy development and will change drastically in future updates.
> Backpex started out as `Ecto`-only and we are working on decoupling things to support multiple data sources.
> This is the first draft of moving all `Ecto` related functions into a dedicated Ecto adapter.

# `apply_criteria`

# `apply_filters`

Applies validated filters to the query.

Receives a map of validated filter values and the filter configurations keyword list.
For each filter value, looks up the corresponding filter config and invokes
the filter's query/4 callback with the already-validated and casted value.

# `apply_search`

# `change`

Applies a change to a given item.

# `count`

Returns the number of items matching the given criteria.

# `delete_all`

Deletes multiple items.

# `get`

Gets a database record with the given primary key value.

# `get_primary_key_field`

# `insert`

Inserts given item.

# `list`

Returns a list of items by given criteria.

# `list_query`

Returns the main database query for selecting a list of items by given criteria.

TODO: Should be private.

# `name_by_schema`

Gets name by schema. This is the last part of the module name as a lowercase atom.

TODO: Make this private once all fields are using the adapter abstractions.

# `update`

Updates given item.

# `update_all`

Updates given items.

# `validate_config!`

---

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