1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.createHasOneInclusionResolver = void 0;
|
8 | const lodash_1 = require("lodash");
|
9 | const __1 = require("../../");
|
10 | const relation_helpers_1 = require("../relation.helpers");
|
11 | const has_one_helpers_1 = require("./has-one.helpers");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function createHasOneInclusionResolver(meta, getTargetRepoDict) {
|
24 | const relationMeta = (0, has_one_helpers_1.resolveHasOneMetadata)(meta);
|
25 | return async function fetchHasOneModel(entities, inclusion, options) {
|
26 | if (!entities.length)
|
27 | return [];
|
28 |
|
29 |
|
30 | const sourceKey = relationMeta.keyFrom;
|
31 | const targetKey = relationMeta.keyTo;
|
32 | const targetDiscriminator = relationMeta.polymorphic
|
33 | ? relationMeta.polymorphic.discriminator
|
34 | : undefined;
|
35 | const scope = typeof inclusion === 'string' ? {} : inclusion.scope;
|
36 |
|
37 | const sourceIdsCategorized = {};
|
38 | if (targetDiscriminator) {
|
39 | entities.forEach((value, index, allEntites) => {
|
40 | const concreteType = String(value[targetDiscriminator]);
|
41 | if (!getTargetRepoDict[concreteType]) {
|
42 | throw new __1.InvalidPolymorphismError(concreteType, targetDiscriminator);
|
43 | }
|
44 | if (!sourceIdsCategorized[concreteType]) {
|
45 | sourceIdsCategorized[concreteType] = [];
|
46 | }
|
47 | sourceIdsCategorized[concreteType].push(value[sourceKey]);
|
48 | });
|
49 | }
|
50 | else {
|
51 | const concreteType = relationMeta.target().name;
|
52 | if (!getTargetRepoDict[concreteType]) {
|
53 | throw new __1.InvalidPolymorphismError(concreteType);
|
54 | }
|
55 | entities.forEach((value, index, allEntites) => {
|
56 | if (!sourceIdsCategorized[concreteType]) {
|
57 | sourceIdsCategorized[concreteType] = [];
|
58 | }
|
59 | sourceIdsCategorized[concreteType].push(value[sourceKey]);
|
60 | });
|
61 | }
|
62 |
|
63 | const changedTargetKeyField = (0, __1.includeFieldIfNot)(scope === null || scope === void 0 ? void 0 : scope.fields, targetKey);
|
64 | let needToRemoveTargetKeyFieldLater = false;
|
65 | if (changedTargetKeyField !== false) {
|
66 | scope.fields = changedTargetKeyField;
|
67 | needToRemoveTargetKeyFieldLater = true;
|
68 | }
|
69 |
|
70 | const targetCategorized = {};
|
71 | for (const k of Object.keys(sourceIdsCategorized)) {
|
72 | const targetRepo = await getTargetRepoDict[k]();
|
73 | const targetsFound = await (0, relation_helpers_1.findByForeignKeys)(targetRepo, targetKey, sourceIdsCategorized[k], scope, Object.assign((0, lodash_1.cloneDeep)(options !== null && options !== void 0 ? options : {}), { polymorphicType: k }));
|
74 | targetCategorized[k] = (0, relation_helpers_1.flattenTargetsOfOneToOneRelation)(sourceIdsCategorized[k], targetsFound, targetKey);
|
75 |
|
76 | if (needToRemoveTargetKeyFieldLater) {
|
77 | targetCategorized[k] = targetCategorized[k].map(e => {
|
78 | if (e) {
|
79 | delete e[targetKey];
|
80 | }
|
81 | return e;
|
82 | });
|
83 | }
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | if (targetDiscriminator) {
|
98 | const allTargets = [];
|
99 | entities.forEach((value, index, allEntites) => {
|
100 | allTargets.push(targetCategorized[String(value[targetDiscriminator])].shift());
|
101 | });
|
102 | return allTargets;
|
103 | }
|
104 | else {
|
105 | return targetCategorized[relationMeta.target().name];
|
106 | }
|
107 | };
|
108 | }
|
109 | exports.createHasOneInclusionResolver = createHasOneInclusionResolver;
|
110 |
|
\ | No newline at end of file |