Skip to content

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 id
  • set - sets specific document
  • update - updates specific document
  • upset - updates or sets specific document
  • remove - removes a document

add

To add a document with random id:

const userRef = await db.users.add({ name: "Sasha" });
userRef.id;
//=> "04orIWcrs1F8bw8CcDUM"

Read more about add

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" });

Read more about set

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" });

Read more about update

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" });

Read more about upset

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();

Read more about remove