View Source Bonfire.Common.Repo (Bonfire v0.9.10-classic-beta.169)

Main Ecto Repo.

Note: functions are defined in Bonfire.Common.RepoTemplate

Summary

Functions

Callback implementation for Ecto.Repo.all/2.

Callback implementation for Ecto.Repo.checked_out?/0.

Callback implementation for Ecto.Repo.checkout/2.

Callback implementation for Ecto.Repo.config/0.

Callback implementation for Ecto.Repo.delete/2.

Callback implementation for Ecto.Repo.delete!/2.

Execute a query to delete all matching records.

A convenience function for SQL-based repositories that forces all connections in the pool to disconnect within the given interval.

A convenience function for SQL-based repositories that executes an EXPLAIN statement or similar depending on the adapter to obtain statistics for the given query.

Execute a query for one result where the primary key matches the given id, and return either an {:ok, result} tuple or a {:error, :not_found}.

Execute a query for multiple results given one or multiple IDs.

Execute a query for one result (using a keyword list to specify the key/value to query with), and return either an {:ok, result} tuple or a {:error, :not_found}.

Like single/1, except on failure, adds an error to the changeset.

Callback implementation for Ecto.Repo.get/3.

Callback implementation for Ecto.Repo.get!/3.

Callback implementation for Ecto.Repo.get_dynamic_repo/0.

Callback implementation for Ecto.Repo.in_transaction?/0.

Callback implementation for Ecto.Repo.insert/2.

Callback implementation for Ecto.Repo.insert!/2.

Insert all or ignore a list of maps into a schema.

Insert or ignore a changeset or struct into a schema.

Insert or ignore a map (or iterate over a list of maps) into a schema.

Callback implementation for Ecto.Repo.load/2.

Execute a query for multiple results and return the results.

Execute a query for multiple results and return one page of results. This uses the main implementation for pagination, which is cursor-based and powered by the Paginator library.

Execute a query for one result and return either a result or a fallback value (nil by default).

Add an ilike clause to a query if the user query is safe.

Callback implementation for Ecto.Repo.one/2.

Callback implementation for Ecto.Repo.one!/2.

Different implementation for pagination using Scrivener (used by eg. rauversion).

Select and return only specific fields (specified as an atom or list of atoms)

Like insert/1, but understands remapping changeset errors to attr names from config (and only config, no overrides at present!)

Like put/1 but for multiple changesets

A convenience function for SQL-based repositories that executes the given query.

A convenience function for SQL-based repositories that executes the given query.

A convenience function for SQL-based repositories that executes the given multi-result query.

A convenience function for SQL-based repositories that executes the given multi-result query.

Callback implementation for Ecto.Repo.reload/2.

Callback implementation for Ecto.Repo.rollback/1.

Execute a query for one result and return either an {:ok, result} or {:error, :not_found} tuple.

Executes raw SQL query.

Callback implementation for Ecto.Repo.start_link/1.

Callback implementation for Ecto.Repo.stop/1.

Callback implementation for Ecto.Repo.stream/2.

A convenience function for SQL-based repositories that translates the given query to SQL.

Can be used to log specific queries (by calling function) in production.

Run a transaction, similar to Repo.transaction/1, but it expects an ok or error tuple. If an error tuple is returned, the transaction is aborted.

Callback implementation for Ecto.Repo.update/2.

Callback implementation for Ecto.Repo.update!/2.

Inserts or updates data in the database with upsert semantics.

Insert or update all entries with upsert semantics.

Functions

Link to this function

aggregate(queryable, aggregate, opts \\ [])

View Source

Callback implementation for Ecto.Repo.aggregate/3.

Link to this function

aggregate(queryable, aggregate, field, opts)

View Source

Callback implementation for Ecto.Repo.aggregate/4.

Link to this function

