View Source Backpex.Resource (Backpex v0.3.1)
Generic context module for Backpex resources.
Summary
Functions
Applies a change to a given item by calling the specified changeset function.
Gets the total count of the current live_resource. Possibly being constrained the item query and the search- and filter options.
Deletes the given record from the database. Additionally broadcasts the corresponding event, when PubSub config is given.
Deletes multiple items from a given repository and schema. Additionally broadcasts the corresponding event, when PubSub config is given.
Gets a database record with the given fields by the given id.
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.
Returns a list of items by given criteria.
Returns the main database query for selecting a list of items by given criteria.
Gets name by schema. This is the last part of the module name as a lowercase atom.
Updates an Ecto changeset with a list of associations. It takes an existing changeset and a list of associations, and it updates the changeset with each association using Ecto.Changeset.put_assoc/3
.
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.
Updates multiple items from a given repository and schema. Additionally broadcasts the corresponding event, when PubSub config is given.
Functions
change(item, attrs, changeset_function, assigns, assocs \\ [], target \\ nil, action \\ :validate)
View SourceApplies a change to a given item by calling the specified changeset function.
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.changeset_function
: A function used to generate the changeset. This function is usually defined elsewhere in your codebase and should follow the changeset Ecto convention.assigns
: The assigns that will be passed to the changeset function.assocs
(optional, default[]
): A list of associations that should be put into the changeset.target
(optional, defaultnil
): 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(assigns, item_query, fields, search_options, filter_options)
View SourceGets the total count of the current live_resource. Possibly being constrained the item query and the search- and filter options.
Deletes the given record from the database. Additionally broadcasts the corresponding event, when PubSub config is given.
Parameters
item
(struct): The item to be deleted.repo
(module): The repository module.pubsub
(map, default:nil
): The PubSub config to use for broadcasting events.
Deletes 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 deleted. The list must contain items that have anid
field.repo
(module): The repository module.schema
(module): The Ecto schema module corresponding to the entities initems
.pubsub
(map, default:nil
): The PubSub config to use for broadcasting events.
Gets a database record with the given fields by the given id.
Parameters
id
: The identifier for the specific item to be fetched.repo
(module): The repository module.schema
: The Ecto schema module corresponding to the itemitem_query
(function): A function that modifies the base query. This function should accept an Ecto.Queryable and return an Ecto.Queryable. It's used to apply additional query logic.fields
(list): A list of atoms representing the fields to be selected and potentially preloaded.
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 thechangeset_function
.repo
(module): The repository module.changeset_function
(function): The function that transforms the item and parameters into a changeset.opts
(keyword list): A list of options for customizing the behavior of the insert function. The available options are::assigns
(map, default:%{}
): The assigns that will be passed to the changeset function.:pubsub
(map, default:nil
): The PubSub config to use for broadcasting events.:assocs
(list, default:[]
): A list of associations.:after_save
(function, default:&{:ok, &1}
): A function to handle operations after the save.
Returns a list of items by given criteria.
Example criteria:
[ order: %{by: :item, direction: :asc}, pagination: %{page: 1, size: 5}, search: {"hello", [:title, :description]} ]
Returns the main database query for selecting a list of items by given criteria.
Gets name by schema. This is the last part of the module name as a lowercase atom.
Examples
iex> Backpex.Resource.name_by_schema(Backpex.Resource)
:resource
Updates an Ecto changeset with a list of associations. It takes an existing changeset and a list of associations, and it updates the changeset with each association using Ecto.Changeset.put_assoc/3
.
Parameters
changeset
: The changeset that you want to update with new associations.assocs
(keyword): A keyword list of associations to be added to the changeset. Each element should be a tuple with the association's key as the first element and the associated value as the second element.
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 thechangeset_function
.repo
(module): The repository module.changeset_function
(function): The function that transforms the item and parameters into a changeset.opts
(keyword list): A list of options for customizing the behavior of the insert function. The available options are::assigns
(map, default:%{}
): The assigns that will be passed to the changeset function.:pubsub
(map, default:nil
): The PubSub config to use for broadcasting events.:assocs
(list, default:[]
): A list of associations.:after_save
(function, default:&{:ok, &1}
): A function to handle operations after the save.
update_all(items, repo, schema, updates, event_name \\ "updated", pubsub \\ nil)
View SourceUpdates 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.repo
(module): The repository module.schema
(module): The Ecto schema module corresponding to the entities initems
.updates
(list): A list of updates passed to Ectoupdate_all
function.event_name
(string, default:updated
): The name to be used when broadcasting the event.pubsub
(map, default:nil
): The PubSub config to use for broadcasting events.