cheekyfinder

cheekyfinder

ExSift 0.2.0 - Filter any Elixir type with MongoDB-style queries

ExSift 0.2.0 is out. The big headline: you can now filter anything not just plain maps. Keyword lists, MapSets, structs, and any custom type all work out of the box.


What’s new

Filter keyword lists

data = [[name: "Alice", age: 30], [name: "Bob", age: 25]]

ExSift.filter(data, %{"age" => %{"$gt" => 28}})
# => [[name: "Alice", age: 30]]

Filter structs directly

Shape matching, dot notation, and all operators work on any struct - no boilerplate needed:

defmodule User, do: defstruct [:name, :age, :role]

data = [
  %User{name: "Alice", age: 30, role: "admin"},
  %User{name: "Bob", age: 25, role: "user"},
  %User{name: "Charlie", age: 35, role: "admin"}
]

ExSift.filter(data, %{role: "admin", age: %{"$gte" => 30}})
# => [%User{name: "Alice", ...}, %User{name: "Charlie", ...}]

# Dot notation through nested structs
ExSift.filter(records, %{"address.city" => "NYC"})

MapSet fields - $size, $all, $in, $elemMatch

data = [
  %{user: "alice", perms: MapSet.new([:read, :write, :delete])},
  %{user: "bob",   perms: MapSet.new([:read])}
]

# Who has both :read and :write?
ExSift.filter(data, %{perms: %{"$all" => [:read, :write]}})
# => [%{user: "alice", ...}]

# Who has exactly 1 permission?
ExSift.filter(data, %{perms: %{"$size" => 1}})
# => [%{user: "bob", ...}]

# Who has :write in their perms?
ExSift.filter(data, %{perms: %{"$in" => [:write]}})
# => [%{user: "alice", ...}]

Custom types via the Collection protocol

Implement ExSift.Collection for any type and it will work with all operators:

defimpl ExSift.Collection, for: MyQueue do
  def fetch(q, key), do: Map.get(q, key)
  def to_pairs(q), do: [size: q.size, name: q.name]
  def member?(q, value), do: value in q.items
  def size(q), do: length(q.items)
end

# Now ExSift works on MyQueue natively
ExSift.filter(queues, %{name: "priority", "$size" => 5})

Compiled filters work across all types

compile/1 returns a plain function. Reuse it across any collection type:

is_senior = ExSift.compile(%{age: %{"$gte" => 30}})

Enum.filter(maps, is_senior)
Enum.filter(structs, is_senior)
Enum.filter(keyword_lists, is_senior)
Enum.count(all_users, is_senior)

Upgrading

No breaking changes. Drop in the new version:

{:ex_sift, "~> 0.2.0"}

Links

As always, feedback and PRs welcome!

Where Next? Top

Trending in News & Updates Top

sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
bartblast
I’ll be using this thread to share Hologram patch release announcements. Minor releases will continue to get dedicated threads with blog ...
New
sorenone
This release unifies configuration for queues, repos, and services, swaps opaque timing integers for readable durations, and backports pe...
New
pcharbon
:heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::heart::hea...
New
jvoegele
Bond brings Design by Contract to Elixir: preconditions, postconditions and invariants as executable specifications, checked at runtime a...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews