PPlusFireStore.Query (pplus_firestore v0.1.4)
View SourceCreate Firestore queries using Elixir syntax.
The operations supported by Firestore are:
==
(EQUALS)!=
(NOT_EQUALS)>
(GREATER_THAN)<
(LESS_THAN)>=
(GREATER_THAN_OR_EQUAL)<=
(LESS_THAN_OR_EQUAL)in
(IN)not in
(NOT_IN)contains
(ARRAY_CONTAINS)contains_any
(ARRAY_CONTAINS_ANY)is_nil
(IS_NULL)
Example
Get from "books" filtered by author and ordered by author name in ascending order:
from("books")
|> where("author" == "John Doe")
|> order_by("author", :asc)
|> limit(5)
|> offset(10)
NOTE: Although it is possible to paginate with limit
and offset
, the query result does not inform the total number of documents in the collection or if there is a next page.
Get from "books" filtered by author and ordered by author name in ascending order:
from("books")
|> where("author" in ["John Doe", "Alice Smith", "Bob Brown"])
|> order_by("author", :asc)
When a field is an array, you can use the contains
operator to filter by the array content:
from("books")
|> where(contains("tags", "fiction") and "pages" > 300)
You can also use the contains_any
operator to filter by any of the values in the array:
from("books")
|> where(contains_any("tags", ["fiction", "non-fiction"]))
You can also pass the values dynamically:
collection = "books"
field = "pages"
value = 300
query =
from(collection)
|> where(field > value)
Summary
Functions
Create a query from a collection with options
Define quantity of documents to return
Define the offset of the query
Add an OR
filter to the query
Orders the query by one or more fields in ascending or descending order
Selects the fields to return in the query
Add a filter to the query
Functions
@spec from(collection :: String.t(), opts :: Keyword.t()) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Create a query from a collection with options
Example
from("books", where: "author" == "John Doe", limit: 5, order_by: {"author", :asc})
from("books", all_descendants: true)
@spec limit( GoogleApi.Firestore.V1.Model.StructuredQuery.t(), limit :: non_neg_integer() ) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Define quantity of documents to return
Example
from("books")
|> limit(5)
@spec offset( GoogleApi.Firestore.V1.Model.StructuredQuery.t(), offset :: non_neg_integer() ) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Define the offset of the query
Example
from("books")
|> limit(5)
|> offset(10)
@spec or_where( GoogleApi.Firestore.V1.Model.StructuredQuery.t(), expression :: Macro.t() ) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Add an OR
filter to the query
Example
from("books")
|> where("author" == "John Doe" and "pages" > 300)
|> or_where("author" == "Alice Smith" and "pages" <= 200)
Essa consulta é equivalente a:
SELECT * FROM books
WHERE (author = "John Doe" AND pages > 300) OR (author = "Alice Smith" AND pages <= 200)
Assim como where, você pode encadear vários or_where.
from("books")
|> where("author" == "John Doe" and "pages" > 300)
|> or_where("author" == "Alice Smith" and "pages" <= 200)
|> or_where("author" == "Bob Brown" and "metadata.year" <= 2021)
|> or_where("author" == "Charlie Brown" and "metadata.publisher" == "Publisher A")
@spec order_by( GoogleApi.Firestore.V1.Model.StructuredQuery.t(), field :: String.t() | [String.t()], direction :: :asc | :desc ) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Orders the query by one or more fields in ascending or descending order
Example
from("books")
|> order_by("author", :asc)
from("books")
|> order_by(["author", "metadata.year"], :desc)
@spec select(GoogleApi.Firestore.V1.Model.StructuredQuery.t(), fields :: [String.t()]) :: GoogleApi.Firestore.V1.Model.StructuredQuery.t()
Selects the fields to return in the query
Example
from("books")
|> select(["author", "metadata.publisher"])
Add a filter to the query
Example
from("books")
|> where("author" == "John Doe")
from("books")
|> where("author" == "John Doe" and "pages" > 300)
Multiple chained filters are equivalent to an AND
.
from("books")
|> where("author" in ["John Doe", "Alice Smith", "Bob Brown"])
|> where("pages" > 300)
|> where("metadata.year" <= 2021)