many
To get documents from a collection by their ids, use the many
method on Collection
.
The method returns an array of Doc
or null
if particular document doesn’t exist:
await db.users.many([userId1, userId2]);//=> Array<null | Doc<User>>
Subscription
Instead of awaiting the promise returned from many
, you can call on
on it to subscribe to documents updates:
db.users.many([userId1, userId2]).on(([user1, user2]) => { user1; //=> null | Doc<User>});
To stop listening to the updates, call the off
function returned from the method:
const off = db.users.many([userId1, userId2]).on((users) => { // ...});
// Unsubscribe after 5 secondssetTimeout(off, 5000);
To catch errors, use catch
after calling on
:
db.users .many([userId1, userId2]) .on((users) => { // ... }) .catch((error) => { //=> PERMISSION_DENIED: Missing or insufficient permissions });
→ 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.many([userId1, userId2], { as: "server" });serverUser && serverUser.data.createdAt;//=> Date
const [clientUser] = await db.users.many([userId1, userId2], { as: "client" });clientUser && clientUser.data.createdAt;//=> Date | null
By default, Typesaurus uses the "client"
option.