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

  • T = DefaultObject

  • RequiredProperties extends keyof OmittedRealmTypes<T> = never

Constructors

Properties

allowValuesArrays: boolean
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

  • Returns string

    Deprecated

    TODO: Remove completely once the type tests are abandoned.

  • A string uniquely identifying the object across all objects of the same type.

    Returns string

  • 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.

  • Returns [string, unknown][]

    An array of key/value pairs of the object's properties.

    Deprecated

    Please use Object.entries()

  • 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 string[]

    An array of the names of the object's properties.

    Deprecated

    Please use Object.keys()

  • Returns all the objects that link to this object in the specified relationship.

    Type Parameters

    • T = DefaultObject

    Parameters

    • objectType: string

      The type of the objects that link to this object's type.

    • propertyName: string

      The name of the property that references objects of this object's type.

    Returns Results<Object<T, never> & T>

    The Results that link to this object.

    Throws

    An AssertionError if the relationship is not valid.

  • Type Parameters

    Parameters

    • objectType: Constructor<T>
    • propertyName: string

    Returns Results<T>

  • Returns the total count of incoming links to this object

    Returns number

    The number of links to this object.

  • Remove all listeners from this object.

    Returns void

  • Remove the listener callback from this object.

    Parameters

    Returns void

    Throws

    A TypeAssertionError if callback is not a function.

  • The plain object representation for JSON serialization. Use circular JSON serialization libraries such as @ungap/structured-clone and flatted to stringify Realm entities that have circular structures.

    Parameters

    • Optional _: string
    • Optional cache: unknown

    Returns DefaultObject

    A plain object.

Generated using TypeDoc