MemoizedRelations mixin

Memoizes an entity's relations so eager loading (withRelated) works reliably for hand-written entities.

Eager loading mutates Relation objects in place (via setRaw). If your relations getter builds a fresh map on every call, those mutations are silently lost and relatedList(...) always returns null. Mix this in and define buildRelations instead of overriding relations — the map is built once, lazily, and reused, so loaded relations persist:

class Blog extends RelationalDatumEntity with MemoizedRelations {
  @override
  Map<String, Relation> buildRelations() => {
        'posts': HasMany<Post>(this, 'blogId'),
      };
}

(Generated entities already memoize via their generated relations map, so this is only needed when hand-writing relational entities.)

Superclass constraints

Properties

createdAt DateTime
The timestamp of when this entity was first created.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
id String
A unique identifier for the entity.
no setterinherited
isDeleted bool
A flag indicating if this entity has been locally marked for deletion.
no setterinherited
isRelational bool
Indicates whether this entity supports relationships. Always true for this class.
no setterinherited
modifiedAt DateTime
The timestamp of the last time this entity was modified.
no setterinherited
props List<Object?>
Provides the list of properties to be used by the Equatable mixin for value equality checks.
no setterinherited
relations Map<String, Relation<DatumEntityInterface>>
A map defining all relationships for this entity.
latefinal
relationSchema Map<String, RelationDescriptor>
An instance-free description of this entity's relations (#21).
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stringify bool?
If set to true, the toString method will be overridden to output this instance's props.
no setterinherited
userId String
The ID of the user who owns or created this entity.
no setterinherited
vectorClock VectorClock?
A vector clock for tracking causality across multiple devices.
no setterinherited
version int
A sequential integer used for optimistic concurrency and tracking changes.
no setterinherited

Methods

buildRelations() Map<String, Relation<DatumEntityInterface>>
Builds this entity's relation map. Called once, lazily, then cached.
copyWith({DateTime? modifiedAt, int? version, bool? isDeleted}) RelationalDatumEntity
Creates a new instance of the entity with updated values.
inherited
diff(covariant DatumEntityInterface oldVersion) Map<String, dynamic>?
Computes the difference between the current entity state and an oldVersion of the entity.
inherited
incrementClock(String replicaId) DatumEntityInterface
Returns a new instance with the vector clock incremented for replicaId.
inherited
merge(covariant DatumEntityInterface other) DatumEntityInterface
Merges this entity with other.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
preserveRelationsFrom(RelationalDatumEntity source) → void
Copies all currently-loaded in-memory relation values from source onto this entity, so a rebuilt (copyWith) instance keeps its relation references instead of losing them (#34).
inherited
relatedList<R extends DatumEntityInterface>(String name) List<R>?
Returns the loaded value of a to-many relation (HasMany / ManyToMany) as a typed List<R>, or null if the relation is absent or not loaded.
inherited
relatedOne<R extends DatumEntityInterface>(String name) → R?
Returns the loaded value of a to-one relation (BelongsTo / HasOne) as a typed R, or null if the relation is absent or not loaded.
inherited
toDatumMap({MapTarget target = MapTarget.local}) Map<String, dynamic>
Converts the entity to a Map<String, dynamic> for persistence.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited