Queries
Dogs offers a few static methods on the FirestoreEntity
class to query the database. These methods
are get
, find
and query
. They are all asynchronous and return a Future
of the requested
entity/entities.
Get
The get
method is used to retrieve a single entity from the database. It takes the id of the
entity as an argument and returns a Future
of the requested entity. You can additionally specify
a orCreate
argument, that takes a supplier function as an argument. If the entity is not found in
the database, the supplier function will be called and the supplied entity will be stored in the
database and returned.
var town = await FirestoreEntity.get<Town>("town_id_here",
orCreate: () => Town("London", "UK"));
);
Find
The find
method is used to retrieve a single entity from the database. It takes a Query
as an
optional argument and returns a Future
of the requested entity. If no Query
is specified, the
first entity of the collection will be returned.
var town = await FirestoreEntity.find<Town>(
query: (q) => q.where("name", isEqualTo: "London"),
);
Query
The query
method is used to retrieve a list of entities from the database. It takes a Query
as
an optional argument and returns a Future
of the requested entities. If no Query
is specified,
all entities of the collection are included in the query. To use pagination, you can specify a
limit using the 'Query.limit' method. To set cursors, use the cursor methods on the query
method
itself, since they also take firestore entities as arguments.