ecto_conditionals v0.1.0 EctoConditionals

Provides Ecto conditional helper functions for find_or_create-ing or upsert-ing records

Basic Usage

First specify the repo you’re going to use:

use EctoConditionals, repo: MyApp.Repo

Then, pipe or pass the record struct to find_or_create/1 or upsert/1. These functions assume finding by id to determine whether to create or upsert. If an id field is present in the record struct, the id field will be used as the unique selector to determine whether to find, insert, or update. If not present, these functions will insert a new record.

%User{id: 1, name: "Flamel"} |> find_or_create
#=> {:ok, %User{id: 1, name: "Flamel"}}

%User{name: "Dumbledore"} |> upsert
#=> {:ok, %User{id: 2, name: "Dumbledore"}}

You can also specify selectors by instead using find_or_create_by/2 or upsert_by/2 and passing a selector or list of selectors as the second argument.

%User{name: "Slughorn"} |> find_or_create_by(:name)
#=> {:ok, %User{id: 3, name: "Slughorn"}}

%User{first_name: "Harry", last_name: "Potter"} |> upsert_by(:last_name)
#=> {:ok, %User{id: 4, first_name: "Harry", last_name: "Potter"}}

Implementation Note

find_or_create_by/2 is a thin wrapper piping through find_by/2 and then or_create/1 upsert_by/2 is a thin wrapper piping through find_by/2 and then update_or_insert/1. find_or_create/1 is a thin wrapper piping through find/1 and then or_create/1 and upsert/1 is a thin wrapper piping through find/1 and Ecto.Repo’s insert_or_update/1. find/1 is also just a thin wrapper around find_by/1 that assumes :id is the selector. It’s functions all the way down!

%User{first_name: "Harry", last_name: "Potter"} |> find_by([:first_name, :last_name])
#=> {:found, %User{id: 4, first_name: "Harry", last_name: "Potter"}}

%User{name: "Buckbeak"} |> find_by(:name)
#=> {:not_found, %User{name: "Buckbeak"}}

# the following is equivalent
%User{id: 1} |> find_by(:id)
%User{id: 1} |> find

Link to this section Summary

Link to this section Functions

Link to this macro find(record_struct) (macro)
Link to this function find(record_struct, repo)
Link to this macro find_by(record_struct, selectors) (macro)
Link to this function find_by(record_struct, selectors, repo)
Link to this macro find_or_create(record_struct) (macro)
Link to this function find_or_create(record_struct, repo)
Link to this macro find_or_create_by(record_struct, selectors) (macro)
Link to this function find_or_create_by(record_struct, selectors, repo)
Link to this macro or_create(arg) (macro)
Link to this function or_create(error, repo)
Link to this macro update_or_insert(arg) (macro)
Link to this macro upsert(record_struct) (macro)
Link to this function upsert(record_struct, repo)
Link to this macro upsert_by(record_struct, selectors) (macro)
Link to this function upsert_by(record_struct, selectors, repo)