plushie/data

Data query pipeline for filtering, searching, sorting, grouping, and paginating in-memory record collections.

Filtering, search, sorting, and pagination each create an intermediate copy of the data list. Grouping collects from the filtered, searched, sorted records before pagination. For very large datasets, consider combining operations or using a different data structure.

Apply a chain of query options to transform a list:

data.query(users, [
  data.Filter(fn(u) { u.active }),
  data.Search(fields: [fn(u) { u.name }], query: "alice"),
  data.SortBy(direction: data.Asc, compare: fn(a, b) {
    string.compare(a.name, b.name)
  }),
  data.Page(1),
  data.PageSize(10),
])

Types

Query options applied in order: filter -> search -> sort -> group -> paginate. Groups and total use the filtered, searched, sorted records before pagination. Entries use the same records after pagination. Repeated Filter and Search opts compose in list order.

pub type QueryOpt(a) {
  Filter(fn(a) -> Bool)
  Search(fields: List(fn(a) -> String), query: String)
  Sort(direction: SortDirection, key: fn(a) -> String)
  SortBy(
    direction: SortDirection,
    compare: fn(a, a) -> order.Order,
  )
  Page(Int)
  PageSize(Int)
  Group(fn(a) -> String)
}

Constructors

  • Filter(fn(a) -> Bool)

    Keep only records matching the predicate.

  • Search(fields: List(fn(a) -> String), query: String)

    Case-insensitive substring search across extracted string fields.

  • Sort(direction: SortDirection, key: fn(a) -> String)

    Sort by a string key extractor (lexicographic comparison).

  • SortBy(
      direction: SortDirection,
      compare: fn(a, a) -> order.Order,
    )

    Sort with a custom comparison function (for numeric or complex sorts).

  • Page(Int)

    Page number (1-based, default 1).

  • PageSize(Int)

    Items per page (default 25).

  • Group(fn(a) -> String)

    Group records by a key extractor before pagination is applied.

Query result with pagination metadata.

pub type QueryResult(a) {
  QueryResult(
    entries: List(a),
    total: Int,
    page: Int,
    page_size: Int,
    groups: dict.Dict(String, List(a)),
  )
}

Constructors

  • QueryResult(
      entries: List(a),
      total: Int,
      page: Int,
      page_size: Int,
      groups: dict.Dict(String, List(a)),
    )

Sort direction.

pub type SortDirection {
  Asc
  Desc
}

Constructors

  • Asc
  • Desc

Values

pub fn query(
  records: List(a),
  opts: List(QueryOpt(a)),
) -> QueryResult(a)

Run a query pipeline on a list of records.

Search Document