Humaans.Query (Humaans v0.6.0)

Copy Markdown View Source

Builder for Humaans API filter query parameters.

The Humaans API (a Feathers.js application) supports filtering list endpoints with operator suffixes like $in, $gt, etc. Hand-encoding these in keyword lists is awkward in Elixir because the keys contain characters that don't survive atom literals, e.g. ["status[$in]": ["active"]]. This module provides a small chainable builder so callers can express filters readably.

Field names and operator suffixes are stored as strings in the resulting params list. This avoids creating new atoms at runtime — important because atoms are not garbage-collected and untrusted or high-cardinality field names could otherwise exhaust the atom table. Req accepts string-keyed params transparently, so callers do not need to do anything different.

Example

query =
  Humaans.Query.new()
  |> Humaans.Query.eq(:companyId, "acme")
  |> Humaans.Query.in_(:status, ["active", "onboarding"])
  |> Humaans.Query.gte(:createdAt, "2025-01-01")
  |> Humaans.Query.to_params()

Humaans.People.list(client, query)

Supported operators

Field names are accepted as atoms or strings. The Humaans API uses camelCase field names (companyId, createdAt, etc.) — pass them as written. Operator-suffixed keys are produced for you.

Use merge/2 to combine a query with other params (e.g. pagination):

Humaans.Query.new()
|> Humaans.Query.in_(:status, ["active"])
|> Humaans.Query.merge("$limit": 50)
|> Humaans.Query.to_params()

Summary

Functions

Adds an equality filter (field=value).

Adds a $gt filter.

Adds a $gte filter.

Adds a $in filter (field[$in]=value).

Adds a $lt filter.

Adds a $lte filter.

Merges arbitrary params (keyword list or another query) into this query.

Returns an empty query.

Adds a $nin filter.

Returns the query as a list of {string_key, value} tuples suitable for passing as params to a resource list function.

Types

field()

@type field() :: atom() | String.t()

t()

@type t() :: %Humaans.Query{params: [{String.t(), value()}]}

value()

@type value() :: any()

Functions

eq(query, field, value)

@spec eq(t(), field(), value()) :: t()

Adds an equality filter (field=value).

gt(query, field, value)

@spec gt(t(), field(), value()) :: t()

Adds a $gt filter.

gte(query, field, value)

@spec gte(t(), field(), value()) :: t()

Adds a $gte filter.

in_(query, field, values)

@spec in_(t(), field(), [value()]) :: t()

Adds a $in filter (field[$in]=value).

lt(query, field, value)

@spec lt(t(), field(), value()) :: t()

Adds a $lt filter.

lte(query, field, value)

@spec lte(t(), field(), value()) :: t()

Adds a $lte filter.

merge(query, more)

@spec merge(t(), keyword() | t()) :: t()

Merges arbitrary params (keyword list or another query) into this query.

Useful for combining filters with pagination params. Atom keys in the keyword list are normalized to strings so the result is uniform.

Raises ArgumentError when given a list that is not a proper keyword list (e.g. contains non-tuple elements or non-atom keys).

new()

@spec new() :: t()

Returns an empty query.

nin(query, field, values)

@spec nin(t(), field(), [value()]) :: t()

Adds a $nin filter.

to_params(query)

@spec to_params(t()) :: [{String.t(), value()}]

Returns the query as a list of {string_key, value} tuples suitable for passing as params to a resource list function.