RelationalDatumEntity class abstract

An extension of DatumEntity that includes support for defining relationships.

Understanding Relationships

The key difference between BelongsTo, HasOne, and HasMany lies in which entity holds the foreign key.

Aspect BelongsTo HasOne / HasMany
Who has the key? This entity has the foreign key. The other entity has the foreign key.
Relationship Role The "child" or "dependent" side. The "parent" or "owner" side.
Example A Post belongs to a User. A User has one Profile or has many Posts.
Code (Post) relations => {'author': BelongsTo('userId')} (Defined in the User class)
Code (User) (Defined in the Post class) relations => {'profile': HasOne('userId')}

BelongsTo

Use this when the current entity's table contains the foreign key that points to the parent.

// In a Post entity:
class Post extends RelationalDatumEntity {
  final String userId; // Foreign key
  @override
  Map<String, Relation> get relations => {'author': BelongsTo<User>(this, 'userId')};
}

HasOne / HasMany

Use these when the other entity's table contains the foreign key that points back to this one.

// In a User entity:
class User extends RelationalDatumEntity {
  @override
  Map<String, Relation> get relations => {
    'profile': HasOne<Profile>(this, 'userId'), // A Profile has a `userId` field
    'posts': HasMany<Post>(this, 'userId'),   // A Post has a `userId` field
  };
}

Entities that have relationships with other syncable entities should extend this class instead of DatumEntity directly.

Inheritance
Mixed-in types

Constructors

RelationalDatumEntity()
Creates a const RelationalDatumEntity.
const

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 setteroverride
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.
no setteroverride
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
version int
A sequential integer used for optimistic concurrency and tracking changes.
no setterinherited

Methods

copyWith({DateTime? modifiedAt, int? version, bool? isDeleted}) RelationalDatumEntity
Creates a new instance of the entity with updated values.
diff(covariant DatumEntityInterface oldVersion) Map<String, dynamic>?
Computes the difference between the current entity state and an oldVersion of the entity.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDatumMap({MapTarget target = MapTarget.local}) Map<String, dynamic>
Converts the entity to a Map<String, dynamic> for persistence.
override
toString() String
A string representation of this object.
inherited

Operators

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