Class Object<T, RequiredProperties>

Base class for a Realm Object.

Example

To define a class Person with required name and age properties, define a static schema:

class Person extends Realm.Object<Person> {
_id!: Realm.BSON.ObjectId;
name!: string;
age!: number;
static schema: Realm.ObjectSchema = {
name: "Person",
primaryKey: "_id",
properties: {
_id: "objectId",
name: "string",
age: "int",
},
};
}

Example

If using the @realm/babel-plugin: To define a class Person with required name and age properties, they would need to be specified in the type argument when it is being constructed to allow Typescript-only model definitions:

class Person extends Realm.Object<Person, "name" | "age"> {
_id = new Realm.Types.ObjectId();
name: Realm.Types.String;
age: Realm.Types.Int;
static primaryKey = "_id";
}

See

ObjectSchema

Type Param

T - The type of this class (e.g. if your class is Person, T should also be Person - this duplication is required due to how TypeScript works)

Type Param

RequiredProperties - The names of any properties of this class which are required when an instance is constructed with new. Any properties not specified will be optional, and will default to a sensible null value if no default is specified elsewhere.

Type Parameters

Hierarchy

  • Object

Constructors

  • Create a RealmObject wrapping an Obj from the binding.

    Type Parameters

    • T = DefaultObject

    • RequiredProperties extends string | number | symbol = never

    Parameters

    • realm: Realm

      The Realm managing the object.

    • values: Unmanaged<T, RequiredProperties>

      The values of the object's properties at creation.

    Returns Object<T, RequiredProperties>

Properties

allowValuesArrays: boolean = false
asymmetric?: boolean

Optionally specify that the schema should sync unidirectionally if using flexible sync when using @realm/babel-plugin.

embedded?: boolean

Optionally specify that the schema is an embedded schema when using @realm/babel-plugin.

primaryKey?: string

Optionally specify the primary key of the schema when using @realm/babel-plugin.

Methods

  • Add a listener callback which will be called when a live object instance changes.

    Throws

    A TypeAssertionError if callback is not a function.

    Example

    wine.addListener((obj, changes) => {
    // obj === wine
    console.log(`object is deleted: ${changes.deleted}`);
    console.log(`${changes.changedProperties.length} properties have been changed:`);
    changes.changedProperties.forEach(prop => {
    console.log(` ${prop}`);
    });
    })

    Note

    Adding the listener is an asynchronous operation, so the callback is invoked the first time to notify the caller when the listener has been added. Thus, when the callback is invoked the first time it will contain empty array for changes.changedProperties.

    Parameters

    Returns void

  • Get underlying type of a property value.

    Throws

    An Error if property does not exist.

    Parameters

    • propertyName: string

      The name of the property to retrieve the type of.

    Returns string

    Underlying type of the property value.

  • Checks if this object has not been deleted and is part of a valid Realm.

    Returns boolean

    true if the object can be safely accessed, false if not.

  • Returns the total count of incoming links to this object

    Returns number

    The number of links to this object.

Generated using TypeDoc