@lobb-js/core 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lobb-js/core",
3
3
  "license": "UNLICENSED",
4
- "version": "0.24.0",
4
+ "version": "0.25.0",
5
5
  "type": "module",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -861,6 +861,7 @@ export class CollectionStore {
861
861
  }
862
862
  } else if (resolved.type === "m2m") {
863
863
  const { junctionCollection, parentFKField, targetFKField } = resolved.junction;
864
+ const isSelfReferential = collectionName === childCollectionName;
864
865
  const parentIds = [...new Set(results.map((r) => r["id"]).filter((id) => id != null).map(String))];
865
866
 
866
867
  if (parentIds.length === 0) {
@@ -868,23 +869,28 @@ export class CollectionStore {
868
869
  continue;
869
870
  }
870
871
 
871
- // Fetch junction rows to get target IDs per parent
872
- const junctionRecords = await this.findAll({
872
+ const targetIdsByParent: Record<string, Set<string>> = {};
873
+ for (const parentId of parentIds) targetIdsByParent[parentId] = new Set();
874
+
875
+ const junctionFilter = isSelfReferential
876
+ ? { $or: [{ [parentFKField]: { $in: parentIds } }, { [targetFKField]: { $in: parentIds } }] }
877
+ : { [parentFKField]: { $in: parentIds } };
878
+
879
+ const junctionRows = await this.findAll({
873
880
  collectionName: junctionCollection,
874
- params: { fields: `${parentFKField},${targetFKField}`, filter: { [parentFKField]: { $in: parentIds } } },
881
+ params: { fields: `${parentFKField},${targetFKField}`, filter: junctionFilter },
875
882
  triggeredBy: "INTERNAL",
876
883
  client,
877
884
  });
878
885
 
879
- const targetIdsByParent: Record<string, string[]> = {};
880
- for (const parentId of parentIds) targetIdsByParent[parentId] = [];
881
- for (const jr of junctionRecords.data) {
882
- const parentId = String(jr[parentFKField]);
883
- const targetId = String(jr[targetFKField]);
884
- if (targetIdsByParent[parentId]) targetIdsByParent[parentId].push(targetId);
886
+ for (const jr of junctionRows.data) {
887
+ const fwd = String(jr[parentFKField]);
888
+ const rev = String(jr[targetFKField]);
889
+ if (targetIdsByParent[fwd]) targetIdsByParent[fwd].add(rev);
890
+ if (isSelfReferential && targetIdsByParent[rev]) targetIdsByParent[rev].add(fwd);
885
891
  }
886
892
 
887
- const allTargetIds = [...new Set(Object.values(targetIdsByParent).flat())];
893
+ const allTargetIds = [...new Set(Object.values(targetIdsByParent).flatMap((s) => [...s]))];
888
894
  const targetMap: Record<string, any> = {};
889
895
 
890
896
  if (allTargetIds.length > 0) {
@@ -904,7 +910,7 @@ export class CollectionStore {
904
910
 
905
911
  for (const result of results) {
906
912
  const parentId = String(result["id"]);
907
- result[childCollectionName] = (targetIdsByParent[parentId] ?? []).map((id) => targetMap[id]).filter(Boolean);
913
+ result[childCollectionName] = [...(targetIdsByParent[parentId] ?? [])].map((id) => targetMap[id]).filter(Boolean);
908
914
  }
909
915
  } else {
910
916
  // Polymorphic — child records have polymorphic FK pointing to parent
@@ -387,7 +387,8 @@ export class ConfigManager {
387
387
  if ("type" in rel && rel.type === "polymorphic") return false;
388
388
  return (
389
389
  (rel as RegularRelation).from.collection === junctionName &&
390
- (rel as RegularRelation).to.collection === toCollection
390
+ (rel as RegularRelation).to.collection === toCollection &&
391
+ (rel as RegularRelation).from.field !== fromRelation.from.field
391
392
  );
392
393
  });
393
394