Skip to content

Commit 7992990

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtai
andauthored
(DOCSP-26991): @realm-react-ify 'Remap a Property' (#2453)
## Pull Request Info ### Jira - https://jira.mongodb.org/browse/DOCSP-26991 ### Staged Changes - [Remap a Property - React Native](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/docsp-26991-remap-a-property/sdk/react-native/realm-database/schemas/remap/) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [N/A] Create Jira ticket for corresponding docs-app-services update(s), if any - [N/A] Checked/updated Admin API - [N/A] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) Co-authored-by: Mohammad Hunan Chughtai <[email protected]>
1 parent 124d3fc commit 7992990

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Realm from 'realm';
2+
3+
// :snippet-start: js-employee-schema
4+
class Employee extends Realm.Object {
5+
static schema = {
6+
name: 'Employee',
7+
properties: {
8+
_id: 'string',
9+
first_name: {type: 'string', mapTo: 'firstName'},
10+
},
11+
primaryKey: '_id',
12+
};
13+
}
14+
// :snippet-end:
15+
16+
export default Employee;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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-employee-schema
5+
class Employee extends Realm.Object {
6+
_id!: string;
7+
first_name!: string;
8+
9+
static schema = {
10+
name: 'Employee',
11+
properties: {
12+
_id: 'string',
13+
first_name: {type: 'string', mapTo: 'firstName'},
14+
},
15+
primaryKey: '_id',
16+
};
17+
}
18+
// :snippet-end:
19+
20+
export default Employee;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Employee extends Realm.Object {
2+
static schema = {
3+
name: 'Employee',
4+
properties: {
5+
_id: 'string',
6+
first_name: {type: 'string', mapTo: 'firstName'},
7+
},
8+
primaryKey: '_id',
9+
};
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Employee extends Realm.Object {
2+
_id!: string;
3+
first_name!: string;
4+
5+
static schema = {
6+
name: 'Employee',
7+
properties: {
8+
_id: 'string',
9+
first_name: {type: 'string', mapTo: 'firstName'},
10+
},
11+
primaryKey: '_id',
12+
};
13+
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ To use a different property name in your code than is stored in
1818
Realm Database, set ``mapTo`` to the name of the property as it appears in
1919
your code.
2020

21-
.. example::
22-
23-
A developer opens a realm using the following ``Dog`` object schema.
24-
Realm Database stores each dog's first name with the snake case
25-
``first_name`` property. The schema maps the property to ``firstName`` for
26-
objects used in JavaScript code.
27-
28-
.. literalinclude:: /examples/Schemas/Mapped.js
29-
:language: javascript
30-
:emphasize-lines: 5
21+
Example
22+
~~~~~~~
23+
24+
In the following example of an ``Employee`` class, we remap the ``first_name``
25+
property to ``firstName``.
26+
27+
.. tabs-realm-languages::
28+
29+
.. tab::
30+
:tabid: typescript
31+
32+
.. literalinclude:: /examples/generated/react-native/ts/Employee.snippet.ts-employee-schema.ts
33+
:language: typescript
34+
:emphasize-lines: 3, 9
35+
:linenos:
36+
37+
38+
.. tab::
39+
:tabid: javascript
40+
41+
.. literalinclude:: /examples/generated/react-native/js/Employee.snippet.js-employee-schema.js
42+
:language: javascript
43+
:emphasize-lines: 6
44+
:linenos:

0 commit comments

Comments
 (0)