Define a Realm Object Schema - Node.js SDK
On this page
- Define Realm Object Types with JavaScript Classes
- Supported Property Types
- Define Object Properties
- Specify an Optional Property
- Specify a Primary Key
- Index a Property
- Define a Default Property Value
- Remap a Property
- Define Relationship Properties
- Define a To-One Relationship Property
- Define a To-Many Relationship Property
- Define an Inverse Relationship Property
- Define an Embedded Object Property
- Define an Asymmetric Object
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. For details on how to define specific properties, see Define
Object Properties.
You can define your schemas with JavaScript classes (like most of the examples on this page), but you can also define them as JavaScript objects.
const Car = { name: "Car", properties: { _id: "objectId", make: "string", model: "string", miles: "int?", }, };
Define Realm Object Types with JavaScript Classes
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', }; }
Note
Class names are limited to a maximum of 57 UTF-8 characters.
Pass the class itself to the schema property of the Realm.Configuration object when opening a realm. You can then read and write data normally.
const realm = await Realm.open({ path: "myrealm", schema: [Car], }); let car1; realm.write(() => { car1 = realm.create(Car, { make: "Nissan", model: "Sentra", miles: 1000, }); });
Supported Property Types
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.
Realm Database supports the following primitive data types:
bool
for boolean valuesint
anddouble
, which map to JavaScriptnumber
valuesDecimal128
for high precision numbersstring
date
, which maps to Datedata
, which maps to ArrayBufferobjectId
, which maps to ObjectId
To specify that a field contains a list of a primitive value type, append []
to the type name.
Define Object Properties
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
.
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', }; }
Specify an Optional Property
To mark a property as optional, append a question mark ?
to its type.
The following Car
schema defines an optional miles
property of type int
.
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', }; }
Specify a Primary Key
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 efficiently read and modify objects based on their primary key.
If an object type has a primary key, then all objects of that type must include the primary key property with a value that is unique among objects of the same type in a realm. An object type may have at most 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.
The following Car
object schema specifies the _id
property as its
primary key.
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', }; }
Index a Property
Realm supports indexing for string, integer, boolean, Date
, UUID
, and ObjectId
properties. To define an index for a given property, set indexed
to
true
.
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.
It's best to only add indexes when optimizing the read performance for specific situations.
The following Car
object schema defines an index on the _id
property.
class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: { type: "objectId", indexed: true }, make: "string", model_name: { type: "string", mapTo: "modelName" }, miles: { type: "int", default: 0 }, }, primaryKey: '_id', }; }
Define a Default Property Value
To define a default value, set the value of the property to an object with a
type
field and a default
field.
The following Car
object schema specifies a default value of 0
for
the miles
property:
class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: { type: "objectId", indexed: true }, make: "string", model_name: { type: "string", mapTo: "modelName" }, miles: { type: "int", default: 0 }, }, primaryKey: '_id', }; }
Remap a Property
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 Car
object schema, Realm Database stores the car's
model name with the snake case model_name
property. The schema maps the property
to modelName
for objects used in client code.
class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: { type: "objectId", indexed: true }, make: "string", model_name: { type: "string", mapTo: "modelName" }, miles: { type: "int", default: 0 }, }, primaryKey: '_id', }; }
Define Relationship Properties
Tip
See also:
Alternatively, you can define your relationships in your App Services app.
Define a To-One Relationship Property
A to-one relationship maps one property to a single instance of another object type. For example, you can model a manufacturer having at most one car as a to-one relationship.
To define a to-one relationship property, specify the related object type name as the property type.
Important
To-one relationships must be optional
When you declare a to-one relationship in your object model, it must be an optional property. If you try to make a to-one relationship required, Realm throws an exception at runtime.
The following Manufacturer
object schema specifies that a manufacturer may or may not
make a single Car
. If they do make a Car
, Realm links to it through the
car
property:
class Manufacturer extends Realm.Object { static schema = { name: "Manufacturer", properties: { _id: "objectId", // A manufacturer that may have one car car: "Car?" }, }; } class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: "objectId", make: "string", model: "string", miles: "int?", }, }; }
Define a To-Many Relationship Property
A to-many relationship maps one property to zero or more instances of another object type. For example, you can model a manufacturer having any number of cars as a to-many relationship.
To define a to-many relationship property, specify the related object type name as a list.
An application could use the following object schemas to indicate that a Manufacturer
may make multiple Car
objects by including them in its cars
property:
class Manufacturer extends Realm.Object { static schema = { name: "Manufacturer", properties: { _id: "objectId", // A manufacturer that may have many cars cars: "Car[]" }, }; } class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: "objectId", make: "string", model: "string", miles: "int?", }, }; }
Define an Inverse Relationship Property
An inverse relationship property is an automatic backlink relationship. Realm Database automatically updates implicit relationships whenever an object is added or removed in a corresponding to-many list. You cannot manually set the value of an inverse relationship property.
To define an inverse relationship property, set the property type to
linkingObjects
and specify the object type and property name that define the
relationship to invert.
An application could use the following object schemas to indicate that a Manufacturer
may make many Car
objects and that each Car
should automatically keep track
of which Manufacturer
makes it.
- The
Manufacturer
object'scars
property is defined as a to-many relationship - with
Car
objects and contains all of a given manufacturer's cars.
- The
- The
Car
object'sassignee
property inverts the relationship and - automatically updates to refer back to any
Manufacturer
object that contains the car in itscars
property.
- The
class Manufacturer extends Realm.Object { static schema = { name: "Manufacturer", properties: { _id: "objectId", // A manufacturer that may have many cars cars: "Car[]" }, }; } class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: "objectId", make: "string", model: "string", miles: "int?", // Backlink to the manufacturer. This is automatically updated whenever // this car is added to or removed from a manufacturer's cars list. assignee: { type: "linkingObjects", objectType: "Manufacturer", property: "cars", } }, }; }
Define an Embedded Object Property
To define a Realm object model with an embedded object (nested Realm
object), set embedded
to true
.
An embedded object exists as nested data inside of a single, specific parent object. It inherits the lifecycle of its parent object and cannot exist as an independent Realm object. Realm automatically deletes embedded objects if their parent object is deleted or when overwritten by a new embedded object instance. Embedded objects cannot have a primary key.
You can reference an embedded object type from parent object types in the same way as a relationship.
The following example requires two parent schemas, Manufacturer
and
Car
. The application requires an embedded child schema Warranty
.
A Manufacturer
object can embed a list of Warranty
objects, whereas a
Car
object can only embed a single Warranty
object.
class Manufacturer extends Realm.Object { static schema = { name: "Manufacturer", properties: { _id: "objectId", name: "string", // Embed an array of objects warranties: { type: "list", objectType: "Warranty" } }, }; } class Car extends Realm.Object { static schema = { name: "Car", properties: { _id: "objectId", make: "string", model: "string", miles: "int?", // Embed one object warranty: "Warranty", }, }; } class Warranty extends Realm.Object { static schema = { name: "Warranty", embedded: true, properties: { name: "string", termLength: "int", cost: "int", }, }; }
Define an Asymmetric Object
If you are using Flexible Sync and need to sync a collection unidirectionally
from your device to your Atlas database, you can set the asymmetric
property
on your object schema.
In the following example of a retail app, the client requires large amounts of
invoice data to be recorded rapidly by store employees. The client specifies
that invoice data does not need to be read after employees have recorded it. To
satisfy this requirement, the application developer defines an invoice
collection with its asymmetric
property set to true
.
const InvoiceSchema = { name: "Invoice", // sync Invoice objects one way from your device to your Atlas database. asymmetric: true, primaryKey: "_id", properties: { _id: "objectId", item: "string", quantity: "int", price: "int", }, };
Note
Attempting to Read Asymmetric Sync Objects
Asymmetric Sync objects cannot be read. If you attempt to query an Asymmetric Sync object, you will get the following error: "Error: You cannot query an asymmetric class.".
To learn more about Asymmetric Sync, read Optimize Sync with Asymmetric Sync.