# `Backpex.Resource`
[🔗](https://github.com/naymspace/backpex/blob/0.18.3/lib/backpex/resource.ex#L1)

Generic context module for Backpex resources.

> ### Work in progress {: .warning}
>
> This module is still under heavy development and will change as we progress with the `Backpex.Adapter`
> implementation in the coming releases. Keep this in mind when using this module directly.

# `broadcast`

Broadcasts `event` on the `live_resource` topic in case `result` contains `{:ok, item}`.

# `build_changeset_metadata`

Builds metadata passed to changeset functions.

TODO: move?

## Parameters

* `assigns`: The assigns that will be passed to the changeset function.
* `target` (optional, default `nil`): The target to be passed to the changeset function.

# `change`

Applies a change to a given item by calling the specified changeset function.
In addition, puts the given assocs into the function and calls the `c:Backpex.Field.before_changeset/6` callback for each field.

## Parameters

* `item`: The initial data structure to be changed.
* `attrs`: A map of attributes that will be used to modify the item. These attributes are passed to the changeset function.
* `fields`: The fields for this change.
* `assigns`: The assigns that will be passed to the changeset function.
* `live_resource`: The `Backpex.LiveResource` to be used.
* `opts` (keyword list): A list of options for customizing the behavior of the change function. The available options are:
  * `assocs` (optional, default `[]`): A list of associations that should be put into the changeset.
  * `target` (optional, default `nil`): The target to be passed to the changeset function.
  * `action` (optional, default `:validate`): An atom indicating the action to be performed on the changeset.

# `count`

Gets the total count of the current live_resource.
Possibly being constrained the item query and the search- and filter options.

# `delete_all`

Deletes multiple items.
Additionally broadcasts the corresponding event for each deleted item.

## Parameters

* `items` (list): A list of structs, each representing an entity to be deleted. The list must contain items that have an `id` field.
* `live_resource` (module): The `Backpex.LiveResource` module.

# `get`

Gets a database record with the given `fields` by the given  `primary_value`.

Returns `{:ok, nil}` if no result was found.

## Parameters

* `primary_value`: The identifier for the specific item to be fetched.
* `assigns` (map): The current assigns of the socket.
* `live_resource` (module): The `Backpex.LiveResource` module.

# `get!`

Same as `get/4` but returns the result or raises an error.

# `insert`

Inserts a new item into a repository with specific parameters and options. It takes a repo module, a changeset function, an item, parameters for the changeset function, and additional options.

## Parameters

* `item` (struct): The Ecto schema struct.
* `attrs` (map): A map of parameters that will be passed to the `changeset_function`.
* TODO: docs

# `list`

Returns a list of items by given criteria.

Example criteria:

[
  order: %{by: :item, direction: :asc},
  pagination: %{page: 1, size: 5},
  search: {"hello", [:title, :description]}
]

# `update`

Handles the update of an existing item with specific parameters and options. It takes a repo module, a changeset function, an item, parameters for the changeset function, and additional options.

## Parameters

* `item` (struct): The Ecto schema struct.
* `attrs` (map): A map of parameters that will be passed to the `changeset_function`.
* TODO: docs

# `update_all`

Updates multiple items from a given repository and schema.
Additionally broadcasts the corresponding event, when PubSub config is given.

## Parameters

* `items` (list): A list of structs, each representing an entity to be updated.
* `updates` (list): A list of updates passed to Ecto `update_all` function.
* `event_name` (string, default: `updated`): The name to be used when broadcasting the event.
* `live_resource` (module): The `Backpex.LiveResource` module.

---

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