Docs Menu

Docs HomeRealm

Define a Realm Object Model - React Native SDK

On this page

  • Key Concept: Object Types & Schemas
  • Realm Schema
  • Define a New Object Type
  • Define Realm Object Types with JavaScript Classes

Every Realm object conforms to a specific object type, which is essentially a class that defines the properties and relationships for objects of that type using a pre-defined schema. Realm guarantees that all objects in a realm conform to the schema for their object type and validates objects whenever they're created, modified, or deleted.

Realm objects are fundamentally similar to a common JavaScript object but they also bring along a few additional features like schema validation and live queries. The React Native SDK memory maps Realm objects directly to native JavaScript objects, which means there's no need to use a special data access library, such as an ORM. Instead, you can work with Realm objects as you would any other object.

A realm schema is a list of valid object schemas that a realm may contain. Every Realm object must conform to an object type that's included in its realm's schema.

If a realm already contains data when you open it, Realm Database validates each object to ensure that an object schema was provided for its type and that it meets all of the constraints specified in the schema.

Example

A realm that contains basic data about books in libraries might use a schema like the following:

[
{
"type": "Library",
"properties": {
"address": "string",
"books": "Book[]"
}
},
{
"type": "Book",
"primaryKey": "isbn",
"properties": {
"isbn": "string",
"title": "string",
"author": "string",
"numberOwned": { "type": "int?", "default": 0 },
"numberLoaned": { "type": "int?", "default": 0 }
}
}
]

To define a Realm object type, create a schema object that specifies the type's name and properties. The type name must be unique among object types in a realm.

const CarSchema = {
name: "Car",
properties: {
make: "string",
model: "string",
miles: "int",
}
}

You can then open a realm using the Realm Object you have defined.

// Open a local realm file with a particular path & predefined Car schema
try {
const realm = await Realm.open({
path: "myrealm",
schema: [Car],
});
} catch (err) {
console.error("Failed to open the realm", err.message);
}

Note

To learn the various types that a Realm Object Model accepts, how to specify optional fields, define relationships, embedded objects, primary keys, and more, check out the Schemas section.

You can define Realm object types with JavaScript classes. To use a class as an object type, define the object schema on the static property schema.

class Car extends Realm.Object {
static schema = {
name: "Car",
properties: {
_id: { type: 'objectId', default: () => new Realm.BSON.ObjectId() },
make: "string",
model: "string",
miles: "int?",
},
primaryKey: '_id',
};
}

Pass the class itself to the schema property of the Realm.Configuration object when opening a realm. You can then read and write normally.

const realm = await Realm.open({
path: "myrealm",
schema: [Car],
});
let car1;
realm.write(() => {
car1 = realm.create(Car, {
make: "Nissan",
model: "Sentra",
miles: 1000,
});
});
←  Realm Database - React Native SDKChange an Object Model - React Native SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

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