ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - joshrieken/plasm: Ecto's composable query multitool (.count, .random, .earliest, .latest, .find, .at, .on, etc.)
Ecto's composable query multitool (.count, .random, .earliest, .latest, .find, .at, .on, etc.) - joshrieken/plasm
Visit Site

GitHub - joshrieken/plasm: Ecto's composable query multitool (.count, .random, .earliest, .latest, .find, .at, .on, etc.)

GitHub - joshrieken/plasm: Ecto's composable query multitool (.count, .random, .earliest, .latest, .find, .at, .on, etc.)

Plasm

Build Status Deps Status Inline docs

A generic composable query library for Ecto.

:heart::heart::heart: Ecto, :cry::cry::cry: because I have to implement my own composable query functions for things like counting records, getting a random record and whatnot in all my models/projects.

NO MORE.

Plasm provides a set of generic, composable, higher-level functions that make working with Ecto more joyful and productive.

Design Objectives

  • Work alongside Ecto.Query so both can be imported without conflict
  • Avoid reimplementing basic Ecto.Query functionality where possible
  • Provide syntactic sugar for common queries (e.g., see count and distinct_by)
  • Easy integration with Phoenix
  • Permissive API (e.g., most functions that accept an atom will alternatively accept a string)

Examples

Instead of writing this in your model:

defmodule MyGhostBustingApp.ProtonPack
  ...

  def count(query) do
    for q in query,
    select: count(q.id)
  end

  ...
end

And using it this way:

ProtonPack |> ProtonPack.count |> Repo.one

Just use Plasm:

ProtonPack |> Plasm.count |> Repo.one

More examples:

PkeMeter |> Plasm.later_than(:updated_at, "2016-01-04T14:00:00Z") |> Repo.all
GhostTrap |> Plasm.find([3,6,9]) |> Repo.all
StayPuftMarshmallowMan |> Plasm.random |> Repo.one

Using in Models

You can import Plasm and use it directly in your models:

defmodule MyApp.SomeModel do
  import Ecto.Query
  import Plasm

  ...

  def random_distinct_names_by_order_of_insertion(query, n) do
    query
    |> order_by(asc: :inserted_at)
    |> distinct_by(:name)
    |> random(n)
  end
end

Using with Phoenix

If you want Plasm to be universally accessible in all your Phoenix models, you can add it to web.ex:

defmodule MyApp.Web do
  ...

  def model do
    quote do
      ...

      import Plasm
    end
  end
end

API

Plasm.at(query, field_name, ecto_date_time)
Plasm.at_or_later_than(query, field_name, ecto_date_time)
Plasm.at_or_earlier_than(query, field_name, ecto_date_time)
Plasm.average(query, field_name)
Plasm.count(query)
Plasm.count_distinct(query, field_name)
Plasm.distinct_by(query, field_name)
Plasm.earlier_than(query, field_name, ecto_date_or_date_time)
Plasm.earliest(query, field_name)
Plasm.earliest(query, field_name, n)
Plasm.find(query, primary_key)
Plasm.find(query, primary_keys)
Plasm.later_than(query, field_name, ecto_date_or_date_time)
Plasm.latest(query, field_name)
Plasm.latest(query, field_name, n)
Plasm.maximum(query, field_name)
Plasm.minimum(query, field_name)
Plasm.on(query, field_name, ecto_date)
Plasm.on_or_later_than(query, field_name, ecto_date)
Plasm.on_or_earlier_than(query, field_name, ecto_date)
Plasm.random(query)
Plasm.random(query, n)
Plasm.total(query, field_name)
Plasm.where_all(query, field_names_and_values)
Plasm.where_none(query, field_names_and_values)

Why not use shorter names, like avg, sum, etc.?

To avoid conflicts when importing. For instance, Plasm.min\2 and Plasm.max\2 would conflict with Kernel.min\2 and Kernel.max\2, so if you're importing them, you'd need to prefix your calls with __MODULE__; e.g., __MODULE__.min(query, field_name). This sucks, so I chose to go with the longer versions and tried to stay consistent.

Note On DB Support

Guaranteed to work with Postgres; others might work but haven't been tested.

Note On Query Syntaxes

Ecto supports two query syntaxes, keyword and query expression.

Example of the keyword syntax:

def for_name_or_age(query, name, age) do
  for x in query,
  where: x.name == ^name or x.age == ^age
end

Example of the query expression syntax:

def for_name_or_age(query, name, age) do
  query
  |> where([x], x.name == ^name or x.age == ^age)
end

The keyword syntax is a bit easier on the eyes, but is not fully compatible with Plasm. A case can be made for sticking with the query expression syntax for all functions that are meant to be composable, and especially if you plan to use Plasm or something like it.

Inspiration

Many thanks to Drew Olson for his talk at ElixirConf 2015 and insightful blog post on the subject of composable Ecto queries.

Also thanks to Henrik Nyh for his Ectoo project, which has similar aims.

TODO:

  • Tests
  • Hex docs

Installation

Add Plasm to your list of dependencies in mix.exs:

def deps do
  [{:plasm, "~> 2.0"}]
end

Ensure Plasm is started before your application:

def application do
  [
    applications: [
      ...
      :plasm
      ...
    ]
  ]
end

If you want to be on the bleeding edge, track the master branch of this repo:

{:plasm, git: "https://github.com/facto/plasm.git", branch: "master"}

Copyright and License

Copyright (c) 2016-2020 Joshua Rieken.

Plasm source code is licensed under the Apache 2 License (see LICENSE.md).

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