Skip to content

Commit e86e607

Browse files
Mohammad Hunan ChughtaiMohammad Hunan Chughtaimongodben
authored
(DOCSP-26981): @realm/reactify - Change an Object Model - React Native (#2399)
## Pull Request Info ### Jira - https://jira.mongodb.org/browse/DOCSP-26981 ### Staged Changes - [Change an Object Model - React Native](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-26981-Change-A-ROM/sdk/react-native/realm-database/change-an-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. - [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]> Co-authored-by: Ben Perlmutter <[email protected]>
1 parent ff70e2f commit e86e607

13 files changed

+671
-77
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-person-schema
4+
class Person extends Realm.Object {
5+
static schema = {
6+
name: 'Person',
7+
properties: {
8+
name: 'string',
9+
age: 'int?',
10+
},
11+
};
12+
}
13+
// :snippet-end:
14+
export default Person;
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import Realm from 'realm';
2+
import {createRealmContext} from '@realm/react';
3+
4+
describe('Change an Object Model Tests', () => {
5+
it('should add a property to a schema', () => {
6+
// :snippet-start: add-a-property-to-schema
7+
// :replace-start: {
8+
// "terms": {
9+
// "MyPerson": "Person"
10+
// }
11+
// }
12+
class MyPerson extends Realm.Object {
13+
static schema = {
14+
name: 'MyPerson',
15+
properties: {
16+
firstName: 'string',
17+
lastName: 'string',
18+
// add a new property, 'age' to the schema
19+
age: 'int',
20+
},
21+
};
22+
}
23+
24+
const config = {
25+
schema: [MyPerson],
26+
// increment the 'schemaVersion', since 'age' has been added to the schema
27+
schemaVersion: 2,
28+
};
29+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
30+
createRealmContext(config);
31+
// :replace-end:
32+
// :snippet-end:
33+
});
34+
35+
it('should delete a property from a schema', () => {
36+
// :snippet-start: delete-a-property-from-a-schema
37+
// :replace-start: {
38+
// "terms": {
39+
// "MyPerson": "Person"
40+
// }
41+
// }
42+
class MyPerson extends Realm.Object {
43+
static schema = {
44+
name: 'MyPerson',
45+
properties: {
46+
firstName: 'string',
47+
age: 'int',
48+
},
49+
};
50+
}
51+
const config = {
52+
schema: [MyPerson],
53+
// increment the 'schemaVersion', since 'lastName' has been removed from the schema
54+
schemaVersion: 2,
55+
};
56+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
57+
createRealmContext(config);
58+
// :replace-end:
59+
// :snippet-end:
60+
});
61+
it('should rename a property', async () => {
62+
// :snippet-start: rename-a-property-of-a-schema
63+
// :replace-start: {
64+
// "terms": {
65+
// "MyPerson": "Person"
66+
// }
67+
// }
68+
class MyPerson extends Realm.Object {
69+
static schema = {
70+
name: 'MyPerson',
71+
properties: {
72+
// rename the 'firstName' and 'lastName' property to 'fullName' in the schema
73+
fullName: 'string',
74+
age: 'int',
75+
},
76+
};
77+
}
78+
79+
const config = {
80+
schema: [MyPerson],
81+
// increment the 'schemaVersion', since 'fullName' has replaced 'firstName' and 'lastName' in the schema
82+
schemaVersion: 2,
83+
migration: (oldRealm, newRealm) => {
84+
// only apply this change if upgrading to schemaVersion 2
85+
if (oldRealm.schemaVersion < 2) {
86+
const oldObjects = oldRealm.objects(MyPerson);
87+
const newObjects = newRealm.objects(MyPerson);
88+
// loop through all objects and set the fullName property in the new schema
89+
for (const objectIndex in oldObjects) {
90+
const oldObject = oldObjects[objectIndex];
91+
const newObject = newObjects[objectIndex];
92+
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
93+
}
94+
}
95+
},
96+
};
97+
// pass the configuration object with the updated 'schemaVersion' and 'migration' function to createRealmContext()
98+
createRealmContext(config);
99+
// :replace-end:
100+
// :snippet-end:
101+
});
102+
103+
it('should modify a property type', () => {
104+
// :snippet-start: modify-a-property-type
105+
// :replace-start: {
106+
// "terms": {
107+
// "MyTask": "Task"
108+
// }
109+
// }
110+
class MyTask extends Realm.Object {
111+
static schema = {
112+
name: 'MyTask',
113+
properties: {
114+
// update the data type of '_id' to be 'objectId' within the schema
115+
_id: 'objectId',
116+
name: 'string',
117+
priority: 'int?',
118+
progressMinutes: 'int?',
119+
assignee: 'Person?',
120+
},
121+
primaryKey: '_id',
122+
};
123+
}
124+
125+
const config = {
126+
schema: [MyTask],
127+
// increment the 'schemaVersion', since the property type of '_id'
128+
// has been modified
129+
schemaVersion: 2,
130+
migration: (oldRealm, newRealm) => {
131+
if (oldRealm.schemaVersion < 2) {
132+
const oldObjects = oldRealm.objects(MyTask);
133+
const newObjects = newRealm.objects(MyTask);
134+
// loop through all objects and set the _id property
135+
// in the new schema
136+
for (const objectIndex in oldObjects) {
137+
const oldObject = oldObjects[objectIndex];
138+
const newObject = newObjects[objectIndex];
139+
newObject._id = new Realm.BSON.ObjectId(oldObject._id);
140+
}
141+
}
142+
},
143+
};
144+
// pass the configuration object with the updated
145+
// 'schemaVersion' and 'migration' function to createRealmContext()
146+
createRealmContext(config);
147+
// :replace-end:
148+
// :snippet-end:
149+
});
150+
});
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-person-schema
5+
class Person extends Realm.Object<Person> {
6+
name!: string;
7+
age?: number;
8+
9+
static schema = {
10+
name: 'Person',
11+
properties: {
12+
name: 'string',
13+
age: 'int?',
14+
},
15+
};
16+
}
17+
// :snippet-end:
18+
export default Person;
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import Realm from 'realm';
2+
import {createRealmContext} from '@realm/react';
3+
import Person from '../Models/Person';
4+
5+
describe('Change an Object Model Tests', () => {
6+
it('should add a property to a schema', () => {
7+
// :snippet-start: add-a-property-to-schema
8+
// :replace-start: {
9+
// "terms": {
10+
// "MyPerson": "Person"
11+
// }
12+
// }
13+
class MyPerson extends Realm.Object<MyPerson> {
14+
firstName!: string;
15+
lastName!: string;
16+
age!: number;
17+
18+
static schema = {
19+
name: 'MyPerson',
20+
properties: {
21+
firstName: 'string',
22+
lastName: 'string',
23+
// add a new property, 'age' to the schema
24+
age: 'int',
25+
},
26+
};
27+
}
28+
29+
const config = {
30+
schema: [MyPerson],
31+
// increment the 'schemaVersion', since 'age' has been added to the schema
32+
schemaVersion: 2,
33+
};
34+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
35+
createRealmContext(config);
36+
// :replace-end:
37+
// :snippet-end:
38+
});
39+
40+
it('should delete a property from a schema', () => {
41+
// :snippet-start: delete-a-property-from-a-schema
42+
// :replace-start: {
43+
// "terms": {
44+
// "MyPerson": "Person"
45+
// }
46+
// }
47+
class MyPerson extends Realm.Object<MyPerson> {
48+
firstName!: string;
49+
age!: number;
50+
51+
static schema = {
52+
name: 'MyPerson',
53+
properties: {
54+
firstName: 'string',
55+
age: 'int',
56+
},
57+
};
58+
}
59+
const config = {
60+
schema: [MyPerson],
61+
// increment the 'schemaVersion', since 'lastName' has been removed from the schema
62+
schemaVersion: 2,
63+
};
64+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
65+
createRealmContext(config);
66+
// :replace-end:
67+
// :snippet-end:
68+
});
69+
it('should rename a property', async () => {
70+
// :snippet-start: rename-a-property-of-a-schema
71+
// :replace-start: {
72+
// "terms": {
73+
// "MyPerson": "Person"
74+
// }
75+
// }
76+
class MyPerson extends Realm.Object<MyPerson> {
77+
fullName!: string;
78+
age!: number;
79+
80+
static schema = {
81+
name: 'MyPerson',
82+
properties: {
83+
// rename the 'firstName' and 'lastName' property, to 'fullName' in the schema
84+
fullName: 'string',
85+
age: 'int',
86+
},
87+
};
88+
}
89+
90+
const config = {
91+
schema: [MyPerson],
92+
// increment the 'schemaVersion', since 'fullName' has replaced 'firstName' and 'lastName' in the schema
93+
schemaVersion: 2,
94+
migration: (oldRealm: Realm, newRealm: Realm) => {
95+
// only apply this change if upgrading to schemaVersion 2
96+
if (oldRealm.schemaVersion < 2) {
97+
const oldObjects = oldRealm.objects(MyPerson);
98+
const newObjects = newRealm.objects(MyPerson);
99+
// loop through all objects and set the fullName property in the new schema
100+
for (const objectIndex in oldObjects) {
101+
const oldObject = oldObjects[objectIndex];
102+
const newObject = newObjects[objectIndex];
103+
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
104+
}
105+
}
106+
},
107+
};
108+
// pass the configuration object with the updated 'schemaVersion' and 'migration' function to createRealmContext()
109+
createRealmContext(config);
110+
// :replace-end:
111+
// :snippet-end:
112+
});
113+
114+
it('should modify a property type', () => {
115+
// :snippet-start: modify-a-property-type
116+
// :replace-start: {
117+
// "terms": {
118+
// "MyTask": "Task"
119+
// }
120+
// }
121+
class MyTask extends Realm.Object<MyTask> {
122+
_id: Realm.BSON.ObjectId = new Realm.BSON.ObjectId();
123+
name!: string;
124+
priority?: number;
125+
progressMinutes?: number;
126+
assignee?: Person;
127+
128+
static schema = {
129+
name: 'MyTask',
130+
properties: {
131+
// update the data type of '_id' to be 'objectId' within the schema
132+
_id: 'objectId',
133+
name: 'string',
134+
priority: 'int?',
135+
progressMinutes: 'int?',
136+
assignee: 'Person?',
137+
},
138+
primaryKey: '_id',
139+
};
140+
}
141+
142+
const config = {
143+
schema: [MyTask],
144+
// increment the 'schemaVersion', since the property type of '_id'
145+
// has been modified
146+
schemaVersion: 2,
147+
migration: (oldRealm: Realm, newRealm: Realm) => {
148+
if (oldRealm.schemaVersion < 2) {
149+
const oldObjects = oldRealm.objects(MyTask);
150+
const newObjects = newRealm.objects(MyTask);
151+
// loop through all objects and set the _id property
152+
// in the new schema
153+
for (const objectIndex in oldObjects) {
154+
const oldObject = oldObjects[objectIndex];
155+
const newObject = newObjects[objectIndex];
156+
newObject._id = new Realm.BSON.ObjectId(oldObject._id);
157+
}
158+
}
159+
},
160+
};
161+
// pass the configuration object with the updated
162+
// 'schemaVersion' and 'migration' function to createRealmContext()
163+
createRealmContext(config);
164+
// :replace-end:
165+
// :snippet-end:
166+
});
167+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Person extends Realm.Object {
2+
static schema = {
3+
name: 'Person',
4+
properties: {
5+
firstName: 'string',
6+
lastName: 'string',
7+
// add a new property, 'age' to the schema
8+
age: 'int',
9+
},
10+
};
11+
}
12+
13+
const config = {
14+
schema: [Person],
15+
// increment the 'schemaVersion', since 'age' has been added to the schema
16+
schemaVersion: 2,
17+
};
18+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
19+
createRealmContext(config);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Person extends Realm.Object {
2+
static schema = {
3+
name: 'Person',
4+
properties: {
5+
firstName: 'string',
6+
age: 'int',
7+
},
8+
};
9+
}
10+
const config = {
11+
schema: [Person],
12+
// increment the 'schemaVersion', since 'lastName' has been removed from the schema
13+
schemaVersion: 2,
14+
};
15+
// pass the configuration object with the updated 'schemaVersion' to createRealmContext()
16+
createRealmContext(config);

0 commit comments

Comments
 (0)