Skip to content

Interface: ReactiveModel<T, PK, R>

Describes the shape of an instance of a ReactiveModel.

Type Parameters

Type ParameterDescription
T extends PlainObjectThe 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

ts
[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 : any

Methods

clone()

ts
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()

ts
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()

ts
fill(value: Partial<DataValues<T, PK>>): this;

Fills missing / undefined properties of the model with the values from the object.

Parameters

ParameterTypeDescription
valuePartial<DataValues<T, PK>>The object containing the values to fill the model with.

Returns

this

The instance of the model.


load()

ts
load(relationship: Extract<keyof R>): Promise<void>;

Lazy-load a specific relationship.

Parameters

ParameterTypeDescription
relationshipExtract<keyof R>The name of the relationship to load

Returns

Promise<void>

A promise that resolves when the relationship is loaded


loadMany()

ts
loadMany(relationships: Extract<keyof R, string>[]): Promise<void>;

Lazy-load many relationships.

Parameters

ParameterTypeDescription
relationshipsExtract<keyof R, string>[]The names of the relationships to load

Returns

Promise<void>

A promise that resolves when all relationships are loaded


merge()

ts
merge(value: Partial<DataValues<T, PK>>): this;

Merges the properties of the object into the model.

Parameters

ParameterTypeDescription
valuePartial<DataValues<T, PK>>The object containing the values to merge into the model.

Returns

this

The instance of the model.


offChange()

ts
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

ParameterTypeDescription
listener?Listener<"change", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed

Returns

this

The current ReactiveModel instance


offDelta()

ts
offDelta(listener?: Listener<"delta", ReactiveModelChangeEmitterEventMap<T>>): this;

Unsubscribe a listener or all listeners from events emitted when the delta of the model changes.

Parameters

ParameterTypeDescription
listener?Listener<"delta", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed

Returns

this

The current ReactiveModel instance


offPropertyChange()

ts
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

ParameterTypeDescription
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()

ts
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

ParameterTypeDescription
listenerListener<"change", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


onceDelta()

ts
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

ParameterTypeDescription
listenerListener<"delta", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


oncePropertyChange()

ts
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

ParameterTypeDescription
key| Extract<keyof T, string> | Extract<keyof R, string> | `${Extract<keyof R, string>}.${number}`The property of the model to listen to
listenerListener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's property is changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


onChange()

ts
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

ParameterTypeDescription
listenerListener<"change", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


onDelta()

ts
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

ParameterTypeDescription
listenerListener<"delta", ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's properties are changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


onPropertyChange()

ts
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

ParameterTypeDescription
key| Extract<keyof T, string> | Extract<keyof R, string> | `${Extract<keyof R, string>}.${number}`The property of the model to listen to
listenerListener<`change:${string}`, ReactiveModelChangeEmitterEventMap<T>>The callback to be called when the model's property is changed
ctx?anyThe this context to be used when calling the callback

Returns

this

The current ReactiveModel instance


ts
related<P>(relationship: P): Promise<RelatedValueMap<R>[P]>;

Retrieve the value of a specific relationship.

Type Parameters

Type Parameter
P extends string

Parameters

ParameterTypeDescription
relationshipPThe name of the relationship to get

Returns

Promise<RelatedValueMap<R>[P]>

The value of the relationship


reset()

ts
reset(): void;

Resets the pending changes for the instance of the model.

Returns

void


save()

ts
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()

ts
toJSON(): T;

Returns the object representation of the model.

Returns

T

The object representation of the model.


toObject()

ts
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()

ts
toString(): string;

Returns the stringified representation of the model.

Returns

string

The stringified representation of the model.


unref()

ts
unref(): Promise<void>;

Cleanup the instance of the model in preparation for garbage collection.

Returns

Promise<void>

Properties

$deleted

Get Signature

ts
get $deleted(): boolean;

A boolean indicating whether the model has been deleted.

Returns

boolean


$dirty

Get Signature

ts
get $dirty(): boolean;

A boolean indicating whether the model has any pending changes.

Returns

boolean


$key

Get Signature

ts
get $key(): PK;

The value of the primary key for the instance of the model.

Returns

PK


$pending

Get Signature

ts
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">>>