View Source Backpex.Resource (Backpex v0.7.0)
Generic context module for Backpex resources.
Summary
Functions
Builds metadata passed to changeset functions.
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 Backpex.Field.before_changeset/6
callback for each field.
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. Returns nil if no result was found.
Gets a database record with the given fields by the given id. Raises Ecto.NoResultsError
if no record was found.
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.
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
before_changesets(changeset, attrs, metadata, repo, fields, assigns)
View SourceBuilds metadata passed to changeset functions.
Parameters
assigns
: The assigns that will be passed to the changeset function.target
(optional, defaultnil
): The target to be passed to the changeset function.
change(item, attrs, changeset_function, repo, fields, assigns, opts \\ [])
View SourceApplies a change to a given item by calling the specified changeset function.
In addition, puts the given assocs into the function and calls the 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.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.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, 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. Returns nil if no result was found.
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.
Gets a database record with the given fields by the given id. Raises Ecto.NoResultsError
if no record was found.
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.fields
(keyword): The keyword list of fields defined in the live resource.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
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.fields
(keyword): The keyword list of fields defined in the live resource.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.