Skip to content

Commit 92ddbeb

Browse files
krollins-mdbmongodben
authored andcommitted
(DOCSP-26996) Add Realm React support to 'Write Transactions' (mongodb#2560)
## Pull Request Info I took this opportunity to bring this page's content in line with the Flutter SDK, as the existing content is hard to follow and overly explanatory. ### Jira - https://jira.mongodb.org/browse/DOCSP-26996 ### Staged Changes - [Write Transactions](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-26996/sdk/react-native/realm-database/write-transactions/) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
1 parent 23360db commit 92ddbeb

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

source/sdk/react-native/realm-database/write-transactions.txt

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,19 @@
55
Write Transactions - React Native SDK
66
=====================================
77

8-
.. contents:: On this page
9-
:local:
10-
:backlinks: none
11-
:depth: 2
12-
:class: singlecol
8+
Within a ``<RealmProvider>``, you can access a realm with
9+
the ``useRealm()`` hook. Then, you can create Realm objects
10+
using a :js-sdk:`Realm.write() <Realm.html#write>` transaction block.
1311

14-
A write transaction is a function that modifies objects in a realm. Write
15-
transactions let you create, modify, or delete Realm objects. They handle
16-
operations in a single, idempotent update. A transaction is
17-
*all or nothing*. Either:
12+
All operations within a write transaction are :wikipedia:`atomic <Atomicity_(database_systems)>`.
13+
If an operation in the write transaction fails, the whole transaction fails,
14+
Realm throws an error, and no changes from the transaction block are applied to the realm.
1815

19-
- All the operations in the transaction succeed, or;
20-
- If any operation fails, none of the operations complete.
16+
Every write operation must occur in a write transaction.
2117

22-
.. important::
23-
24-
Every write operation must occur in a write transaction.
25-
26-
.. example::
27-
28-
Write transactions are callback functions that you pass to a realm
29-
instance. For examples of specific write operations, see
30-
:ref:`react-native-create-objects` or :ref:`react-native-update-objects`.
31-
32-
.. code-block:: javascript
33-
34-
const realm = await Realm.open();
35-
realm.write(() => {
36-
const tesla = realm.create("Car", {
37-
make: "Tesla",
38-
model: "Model S",
39-
year: 2020,
40-
miles: 12000
41-
})
42-
const honda = realm.create("Car", {
43-
make: "Honda",
44-
model: "Civic",
45-
year: 2018,
46-
miles: 30000
47-
})
48-
})
18+
.. literalinclude:: /examples/generated/react-native/ts/create-test.snippet.crud-create-object.tsx
19+
:language: typescript
20+
:emphasize-lines: 6-8
4921

5022
Transaction Lifecycle
5123
---------------------
@@ -61,12 +33,13 @@ After processing a transaction, Realm Database either **commits** it or
6133
Once applied, the realm automatically updates :ref:`live queries
6234
<react-native-live-queries>`. It notifies listeners of created, modified,
6335
and deleted objects.
64-
65-
.. note::
6636

67-
When using :ref:`Sync <sync>`, the SDK also queues
68-
the changes to send to Atlas App Services. The SDK sends
69-
these changes when a network is available.
37+
- When using :ref:`Sync <sync>`, the SDK also queues
38+
the changes to send to Atlas App Services. The SDK sends
39+
these changes when a network is available.
40+
41+
- After a commit, objects and collections returned by ``useQuery`` or
42+
``useObject`` rerender to include relevant changes.
7043

7144
- Realm Database does not apply any operations in a **cancelled**
7245
transaction. Realm Database cancels a transaction if an operation

0 commit comments

Comments
 (0)