-
Notifications
You must be signed in to change notification settings - Fork 88
(DOCSP-26981): @realm/reactify - Change an Object Model - React Native #2399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mohammadhunan-dev
merged 20 commits into
mongodb:realm-react-guidance
from
mohammadhunan-dev:DOCSP-26981-Change-A-ROM
Jan 4, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1a5396e
added change an object model tests
d057a17
added literal includes to add a prop
e009254
literal included generated files
1f181d0
fix rst on tabs
a8c12ee
added more comments
cc0136a
wording improvements
d1c38de
fix line emphasis
6d1d35c
wording improvements
13661ce
remove mention of opening a realm
4e9884a
grammar fixes
5de8ce2
address Pr comments
4e9742f
fix line emphasis + wording improvements
f4cf94c
added comment
389c37b
Update source/sdk/react-native/realm-database/change-an-object-model.txt
c37efd0
removed unneeded link
eee06a2
change RealmConfiguration -> Configuration
d2f1d0f
changed 'the developer' -> 'you'
a143735
Grammar update
d6ee8ab
Typo fix
019253c
2nd person imperative
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Realm from 'realm'; | ||
|
||
// :snippet-start: js-person-schema | ||
class Person extends Realm.Object { | ||
static schema = { | ||
name: 'Person', | ||
properties: { | ||
name: 'string', | ||
age: 'int?', | ||
}, | ||
}; | ||
} | ||
// :snippet-end: | ||
export default Person; |
150 changes: 150 additions & 0 deletions
150
examples/react-native/__tests__/js/realm-database/change-an-object-model-test.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import Realm from 'realm'; | ||
import {createRealmContext} from '@realm/react'; | ||
|
||
describe('Change an Object Model Tests', () => { | ||
it('should add a property to a schema', () => { | ||
// :snippet-start: add-a-property-to-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object { | ||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
firstName: 'string', | ||
lastName: 'string', | ||
// add a new property, 'age' to the schema | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'age' has been added to the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
|
||
it('should delete a property from a schema', () => { | ||
// :snippet-start: delete-a-property-from-a-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object { | ||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
firstName: 'string', | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'lastName' has been removed from the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
it('should rename a property', async () => { | ||
// :snippet-start: rename-a-property-of-a-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object { | ||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
// rename the 'firstName' and 'lastName' property to 'fullName' in the schema | ||
fullName: 'string', | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'fullName' has replaced 'firstName' and 'lastName' in the schema | ||
schemaVersion: 2, | ||
migration: (oldRealm, newRealm) => { | ||
// only apply this change if upgrading to schemaVersion 2 | ||
if (oldRealm.schemaVersion < 2) { | ||
const oldObjects = oldRealm.objects(MyPerson); | ||
const newObjects = newRealm.objects(MyPerson); | ||
// loop through all objects and set the fullName property in the new schema | ||
for (const objectIndex in oldObjects) { | ||
const oldObject = oldObjects[objectIndex]; | ||
const newObject = newObjects[objectIndex]; | ||
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`; | ||
} | ||
} | ||
}, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' and 'migration' function to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
|
||
it('should modify a property type', () => { | ||
// :snippet-start: modify-a-property-type | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyTask": "Task" | ||
// } | ||
// } | ||
class MyTask extends Realm.Object { | ||
static schema = { | ||
name: 'MyTask', | ||
properties: { | ||
// update the data type of '_id' to be 'objectId' within the schema | ||
_id: 'objectId', | ||
name: 'string', | ||
priority: 'int?', | ||
progressMinutes: 'int?', | ||
assignee: 'Person?', | ||
}, | ||
primaryKey: '_id', | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyTask], | ||
// increment the 'schemaVersion', since the property type of '_id' | ||
// has been modified | ||
schemaVersion: 2, | ||
migration: (oldRealm, newRealm) => { | ||
if (oldRealm.schemaVersion < 2) { | ||
const oldObjects = oldRealm.objects(MyTask); | ||
const newObjects = newRealm.objects(MyTask); | ||
// loop through all objects and set the _id property | ||
// in the new schema | ||
for (const objectIndex in oldObjects) { | ||
const oldObject = oldObjects[objectIndex]; | ||
const newObject = newObjects[objectIndex]; | ||
newObject._id = new Realm.BSON.ObjectId(oldObject._id); | ||
} | ||
} | ||
}, | ||
}; | ||
// pass the configuration object with the updated | ||
// 'schemaVersion' and 'migration' function to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Realm from 'realm'; | ||
|
||
// 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 | ||
// :snippet-start: ts-person-schema | ||
class Person extends Realm.Object<Person> { | ||
name!: string; | ||
age?: number; | ||
|
||
static schema = { | ||
name: 'Person', | ||
properties: { | ||
name: 'string', | ||
age: 'int?', | ||
}, | ||
}; | ||
} | ||
// :snippet-end: | ||
export default Person; |
167 changes: 167 additions & 0 deletions
167
examples/react-native/__tests__/ts/realm-database/change-an-object-model-test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import Realm from 'realm'; | ||
mongodben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import {createRealmContext} from '@realm/react'; | ||
import Person from '../Models/Person'; | ||
|
||
describe('Change an Object Model Tests', () => { | ||
it('should add a property to a schema', () => { | ||
// :snippet-start: add-a-property-to-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object<MyPerson> { | ||
firstName!: string; | ||
lastName!: string; | ||
age!: number; | ||
|
||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
firstName: 'string', | ||
lastName: 'string', | ||
// add a new property, 'age' to the schema | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'age' has been added to the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
|
||
it('should delete a property from a schema', () => { | ||
// :snippet-start: delete-a-property-from-a-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object<MyPerson> { | ||
firstName!: string; | ||
age!: number; | ||
|
||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
firstName: 'string', | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'lastName' has been removed from the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
it('should rename a property', async () => { | ||
// :snippet-start: rename-a-property-of-a-schema | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyPerson": "Person" | ||
// } | ||
// } | ||
class MyPerson extends Realm.Object<MyPerson> { | ||
fullName!: string; | ||
age!: number; | ||
|
||
static schema = { | ||
name: 'MyPerson', | ||
properties: { | ||
// rename the 'firstName' and 'lastName' property, to 'fullName' in the schema | ||
fullName: 'string', | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyPerson], | ||
// increment the 'schemaVersion', since 'fullName' has replaced 'firstName' and 'lastName' in the schema | ||
schemaVersion: 2, | ||
migration: (oldRealm: Realm, newRealm: Realm) => { | ||
// only apply this change if upgrading to schemaVersion 2 | ||
if (oldRealm.schemaVersion < 2) { | ||
const oldObjects = oldRealm.objects(MyPerson); | ||
const newObjects = newRealm.objects(MyPerson); | ||
// loop through all objects and set the fullName property in the new schema | ||
for (const objectIndex in oldObjects) { | ||
const oldObject = oldObjects[objectIndex]; | ||
const newObject = newObjects[objectIndex]; | ||
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`; | ||
} | ||
} | ||
}, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' and 'migration' function to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
|
||
it('should modify a property type', () => { | ||
// :snippet-start: modify-a-property-type | ||
// :replace-start: { | ||
// "terms": { | ||
// "MyTask": "Task" | ||
// } | ||
// } | ||
class MyTask extends Realm.Object<MyTask> { | ||
_id: Realm.BSON.ObjectId = new Realm.BSON.ObjectId(); | ||
name!: string; | ||
priority?: number; | ||
progressMinutes?: number; | ||
assignee?: Person; | ||
|
||
static schema = { | ||
name: 'MyTask', | ||
properties: { | ||
// update the data type of '_id' to be 'objectId' within the schema | ||
_id: 'objectId', | ||
name: 'string', | ||
priority: 'int?', | ||
progressMinutes: 'int?', | ||
assignee: 'Person?', | ||
}, | ||
primaryKey: '_id', | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [MyTask], | ||
// increment the 'schemaVersion', since the property type of '_id' | ||
// has been modified | ||
schemaVersion: 2, | ||
migration: (oldRealm: Realm, newRealm: Realm) => { | ||
if (oldRealm.schemaVersion < 2) { | ||
const oldObjects = oldRealm.objects(MyTask); | ||
const newObjects = newRealm.objects(MyTask); | ||
// loop through all objects and set the _id property | ||
// in the new schema | ||
for (const objectIndex in oldObjects) { | ||
const oldObject = oldObjects[objectIndex]; | ||
const newObject = newObjects[objectIndex]; | ||
newObject._id = new Realm.BSON.ObjectId(oldObject._id); | ||
} | ||
} | ||
}, | ||
}; | ||
// pass the configuration object with the updated | ||
// 'schemaVersion' and 'migration' function to createRealmContext() | ||
createRealmContext(config); | ||
// :replace-end: | ||
// :snippet-end: | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
source/examples/generated/react-native/js/Person.snippet.js-person-schema.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Person extends Realm.Object { | ||
static schema = { | ||
name: 'Person', | ||
properties: { | ||
name: 'string', | ||
age: 'int?', | ||
}, | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
...enerated/react-native/js/change-an-object-model-test.snippet.add-a-property-to-schema.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Person extends Realm.Object { | ||
static schema = { | ||
name: 'Person', | ||
properties: { | ||
firstName: 'string', | ||
lastName: 'string', | ||
// add a new property, 'age' to the schema | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
|
||
const config = { | ||
schema: [Person], | ||
// increment the 'schemaVersion', since 'age' has been added to the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); |
16 changes: 16 additions & 0 deletions
16
...d/react-native/js/change-an-object-model-test.snippet.delete-a-property-from-a-schema.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Person extends Realm.Object { | ||
static schema = { | ||
name: 'Person', | ||
properties: { | ||
firstName: 'string', | ||
age: 'int', | ||
}, | ||
}; | ||
} | ||
const config = { | ||
schema: [Person], | ||
// increment the 'schemaVersion', since 'lastName' has been removed from the schema | ||
schemaVersion: 2, | ||
}; | ||
// pass the configuration object with the updated 'schemaVersion' to createRealmContext() | ||
createRealmContext(config); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] Why is this replace needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a Person schema with different properties used elsewhere in the unit test suite.