Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaAK committed Jul 26, 2024
1 parent 7b0f842 commit fdf2ed1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
45 changes: 45 additions & 0 deletions lib/actions.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
defmodule EctoShorts.Actions do
@moduledoc """
Actions for CRUD in ecto, these can be used by all schemas/queries
Generally we can define our contexts to be very reusable by creating
them to look something like this:
defmodule MyApp.Accounts do
alias EctoShorts.Actions
alias MyApp.Accounts.User
def all_users(params), do: Actions.all(User, params)
def find_user(params), do: Actions.find(User, params)
end
We're then able to use this context with all filters that are
supported by `EctoShorts.CommonFilters` without having to create new queries
def do_something do
MyApp.Accounts.all_user(%{
first_name: %{ilike: "john"},
age: %{gte: 18},
priority_level: 5,
address: %{country: "Canada"}
})
end
You can read more on reusable ecto code [here](https://learn-elixir.dev/blogs/creating-reusable-ecto-code)
### Supporting multiple Repos
To support multiple repos, what we can do is pass arguments to the last parameter
of most `EctoShorts.Actions` calls
#### Example
defmodule MyApp.Accounts do
alias EctoShorts.Actions
alias MyApp.Accounts.User
@repo [repo: MyApp.Repo.Replica1]
def all_users(params), do: Actions.all(User, params, @repo)
def find_user(params), do: Actions.find(User, params, @repo)
end
"""

@type query :: Ecto.Query.t() | Ecto.Schema.t()| module()
Expand Down
53 changes: 52 additions & 1 deletion lib/common_changes.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
defmodule EctoShorts.CommonChanges do
@moduledoc """
This module is responsible for determining put/cast assoc as well as creating and updating model relations
`CommonChanges` is a collection of functions to help with managing
and creating our `&changeset/2` function in our schemas.
### Preloading associations on change
Often times we want to be able to change an association with
`(put/cast)_assoc`, but we have an awkwardness of having to use
a preload in a spot to do this. We can aleviate that by doing the following:
defmodule MyApp.Accounts.User do
def changeset(changeset, params) do
changeset
|> cast([:name, :email])
|> validate_required([:name, :email])
|> EctoShorts.CommonChanges.preload_change_assoc(:address)
end
end
Doing this allows us to then pass address in via a map, or even using
the struct from the database directly to add as a relation
### Validating relation is passed in somehow
We can validate for a relation being passed in via id or by using our
preload_change_assoc by doing the following:
defmodule MyApp.Accounts.User do
def changeset(changeset, params) do
changeset
|> cast([:name, :email, :address_id])
|> validate_required([:name, :email])
|> EctoShorts.CommonChanges.preload_change_assoc(:address,
required_when_missing: :address_id
)
end
end
### Conditional functions
We can also run functions when something happens by defining conditional functions like so:
defmodule MyApp.Accounts.User do
alias EctoShorts.CommonChanges
def changeset(changeset, params) do
changeset
|> cast([:name, :email, :address_id])
|> validate_required([:name, :email])
|> CommonChanges.put_when(
&CommonChanges.changeset_field_nil?(&1, :email),
&put_change(&1, :email, "some_default@gmail.com")
)
end
end
"""
require Logger

Expand Down
2 changes: 1 addition & 1 deletion lib/ecto_shorts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule EctoShorts do
There are 2 main modules Actions and CommonChanges
There are 2 main modules `EctoShorts.Actions` and `EctoShorts.CommonChanges`
### Actions
`EctoShorts.Actions` allows for filters to be constructed from data such as
Expand Down

0 comments on commit fdf2ed1

Please sign in to comment.