Docs Menu

Docs HomeRealm

Write Transactions - Node.js SDK

On this page

  • Transaction Lifecycle

A write transaction is a function that modifies objects in a realm. Write transactions let you create, modify, or delete Realm objects. They handle operations in a single, idempotent update. A transaction is all or nothing. Either:

  • All the operations in the transaction succeed, or;

  • If any operation fails, none of the operations complete.

Important

Every write operation must occur in a write transaction.

Example

Write transactions are callback functions that you pass to a realm instance. For examples of specific write operations, see Write Operations.

const realm = await Realm.open();
realm.write(() => {
const tesla = realm.create("Car", {
make: "Tesla",
model: "Model S",
year: 2020,
miles: 12000
})
const honda = realm.create("Car", {
make: "Honda",
model: "Civic",
year: 2018,
miles: 30000
})
})

A given realm only processes one write transaction at a time. When you make a write transaction, the realm adds the transaction to a queue. The realm evaluates each transaction in the order it arrived.

After processing a transaction, Realm Database either commits it or cancels it:

  • After a commit, the realm applies all operations in the transaction. Once applied, the realm automatically updates live queries. It notifies listeners of created, modified, and deleted objects.

    Note

    When using Sync, the SDK also queues the changes to send to Atlas App Services. The SDK sends these changes when a network is available.

  • Realm Database does not apply any operations in a cancelled transaction. Realm Database cancels a transaction if an operation fails or is invalid.

←  Live Queries - Node.js SDKRelationships & Embedded Objects - Node.js SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.