getKeyForId method
Finds a model's key in the graph.
- Attempts a lookup by
type
/id
- If the key was not found, it returns a default
keyIfAbsent
(if provided) - It associates
keyIfAbsent
with the suppliedtype
/id
(if bothkeyIfAbsent
&type
/id
were provided)
Implementation
String? getKeyForId(String type, Object? id, {String? keyIfAbsent}) {
type = DataHelpers.internalTypeFor(type);
if (id != null) {
final namespace = id is int ? '_id_int' : '_id';
final namespacedId =
id.toString().typifyWith(type).namespaceWith(namespace);
if (_getNode(namespacedId) != null) {
final tos = _getEdge(namespacedId, metadata: '_key');
if (tos.isNotEmpty) {
final key = tos.first;
return key;
}
}
if (keyIfAbsent != null) {
// this means the method is instructed to
// create nodes and edges
_addNode(keyIfAbsent, notify: false);
_addNode(namespacedId, notify: false);
_removeEdges(keyIfAbsent,
metadata: '_id', inverseMetadata: '_key', notify: false);
_addEdge(keyIfAbsent, namespacedId,
metadata: '_id', inverseMetadata: '_key', notify: false);
return keyIfAbsent;
}
} else if (keyIfAbsent != null) {
// if no ID is supplied but keyIfAbsent is, create node for key
_addNode(keyIfAbsent, notify: false);
return keyIfAbsent;
}
return null;
}