Create a RealmObject
wrapping an Obj
from the binding.
The Realm managing the object.
The values of the object's properties at creation.
Static
allowStatic
Optional
asymmetricOptionally specify that the schema should sync unidirectionally if using flexible sync when using @realm/babel-plugin.
Static
Optional
embeddedOptionally specify that the schema is an embedded schema when using @realm/babel-plugin.
Static
Optional
primaryOptionally specify the primary key of the schema when using @realm/babel-plugin.
Add a listener callback
which will be called when a live object instance changes.
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.
A TypeAssertionError if callback
is not a function.
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}`);
});
})
wine.addListener((obj, changes) => {
console.log("The wine got deleted or its brand might have changed");
}, ["brand"])
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
.
An array of key/value pairs of the object's properties.
Please use Object.entries()
An array of the names of the object's properties.
Please use Object.keys()
Returns all the objects that link to this object in the specified relationship.
The type of the objects that link to this object's type.
The name of the property that references objects of this object's type.
The Results that link to this object.
An AssertionError if the relationship is not valid.
The schema for the type this object belongs to.
The CanonicalObjectSchema that describes this object.
Remove the listener callback
from this object.
A function previously added as listener
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.
Optional
_: stringOptional
cache: unknownA plain object.
Generated using TypeDoc
Base class for a Realm Object.
Example
To define a class
Person
with requiredname
andage
properties, define astatic schema
:Example
If using the @realm/babel-plugin: To define a class
Person
with requiredname
andage
properties, they would need to be specified in the type argument when it is being constructed to allow Typescript-only model definitions:See
ObjectSchema
Type Param
T
- The type of this class (e.g. if your class isPerson
,T
should also bePerson
- 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 withnew
. Any properties not specified will be optional, and will default to a sensible null value if no default is specified elsewhere.