View Source Fob.Cursor (Fob v1.0.5)
A cursor data structure that can be used to fetch the next pages
The cursor data structure is somewhat similar to a Stream
except that it
cannot wrap a stateful process and does not require any life-cycle hooks.
The next pages of data can be fetched by calling next/1
successively,
passing in the updated t/0
data structure each time.
examples
Examples
iex> import Ecto.Query, only: [order_by: 2]
iex> cursor = Fob.Cursor.new(order_by(MyApp.Schema, asc: :id), MyApp.Repo, nil, 3)
#Fob.Cursor<...>
iex> {_records, cursor} = Fob.Cursor.next(cursor)
{[%{id: 1}, %{id: 2}, %{id: 3}], #Fob.Cursor<...>}
iex> {_records, cursor} = Fob.Cursor.next(cursor)
{[%{id: 4}, %{id: 5}, %{id: 6}], #Fob.Cursor<...>}
iex> {_records, cursor} = Fob.Cursor.split(cursor, 2)
{[%{id: 7}, %{id: 8}, %{id: 9}, %{id: 10}, %{id: 11}, %{id: 12}], #Fob.Cursor<...>}
Link to this section Summary
Functions
Creates a new cursor
Fetches the next set of records and returns the next cursor
Fetches count
next pages and returns the next cursor
Link to this section Types
@opaque t()
A data structure representing a position in a data set
Cursors are immutable data structures that do not spawn or interact with processes.
Link to this section Functions
@spec new(Ecto.Queryable.t(), Ecto.Repo.t(), [Fob.PageBreak.t()] | nil, pos_integer()) :: t()
Creates a new cursor
Pass nil
as page_breaks
to start at the beginning of the data set
(ordered by whatever ordering conditions are on the queryable).
Fetches the next set of records and returns the next cursor
If the underlying table doesn't change, calling next/1
repeatedly
will give the same records. The returned cursor
can be used to get
the next page of records.
@spec split(cursor :: t(), count :: non_neg_integer()) :: {records :: [map()], cursor :: t()}
Fetches count
next pages and returns the next cursor
For reference: next/1
is implemented as split(cursor, 1)
.