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

Constructors

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.

    Parameters

    • callback: ObjectChangeCallback<T>

      A function to be called when changes occur.

    • Optional keyPaths: string | string[]

      Indicates a lower bound on the changes relevant for the listener. This is a lower bound, since if multiple listeners are added (each with their own keyPaths) the union of these key-paths will determine the changes that are considered relevant for all listeners registered on the object. In other words: A listener might fire more than the key-paths specify, if other listeners with different key-paths are present.

    Returns void

    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}`);
    });
    })

    Example

    wine.addListener((obj, changes) => {
    console.log("The wine got deleted or its brand might have changed");
    }, ["brand"])

    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.

  • Get underlying type of a property value.

    Parameters

    • propertyName: string

      The name of the property to retrieve the type of.

    Returns string

    Underlying type of the property value.

    Throws

    An Error if property does not exist.

  • 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