all(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.all/2.

Callback implementation for Ecto.Repo.checked_out?/0.

Link to this function

checkout(fun, opts \\ [])

View Source

Callback implementation for Ecto.Repo.checkout/2.

Callback implementation for Ecto.Repo.config/0.

Link to this function

default_options(operation)

View Source

Callback implementation for Ecto.Repo.default_options/1.

Link to this function

delete(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete/2.

Link to this function

delete!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete!/2.

Link to this function

delete_all(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.delete_all/2.

Execute a query to delete all matching records.

Examples

iex> delete_many(from u in User, where: u.id < 100)
{:ok, _count}
Link to this function

disconnect_all(interval, opts \\ [])

View Source

A convenience function for SQL-based repositories that forces all connections in the pool to disconnect within the given interval.

See Ecto.Adapters.SQL.disconnect_all/3 for more information.

Link to this function

exists?(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.exists?/2.

Link to this function

explain(operation, queryable, opts \\ [])

View Source

A convenience function for SQL-based repositories that executes an EXPLAIN statement or similar depending on the adapter to obtain statistics for the given query.

See Ecto.Adapters.SQL.explain/4 for more information.

@spec fetch(atom(), integer() | binary()) :: {:ok, atom()} | {:error, :not_found}

Execute a query for one result where the primary key matches the given id, and return either an {:ok, result} tuple or a {:error, :not_found}.

Examples

iex> fetch(User, 1)
{:ok, %User{}}

iex> fetch(User, 999)
{:error, :not_found}
Link to this function

fetch_all(queryable, id_or_ids)

View Source

Execute a query for multiple results given one or multiple IDs.

Examples

iex> fetch_all(User, [1, 2, 3])
[%User{}, %User{}, %User{}]

iex> fetch_all(User, 999)
[]
Link to this function

fetch_by(queryable, term)

View Source

Execute a query for one result (using a keyword list to specify the key/value to query with), and return either an {:ok, result} tuple or a {:error, :not_found}.

Examples

iex> fetch_by(User, name: "Alice")
{:ok, %User{}}

iex> fetch_by(User, name: "Nonexistent")
{:error, :not_found}
Link to this function

find(q, changeset, field \\ :form)

View Source

Like single/1, except on failure, adds an error to the changeset.

Examples

iex> changeset = %Ecto.Changeset{}
iex> find(from u in User, where: u.id == 1, changeset)
{:ok, %User{}}

iex> changeset = %Ecto.Changeset{}
iex> find(from u in User, where: u.id == 999, changeset)
{:error, %Ecto.Changeset{}}
Link to this function

get(queryable, id, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get/3.

Link to this function

get!(queryable, id, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get!/3.

Link to this function

get_by(queryable, clauses, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get_by/3.

Link to this function

get_by!(queryable, clauses, opts \\ [])

View Source

Callback implementation for Ecto.Repo.get_by!/3.

Callback implementation for Ecto.Repo.get_dynamic_repo/0.

Callback implementation for Ecto.Repo.in_transaction?/0.

Link to this function

insert(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert/2.

Link to this function

insert!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert!/2.

Link to this function

insert_all(schema_or_source, entries, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_all/3.

Link to this function

insert_all_or_ignore(schema, data)

View Source

Insert all or ignore a list of maps into a schema.

Examples

iex> insert_all_or_ignore(MySchema, [%{field: "value1"}, %{field: "value2"}])
{:ok, _result}
Link to this function

insert_or_ignore(cs_or_struct)

View Source

Insert or ignore a changeset or struct into a schema.

Examples

iex> insert_or_ignore(%Ecto.Changeset{})
{:ok, _result}

iex> insert_or_ignore(%MySchema{field: "value"})
{:ok, _result}
Link to this function

insert_or_ignore(schema, object)

View Source

Insert or ignore a map (or iterate over a list of maps) into a schema.

Examples

iex> insert_or_ignore(MySchema, %{field: "value"})
[{:ok, _result}]

iex> insert_or_ignore(MySchema, [%{field: "value1"}, %{field: "value2"}])
[{:ok, _result}]
Link to this function

insert_or_update(changeset, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_or_update/2.

Link to this function

insert_or_update!(changeset, opts \\ [])

View Source

Callback implementation for Ecto.Repo.insert_or_update!/2.

Link to this function

load(schema_or_types, data)

View Source

Callback implementation for Ecto.Repo.load/2.

Execute a query for multiple results and return the results.

Examples

iex> many(from u in User)
[%User{}, %User{}]

iex> many(from u in User, return: :query)
#Ecto.Query<...>
Link to this function

many_paginated(queryable, opts \\ [], repo_opts \\ default_repo_opts())

View Source

Execute a query for multiple results and return one page of results. This uses the main implementation for pagination, which is cursor-based and powered by the Paginator library.

Examples

iex> many_paginated(User, [limit: 10])
%Paginator.Page{}
Link to this function

maybe_one(q, fallback \\ nil)

View Source

Execute a query for one result and return either a result or a fallback value (nil by default).

Examples

iex> maybe_one(from u in User, where: u.id == 1)
%User{}

iex> maybe_one(from u in User, where: u.id == 999, "fallback")
"fallback"
Link to this function

maybe_preload(obj, preloads, opts \\ [])

View Source

See Bonfire.Common.Repo.Preload.maybe_preload/3.

Link to this function

maybe_where_ilike(query, field, user_query, system_prefix \\ "", system_suffix \\ "")

View Source

Add an ilike clause to a query if the user query is safe.

Examples

iex> maybe_where_ilike(Needle.Pointer, :id, "Alice")
#Ecto.Query<...>

iex> maybe_where_ilike(Needle.Pointer, :id, "Al%ice")
Needle.Pointer 
# ^ unchanged due to unsafe query
Link to this function

one(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.one/2.

Link to this function

one!(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.one!/2.

Link to this function

paginate(pageable, options \\ [])

View Source

Different implementation for pagination using Scrivener (used by eg. rauversion).

Examples

iex> paginate(User, page: 1, page_size: 10)
%Scrivener.Page{}
Link to this function

pluck(query, fields, opts \\ [])

View Source

Select and return only specific fields (specified as an atom or list of atoms)

Examples

> pluck(:id)
[id1, id2]

> pluck([:id, :inserted_at])
[%{id: id1, inserted_at: _}, %{id: id2, inserted_at: _}]
Link to this function

preload(struct_or_structs_or_nil, preloads, opts \\ [])

View Source

Callback implementation for Ecto.Repo.preload/3.

Link to this function

preload_all(obj, opts \\ [])

View Source

See Bonfire.Common.Repo.Preload.preload_all/2.

Link to this function

preload_mixins(obj, opts \\ [])

View Source

See Bonfire.Common.Repo.Preload.preload_mixins/2.

Link to this function

prepare_query(operation, query, opts)

View Source

Callback implementation for Ecto.Repo.prepare_query/3.

Like insert/1, but understands remapping changeset errors to attr names from config (and only config, no overrides at present!)

Examples

iex> changeset = %Ecto.Changeset{valid?: false}
iex> put(changeset)
{:error, %Ecto.Changeset{}}
Link to this function

put_dynamic_repo(dynamic)

View Source

Callback implementation for Ecto.Repo.put_dynamic_repo/1.

Like put/1 but for multiple changesets

Examples

iex> changesets = [%{valid?: true}, %{valid?: false}]
iex> put_many(changesets)
{:error, [%{valid?: false}]}

iex> changesets = [%{valid?: true}, %{valid?: true}]
iex> put_many(changesets)
{:ok, _result}
Link to this function

query(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given query.

See Ecto.Adapters.SQL.query/4 for more information.

Link to this function

query!(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given query.

See Ecto.Adapters.SQL.query!/4 for more information.

Link to this function

query_many(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given multi-result query.

See Ecto.Adapters.SQL.query_many/4 for more information.

Link to this function

query_many!(sql, params \\ [], opts \\ [])

View Source

A convenience function for SQL-based repositories that executes the given multi-result query.

See Ecto.Adapters.SQL.query_many!/4 for more information.

Link to this function

reload(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.reload/2.

Link to this function

reload!(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.reload!/2.

@spec rollback(term()) :: no_return()

Callback implementation for Ecto.Repo.rollback/1.

Execute a query for one result and return either an {:ok, result} or {:error, :not_found} tuple.

Examples

iex> single(from u in User, where: u.id == 1)
{:ok, %User{}}

iex> single(from u in User, where: u.id == 999)
{:error, :not_found}
Link to this function

sql(raw_sql, data \\ [], opts \\ [])

View Source

Executes raw SQL query.

Examples

> YourModule.sql("SELECT * FROM pointers")

Callback implementation for Ecto.Repo.start_link/1.

Callback implementation for Ecto.Repo.stop/1.

Link to this function

stream(queryable, opts \\ [])

View Source

Callback implementation for Ecto.Repo.stream/2.

Link to this function

to_sql(operation, queryable)

View Source

A convenience function for SQL-based repositories that translates the given query to SQL.

See Ecto.Adapters.SQL.to_sql/3 for more information.

Can be used to log specific queries (by calling function) in production.

Examples

iex> trace(fn -> Repo.all(User) end)
[%User{}, %User{}]
Link to this function

transact_with(fun, opts \\ [])

View Source

Run a transaction, similar to Repo.transaction/1, but it expects an ok or error tuple. If an error tuple is returned, the transaction is aborted.

Examples

iex> transact_with(fn -> {:ok, "success"} end)
"success"

iex> transact_with(fn -> {:error, "failure"} end)
** (Ecto.RollbackError) Rolling back the DB transaction, error reason: failure
Link to this function

transaction(fun_or_multi, opts \\ [])

View Source

Callback implementation for Ecto.Repo.transaction/2.

Link to this function

update(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update/2.

Link to this function

update!(struct, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update!/2.

Link to this function

update_all(queryable, updates, opts \\ [])

View Source

Callback implementation for Ecto.Repo.update_all/3.

Link to this function

upsert(cs, keys_or_attrs_to_update \\ nil, conflict_target \\ [:id])

View Source

Inserts or updates data in the database with upsert semantics.

  • cs - The changeset or schema to insert or update.
  • keys_or_attrs_to_update - A list of keys or a map of attributes to update.
  • conflict_target - The column(s) or constraint to check for conflicts, defaults to [:id].

Examples

iex> upsert(%Ecto.Changeset{}, [:field1, :field2])
{:ok, _result}

iex> upsert(%Ecto.Changeset{}, %{field1: "value"})
{:ok, _result}
Link to this function

upsert_all(schema, data, conflict_target \\ [:id])

View Source

Insert or update all entries with upsert semantics.

  • schema - The schema or table name to insert or update.
  • data - A list of maps containing the data to insert or update.
  • conflict_target - The column(s) or constraint to check for conflicts, defaults to [:id].

Examples

iex> upsert_all(User, [%{id: 1, name: "Alice"}, %{id: 2, name: "Bob"}])
{:ok, _result}

iex> upsert_all(User, [%{id: 1, name: "Alice Updated"}], [:id])
{:ok, _result}