@@ -104,7 +104,25 @@ Create an Embedded Object
104
104
~~~~~~~~~~~~~~~~~~~~~~~~~
105
105
106
106
To create an embedded object, assign an instance of the embedded object
107
- to a parent object's property:
107
+ to a parent object's property.
108
+
109
+ Example
110
+ ```````
111
+
112
+ In the following ``CreateContact`` example, we create a new ``Contact`` object
113
+ with an embedded ``Address`` object.
114
+
115
+ The ``CreateContact`` component does the following:
116
+
117
+ - Creates React `state <https://reactjs.org/docs/react-component.html#state>`__ variables that
118
+ represent the contact's name and address details.
119
+ - Gets access to an open realm instance by calling the ``useRealm()`` hook within the
120
+ component.
121
+ - Creates a component method ``submitContact()`` that performs a write transaction
122
+ to create a new ``Address`` embedded object and a new ``Contact`` parent object based
123
+ on the ``TextInput`` values for the contact's name and address details.
124
+ - Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
125
+ submit button that calls ``submitContact()``
108
126
109
127
.. tabs-realm-languages::
110
128
@@ -127,9 +145,22 @@ to a parent object's property:
127
145
Query a Collection on Embedded Object Properties
128
146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129
147
130
- Use dot notation to filter or sort a :ref:`collection
148
+ You can use dot notation to filter or sort a :ref:`collection
131
149
<react-native-results-collections>` of objects based on an embedded object
132
- property value:
150
+ property value.
151
+
152
+ Example
153
+ ```````
154
+
155
+ In the following ``ContactList`` example, we filter and query an embedded
156
+ ``Address`` object.
157
+
158
+ The ``ContactList`` component does the following:
159
+
160
+ - Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook.
161
+ - Filters for contacts with the name ``John Smith`` by passing :js-sdk:`collection.filtered()
162
+ <Realm.Collection.html#filtered>` on the query: ``"name == 'John Smith'"``.
163
+ - Retrieves the property value of the contact's street address by using dot notation.
133
164
134
165
.. tabs-realm-languages::
135
166
@@ -153,7 +184,27 @@ Update an Embedded Object Property
153
184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
185
155
186
To update a property in an embedded object, modify the property in a
156
- write transaction:
187
+ write transaction.
188
+
189
+ Example
190
+ ```````
191
+
192
+ In the following ``UpdateContact`` example, we update the ``street`` property for
193
+ an embedded ``Address`` object.
194
+
195
+ The ``UpdateContact`` component does the following:
196
+
197
+ - Creates a React `state <https://reactjs.org/docs/react-component.html#state>`__ variable
198
+ that represents the contact's new street address.
199
+ - Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook
200
+ and filters for the contact that matches the name passed into the component as a prop.
201
+ - Gets access to an opened realm instance by calling the ``useRealm()`` hook within the
202
+ component.
203
+ - Creates a component method ``updateStreet()`` that performs a write transaction and uses
204
+ dot notation to set the contact's street address to the value of the ``street`` state variable.
205
+ - Renders a ``TextInput`` that displays and changes the ``street`` state variable.
206
+ - Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
207
+ ``"Update Street Address"`` button that calls ``updateStreet()``.
157
208
158
209
.. tabs-realm-languages::
159
210
@@ -177,7 +228,26 @@ Overwrite an Embedded Object
177
228
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178
229
179
230
To overwrite an embedded object, reassign the embedded object property
180
- of a party to a new instance in a write transaction:
231
+ of a party to a new instance in a write transaction.
232
+
233
+ Example
234
+ ```````
235
+
236
+ In the following ``OverwriteContact`` example, we overwrite an embedded ``Address`` object.
237
+
238
+ The ``OverwriteContact`` component does the following:
239
+
240
+ - Creates React `state <https://reactjs.org/docs/react-component.html#state>`__ variables
241
+ that represent the contact's new address details.
242
+ - Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook
243
+ and filters for the contact that matches the name passed into the component as a prop.
244
+ - Gets access to an opened realm instance by calling the ``useRealm()`` hook within the
245
+ component.
246
+ - Creates a component method ``updateAddress()`` that performs a write transaction to
247
+ create a new ``Address`` object based on the ``TextInput`` values for the
248
+ contact's address details and update the ``Contact`` object with the new address.
249
+ - Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
250
+ ``"Overwrite Address"`` button that calls ``updateAddress()``.
181
251
182
252
.. tabs-realm-languages::
183
253
@@ -202,6 +272,22 @@ Delete an Embedded Object
202
272
Realm Uses Cascading Deletes for Embedded Objects. To delete an embedded object,
203
273
delete the embedded object's parent.
204
274
275
+ Example
276
+ ```````
277
+
278
+ In the following ``DeleteContact`` example, we delete an embedded object and its
279
+ parent object.
280
+
281
+ The ``DeleteContact`` component does the following:
282
+
283
+ - Performs a query for all contacts by passing the ``Contact` class to the ``useQuery`` hook.
284
+ - Gets access to an open realm instance by calling the ``useRealm()`` hook within the component.
285
+ - Creates a component method ``deleteContact()`` that performs a write transaction and calls
286
+ :js-sdk:`Realm.delete() <Realm.html#delete>` to remove the ``Contact`` object that matches
287
+ the name passed into the component as a prop.
288
+ - Add an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the "Delete Contact``
289
+ button that calls ``deleteContact()``.
290
+
205
291
.. tabs-realm-languages::
206
292
207
293
.. tab::
0 commit comments