Skip to content

Module for querying list of structs or maps in elixir

License

Notifications You must be signed in to change notification settings

RohanPoojary/membrane

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Membrane

Build Status Inline docs License: MIT

Membrane provides a wrapper for filtering data with simplicity and efficiently.

It filters out list of structs or maps that satisfy the query. The Query is inspired by Mongo, hence there's a lot of similarities. To learn more please visit our documentation https://hexdocs.pm/membrane/Membrane.html .

Installation

To install just add the module to your mix file.

def deps do
  [
    ...
    {:membrane, "~> 0.1.0"}
  ]
end

Examples

Membrane currently provides a single function named filter for filtering the data

iex> data = [
  %Person{id: 1, name: "Bob", action: "talk", age: 30},
  %Person{id: 3, name: "Helen", action: "talk", age: 30},
  %Dog{id: 1, name: "Rocky",  action: "bark", age: 10},
  %Cat{id: 3, name: "Rocky",  action: "meow", age: 6},
  %Chair{id: 10, age: 3},
  %{id: 1, type: "car", age: 12}
]

# The query matches all objects with `id` as 1
iex> Membrane.filter(data, id: 1)
[
  %{id: 1, type: "car", age: 12},
  %Dog{id: 1, name: "Rocky",  action: "bark", age: 10},
  %Person{id: 1, name: "Bob", action: "talk", age: 30},
]

As you can notice Membrane.filter is independent of structs or maps in the data. This is because all structs are just bare maps

It also supports complex queries. Say for the same data.

# The query matches all objects with `age` greater than 8 and has `action` attribute.
iex> Membrane.filter(data, age: [gt: 8], action: :exists)
[
  %Dog{id: 1, name: "Rocky",  action: "bark", age: 10},
  %Person{id: 3, name: "Helen", action: "talk", age: 30},
  %Person{id: 1, name: "Bob", action: "talk", age: 30},
]

Task Lists

  • Logical and Comparision Operations
  • List based Operations
  • Filtering based on existence of an attribute
  • Not Operation
  • Comparision between attributes of a struct
  • Renaming of attribute names ( Final output has to be a list of maps )
  • Filtering struct which has query keywords as attributes.

Contributing

It's a new project so please feel free to contribute. Reporting bugs will also help.

License

The source code is released under MIT License. Check the License file for more information