preserveRelationsFrom method
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).
Datum entities are immutable, so updating a field produces a new instance whose relations start empty. Rather than re-fetching or re-linking, carry the already-loaded relations across with a cascade:
final updated = user.copyWith(name: 'New')..preserveRelationsFrom(user);
// updated.relations['posts'].value is the same list as user's — no refetch.
Only relations that are already loaded on source are copied.
Implementation
void preserveRelationsFrom(RelationalDatumEntity source) {
for (final entry in source.relations.entries) {
final loaded = entry.value.value;
if (loaded != null) {
relations[entry.key]?.setRaw(loaded);
}
}
}