Skip to content

Commit 23360db

Browse files
(DOCSP-26980) Realm Reactify "Define a Realm Object Model" page (#2538)
## Pull Request Info I didn't need to Bluehawk anything new here. Just used existing examples and adjusted surrounding text to fit the `@realm/react` context. The page has IA/structure issues that we should address later on. ### Jira - https://jira.mongodb.org/browse/DOCSP-26980 ### Staged Changes - [Define a Realm Object Model](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-26980/sdk/react-native/realm-database/define-a-realm-object-model/) ### 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) ### Animal wearing a hat ![](https://64.media.tumblr.com/3dedcb3ca02d3c1dd61858594d391862/tumblr_inline_p36xj4ezFy1rzifqz_500.png) --------- Co-authored-by: Ben Perlmutter <[email protected]>
1 parent 0a6ff41 commit 23360db

File tree

1 file changed

+28
-68
lines changed

1 file changed

+28
-68
lines changed

source/sdk/react-native/realm-database/define-a-realm-object-model.txt

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ Define a Realm Object Model - React Native SDK
1111
:depth: 2
1212
:class: singlecol
1313

14-
1514
.. _react-native-object-types:
1615
.. _react-native-object-schemas:
1716
.. _react-native-object-models:
1817

1918
Key Concept: Object Types & Schemas
2019
-----------------------------------
2120

22-
Every Realm object conforms to a specific **object type**, which is essentially
23-
a class that defines the properties and :ref:`relationships <react-native-relationships>`
21+
Every Realm object conforms to a specific **object type**. Object types are
22+
classes you define that contain the properties and :ref:`relationships <react-native-relationships>`
2423
for objects of that type using a pre-defined schema. Realm guarantees that all
2524
objects in a realm conform to the schema for their object type and validates
2625
objects whenever they're created, modified, or deleted.
@@ -45,81 +44,42 @@ If a realm already contains data when you open it, Realm Database
4544
validates each object to ensure that an object schema was provided for its type
4645
and that it meets all of the constraints specified in the schema.
4746

48-
.. example::
49-
50-
A realm that contains basic data about books in libraries might use a
51-
schema like the following:
52-
53-
.. code-block:: json
54-
55-
[
56-
{
57-
"type": "Library",
58-
"properties": {
59-
"address": "string",
60-
"books": "Book[]"
61-
}
62-
},
63-
{
64-
"type": "Book",
65-
"primaryKey": "isbn",
66-
"properties": {
67-
"isbn": "string",
68-
"title": "string",
69-
"author": "string",
70-
"numberOwned": { "type": "int?", "default": 0 },
71-
"numberLoaned": { "type": "int?", "default": 0 }
72-
}
73-
}
74-
]
47+
Using ``@realm/react``, you define a realm schema by passing individual object
48+
schemas to ``<RealmProvider>`` or ``createRealmContext()``.
7549

50+
.. literalinclude:: /examples/generated/react-native/js/RealmConfig.snippet.create-realm-context.js
51+
:language: javascript
52+
:emphasize-lines: 8
7653

7754
.. _react-native-define-a-new-object-type:
7855

79-
Define a New Object Type
80-
------------------------
56+
Define an Object Type
57+
---------------------
8158

82-
To define a Realm object type, create a schema object that specifies the type's
83-
``name`` and ``properties``. The type name must be unique among object types in
84-
a realm.
85-
86-
.. code-block:: javascript
87-
88-
const CarSchema = {
89-
name: "Car",
90-
properties: {
91-
make: "string",
92-
model: "string",
93-
miles: "int",
94-
}
95-
}
96-
97-
You can then :ref:`open a realm <node-open-a-local-realm>` using the Realm
98-
Object you have defined.
99-
100-
.. literalinclude:: /examples/generated/node/open-and-close-a-local-realm.snippet.open-local-realm-with-car-schema.js
101-
:language: javascript
59+
To define a Realm object type, create a class that extends ``Realm.Object``.
60+
Define the type's ``name`` and ``properties`` in a static property called ``schema``.
61+
The type's name must be unique among object types in a realm.
10262

103-
.. note::
104-
105-
To learn the various types that a Realm Object Model accepts, how to specify optional fields, define relationships, embedded objects, primary keys, and more, check out the :ref:`Schemas <react-native-schemas-overview>` section.
63+
.. tabs-realm-languages::
10664

65+
.. tab::
66+
:tabid: typescript
10767

68+
.. literalinclude:: /examples/generated/react-native/ts/Book.snippet.ts-book-schema.ts
69+
:language: javascript
10870

109-
.. _react-native-define-objects-with-js-classes:
71+
.. tab::
72+
:tabid: javascript
11073

111-
Define Realm Object Types with JavaScript Classes
112-
-------------------------------------------------
74+
.. literalinclude:: /examples/generated/react-native/js/Book.snippet.js-book-schema.js
75+
:language: javascript
11376

114-
You can define Realm object types with JavaScript classes. To use a class as an
115-
object type, define the object schema on the static property ``schema``.
77+
Then you can pass the class itself to the schema property of the :js-sdk:`Realm.Configuration
78+
<Realm.html#~Configuration>` object when opening a realm.
11679

117-
.. literalinclude:: /examples/generated/node/define-a-realm-object-schema.snippet.define-a-realm-object-schema-define-js-classes.js
118-
:language: javascript
80+
.. note::
11981

120-
Pass the class itself to the schema property of the :js-sdk:`Realm.Configuration
121-
<Realm.html#~Configuration>` object when opening a realm. You can then :doc:`read and write
122-
</sdk/node/examples/read-and-write-data>` normally.
123-
124-
.. literalinclude:: /examples/generated/node/define-a-realm-object-schema.snippet.define-a-realm-object-schema-js-classes-open-and-access-properties.js
125-
:language: javascript
82+
To learn the various types that a Realm Object Model accepts, how to specify
83+
optional fields, define relationships, embedded objects, primary keys, and more,
84+
check out the :ref:`Schemas <react-native-schemas-overview>` section.
85+

0 commit comments

Comments
 (0)