Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

Build Status Coverage Status Version License

Provides simple, sane and terse shortcuts for Ecto models.

Ecto.Rut is a wrapper around Ecto.Repo methods that usually require you to pass the module as the subject and sometimes even require you do extra work before hand, (as in the case of Repo.insert/3) to perform operations on your database. Ecto.Rut tries to reduce code repetition by following the "Convention over Configuration" ideology.

See the Ecto.Rut Documentation on HexDocs.

Basic Usage

You can call normal Ecto.Repo methods directly on the Models:

Post.all
# instead of YourApp.Repo.all(Post)

Post.get(2)
# instead of YourApp.Repo.get(Post, 2)

Post.insert(title: "Awesome Post", slug: "awesome-post", category_id: 3)
# instead of:
# changeset = Post.changeset(%Post{}, %{title: "Awesome Post", slug: "awesome-post", category_id: 3})
# YourApp.Repo.insert(changeset)

See the Repo Coverage Section or the Hex Documentation for a full list of supported methods.

Installation

Add :ecto_rut as a dependency in your mix.exs file:

defp deps do
  [{:ecto_rut, "~> 1.2.2"}]
end

and run:

$ mix deps.get

Phoenix Projects

If you have an app built in Phoenix Framework, just add use Ecto.Rut in the models method in web/web.ex or anywhere else you have defined your basic Schema structure:

# web/web.ex

def model do
  quote do
    use Ecto.Schema
    use Ecto.Rut, repo: YourApp.Repo

    # Other stuff...
  end
end

That's it! Ecto.Rut will automatically be loaded in all of your models. If you don't have a central macro defined for your Schema, take a look at this example: Repo.Schema.

Other Ecto Projects

