View Source Backpex.Resource (Backpex v0.9.1)

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

build_changeset_metadata(assigns, target \\ nil)

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(item, attrs, fields, assigns, live_resource, opts \\ [])

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.

count(fields, assigns, live_resource, criteria \\ [])

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

delete_all(items, live_resource)

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(primary_value, assigns, live_resource)

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.

get!(primary_value, assigns, live_resource)

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.

insert(item, attrs, fields, assigns, live_resource, opts)

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(fields, assigns, live_resource, criteria \\ [])

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(item, attrs, fields, assigns, live_resource, opts \\ [])

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(items, updates, event_name \\ "updated", live_resource)

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.