ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - MishaConway/ecto_shortcuts: Shortcuts for common operations in ecto
Shortcuts for common operations in ecto. Contribute to MishaConway/ecto_shortcuts development by creating an account on GitHub.
Visit Site

GitHub - MishaConway/ecto_shortcuts: Shortcuts for common operations in ecto

GitHub - MishaConway/ecto_shortcuts: Shortcuts for common operations in ecto

EctoShortcuts 🚅

Lightweight Elixir extension to simplify common use cases in Ecto.

Installation

  1. Get Hex

  2. Add ecto_shortcuts to your list of dependencies in mix.exs:

  def deps do
    [{:ecto_shortcuts, "~> 0.1.6"}]
  end

Configuration

Add it to your Ecto Models like:

defmodule MyApp.User do
  ...
  use EctoShortcuts, repo: MyApp.Repo
  ...
end  

or (to closely mirror Phoenix model standards):

defmodule MyApp.RepoAUsers do
  ...
  use EctoShortcuts, repo: MyApp.RepoA, model: MyApp.User
  ...
end

defmodule MyApp.RepoBUsers do
  ...
  use EctoShortcuts, repo: MyApp.RepoB, model: MyApp.User
  ...
end  

Usage

insert

  # create a new user named Bob
  MyApp.User.insert name: "Bob"
  MyApp.User.insert %{name: "Bob"}

  # create a new user named Alice without validation
  MyApp.User.insert name: "Alice", validate: false
  MyApp.User.insert %{name: "Alice"}, %{validate: false}

insert!

  MyApp.User.insert! name: "Bob"
  MyApp.User.insert! %{name: "Bob"}

  MyApp.User.insert! name: "Alice", validate: false
  MyApp.User.insert! %{name: "Alice"}, %{validate: false}

-- note: If your model defines a changeset, insert, insert! & any validations will by default use that changeset. To disable validation, use the validate: false option shown below. If your model lacks a changeset, then your model will be inserted without any validation.

--

update_all

  # set status_id to 3 on all users
  MyApp.User.update_all set: [status_id: 3]
  MyApp.User.update_all %{set: [status_id: 3]}

update_by

  # set status_id to 4 where mode is 3
  MyApp.User.update_by [mode: 3], set: [status_id: 4]
  MyApp.User.update_by %{mode: 3}, %{set: [status_id: 4]}

update_by_returning

  # set status_id to 4 where mode is 3
  updated_users = MyApp.User.update_by_returning [mode: 3], set: [status_id: 4]

  # set status_id to 3 for user 1 and return updated user with posts association preloaded
  [updated_user] = MyApp.User.update_by_returning [id: 1], [set: [status_id: 3]], preload: [:posts]

  # same as above but using maps
  [updated_user] = MyApp.User.update_by_returning %{id: 1}, %{set: [status_id: 3]}, preload: [:posts]

delete_all

  # delete all users
  MyApp.User.delete_all

delete_by

  # delete all users where mode is 3
  MyApp.User.delete_by mode: 3
  MyApp.User.delete_by %{mode: 3}

get

  # get user with id 3
  MyApp.User.get 3

  # get user with id 3 and preload posts association
  MyApp.User.get 3, preload: [:posts]

  # get user with id 3 and preload posts associations in addition to posts.post_type association
  MyApp.User.get 3, preload: [{:posts, :post_type}]

get!

  MyApp.User.get! 3
  MyApp.User.get! 3, preload: [:posts]
  MyApp.User.get! 3, preload: [{:posts, :post_type}]

get_by

  # fetch a single user where name is Sally and age is 30
  MyApp.User.get_by name: "Sally", age: 30

  # fetch a single user where name is Sally and preload the posts association
  MyApp.User.get_by [name: "Sally"],  preload: [:posts]

  # same as above but using maps
  MyApp.User.get_by %{name: "Sally"},  preload: [:posts]

get_by!

  MyApp.User.get_by! name: "Sally", age: 30
  MyApp.User.get_by! [name: "Sally", age: 30],  preload: [:posts]

where

  # get all users where status is 3
  MyApp.User.where status: 3

  # get all users where status is 3 and limit to 10 ordering by created_at
  MyApp.User.where [status: 3], limit: 10, order_by: [desc: :created_at]

  # same as above but using maps
  MyApp.User.where %{status: 3}, %{limit: 10, order_by: [desc: :created_at]}

  # same as above but preload the posts association
  MyApp.User.where [status: 3],  limit: 10, order_by: [desc: :inserted_at], preload: [:posts]

get_or_insert

  # get user with name John Smith or insert if user does not exist
  MyApp.User.get_or_insert first_name: "John", last_name: "Smith"

  MyApp.User.get_or_insert %{first_name: "John", last_name: "Smith"}

get_or_insert!

  MyApp.User.get_or_insert! first_name: "John", last_name: "Smith"

first

  # get first user
  MyApp.User.first

  # get first user preloading the posts association
  MyApp.User.first preload: [:posts]

all

  # get all users
  MyApp.User.all

  # get all users preloading the posts association
  MyApp.User.all preload: [:posts]

count

  # get count of all users
  MyApp.User.count

count_where

  # get count of all users where status is 4
  MyApp.User.count_where status_id: 4

  MyApp.User.count_where %{status_id: 4}

Wilcard Preloads

You can preload all associations via a wildcard, do

  MyApp.User.get 3, preload: "*"

or

  MyApp.User.get 3, preload: :*

Default Preloads

You can set a default set of preloads.

defmodule MyApp.Users do
  ...
  use EctoShortcuts, repo: MyApp.Repo,
                     model: MyApp.User,
                     default_preload: [:friends, :user_status, :posts]
  ...
end

or using wildcards

defmodule MyApp.Users do
  ...
  use EctoShortcuts, repo: MyApp.Repo,
                     model: MyApp.User,
                     default_preload: "*"
  ...
end

If you want to override defaults, pass in specific preloads.

Running Tests

To run tests:

  • mysql must be running, then

  • create a database you want tests to run on &

  • configure /config.exs. This creates database called ecto_shortcuts_test with the username root and password root. The default is mysql port 3306.

  • Run your test. mix test

Coming Soon:

  • support for greater & less than comparisons
  • support for basic joins

NOTE: Ecto's DSL is rich & flexible. It should be deferred to for anything complex. 🙌

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory