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

@type t() :: %SanityEx.Query{
  base_query: String.t(),
  filters: list(),
  order: list(),
  projections: list(),
  slice: nil | integer() | String.t() | {integer(), integer()}
}

Link to this section Functions

Link to this function

build(arg1)

View Source (since 0.1.0)
@spec build(t()) :: {:error, String.t(), t()} | String.t()

Builds a GROQ query from the given structure.

The final query is returned as a string.

Link to this function

build!(query)

View Source (since 0.1.0)
@spec build!(t()) :: String.t()

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.

Link to this function

filter(template, filter)

View Source (since 0.1.0)
@spec filter(t(), maybe_improper_list() | map()) :: {:error, String.t(), t()} | t()

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

Link to this function

new(opts \\ [])

View Source (since 0.1.0)
@spec new(Keyword.t()) :: t()

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 *.
Link to this function

order(template, order)

View Source (since 0.1.0)
@spec order(t(), integer() | String.t() | maybe_improper_list()) ::
  {:error, String.t(), t()} | t()

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

Link to this function

project(template, projections)

View Source (since 0.1.0)
@spec project(t(), any()) :: {:error, String.t(), t()} | t()

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

Link to this function

slice(template, slice)

View Source (since 0.1.0)
@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