Ref
The Ref
class represents a reference to a document in the database. It provides methods to read and write the document.
Refs get returned from writing operations, it can be accessed through doc or created by a collection:
import { schema } from "typesaurus";
const db = schema({ users: collection<User>("users"), posts: collection<Post>("posts"),});
const ref = await db.users.add({ name: "Sasha" });//=> Ref<User>
ref.id;//=> "00sHm46UWKObv2W7XK9e" (Id<"users">)
await ref.update({ name: "Alexander" });
id
The property contains the document id:
ref.id;//=> "00sHm46UWKObv2W7XK9e" (Id<"users">)
get
The method allows to get the document:
await ref.get();//=> null | Doc<User>
→ Read more about the get
method
set
The method allows to set the document:
await ref.set({ name: "Sasha" });
→ Read more about the set
method
update
The method allows to update the document:
await ref.update({ name: "Alexander" });
→ Read more about the update
method
upset
The method allows to update the document if it exists or set it if it doesn’t:
await ref.upset({ name: "Alexander" });
→ Read more about the upset
method
remove
The method allows to remove the document:
await ref.remove();
→ Read more about the remove
method
as
⚠️ Available starting with v10.3.0
The method resolves Typesaurus.SharedRef
if the model extends the given type. Otherwise, it resolves unknown
preventing the usage of incompatible models:
rename(ref.as<NameFields>());
It allows sharing functionality across the db entities in a type-safe way.