@@ -11,16 +11,15 @@ Define a Realm Object Model - React Native SDK
11
11
:depth: 2
12
12
:class: singlecol
13
13
14
-
15
14
.. _react-native-object-types:
16
15
.. _react-native-object-schemas:
17
16
.. _react-native-object-models:
18
17
19
18
Key Concept: Object Types & Schemas
20
19
-----------------------------------
21
20
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>`
24
23
for objects of that type using a pre-defined schema. Realm guarantees that all
25
24
objects in a realm conform to the schema for their object type and validates
26
25
objects whenever they're created, modified, or deleted.
@@ -45,81 +44,40 @@ If a realm already contains data when you open it, Realm Database
45
44
validates each object to ensure that an object schema was provided for its type
46
45
and that it meets all of the constraints specified in the schema.
47
46
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()``.
75
49
50
+ .. literalinclude:: /examples/generated/react-native/js/RealmConfig.snippet.create-realm-context.js
51
+ :language: javascript
52
+ :emphasize-lines: 8
76
53
77
54
.. _react-native-define-a-new-object-type:
78
55
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