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

Link to this function

apply_criteria(query, criteria, fields)

View Source
Link to this function

apply_filters(query, filters, empty_filter_key)

View Source
Link to this function

apply_search(query, schema, full_text_search, arg)

View Source
Link to this function

before_changesets(changeset, attrs, metadata, repo, fields, assigns)

View Source
Link to this function

build_changeset_metadata(assigns, target \\ nil)

View Source

Builds metadata passed to changeset functions.

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.
Link to this function

change(item, attrs, changeset_function, repo, fields, assigns, opts \\ [])

View Source

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.

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, 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.
Link to this function

count(assigns, item_query, fields, search_options, filter_options)

View Source

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

Link to this function

delete(item, repo, pubsub \\ nil)

View Source

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.
Link to this function

delete_all(items, repo, schema, pubsub \\ nil)

View Source

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 an id field.
  • repo (module): The repository module.
  • schema (module): The Ecto schema module corresponding to the entities in items.
  • pubsub (map, default: nil): The PubSub config to use for broadcasting events.
Link to this function

get(id, repo, schema, item_query, fields)

View Source

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 item
  • item_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.
Link to this function

get!(id, repo, schema, item_query, fields)

View Source

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 item
  • item_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.
Link to this function

insert(item, attrs, repo, fields, changeset_function, opts)

View Source

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.
  • 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.
Link to this function

list(assigns, item_query, fields, criteria \\ [])

View Source

Returns a list of items by given criteria.

Example criteria:

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

Link to this function

list_query(assigns, item_query, fields, criteria \\ [])

View Source

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

Link to this function

metric_data(assigns, select, item_query, fields, criteria \\ [])

View Source

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
Link to this function

update(item, attrs, repo, fields, changeset_function, opts)

View Source

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.
  • 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.
Link to this function

update_all(items, repo, schema, updates, event_name \\ "updated", pubsub \\ nil)

View Source

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.
  • repo (module): The repository module.
  • schema (module): The Ecto schema module corresponding to the entities in items.
  • 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.
  • pubsub (map, default: nil): The PubSub config to use for broadcasting events.