GreenFairy.CQL.Adapters.Memory (GreenFairy v0.3.0)

View Source

Memory/Enum-based CQL adapter for plain structs.

This is the fallback adapter when no other adapter (Ecto, Elasticsearch, etc.) matches the struct backing a type. It provides basic CQL operations using Elixir's Enum module functions.

Supported Operations

Filter Operators:

  • _eq - Equality check (==)
  • _neq - Not equal (!=)
  • _gt, _gte, _lt, _lte - Comparisons (>, >=, <, <=)
  • _in - Value in list
  • _nin - Value not in list
  • _is_null - Null check

Sort:

  • asc - Sort ascending
  • desc - Sort descending

How It Works

Unlike database adapters, the Memory adapter works on in-memory lists:

users = [%User{id: 1, name: "Alice"}, %User{id: 2, name: "Bob"}]

# Filter: name == "Alice"
Memory.filter(users, :name, :_eq, "Alice")
#=> [%User{id: 1, name: "Alice"}]

# Sort by name ascending
Memory.sort(users, :name, :asc)
#=> [%User{id: 1, name: "Alice"}, %User{id: 2, name: "Bob"}]

Usage with GreenFairy

This adapter is automatically selected for types that:

  • Use plain structs (not Ecto schemas)
  • Don't have a repo configured
  • Don't match any other adapter

Users must provide their own data source (typically via resolver):

type "User", struct: MyApp.User do
  # CQL operators are available for filtering/sorting
  # but you provide the data in your resolver
  field :id, non_null(:id)
  field :name, :string
end

# In your resolver:
def list_users(_parent, args, _ctx) do
  users = MyApp.get_all_users()
  filtered = GreenFairy.CQL.Adapters.Memory.apply_filters(users, args.filter)
  sorted = GreenFairy.CQL.Adapters.Memory.apply_order(filtered, args.order)
  {:ok, sorted}
end

Summary

Functions

Applies filter operations to a list of items.

Applies ordering to a list of items.

Applies both filtering and ordering to a list of items.

Functions

apply_filters(items, filter)

Applies filter operations to a list of items.

Parameters

  • items - List of structs/maps to filter
  • filter - Map of field => operator map (e.g., %{name: %{_eq: "Alice"}})

Returns

Filtered list of items.

Examples

users = [%User{name: "Alice"}, %User{name: "Bob"}]

apply_filters(users, %{name: %{_eq: "Alice"}})
#=> [%User{name: "Alice"}]

apply_filters(users, %{name: %{_in: ["Alice", "Bob"]}})
#=> [%User{name: "Alice"}, %User{name: "Bob"}]

apply_order(items, order)

Applies ordering to a list of items.

Parameters

  • items - List of structs/maps to sort
  • order - List of %{field: atom, direction: :asc | :desc} maps

Returns

Sorted list of items.

Examples

users = [%User{name: "Bob"}, %User{name: "Alice"}]

apply_order(users, [%{field: :name, direction: :asc}])
#=> [%User{name: "Alice"}, %User{name: "Bob"}]

apply_order(users, [%{field: :name, direction: :desc}])
#=> [%User{name: "Bob"}, %User{name: "Alice"}]

apply_query(items, filter \\ nil, order \\ nil)

Applies both filtering and ordering to a list of items.

Parameters

  • items - List of structs/maps
  • filter - Filter map (optional)
  • order - Order list (optional)

Returns

Filtered and sorted list of items.