Trash.Repo (trash v0.1.0)

Provides functions for discarding and keeping records and querying for them via Ecto.Repo functions.

Link to this section Summary

Functions

Imports functions from Trash.Repo.

Fetches all entries matching the given query that have been discarded.

Fetches all entries matching the given query that have been kept.

Updates a record as discarded.

Updates a record as discarded.

Checks if there exists an entry that matches the given query that has been discarded.

Fetches a single discarded result where the primary key matches the given id.

Fetches a single discarded result where the primary key matches the given id.

Fetches a single discarded result from the query.

Fetches a single discarded result from the query.

Fetches a single kept result where the primary key matches the given id.

Fetches a single kept result where the primary key matches the given id.

Fetches a single kept result from the query.

Fetches a single kept result from the query.

Checks if there exists an entry that matches the given query that has been kept.

Fetches a single discarded result from the query.

Fetches a single discarded result from the query.

Fetches a single kept result from the query.

Fetches a single kept result from the query.

Updates a record as kept.

Updates a record as kept.

Link to this section Functions

Link to this macro

__using__(opts)

(macro)

Specs

__using__(opts :: list()) :: Macro.t()

Imports functions from Trash.Repo.

It's not required to use this module in order to use Trash. Doing so will import shorthand functions into your app's Repo module with the repo implicitly passed. It's a bit more convenient, but the functions are public on Trash.Repo, so if preferred they can be called directly.

# Shorthand with `use`
MyRepo.all_discarded(Post)

# Long form without
Trash.Repo.all_discarded(Post, [], MyRepo)

Options

Examples

defmodule MyApp.Repo
  use Ecto.Schema
  use Trash.Schema, repo: __MODULE__
end
Link to this function

all_discarded(queryable, opts \\ [], repo)

Specs

all_discarded(
  queryable :: Ecto.Queryable.t(),
  opts :: Keyword.t(),
  repo :: atom()
) :: [Ecto.Schema.t()]

Fetches all entries matching the given query that have been discarded.

Examples

iex> Trash.Repo.all_discarded(Post, [], MyApp.Repo)
[%Post{title: "Hello World", discarded_at: %DateTime{}, discarded?: nil}]
Link to this function

all_kept(queryable, opts \\ [], repo)

Specs

all_kept(queryable :: Ecto.Queryable.t(), opts :: Keyword.t(), repo :: atom()) ::
  [Ecto.Schema.t()]

Fetches all entries matching the given query that have been kept.

Examples

iex> Trash.Repo.all_kept(Post, [], MyApp.Repo)
[%Post{title: "Hello World", discarded_at: nil, discarded?: nil}]
Link to this function

discard(changeset, repo)

Specs

