Define an object model for React Native
You need to define the data types for the fields you want to store in a Realm database:
const TaskSchema = {
name: 'Task',
primaryKey: '_id',
properties: {
_id: 'int',
description: 'string',
}
};
To define a Realm object schema, you must first specify the schema name and the primary key.
Next, define the data types for each property in the object. You are required to include an _id property, which serves as the primary key. In the example above, the _id is defined with the "int" type to represent an integer.
For other properties, such as description, the data type is defined as "string".
Important: The object model (schema) should be defined outside of the main application function for proper usage and maintainability.
How to change an object model of a local realm (React Native SDK)
If you change the object model, the version of the schema changes and you need to increment schemaVersion. You need to specify the schema version in the object model. If you didn't make any changes, you don't need to specify the version.
Some changes require migration. Please visit the migrations page of the official website. You need to update the existing objects. Changing the data model may cause data loss. Therefore, if you have another option, don't change your object model.
Before making a change, create a Configuration with a schema version:
const config = {
schema: [TaskSchema],
schemaVersion: 1,
};
Pass the configuration object with the updated 'schemaVersion' to createRealmContext():
const {RealmProvider} = createRealmContext(config);
Add a property
You can add a property to your schema and increment schemaVersion. For example, you can add the property below:
notes: 'string'
Delete a property
You can delete the property you want and increment the schema.
Change property name
You can use mapTo method to change a property's name. Let's remap the notes property to description:
notes: {type: "string",
mapTo: "description"}
Change a property type
You can change a property type but keep the name:
notes: {type: "mixed"}
The value of existing documents will be null. It's a breaking change and it's not recommended. After you change a property type, you should update existing objects.
Adding a default value
When you add a default value to your object model, it does not change the existing documents. The default value is applied to future documents.
Changing a property's status between optional and required
Changing a property's status is a breaking change and not recommended.
How to change data model of a sync realm (React Native SDK)
According to the Atlas Device SDK guide, there are two types of changes: "breaking" and "non-breaking". Please visit the official website for more information about "breaking" and "non-breaking" changes. "A breaking change is a change that you make in your server-side schema that requires additional action to handle." As it may cause data loss and inconsistency between client-side application and server-side documents, applying non-breaking changes is not recommended by Realm. "A non-breaking change is a change that you can make in your server-side schema or your object model without requiring additional handling in your app." You can find methods and examples for non-breaking changes in this section. You should turn development mode on before making a change. The changes below will be made to the client object model.
Add a property
You can directly add a property to your schema.
Remove a property
You can delete the property you want.
Change property name
You can use the mapTo method to change a property's name. Let's change the description property name with notes:
notes: {type: "string",
mapTo: "description"}
Add a default value
You can also add a default value to a property:
notes: {type: "string", default: "none"}
When you add a default value to your object model, it does not change the existing documents. The default value is applied to future documents and you need to update existing documents.