• Stars
    star
    112
  • Rank 312,182 (Top 7 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ecto Model shortcuts to make your life easier! ๐ŸŽ‰

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

iex> Post.all
[%Post{id: 1, title: "Post 1"},
 %Post{id: 2, title: "Post 2"},
 %Post{id: 3, title: "Post 3"}]
iex> Post.get(2).title
"Blog Post No. 2"

Also see get!/1

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

Also see get_by!/1

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

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

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

Also see delete!/1

Post.delete_all

Method Coverage

Ecto.Repo Methods Ecto.Rut Methods Additional Notes
Repo.aggregate โ€” โ€”
Repo.all Model.all โ€”
Repo.config โ€” โ€”
Repo.delete Model.delete โ€”
Repo.delete! Model.delete! โ€”
Repo.delete_all Model.delete_all โ€”
Repo.get Model.get โ€”
Repo.get! Model.get! โ€”
Repo.get_by Model.get_by โ€”
Repo.get_by! Model.get_by! โ€”
Repo.in_transaction? โ€” โ€”
Repo.insert Model.insert Accepts 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.update Model.update Accepts 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.

More Repositories

1

memento

Simple + Powerful interface to the Mnesia Distributed Database ๐Ÿ’พ
Elixir
737
star
2

que

Simple Job Processing in Elixir with Mnesia โšก
Elixir
668
star
3

mongo-sync

Sync Remote and Local MongoDB Databases ๐Ÿ”ฅ
Shell
322
star
4

dotfiles

๐Ÿ“ฆ My Configs and Dotfiles
Shell
121
star
5

cloudup.dev

Tools to jump-start development on the Cloud โ˜๏ธ
JavaScript
106
star
6

better_params

Cleaner request parameters in Elixir web applications ๐Ÿ™Œ
Elixir
97
star
7

dbfs

Distributed Blockchain-based File Storage ๐Ÿ“ก
Elixir
63
star
8

capistrano-rake

Execute rake tasks on remote servers (Only Capistrano 3+)
Ruby
41
star
9

github-trending

A gem that fetches trending github repos
Ruby
40
star
10

ztd

Distributed real-time Todo App over RabbitMQ in Elixir ๐Ÿฐโšก๏ธ๐ŸŒ๐Ÿ’ง๐Ÿ’ป
Elixir
24
star
11

mongo-sync-ruby

A Ruby Gem for syncing local and remote Mongo Databases
Ruby
16
star
12

explay

Google Play API in Elixir ๐Ÿ’ป
Protocol Buffer
16
star
13

cipherjs

Javascript Implementation of simple Ciphers
JavaScript
12
star
14

collab

Details and ElixirConf talk: https://shyr.io/t/real-time-collab
Elixir
12
star
15

sheharyarn.github.io

My Blog
HTML
11
star
16

simple-phoenix-chat

Super simple implementation of Real-time chat in Elixir & Phoenix using WebSockets / Channels
Elixir
11
star
17

freelancer-leaderboard

Displays Freelancer Scavenger 2014 Leaderboard
Ruby
10
star
18

vncviewer

Simple utility to invoke VNC from shell in OSX
Shell
6
star
19

ex_utils

โšก Collection of Awesome Elixir shortcuts and utilities โšก
Elixir
6
star
20

werewolf.nvim

๐Ÿ”†๐ŸŒ‘ Apply custom Neovim configs based on system theme
Lua
6
star
21

httpbin-node

Node.js implementation of httpbin
JavaScript
5
star
22

httpcodes.co

Quick Informational pages on HTTP Status Codes and HTTP Verbs
HTML
4
star
23

s3_direct_upload_example

Example RoR Application on using the s3_direct_upload gem
Ruby
4
star
24

etv

Ethereum Transaction Verifier
Elixir
2
star
25

dbfs-web

DBFS Web Client
JavaScript
2
star
26

geo-bounds

Axis-aligned geospatial bounded-boxes in Elixir
Elixir
2
star
27

zippy

DEPRECATED! - Zippy is a Ruby Program that downloads files from Zippyshare in your Terminal
Ruby
2
star
28

vertex

2
star
29

cm-test-solution

CareMerge Test Solution
JavaScript
1
star
30

daniyal.me

HTML
1
star
31

nustalumni-android

Android app for the NUST Alumni Association
Java
1
star
32

syklean-html

HTML Layout for the Syklean Octopress Theme
JavaScript
1
star
33

sparkrelay-android

Java
1
star
34

harisandhafsa

HTML
1
star
35

rmd-template

Rails 4 + Mongoid 4 + Devise Template
Ruby
1
star
36

ecto_atomized_map

Use Atom keys for Ecto Schema Maps โš›๏ธ
Elixir
1
star
37

elixir-yaml

YAML Parser and Encoder for Elixir โ€“ย WIP!
Elixir
1
star
38

ex_gun

Mailgun Integration in Elixir
Elixir
1
star