Counterpoint.Query (counterpoint v0.1.0)

Copy Markdown View Source

Composable query builder for filtering events from the store.

A query is a list of items, each specifying event types and/or tags that must match. Multiple items act as a union (OR): any event matching at least one item is returned. Within an item, types and tags are ANDed.

Options

  • after_position – return only events appended after this store position.
  • limit – cap the number of returned events (0 = no limit).
  • reverse – return events in reverse chronological order.

Example

alias Counterpoint.Query
alias MyApp.Events.{OrderPlaced, OrderCancelled}

# Fetch the last 1 event for a specific order, newest first
Query.new()
|> Query.add_item(types: [OrderPlaced, OrderCancelled], tags: ["order_id:123"])
|> Query.reverse()
|> Query.limit(1)

Summary

Functions

Add a filter item to the query.

Return only events appended after position.

Limit results to at most n events (0 = unlimited).

Create a new empty query.

Return events in reverse chronological order.

Types

item()

@type item() :: %{types: [String.t()], tags: [String.t()]}

t()

@type t() :: %Counterpoint.Query{
  after_position: binary() | nil,
  items: [item()],
  limit: non_neg_integer(),
  reverse: boolean()
}

Functions

add_item(q, opts)

Add a filter item to the query.

Options

  • :types – list of event modules (or type-string binaries) to match.
  • :tags – list of tag strings to match.

Both default to [] (match anything for that field).

after_position(q, position)

Return only events appended after position.

limit(q, n)

Limit results to at most n events (0 = unlimited).

new()

Create a new empty query.

reverse(q)

Return events in reverse chronological order.