EctoCrux.Schema.Baguettes (EctoCrux v1.2.16)

Sample module using EctoCrux

Link to this section Summary

Functions

[Repo] Fetches all entries from the data store

call schema_module changeset method

Count number of elements

Count number of entries with opts

Count number of entries from a query with opts

Count number of entries complying with the filter clauses.

[Repo proxy of insert/2] Create (insert) a new baguette from attrs

Create (insert) a baguette from attrs if it doesn't exist

[Repo] Create (insert) a baguette from attrs if it doesn't exist

[Repo] Create (insert) a baguette from attrs if it doesn't exist

[Repo proxy] Deletes a struct using its primary key.

value of except

Test if an entry with <presence_attrs> exists

[Repo] Fetches all results from the filter clauses.

[Repo] Fetches all results from the filter clauses, with opts

[Repo] Fetches a single struct from the data store where the primary key matches the given id.

[Repo] Similar to get/2 but raises Ecto.NoResultsError if no record was found.

[Repo] Fetches a single result from the clauses.

[Repo] Similar to get_by/2 but raises Ecto.NoResultsError if no record was found.

create a new query using schema module

value of default order by

default page size if using pagination

[Repo proxy] Preloads all associations on the given struct or structs.

value of read_only mode

configured repo to use

schema module is it associated to

value of default select

Like find_by/1 by returns a stream to handle large requests

Create an atom-keyed map from the given map. If both a string and an atom keys are provided in the original map, atom key gets priority.

[Repo proxy] Updates a changeset using its primary key.

[Repo proxy] Same as update/2 but return the struct or raises if the changeset is invalid

Link to this section Functions

Link to this function

all(opts \\ [])

Specs

all(opts :: Keyword.t()) :: [EctoCrux.Schema.Baguette.t()]

[Repo] Fetches all entries from the data store

# Fetch all Baguettes
Baguettes.all()
# Fetch all Baguettes within Repo prefix "francaise"
Baguettes.all(prefix: "francaise")

Options

  • order_by - order_by expression, overrides default order_by for the crux usage
  • select - select expression, overrides default select for the crux usage
  • @see Repo.all/2
Link to this function

change(blob, attrs \\ %{})

call schema_module changeset method

Specs

count() :: integer()

Count number of elements

baguettes_count = Baguettes.count()

Specs

count(query :: Ecto.Query.t()) :: integer()
count(opts :: Keyword.t()) :: integer()

Count number of entries with opts

baguettes_count = Baguettes.count(prefix: "francaise")

Options

Link to this function

count(query, opts)

Specs

count(query :: Ecto.Query.t(), opts :: Keyword.t()) :: integer()

Count number of entries from a query with opts

query = from b in Baguette, where :kind in ["tradition"]
baguettes_count = Baguettes.count(query, prefix: "francaise")

Options

Link to this function

count_by(filters, opts \\ [])

Specs

count_by(filters :: Keyword.t() | map(), opts :: Keyword.t()) :: integer()

Count number of entries complying with the filter clauses.

Options

Link to this function

create(attrs \\ %{}, opts \\ [])

Specs

create(attrs :: map(), opts :: Keyword.t()) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

[Repo proxy of insert/2] Create (insert) a new baguette from attrs

# Create a new baguette with `:kind` value set to `:tradition`
{:ok, baguette} = Baguettes.create(%{kind: :tradition})

Options

@see Repo.insert/2

Link to this function

create_if_not_exist(attrs)

Specs

create_if_not_exist(attrs :: map()) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

Create (insert) a baguette from attrs if it doesn't exist

# Create a new baguette with :kind value set to :tradition baguette = Baguettes.create(%{kind: :tradition}) # Create another one with the same kind {:ok, another_ baguette} = Baguettes.create_if_not_exist(%{kind: :tradition}) # baguette and another_baguette are the same Baguette

Options

@see Repo.insert/2

Link to this function

create_if_not_exist(attrs, opts)

Specs

create_if_not_exist(attrs :: map(), opts :: Keyword.t()) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}
create_if_not_exist(presence_attrs :: map(), creation_attrs :: map()) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

[Repo] Create (insert) a baguette from attrs if it doesn't exist

Behave like create_if_not_exist/1 but you can specify attrs for the presence test, and creation attrs.

Options

@see Repo.insert/2

Link to this function

create_if_not_exist(presence_attrs, creation_attrs, opts)

Specs

create_if_not_exist(
  presence_attrs :: map(),
  creation_attrs :: map(),
  opts :: Keyword.t()
) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

[Repo] Create (insert) a baguette from attrs if it doesn't exist

Behave like create_if_not_exist/1 but you can specify attrs for the presence test, and creation attrs.

Options

@see Repo.insert/2

Link to this function

delete(blob, opts \\ [])

Specs

delete(blob :: EctoCrux.Schema.Baguette.t(), opts :: Keyword.t()) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

[Repo proxy] Deletes a struct using its primary key.

= Baguettes.delete(baguette)

Options

value of except

Link to this function

exist?(presence_attrs, opts \\ [])

Specs

exist?(presence_attrs :: map(), opts :: Keyword.t()) :: boolean()

Test if an entry with <presence_attrs> exists

Specs

find_by(Ecto.Query.t()) :: [EctoCrux.Schema.Baguette.t()]
find_by(filters :: Keyword.t() | map()) :: [EctoCrux.Schema.Baguette.t()]

[Repo] Fetches all results from the filter clauses.

