withKeyOf<T extends DataModelMixin<T> > static method
T
withKeyOf<T extends DataModelMixin<T> >({
- required T source,
- required T destination,
Apply source
's key to destination
.
Implementation
static T withKeyOf<T extends DataModelMixin<T>>(
{required T source, required T destination}) {
if (source._key == null) {
throw Exception("Model must be initialized:\n\n$source");
}
final graph = source._remoteAdapter.graph;
final type = source._internalType;
// ONLY data we keep from source is its key
// ONLY data we remove from destination is its key
if (source._key != destination._key) {
final destKey = destination._key;
// assign correct key to destination
destination._key = source._key;
// migrate relationships to new key
destination._remoteAdapter.localAdapter._initializeRelationships(
destination,
from: source,
);
if (destKey != null) {
// remove node
graph._removeNode(destKey);
}
if (destination.id != null) {
// if present, remove existent ID association
graph.removeId(type, destination.id!, notify: false);
// and associate ID with source key
graph.getKeyForId(type, destination.id, keyIfAbsent: source._key);
}
}
return destination;
}