join method
Implementation
List<Map<String, dynamic>> join(HarnCollection collectionB, String joinField,
{List<String> propertiesA = const [],
List<String> propertiesB = const []}) {
final Map<dynamic, List<Map<String, dynamic>>> indexB = {};
for (final documentB in collectionB.collection.values) {
final joinValueB = documentB[joinField];
if (!indexB.containsKey(joinValueB)) {
indexB[joinValueB] = [];
}
indexB[joinValueB]!.add(documentB);
}
final List<Map<String, dynamic>> result = [];
final Map<String, dynamic> joinedDocument = {};
for (final documentA in collection.values) {
final joinValueA = documentA[joinField];
final matchingDocumentsB = indexB[joinValueA];
if (matchingDocumentsB != null) {
for (final documentB in matchingDocumentsB) {
if (propertiesA.isEmpty) {
joinedDocument.addAll(documentA);
} else {
for (int i = 0; i < propertiesA.length; i++) {
if (documentA.containsKey(propertiesA[i])) {
joinedDocument[propertiesA[i]] = documentA[propertiesA[i]];
}
}
}
if (propertiesB.isEmpty) {
joinedDocument.addAll(documentB);
} else {
for (int i = 0; i < propertiesB.length; i++) {
if (documentB.containsKey(propertiesB[i])) {
joinedDocument[propertiesB[i]] = documentB[propertiesB[i]];
}
}
}
result.add(Map.from(joinedDocument));
joinedDocument.clear();
}
}
}
return result;
}