View Source Loupe.Stream (Loupe v0.10.1)
Filters a stream with Loupe's ast. Schema will likely have no impact on the way this filtering is being done
Link to this section Summary
Link to this section Types
@type build_query_error() :: any()
@type option() :: {:limit?, boolean()} | {:variables, map()} | Loupe.Stream.Context.option()
Link to this section Functions
@spec query(String.t() | Loupe.Language.Ast.t(), Enumerable.t(), [option()]) :: {:ok, Enumerable.t()} | {:error, build_query_error()}
Queries a stream using an AST or a Query. The returned result will
be an enumerable function to be executed with Enum
functions.
For stream queries, the Schema is unused, up to you to perform pre-filtering, put a static one in place or omit it.
examples
Examples
The following example will filter records whose age is greater than 18
iex> {:ok, stream} = Loupe.Stream.query(~s|get where age > 18|, [
...> %{age: 76},
...> %{age: 13},
...> %{age: 28},
...> %{age: 6},
...> %{age: 34},
...> ])
iex> Enum.to_list(stream)
[%{age: 76}]
The same parsing rules applies here, so only one record is returned because
when quantifier is provided, it defaults to 1. One could use all
to
get all the records that are matching or a range.
iex> {:ok, stream} = Loupe.Stream.query(~s|get all where age > 18|, [
...> %{age: 76},
...> %{age: 13},
...> %{age: 28},
...> %{age: 6},
...> %{age: 34},
...> ])
iex> Enum.to_list(stream)
[%{age: 76}, %{age: 28}, %{age: 34}]
iex> {:ok, stream} = Loupe.Stream.query(~s|get 2..3 where age > 18|, [
...> %{age: 76},
...> %{age: 13},
...> %{age: 28},
...> %{age: 6},
...> %{age: 34},
...> ])
iex> Enum.to_list(stream)
[%{age: 28}, %{age: 34}]
overriding-query-s-limit
Overriding query's limit
In case you wanna enforce a limit of your own to the stream and don't wanna
depend on the query's quantifier
, you can pass limit?: false
to the function
iex> {:ok, stream} = Loupe.Stream.query(~s|get 1 where age > 18|, [
...> %{age: 76},
...> %{age: 13},
...> %{age: 28},
...> %{age: 6},
...> %{age: 34},
...> ], limit?: false)
iex> Enum.to_list(stream)
[%{age: 76}, %{age: 28}, %{age: 34}]
using-variables
Using variables
You can provide variables to your query with the variables
option. Keys
must be string to match what is decoded from the query.
iex> {:ok, stream} = Loupe.Stream.query(~s|get 1 where age > adult|, [
...> %{age: 76},
...> %{age: 13},
...> %{age: 28},
...> %{age: 6},
...> %{age: 34},
...> ], limit?: false, variables: %{"adult" => 18})
iex> Enum.to_list(stream)
[%{age: 76}, %{age: 28}, %{age: 34}]