Skip to content

(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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/react-native/__tests__/js/Models/Person.js
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;
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"
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

// }
// }
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:
});
});
18 changes: 18 additions & 0 deletions examples/react-native/__tests__/ts/Models/Person.ts
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import Realm from 'realm';
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:
});
});
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?',
},
};
}
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);
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);
Loading