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