Skip to content

Commit 9979616

Browse files
(DOCSP-26980) Realm Reactify "Define a Realm Object Model" page (#2538)
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. - https://jira.mongodb.org/browse/DOCSP-26980 - [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/) 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 [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) ![](https://64.media.tumblr.com/3dedcb3ca02d3c1dd61858594d391862/tumblr_inline_p36xj4ezFy1rzifqz_500.png) --------- Co-authored-by: Ben Perlmutter <[email protected]>
1 parent 21ee708 commit 9979616

File tree

1 file changed

+35
-77
lines changed

1 file changed

+35
-77
lines changed

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

Lines changed: 35 additions & 77 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,40 @@ 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-
------------------------
81-
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-
.. include:: /includes/note-class-char-limit.rst
98-
99-
You can then :ref:`open a realm <node-open-a-local-realm>` using the Realm
100-
Object you have defined.
101-
102-
.. literalinclude:: /examples/generated/node/open-and-close-a-local-realm.snippet.open-local-realm-with-car-schema.js
103-
:language: javascript
104-
105-
.. note::
106-
107-
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.
108-
109-
.. _react-native-define-objects-with-js-classes:
110-
111-
Define Realm Object Types with JavaScript Classes
112-
-------------------------------------------------
113-
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``.
116-
117-
.. literalinclude:: /examples/generated/node/define-a-realm-object-schema.snippet.define-a-realm-object-schema-define-js-classes.js
118-
:language: javascript
119-
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
56+
Define an Object Type
57+
---------------------
58+
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.
62+
63+
.. tabs-realm-languages::
64+
65+
.. tab::
66+
:tabid: typescript
67+
68+
.. literalinclude:: /examples/generated/react-native/ts/Book.snippet.ts-book-schema.ts
69+
:language: javascript
70+
71+
.. tab::
72+
:tabid: javascript
73+
74+
.. literalinclude:: /examples/generated/react-native/js/Book.snippet.js-book-schema.js
75+
:language: javascript
76+
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.
79+
80+
To learn the various types that a Realm Object Model accepts, how to specify
81+
optional fields, define relationships, embedded objects, primary keys, and more,
82+
check out the :ref:`Schemas <react-native-schemas-overview>` section.
83+

0 commit comments

Comments
 (0)