discard(
  changeset_or_schema :: Ecto.Changeset.t() | Ecto.Schema.t(),
  repo :: atom()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Updates a record as discarded.

This takes either an Ecto.Changeset or an Ecto.Schema struct. If a struct is given a bare changeset is generated first.

A change is added to the changeset to set discarded_at to DateTime.utc_now/1. It calls repo.update/2 to finalize the changes.

It returns {:ok, struct} if the struct has been successfully updated or {:error, changeset} if there was an error.

Examples

iex> Post.changeset(post, %{title: "[Archived] Hello, world"})
     |> Trash.Repo.discard(MyApp.Repo)
{:ok, %Post{title: "[Archived] Hello, world", discarded_at: %DateTime{}}}
Link to this function

discard!(changeset, repo)

Specs

discard!(
  changeset_or_schema :: Ecto.Changeset.t() | Ecto.Schema.t(),
  repo :: atom()
) :: Ecto.Schema.t()

Updates a record as discarded.

This takes either an Ecto.Changeset or an Ecto.Schema struct. If a struct is given a bare changeset is generated first.

A change is added to the changeset to set discarded_at to DateTime.utc_now/1. It calls repo.update/2 to finalize the changes.

Raises Ecto.InvalidChangesetError if the changeset is invalid.

Note: since an Ecto.Schema struct can be passed which generates a bare changeset, this will never raise when given a struct.

Examples

iex> Post.changeset(post, %{title: "[Archived] Hello, world"})
     |> Trash.Repo.discard!(MyApp.Repo)
%Post{title: "[Archived] Hello, world", discarded_at: %DateTime{}}

iex> Post.changeset(post, %{}) |> Trash.Repo.discard!(MyApp.Repo)
** (Ecto.InvalidChangesetError)
Link to this function

discarded?(queryable, opts \\ [], repo)

Specs

discarded?(queryable :: Ecto.Queryable.t(), opts :: Keyword.t(), repo :: atom()) ::
  boolean()

Checks if there exists an entry that matches the given query that has been discarded.

Returns a boolean.

Examples

iex> Trash.Repo.discarded?(post, [], MyApp.Repo)
true
Link to this function

get_discarded(queryable, id, opts \\ [], repo)

Specs

get_discarded(
  queryable :: Ecto.Queryable.t(),
  id :: term(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single discarded result where the primary key matches the given id.

Returns nil if no result was found.

Examples

iex> Trash.Repo.get_discarded(Post, 1, [], MyApp.Repo)
%Post{}
Link to this function

get_discarded!(queryable, id, opts \\ [], repo)

Specs

get_discarded!(
  queryable :: Ecto.Queryable.t(),
  id :: term(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single discarded result where the primary key matches the given id.

Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.get_discarded!(Post, 1, [], MyApp.Repo)
%Post{}

iex> Trash.Repo.get_discarded!(Post, 2, [], MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

get_discarded_by(queryable, clauses, opts \\ [], repo)

Specs

get_discarded_by(
  queryable :: Ecto.Queryable.t(),
  clauses :: Keyword.t() | map(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single discarded result from the query.

Returns nil if no result was found or raises Ecto.MultipleResultsError if more than one entry.

Examples

iex> Trash.Repo.get_discarded_by(Post, [title: "Hello World"], [],
MyApp.Repo)
%Post{title: "Hello World", discarded_at: %DateTime{}}
Link to this function

get_discarded_by!(queryable, clauses, opts \\ [], repo)

Specs

get_discarded_by!(
  queryable :: Ecto.Queryable.t(),
  clauses :: Keyword.t() | map(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t()

Fetches a single discarded result from the query.

Raises Ecto.MultipleResultsError if more than one result. Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.get_discarded_by!(Post, [title: "Hello World"], [],
MyApp.Repo)
%Post{title: "Hello World", discarded_at: %DateTime{}}

iex> Trash.Repo.get_discarded_by!(Post, [title: "Hello World"], [],
MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

get_kept(queryable, id, opts \\ [], repo)

Specs

get_kept(
  queryable :: Ecto.Queryable.t(),
  id :: term(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single kept result where the primary key matches the given id.

Returns nil if no result was found.

Examples

iex> Trash.Repo.get_kept(Post, 1, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: nil}
Link to this function

get_kept!(queryable, id, opts \\ [], repo)

Specs

get_kept!(
  queryable :: Ecto.Queryable.t(),
  id :: term(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single kept result where the primary key matches the given id.

Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.get_kept!(Post, 1, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: nil}

iex> Trash.Repo.get_kept!(Post, 2, [], MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

get_kept_by(queryable, clauses, opts \\ [], repo)

Specs

get_kept_by(
  queryable :: Ecto.Queryable.t(),
  clauses :: Keyword.t() | map(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single kept result from the query.

Returns nil if no result was found or raises Ecto.MultipleResultsError if more than one entry.

Examples

iex> Trash.Repo.get_kept_by(Post, title: "Hello World", [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: nil}
Link to this function

get_kept_by!(queryable, clauses, opts \\ [], repo)

Specs

get_kept_by!(
  queryable :: Ecto.Queryable.t(),
  clauses :: Keyword.t() | map(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t()

Fetches a single kept result from the query.

Raises Ecto.MultipleResultsError if more than one result. Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.get_kept_by!(Post, title: "Hello World", [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: %DateTime{}}

iex> Trash.Repo.get_kept_by!(Post, title: "Not Written", [], MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

kept?(queryable, opts \\ [], repo)

Specs

kept?(queryable :: Ecto.Queryable.t(), opts :: Keyword.t(), repo :: atom()) ::
  boolean()

Checks if there exists an entry that matches the given query that has been kept.

Returns a boolean.

Examples

iex> Trash.Repo.kept?(post, [], MyApp.Repo)
true
Link to this function

one_discarded(queryable, opts \\ [], repo)

Specs

one_discarded(
  queryable :: Ecto.Queryable.t(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t() | nil

Fetches a single discarded result from the query.

Returns nil if no result was found or raises Ecto.MultipleResultsError if more than one entry.

Examples

iex> Trash.Repo.one_discarded(Post, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: %DateTime{}}
Link to this function

one_discarded!(queryable, opts \\ [], repo)

Specs

one_discarded!(
  queryable :: Ecto.Queryable.t(),
  opts :: Keyword.t(),
  repo :: atom()
) :: Ecto.Schema.t()

Fetches a single discarded result from the query.

Raises Ecto.MultipleResultsError if more than one entry. Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.one_discarded!(Post, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: %DateTime{}}

iex> Trash.Repo.one_discarded!(Post, [], MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

one_kept(queryable, opts \\ [], repo)

Specs

one_kept(queryable :: Ecto.Queryable.t(), opts :: Keyword.t(), repo :: atom()) ::
  Ecto.Schema.t() | nil

Fetches a single kept result from the query.

Returns nil if no result was found or raises Ecto.MultipleResultsError if more than one entry.

Examples

iex> Trash.Repo.one_kept(Post, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: nil}
Link to this function

one_kept!(queryable, opts \\ [], repo)

Specs

one_kept!(queryable :: Ecto.Queryable.t(), opts :: Keyword.t(), repo :: atom()) ::
  Ecto.Schema.t()

Fetches a single kept result from the query.

Raises Ecto.MultipleResultsError if more than one entry. Raises Ecto.NoResultsError if no result was found.

Examples

iex> Trash.Repo.one_kept!(Post, [], MyApp.Repo)
%Post{title: "Hello World", discarded_at: nil}

iex> Trash.Repo.one_kept!(Post, [], MyApp.Repo)
** (Ecto.NoResultsError)
Link to this function

restore(changeset, repo)

Specs

restore(
  changeset_or_schema :: Ecto.Changeset.t() | Ecto.Schema.t(),
  repo :: atom()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Updates a record as kept.

This takes either an Ecto.Changeset or an Ecto.Schema struct. If a struct is given a bare changeset is generated first.

A change is added to the changeset to set discarded_at to nil. It calls repo.update/2 to finalize the changes.

It returns {:ok, struct} if the struct has been successfully updated or {:error, changeset} if there was an error.

Examples

iex> Post.changeset(post, %{title: "Hello, world"})
     |> Trash.Repo.restore(MyApp.Repo)
{:ok, %Post{title: "Hello, world", discarded_at: nil}}
Link to this function

restore!(changeset, repo)

Specs

restore!(
  changeset_or_schema :: Ecto.Changeset.t() | Ecto.Schema.t(),
  repo :: atom()
) :: Ecto.Schema.t()

Updates a record as kept.

This takes either an Ecto.Changeset or an Ecto.Schema struct. If a struct is given a bare changeset is generated first.

A change is added to the changeset to set discarded_at to nil. It calls repo.update/2 to finalize the changes.

Raises Ecto.InvalidChangesetError if the changeset is invalid.

Note: since an Ecto.Schema struct can be passed which generates a bare changeset, this will never raise when given a struct.

Examples

iex> Post.changeset(post, %{title: "[Archived] Hello, world"})
     |> Trash.Repo.restore!(MyApp.Repo)
%Post{title: "[Archived] Hello, world", discarded_at: nil}

iex> Post.changeset(post, %{}) |> Trash.Repo.restore!(MyApp.Repo)
** (Ecto.InvalidChangesetError)