joist-core 2.3.0-next.58 → 2.3.0-next.59

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.
@@ -496,6 +496,7 @@ var EntityManager = class EntityManager {
496
496
  skipIf,
497
497
  skip
498
498
  });
499
+ const deferredRelations = [];
499
500
  const clones = [...todo].map((entity) => {
500
501
  const skip = skipMap.get(entity) ?? [];
501
502
  const meta = require_EntityMetadata.getMetadata(entity);
@@ -504,10 +505,22 @@ var EntityManager = class EntityManager {
504
505
  switch (f.kind) {
505
506
  case "primitive": if (!f.derived && !f.protected) return [f.fieldName, require_fields.getField(entity, f.fieldName)];
506
507
  else return;
507
- case "m2o":
508
508
  case "enum": if (f.derived) return;
509
509
  else return [f.fieldName, require_fields.getField(entity, f.fieldName)];
510
- case "poly": return [f.fieldName, require_fields.getField(entity, f.fieldName)];
510
+ case "m2o":
511
+ case "poly": {
512
+ if (f.kind === "m2o" && f.derived) return void 0;
513
+ const existingIdOrEntity = require_fields.getField(entity, f.fieldName);
514
+ const target = require_Entity.isEntity(existingIdOrEntity) ? existingIdOrEntity : this.getEntity(existingIdOrEntity);
515
+ if (target && todo.has(target)) {
516
+ deferredRelations.push([
517
+ entity,
518
+ f.fieldName,
519
+ target
520
+ ]);
521
+ return [f.fieldName, void 0];
522
+ } else return [f.fieldName, existingIdOrEntity];
523
+ }
511
524
  case "primaryKey":
512
525
  case "o2m":
513
526
  case "m2m":
@@ -529,6 +542,9 @@ var EntityManager = class EntityManager {
529
542
  }
530
543
  });
531
544
  });
545
+ deferredRelations.forEach(([source, fieldName, target]) => {
546
+ entityToClone.get(source)[fieldName].set(entityToClone.get(target));
547
+ });
532
548
  if (postClone) await Promise.all(clones.map(([original, clone]) => postClone(original, clone)));
533
549
  return Array.isArray(entityOrArray) ? entityOrArray.filter((original) => entityToClone.has(original)).map((original) => entityToClone.get(original)) : clones[0] ? clones[0][1] : require_utils.fail("no entities were cloned given the provided options");
534
550
  }