Skip to content

Commit e31bc4d

Browse files
committed
Update example descriptions
1 parent 5e5245d commit e31bc4d

File tree

6 files changed

+99
-12
lines changed

6 files changed

+99
-12
lines changed

examples/react-native/__tests__/js/Models/Business.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Business extends Realm.Object {
88
properties: {
99
_id: "objectId",
1010
name: "string",
11-
addresses: { type: "list?", objectType: "Address" }, // Embed an array of objects
11+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
1212
},
1313
};
1414
}

examples/react-native/__tests__/ts/Models/Business.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import Address from './Address';
66
class Business extends Realm.Object {
77
_id!: string;
88
name!: string;
9-
addresses?: Realm.List<Address>;
9+
addresses!: Realm.List<Address>;
1010
static schema = {
1111
name: "Business",
1212
primaryKey: "_id",
1313
properties: {
1414
_id: "objectId",
1515
name: "string",
16-
addresses: { type: "list?", objectType: "Address" }, // Embed an array of objects
16+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
1717
},
1818
};
1919
}

source/examples/generated/react-native/js/Business.snippet.js-business-schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Business extends Realm.Object {
55
properties: {
66
_id: "objectId",
77
name: "string",
8-
addresses: { type: "list?", objectType: "Address" }, // Embed an array of objects
8+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
99
},
1010
};
1111
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Business extends Realm.Object {
22
_id!: string;
33
name!: string;
4-
addresses?: Realm.List<Address>;
4+
addresses!: Realm.List<Address>;
55

66
static schema = {
77
name: "Business",
88
primaryKey: "_id",
99
properties: {
1010
_id: "objectId",
1111
name: "string",
12-
addresses: { type: "list?", objectType: "Address" }, // Embed an array of objects
12+
addresses: { type: "list", objectType: "Address" }, // Embed an array of objects
1313
},
1414
};
1515
}

source/examples/generated/react-native/ts/embedded-objects-test.snippet.update-embedded-object.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Find the contact you want to update
22
const UpdateContact = ({contactName}: {contactName: string}) => {
3-
const contact = useQuery(Contact).filtered(`name == '${contactName}'`)[0];
43
const [street, setStreet] = useState('');
4+
const contact = useQuery(Contact).filtered(`name == '${contactName}'`)[0];
55
const realm = useRealm();
66

77
const updateStreet = () => {

source/sdk/react-native/realm-database/schemas/embedded-objects.txt

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,25 @@ Create an Embedded Object
104104
~~~~~~~~~~~~~~~~~~~~~~~~~
105105

106106
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 ``Contact`` parent object based
123+
on the ``TextInput`` values for the contact's name and address.
124+
- Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
125+
``'Submit Contact'`` button that calls ``submitContact()``.
108126

109127
.. tabs-realm-languages::
110128

@@ -127,9 +145,22 @@ to a parent object's property:
127145
Query a Collection on Embedded Object Properties
128146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129147

130-
Use dot notation to filter or sort a :ref:`collection
148+
You can use dot notation to filter or sort a :ref:`collection
131149
<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 contact's street address by using dot notation.
133164

134165
.. tabs-realm-languages::
135166

@@ -153,7 +184,27 @@ Update an Embedded Object Property
153184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154185

155186
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
204+
sets 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()``.
157208

158209
.. tabs-realm-languages::
159210

@@ -177,7 +228,27 @@ Overwrite an Embedded Object
177228
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178229

179230
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.
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 and
247+
creates a new ``Address`` object that overwrites the existing address in the ``Contact`` object.
248+
- Renders ``TextInput`` components that display and change the state variables for the
249+
new address.
250+
- Adds an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the
251+
``'Overwrite Address'`` button that calls ``updateAddress()``.
181252

182253
.. tabs-realm-languages::
183254

@@ -202,6 +273,22 @@ Delete an Embedded Object
202273
Realm Uses Cascading Deletes for Embedded Objects. To delete an embedded object,
203274
delete the embedded object's parent.
204275

276+
Example
277+
```````
278+
279+
In the following ``DeleteContact`` example, we delete an embedded object and its
280+
parent object.
281+
282+
The ``DeleteContact`` component does the following:
283+
284+
- Performs a query for all contacts by passing the ``Contact`` class to the ``useQuery`` hook.
285+
- Gets access to an open realm instance by calling the ``useRealm()`` hook within the component.
286+
- Creates a component method ``deleteContact()`` that performs a write transaction and calls
287+
:js-sdk:`Realm.delete() <Realm.html#delete>` to remove the ``Contact`` object that matches
288+
the name passed into the component as a prop.
289+
- Add an `onPress <https://reactnative.dev/docs/handling-touches>`__ event on the ``'Delete Contact'``
290+
button that calls ``deleteContact()``.
291+
205292
.. tabs-realm-languages::
206293

207294
.. tab::

0 commit comments

Comments
 (0)