all
To get all documents in a collection, use the all
method on Collection
.
The method returns an array of Doc
instances:
await db.users.all();//=> Doc<User>[]
Subscription
Instead of awaiting the promise returned from all
, you can call on
on it to subscribe to the document updates:
db.users.all().on((users) => { // Doc<User>[]});
To catch errors, use catch
after calling on
:
db.users .all() .on((users) => { // ... }) .catch((error) => { //=> PERMISSION_DENIED: Missing or insufficient permissions });
To stop listening to the updates, call the off
function returned from the method:
const off = db.users.all().on((users) => { // ...});
// Unsubscribe after 5 secondssetTimeout(off, 5000);
→ Read more about subscribing to real-time updates
Options
as
You can tell Typesaurus that it’s safe to use dates by passing the as
option ("server" | "client"
):
const [serverUser] = await db.users.all({ as: "server" });serverUser && serverUser.data.createdAt;//=> Date
const [clientUser] = await db.users.all({ as: "client" });clientUser && clientUser.data.createdAt;//=> Date | null
By default, Typesaurus uses the "client"
option.