Docs Menu

Docs HomeRealm

Define a Realm Object Model - React Native SDK

On this page

  • Define an Object Type
  • Supported Property Types
  • Define Object Properties
  • Declare an Optional Property
  • Declare a Primary Key
  • Index a Property
  • Set a Default Property Value
  • Remap a Property
  • TypeScript and Required Properties

To define a Realm object type, create a class that extends Realm.Object. Define the type's name and properties in a static property called schema. The type's name must be unique among object types in a realm.

Then you can pass the class itself to the schema property of the Realm.Configuration object when opening a realm.

Every property in a Realm object has a strongly defined data type. A property's type can be a primitive data type or an object type defined in the same realm. The type also specifies whether the property contains a single value or a list of values.

To specify that a field contains a list of a primitive value type, append [] to the type name.

For a list of supported property types, see Property Types

To define a property for an object type, create a key-value pair representing the name and data type of the property under the properties field.

The following schema defines a Car type that has these properties: _id make, model, and miles.

To mark a property as optional, append a question mark ? to its type. You can also add optional to more complicated properties and set it to true or false.

In the following example of a Dog class, the age property of type int is optional.

To specify a property as an object type's primary key, set the schema's primaryKey field to the property name.

Note

A primary key is a property that uniquely identifies an object. Realm Database automatically indexes primary key properties, which allows you to read and modify objects based on their primary key efficiently.

If an object type has a primary key, then all objects of that type must include the primary key property with a unique value among objects of the same type in a realm. An object type can have only one primary key. You cannot change the primary key property for an object type after any object of that type is added to a realm, and you cannot modify an object's primary key value.

In the following example of a Task class, we specify the _id property as the primary key.

If you frequently run read operations based on a specific property, you can index the property to optimize performance. Realm supports indexing for string, integer, boolean, Date, UUID, and ObjectId properties.

Note

An index significantly increases the speed of certain read operations at the cost of slightly slower write times and additional storage and memory overhead. Realm Database stores indexes on disk, which makes your realm files larger. Each index entry is a minimum of 12 bytes. The ordering of the index entries supports efficient equality matches and range-based query operations.

To index a given property, set the property's indexed field to true.

In the following example of a Book class, we define an index on the name property.

To define a default value, set the value of the property to an object with a type field and a default field.

In the following example of a Car class, we define a miles property with a default value of 0.

New in version 11.1.0.

In Realm.js v11.1.0 and later, you can use a function to define a dynamic default value, like the timestamp property in the example below.

To use a different property name in your code than is stored in Realm Database, set mapTo to the name of the property as it appears in your code.

In the following example of an Employee class, we remap the first_name property to firstName.

We recommend creating Realm objects with Realm.create(), but you can also use the new operator for your object model's class.

If you use new, you must add your class as a generic, along with any required properties, when extending Realm.Object. This enables full TypeScript support for your object model, including type errors when required fields are not defined.

class Book extends Realm.Object<Book, 'name' | 'store'> {
name!: string;
store!: string;
price?: number;
static schema = {
name: 'Book',
properties: {
name: {type: 'string', indexed: true},
store: 'string',
price: 'int?',
},
};
}
←  Model Data - React Native SDKRelationships & Embedded Objects - React Native SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.