Doc
The Doc
class represents a document in the database. It provides methods to read and write the document.
Docs get returned from reading operations or created by a collection:
import { schema } from "typesaurus";
const db = schema({ users: collection<User>("users"), posts: collection<Post>("posts"),});
const doc = await db.users.get(db.users.id("00sHm46UWKObv2W7XK9e"));//=> Doc<User>
doc.ref.id;//=> "00sHm46UWKObv2W7XK9e" (Id<"users">)
await doc.ref.update({ name: "Alexander" });
ref
The property contains the references to the document:
doc.ref;//=> Ref<User>
→ Read more about the Ref
class
data
The property contains the document data:
doc.data;//=> User
get
The method allows to get the document:
await doc.get();//=> Doc<User>
→ Read more about the get
method
set
The method allows to set the document:
await doc.set({ name: "Sasha" });
→ Read more about the set
method
update
The method allows to update the document:
await doc.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 doc.upset({ name: "Alexander" });
→ Read more about the upset
method
remove
The method allows to remove the document:
await doc.remove();
→ Read more about the remove
method
narrow
The method narrows the variable model doc type:
const ghAccount = account.narrow<GitHubAccount>( (data) => data.type === "github" && data,);//=> Doc<GitHubAccount> | undefined
It checks the data structure on the runtime and asserts the type.
→ Read more about variable models
→ Read more about the narrow
method
as
⚠️ Available starting with v10.3.0
The method resolves Typesaurus.SharedDoc
if the model extends the given type. Otherwise, it resolves unknown
preventing the usage of incompatible models:
rename(doc.as<NameFields>());
It allows sharing functionality across the db entities in a type-safe way.