best_baguettes = Baguettes.find_by(kind: "best")
Link to this function

find_by(query, opts)

Specs

find_by(Ecto.Query.t(), map()) :: [EctoCrux.Schema.Baguette.t()]
find_by(Ecto.Query.t(), Keyword.t()) :: [EctoCrux.Schema.Baguette.t()]
find_by(Keyword.t(), Keyword.t() | map()) :: [EctoCrux.Schema.Baguette.t()]
find_by(filters :: Keyword.t() | map(), opts :: map()) :: [
  EctoCrux.Schema.Baguette.t()
]

[Repo] Fetches all results from the filter clauses, with opts

best_baguettes = Baguettes.find_by(kind: "best", prefix: "francaise")

Options

  • order_by - order_by expression, overrides default order_by for the crux usage
  • select - select expression, overrides default select for the crux usage
  • @see Repo.all/2
Link to this function

get(id, opts \\ [])

Specs

get(id :: term(), opts :: Keyword.t()) :: EctoCrux.Schema.Baguette.t() | nil

[Repo] Fetches a single struct from the data store where the primary key matches the given id.

# Get the baguette with id primary key `01DACBCR6REMDH6446VCQEZ5EC`
Baguettes.get("01DACBCR6REMDH6446VCQEZ5EC")
# Get the baguette with id primary key `01DACBCR6REMDH6446VCQEZ5EC` and preload it's bakery and flavor
Baguettes.get("01DACBCR6REMDH6446VCQEZ5EC", preloads: [:bakery, :flavor])

Options

  • preloads - list of atom to preload
  • select - select expression, overrides default select for the crux usage
  • @see Repo.get/3
Link to this function

get!(id, opts \\ [])

Specs

get!(id :: term(), opts :: Keyword.t()) :: EctoCrux.Schema.Baguette.t()

[Repo] Similar to get/2 but raises Ecto.NoResultsError if no record was found.

Options

  • preloads - list of atom to preload
  • select - select expression, overrides default select for the crux usage
  • @see Repo.get!/3
Link to this function

get_by(clauses, opts \\ [])

Specs

get_by(clauses :: Keyword.t() | map(), opts :: Keyword.t()) ::
  EctoCrux.Schema.Baguette.t() | nil

[Repo] Fetches a single result from the clauses.

best_baguette = Baguettes.get_by(kind: "best")

Options

  • preloads - list of atom to preload
  • select - select expression, overrides default select for the crux usage
  • @see Repo.get_by/3
Link to this function

get_by!(clauses, opts \\ [])

Specs

get_by!(clauses :: Keyword.t() | map(), opts :: Keyword.t()) ::
  EctoCrux.Schema.Baguette.t()

[Repo] Similar to get_by/2 but raises Ecto.NoResultsError if no record was found.

Options

  • preloads - list of atom to preload
  • select - select expression, overrides default select for the crux usage
  • @see Repo.get_by!/2

create a new query using schema module

Link to this function

offset_to_page(offset, page_size)

value of default order by

default page size if using pagination

Link to this function

page_to_offset(page, page_size)

Link to this function

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

Specs

preload(structs_or_struct_or_nil, preloads :: term(), opts :: Keyword.t()) ::
  structs_or_struct_or_nil
when structs_or_struct_or_nil:
       [EctoCrux.Schema.Baguette.t()] | EctoCrux.Schema.Baguette.t() | nil

[Repo proxy] Preloads all associations on the given struct or structs.

my_baguette = Baguettes.preload(baguette, [:floor, :boulanger])

Options

value of read_only mode

configured repo to use

Link to this function

schema_module()

schema module is it associated to

value of default select

Link to this function

stream(filters, opts \\ [])

Specs

stream(filters :: Keyword.t(), opts :: Keyword.t()) :: Enum.t()

Like find_by/1 by returns a stream to handle large requests

Repo.transaction(fn ->
  Baguettes.stream(kind: "best")
  |> Stream.chunk_every(@chunk_size)
  |> Stream.each(fn baguettes_chunk ->
    # eat them
  end)
  |> Stream.run()
end)

Options

Link to this function

to_schema_atom_params(mixed_keyed_map, opts \\ [with_assoc: true])

Create an atom-keyed map from the given map. If both a string and an atom keys are provided in the original map, atom key gets priority.

Baguettes.to_schema_atom_params(%{"kind" => "baguepi", :kind => "tradition", "half?" => true})
%{kind: "tradition"}

Options

  • with_assoc [optional] - add associations fields to the list of allowed fields, defaults to true.
Link to this function

update(blob, attrs, opts \\ [])

Specs

update(
  blob :: EctoCrux.Schema.Baguette.t(),
  attrs :: map(),
  opts :: Keyword.t()
) ::
  {:ok, EctoCrux.Schema.Baguette.t()} | {:error, Ecto.Changeset.t()}

[Repo proxy] Updates a changeset using its primary key.

{:ok, updated_baguette} = Baguettes.update(baguette, %{kind: "best"})

Options

Link to this function

update!(blob, attrs, opts \\ [])

Specs

update!(
  blob :: EctoCrux.Schema.Baguette.t(),
  attrs :: map(),
  opts :: Keyword.t()
) ::
  EctoCrux.Schema.Baguette.t()

[Repo proxy] Same as update/2 but return the struct or raises if the changeset is invalid

updated_baguette = Baguettes.update!(baguette, %{kind: "best"})

Options