Writing data
In this document, you’ll learn how to write data to your database with Typesaurus.
There are several methods that allow writing documents:
add
- adds a document with a random idset
- sets specific documentupdate
- updates specific documentupset
- updates or sets specific documentremove
- removes a document
add
To add a document with random id:
const userRef = await db.users.add({ name: "Sasha" });
userRef.id;//=> "04orIWcrs1F8bw8CcDUM"
set
To set a document:
await db.users.set(userId, { name: "Sasha" });
set
is also available on Ref
and Doc
:
const userRef = await db.users.add({ name: "Sasha" });
// Later:await userRef.set({ name: "Alexander" });
update
To update a document with a specific id:
await db.users.update(userId, { name: "Alex" });
If the document doesn’t exist, it will throw an error.
update
is also available on Ref
and Doc
:
const userRef = await db.users.add({ name: "Sasha" });
// Later:await userRef.update({ name: "Alexander" });
upset
To update or set a document:
await db.users.upset(userId, { name: "Alex" });
If the document doesn’t exist, it will be set.
upset
is also available on Ref
and Doc
:
const userRef = await db.users.add({ name: "Sasha" });
// Later:await userRef.upset({ name: "Alexander" });
remove
To remove a document:
await db.users.remove(userId);
remove
is also available on Ref
and Doc
:
const userRef = await db.users.add({ name: "Sasha" });
// Later:await userRef.remove();