View Source SanityEx.Query (SanityEx v0.1.1)
SanityEx.Query
is used for constructing GROQ queries from Elixir syntax.
example
Example
iex> Query.new()
|> Query.filter(%{"_type" => "'post'"})
|> Query.project([
"title",
"body",
%{
"'author'" => [
["'_id'", ["author", "_id", :follow]],
["'_type'", ["author", "_type", :follow]],
["'name'", ["author", "name", :follow]],
["'slug'", ["author", "slug", :follow]],
["'image'", ["author", "image", :follow]]
]
}
])
|> Query.slice(0)
|> Query.build()
"*[!(_id in path('drafts.**')) && _type == 'post']{title, body, 'author':{'_id':author->_id, '_type':author->_type, 'name':author->name, 'slug':author->slug, 'image':author->image}}[0]"
Link to this section Summary
Functions
Builds a GROQ query from the given structure.
Builds a GROQ query from the given structure.
Add a filter to the query. The filter retains documents for which the expression evaluates to true.
Initialize the building of a GROQ query pipeline. Returns a SanityEx.Query struct representing a GROQ query.
Specify the ordering of the results of the query.
Adds object projection(s) to the query. This determines how the results of the query should be formatted.
Slice the results returned by the query, or access a specific index.
Link to this section Types
Link to this section Functions
Builds a GROQ query from the given structure.
The final query is returned as a string.
Builds a GROQ query from the given structure.
The final query is returned as a string, or an error is raised if the query could not be built.
Add a filter to the query. The filter retains documents for which the expression evaluates to true.
In a GROQ query, the Filter expression is the first expression in the query, and is wrapped in square brackets.
example
Example
filter(query, [ %{ "_type" => "'post'" }, [{ "_id" => "'id'" }, { "slug.current" => "'slug'" }, { :join => "||" }] ])
# => *[_type == 'post' && (_id == 'id' || slug.current == 'slug')]
Read more about filtering queries
Initialize the building of a GROQ query pipeline. Returns a SanityEx.Query struct representing a GROQ query.
options
Options
:include_drafts
- If set, the query will include draft documents.:base_query
- If set, the query will use this as its basis instead of*
.
Specify the ordering of the results of the query.
example
Example
order(query, "_createdAt desc")
# or, equivalently:
order(query, ["_createdAt", "desc"])
# => *[_type == 'post'] | order(_createdAt desc)
The examples above order the query results by the _createdAt
attribute in descending order.
Read more about ordering results
Adds object projection(s) to the query. This determines how the results of the query should be formatted.
example
Example
project(query, ["_id", ["'objectID'", "_id"], "_rev", "_type", "title", ... ])
# => *[_type == 'post']{_id, 'objectID':_id, _rev, _type, title, ... }
In order to Join certain attributes inside a projection, you can use a nested map with the :follow
atom present:
project(
query,
[
"_id",
%{
"'author'" => [
["'_id'", ["author", "_id", :follow]]
]
}
]
)
# => *[_type == 'post']{_id, 'author':{'_id':author->_id}}
Read more about object projections
@spec slice(t(), integer() | String.t() | {integer(), integer()}) :: {:error, String.t(), t()} | t()
Slice the results returned by the query, or access a specific index.
example
Example
slice(query, {0, 5})
# => *[_type == 'post'][0...5]
slice(query, 0)
# => *[_type == 'post'][0]
The first example above limits the query results to a maximum of 5 documents, starting at index 0. The second returns only the first index of the query results.
Read more about slicing query results