Interface: ReactiveModel<T, PK, R>
Describes the shape of an instance of a ReactiveModel.
Type Parameters
| Type Parameter | Description |
|---|---|
T extends PlainObject | The type of the object that will be used as the model. |
PK extends StringKeyOf<T> | The key of the object which is used as the primary key for the model. |
R extends Record<string, RelationshipConfiguration> | - |
Indexable
[key: string]: R[string] extends [RelType, ...any[]] ? RelType extends
| typeof BelongsTo
| typeof HasOne
| typeof MorphOne
| typeof MorphTo ? any : RelType extends
| typeof HasMany
| typeof HasManyThrough
| typeof ManyToMany
| typeof MorphMany ? undefined | any[] : any : anyMethods
clone()
clone: () => ReactiveModel<T, PK, R>;Creates a new instance of the model with the same properties as the current instance.
Returns
ReactiveModel<T, PK, R>
A new instance of the model with the same properties as the current instance.
delete()
delete(): Promise<ReactiveModel<T, PK, R>>;Deletes the instance of the model from the database and propagates the changes to the rest of the swarm.
Returns
Promise<ReactiveModel<T, PK, R>>
The instance of the model.
fill()
fill(value: Partial<DataValues<T, PK>>): this;Fills missing / undefined properties of the model with the values from the object.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | Partial<DataValues<T, PK>> | The object containing the values to fill the model with. |
Returns
this
The instance of the model.
load()
load(relationship: Extract<keyof R>): Promise<void>;Lazy-load a specific relationship.
Parameters
| Parameter | Type | Description |
|---|---|---|
relationship | Extract<keyof R> | The name of the relationship to load |
Returns
Promise<void>
A promise that resolves when the relationship is loaded
loadMany()
loadMany(relationships: Extract<keyof R, string>[]): Promise<void>;Lazy-load many relationships.
Parameters
| Parameter | Type | Description |
|---|---|---|
relationships | Extract<keyof R, string>[] | The names of the relationships to load |
Returns
Promise<void>
A promise that resolves when all relationships are loaded
merge()
merge(value: Partial<DataValues<T, PK>>): this;Merges the properties of the object into the model.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | Partial<DataValues<T, PK>> | The object containing the values to merge into the model. |
Returns
this
The instance of the model.
offChange()
offChange(listener?: Listener<"change", ReactiveModelChangeEmitterEventMap<T>>): this;Unsubscribe a listener or all listeners from events emitted when any of the properties of the model change.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener? | Listener<"change", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
Returns
this
The current ReactiveModel instance
offDelta()
offDelta(listener?: Listener<"delta", ReactiveModelChangeEmitterEventMap<T>>): this;Unsubscribe a listener or all listeners from events emitted when the delta of the model changes.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener? | Listener<"delta", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
Returns
this
The current ReactiveModel instance
offPropertyChange()
offPropertyChange(key:
| Extract<keyof T, string>
| Extract<keyof R, string>
| `${Extract<keyof R, string>}.${number}`, listener?: Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>>): this;Unsubscribe a listener or all listeners from events emitted when a specific property of the model changes.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | | Extract<keyof T, string> | Extract<keyof R, string> | `${Extract<keyof R, string>}.${number}` | The property of the model to listen to |
listener? | Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's property is changed |
Returns
this
The current ReactiveModel instance
onceChange()
onceChange(listener: Listener<"change", ReactiveModelChangeEmitterEventMap<T>>, ctx?: any): this;Subscribe a listener once to events emitted when any of the properties of the model change.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | Listener<"change", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
onceDelta()
onceDelta(listener: Listener<"delta", ReactiveModelChangeEmitterEventMap<T>>, ctx?: any): this;Subscribe a listener once to events emitting the delta of the model when any of the properties change.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | Listener<"delta", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
oncePropertyChange()
oncePropertyChange(
key:
| Extract<keyof T, string>
| Extract<keyof R, string>
| `${Extract<keyof R, string>}.${number}`,
listener: Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>>,
ctx?: any): this;Subscribe a listener once to events emitted when a specific property of the model changes.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | | Extract<keyof T, string> | Extract<keyof R, string> | `${Extract<keyof R, string>}.${number}` | The property of the model to listen to |
listener | Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's property is changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
onChange()
onChange(listener: Listener<"change", ReactiveModelChangeEmitterEventMap<T>>, ctx?: any): this;Subscribe a listener to events emitted when any of the properties of the model change.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | Listener<"change", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
onDelta()
onDelta(listener: Listener<"delta", ReactiveModelChangeEmitterEventMap<T>>, ctx?: any): this;Subscribe a listener to events emitting the delta of the model when any of the properties change.
Parameters
| Parameter | Type | Description |
|---|---|---|
listener | Listener<"delta", ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's properties are changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
onPropertyChange()
onPropertyChange(
key:
| Extract<keyof T, string>
| Extract<keyof R, string>
| `${Extract<keyof R, string>}.${number}`,
listener: Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>>,
ctx?: any): this;Subscribe a listener to events emitted when a specific property of the model changes.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | | Extract<keyof T, string> | Extract<keyof R, string> | `${Extract<keyof R, string>}.${number}` | The property of the model to listen to |
listener | Listener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>> | The callback to be called when the model's property is changed |
ctx? | any | The this context to be used when calling the callback |
Returns
this
The current ReactiveModel instance
related()
related<P>(relationship: P): Promise<RelatedValueMap<R>[P]>;Retrieve the value of a specific relationship.
Type Parameters
| Type Parameter |
|---|
P extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
relationship | P | The name of the relationship to get |
Returns
Promise<RelatedValueMap<R>[P]>
The value of the relationship
reset()
reset(): void;Resets the pending changes for the instance of the model.
Returns
void
save()
save(): Promise<ReactiveModel<T, PK, R>>;Saves the pending changes for the instance of the model to the database and propagates the changes to the rest of the swarm.
Returns
Promise<ReactiveModel<T, PK, R>>
The instance of the model.
toJSON()
toJSON(): T;Returns the object representation of the model.
Returns
T
The object representation of the model.
toObject()
toObject(): T & Partial<Record<Extract<keyof R, string>, any>>;Returns the object representation of the model.
Returns
T & Partial<Record<Extract<keyof R, string>, any>>
The object representation of the model.
toString()
toString(): string;Returns the stringified representation of the model.
Returns
string
The stringified representation of the model.
unref()
unref(): Promise<void>;Cleanup the instance of the model in preparation for garbage collection.
Returns
Promise<void>
Properties
$deleted
Get Signature
get $deleted(): boolean;A boolean indicating whether the model has been deleted.
Returns
boolean
$dirty
Get Signature
get $dirty(): boolean;A boolean indicating whether the model has any pending changes.
Returns
boolean
$key
Get Signature
get $key(): PK;The value of the primary key for the instance of the model.
Returns
PK
$pending
Get Signature
get $pending(): Partial<Record<Extract<keyof T, string>, Omit<PendingStateChange<T, Extract<keyof T, string>>, "property">>>;An object containing all of the pending changes to be made to the model
Returns
Partial<Record<Extract<keyof T, string>, Omit<PendingStateChange<T, Extract<keyof T, string>>, "property">>>