Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 3f01592

Browse files
authored
(DOCSP-26989): @realm/reactify 'Index a Property' (#2451)
## Pull Request Info ### Jira - https://jira.mongodb.org/browse/DOCSP-26989 ### Staged Changes - [Index a Property](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-26989/sdk/react-native/realm-database/schemas/index/) ### 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)
1 parent 1003e38 commit 3f01592

File tree

6 files changed

+86
-16
lines changed

6 files changed

+86
-16
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Realm from 'realm';
2+
3+
// :snippet-start: js-book-schema
4+
class Book extends Realm.Object {
5+
static schema = {
6+
name: 'Book',
7+
properties: {
8+
name: { type: 'string', indexed: true },
9+
price: 'int?',
10+
},
11+
};
12+
}
13+
// :snippet-end:
14+
export default Book;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Realm from 'realm';
2+
3+
// TODO: Replace `static schema` with TS-first models + realm-babel-plugin (https://www.npmjs.com/package/@realm/babel-plugin) approach once realm-babel-plugin version 0.1.2 releases with bug fixes
4+
// :snippet-start: ts-book-schema
5+
class Book extends Realm.Object<Book> {
6+
name!: string;
7+
price?: number;
8+
9+
static schema = {
10+
name: 'Book',
11+
properties: {
12+
name: { type: 'string', indexed: true },
13+
price: 'int?',
14+
},
15+
};
16+
}
17+
// :snippet-end:
18+
export default Book;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Book extends Realm.Object {
2+
static schema = {
3+
name: 'Book',
4+
properties: {
5+
name: { type: 'string', indexed: true },
6+
price: 'int?',
7+
},
8+
};
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Book extends Realm.Object<Book> {
2+
name!: string;
3+
price?: number;
4+
5+
static schema = {
6+
name: 'Book',
7+
properties: {
8+
name: { type: 'string', indexed: true },
9+
price: 'int?',
10+
},
11+
};
12+
}

source/sdk/node/examples/define-a-realm-object-model.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ properties. To define an index for a given property, set ``indexed`` to
159159
.. note::
160160

161161
An **index** significantly increases the speed of certain read operations at
162-
the cost of slightly slower write times and additional storage and memory
162+
the cost of slightly slower write times and additional storage and memory
163163
overhead. Realm Database stores indexes on disk, which makes your realm files
164164
larger. Each index entry is a minimum of 12 bytes. The ordering of the index
165165
entries supports efficient equality matches and range-based query operations.

source/sdk/react-native/realm-database/schemas/index.txt

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,43 @@ Index a Property - React Native SDK
1515
Overview
1616
--------
1717

18-
Realm supports indexing for string, integer, boolean, ``Date``, ``UUID``, and ``ObjectId``
19-
properties. To define an index for a given property, set ``indexed`` to
20-
``true``.
18+
If you frequently run :ref:`read operations <react-native-read-operations>`
19+
based on a specific property, you can index the property to optimize
20+
performance. Realm supports indexing for string, integer, boolean, ``Date``,
21+
``UUID``, and ``ObjectId`` properties.
2122

2223
.. note::
2324

2425
An **index** significantly increases the speed of certain read operations at
25-
the cost of slightly slower write times and additional storage and memory
26+
the cost of slightly slower write times and additional storage and memory
2627
overhead. Realm Database stores indexes on disk, which makes your realm files
2728
larger. Each index entry is a minimum of 12 bytes. The ordering of the index
2829
entries supports efficient equality matches and range-based query operations.
29-
30-
It's best to only add indexes when optimizing the read performance for
31-
specific situations.
32-
33-
.. example::
34-
35-
The following ``Book`` object schema defines an index on the ``name``
36-
property.
30+
31+
Usage
32+
-----
33+
To index a given property, set the property's ``indexed`` field to ``true``.
34+
35+
Example
36+
~~~~~~~
37+
38+
In the following example of a ``Book`` class, we define an index on the ``name``
39+
property.
3740

38-
.. literalinclude:: /examples/Schemas/Indexed.js
39-
:language: javascript
40-
:emphasize-lines: 4
41+
.. tabs-realm-languages::
42+
43+
.. tab::
44+
:tabid: typescript
45+
46+
.. literalinclude:: /examples/generated/react-native/ts/Book.snippet.ts-book-schema.ts
47+
:language: typescript
48+
:emphasize-lines: 8
49+
:linenos:
50+
51+
.. tab::
52+
:tabid: javascript
53+
54+
.. literalinclude:: /examples/generated/react-native/js/Book.snippet.js-book-schema.js
55+
:language: javascript
56+
:emphasize-lines: 5
57+
:linenos:

0 commit comments

Comments
 (0)