If you're not using Phoenix or you don't want to use Ecto.Rut with all of your models (why wouldn't you!?), you'll have to manually add use Ecto.Rut:

defmodule YourApp.Post do
  use Ecto.Schema
  use Ecto.Rut, repo: YourApp.Repo

  # Schema, Changeset and other stuff...
end

Configuration

You do not need to configure Ecto.Rut unless your app is set up differently. All values are inferred automatically and it should just work, but if you absolutely have to, you can specify the repo and model modules:

defmodule YourApp.Post do
  use Ecto.Schema
  use Ecto.Rut, model: YourApp.Other.Post, repo: YourApp.Repo
end

See the Configuration Section in HexDocs for more details.

Not for Complex Queries

Ecto.Rut has been designed for simple use cases. It's not meant for advanced queries and operations on your Database. Ecto.Rut is supposed to do only simple tasks such as getting, updating or deleting records. For any complex queries, you should use the original Ecto.Repo methods.

You shouldn't also let Ecto.Rut handicap you; Ideally, you should understand how Ecto.Repo lets you do advanced queries and how Ecto.Rut places some limits on you (for example, not being able to specify custom options). José has also shown his concern:

My concern with using something such as shortcuts is that people will forget those basic tenets schemas and repositories were built on.

That being said, Ecto.Rut's goal is to save time (and keystrokes) by following the convention over configuration ideology. You shouldn't need to call YourApp.Repo for every little task, or involve yourself with changesets everytime when you've already defined them in your model.

Methods

All

iex> Post.all
[%Post{id: 1, title: "Post 1"},
 %Post{id: 2, title: "Post 2"},
 %Post{id: 3, title: "Post 3"}]

Get

iex> Post.get(2).title
"Blog Post No. 2"

Also see get!/1

Get By

Post.get_by(published_date: "2014-10-22")

Also see get_by!/1

Insert

You can insert Changesets, Structs, Keyword Lists or Maps. When Structs, Keyword Lists or Maps are given, they'll be first converted into a changeset (as defined by your model) and validated before inserting.

So you don't need to create a changeset every time before inserting a new record.

# Accepts Keyword Lists
Post.insert(title: "Introduction to Elixir")

# Accepts Maps
Post.insert(%{title: "Building your first Phoenix app"})

# Even accepts Structs (these would also be validated by your defined changeset)
Post.insert(%Post{title: "Concurrency in Elixir", categories: ["programming", "elixir"]})

Also see insert!/1

Update

There are two variations of the update method; update/1 and update/2. For the first, you can either pass a changeset (which would be inserted immediately) or a modified struct (which would first be converted into a changeset by comparing it with the existing record):

post = Post.get(2)
modified_post = %{ post | title: "New Post Title" }
Post.update(modified_post)

The second method is to pass the original record as the subject and a Keyword List (or a Map) of the new attributes. This method is recommended:

post = Post.get(2)
Post.update(post, title: "New Post Title")

Also see update!/1 and update!/2

Delete

post = Post.get_by(id: 9)
Post.delete(post)

Also see delete!/1

Delete All

Post.delete_all

Method Coverage

Ecto.Repo MethodsEcto.Rut MethodsAdditional Notes
Repo.aggregate
Repo.allModel.all
Repo.config
Repo.deleteModel.delete
Repo.delete!Model.delete!
Repo.delete_allModel.delete_all
Repo.getModel.get
Repo.get!Model.get!
Repo.get_byModel.get_by
Repo.get_by!Model.get_by!
Repo.in_transaction?
Repo.insertModel.insertAccepts structs, changesets, keyword lists and maps
Repo.insert!Model.insert!Accepts structs, changesets, keyword lists and maps
Repo.insert_all
Repo.insert_or_update
Repo.insert_or_update!
Repo.one
Repo.one!
Repo.preload
Repo.query
Repo.query!
Repo.rollback
Repo.start_link
Repo.stop
Repo.transaction
Repo.updateModel.updateAccepts structs, changesets, keyword lists and maps
Repo.update!Model.update!Accepts structs, changesets, keyword lists and maps
Repo.update_all

Roadmap

  • Write Tests
  • Write Documentation
  • Cover all main Ecto.Repo methods
  • Allow explicitly passing Application and Repo modules to the use Ecto.Rut statement
  • Add support for preloading with existing methods
  • Introduce new wrapper and helper methods that do not exist in Ecto, such as:
    • Model.count
    • Model.first and Model.last
    • Model.find_or_create(obj)
    • Methods that accept direct arguments (E.g. delete_by_id(3))

Contributing

  • Fork, Enhance, Send PR
  • Lock issues with any bugs or feature requests
  • Implement something from Roadmap
  • Spread the word

License

This package is available as open source under the terms of the MIT License.

Elixir
Elixir
Elixir is a dynamic, functional programming language designed for building scalable and maintainable applications. Built on the Erlang VM, it's known for its high concurrency and fault tolerance, making it ideal for real-time systems and web services.
GitHub - chrismccord/atlas: Object Relational Mapper for Elixir
GitHub - chrismccord/atlas: Object Relational Mapper for Elixir
GitHub - mbuhot/ecto_job: Transactional job queue with Ecto, PostgreSQL and GenStage
GitHub - mbuhot/ecto_job: Transactional job queue with Ecto, PostgreSQL and GenStage
GitHub - zamith/tomlex: A TOML parser for elixir
GitHub - zamith/tomlex: A TOML parser for elixir
GitHub - pablomartinezalvarez/glayu: A static site generator for mid-sized sites.
GitHub - pablomartinezalvarez/glayu: A static site generator for mid-sized sites.
GitHub - jui/mustachex: Mustache for Elixir
GitHub - jui/mustachex: Mustache for Elixir
GitHub - joaothallis/elixir-auto-test: Run test when file is saved
GitHub - joaothallis/elixir-auto-test: Run test when file is saved
GitHub - campezzi/ignorant: Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values.
GitHub - campezzi/ignorant: Simplify comparison of Elixir data structures by ensuring fields are present but ignoring their values.
GitHub - Driftrock/mockingbird: A set of helpers to create http-aware modules that are easy to test.
GitHub - Driftrock/mockingbird: A set of helpers to create http-aware modules that are easy to test.
GitHub - gutschilla/elixir-pdf-generator: Create PDFs with wkhtmltopdf or puppeteer/chromium from Elixir.
GitHub - gutschilla/elixir-pdf-generator: Create PDFs with wkhtmltopdf or puppeteer/chromium from Elixir.
GitHub - antirez/disque: Disque is a distributed message broker
GitHub - antirez/disque: Disque is a distributed message broker
GitHub - jcomellas/ex_hl7: HL7 Parser for Elixir
GitHub - jcomellas/ex_hl7: HL7 Parser for Elixir
GitHub - Cirru/parser.ex: Cirru Parser in Elixir
GitHub - Cirru/parser.ex: Cirru Parser in Elixir
GitHub - thiamsantos/pwned: Check if your password has been pwned
GitHub - thiamsantos/pwned: Check if your password has been pwned
GitHub - suvash/hulaaki: DEPRECATED : An Elixir library (driver) for clients communicating with MQTT brokers(via the MQTT 3.1.1 protocol).
GitHub - suvash/hulaaki: DEPRECATED : An Elixir library (driver) for clients communicating with MQTT brokers(via the MQTT 3.1.1 protocol).
GitHub - sinetris/factory_girl_elixir: Minimal implementation of Ruby's factory_girl in Elixir.
GitHub - sinetris/factory_girl_elixir: Minimal implementation of Ruby's factory_girl in Elixir.
GitHub - navinpeiris/ex_unit_notifier: Desktop notifications for ExUnit
GitHub - navinpeiris/ex_unit_notifier: Desktop notifications for ExUnit
GitHub - DefactoSoftware/test_selector: Elixir library to help selecting the right elements in your tests.
GitHub - DefactoSoftware/test_selector: Elixir library to help selecting the right elements in your tests.
GitHub - xerions/ecto_migrate: Automatic migrations for ecto
GitHub - xerions/ecto_migrate: Automatic migrations for ecto
GitHub - meh/reagent: You need more reagents to conjure this server.
GitHub - meh/reagent: You need more reagents to conjure this server.
GitHub - stevegraham/hypermock: HTTP request stubbing and expectation Elixir library
GitHub - stevegraham/hypermock: HTTP request stubbing and expectation Elixir library
GitHub - msharp/elixir-statistics: Statistical functions and distributions for Elixir
GitHub - msharp/elixir-statistics: Statistical functions and distributions for Elixir
GitHub - Joe-noh/colorful: colorful is justice
GitHub - Joe-noh/colorful: colorful is justice
GitHub - ijcd/taggart: HTML as code in Elixir
GitHub - ijcd/taggart: HTML as code in Elixir
Build software better, together
Build software better, together
GitHub - yeshan333/ex_integration_coveralls: A library for run-time system code line-level coverage analysis.
GitHub - yeshan333/ex_integration_coveralls: A library for run-time system code line-level coverage analysis.
GitHub - PSPDFKit-labs/cobertura_cover: Output test coverage information in Cobertura-compatible format
GitHub - PSPDFKit-labs/cobertura_cover: Output test coverage information in Cobertura-compatible format
GitHub - basho/enm: Erlang driver for nanomsg
GitHub - basho/enm: Erlang driver for nanomsg
GitHub - pawurb/ecto_psql_extras: Ecto PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
GitHub - pawurb/ecto_psql_extras: Ecto PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
GitHub - crate/craterl: Client Libraries for Erlang
GitHub - crate/craterl: Client Libraries for Erlang
GitHub - sheharyarn/ecto_rut: Ecto Model shortcuts to make your life easier! :tada:
GitHub - sheharyarn/ecto_rut: Ecto Model shortcuts to make your life easier! :tada:
Elixir
More on Elixir

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.