View Source Backpex.Resource (Backpex v0.8.2)

Generic context module for Backpex resources.

Work in progress

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.

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 multiple items. Additionally broadcasts the corresponding event for each deleted item.

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

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

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.

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

build_changeset_metadata(assigns, target \\ nil)

View Source

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

change(item, attrs, fields, assigns, live_resource, 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.
  • 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(fields, assigns, live_resource, criteria \\ [])

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_all(items, live_resource)

View Source

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

get(primary_value, assigns, live_resource)

View Source

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

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

get!(primary_value, assigns, live_resource)

View Source

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

Raises Ecto.NoResultsError if no record 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.
Link to this function

insert(item, attrs, fields, assigns, live_resource, 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.
  • TODO: docs
Link to this function

list(fields, assigns, live_resource, 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

update(item, attrs, fields, assigns, live_resource, 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.
  • TODO: docs
Link to this function

update_all(items, updates, event_name \\ "updated", live_resource)

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.
  • 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.