1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.DefaultBelongsToRepository = void 0;
|
8 | const lodash_1 = require("lodash");
|
9 | const __1 = require("../../");
|
10 | const repositories_1 = require("../../repositories");
|
11 | class DefaultBelongsToRepository {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | constructor(getTargetRepository, constraint, targetResolver) {
|
25 | this.getTargetRepository = getTargetRepository;
|
26 | this.constraint = constraint;
|
27 | this.targetResolver = targetResolver;
|
28 | if (typeof getTargetRepository === 'function') {
|
29 | this.getTargetRepositoryDict = {
|
30 | [targetResolver().name]: getTargetRepository,
|
31 | };
|
32 | }
|
33 | else {
|
34 | this.getTargetRepositoryDict = getTargetRepository;
|
35 | }
|
36 | }
|
37 | async get(options) {
|
38 | let polymorphicTypes = options === null || options === void 0 ? void 0 : options.polymorphicType;
|
39 | let allKeys;
|
40 | if (Object.keys(this.getTargetRepositoryDict).length <= 1) {
|
41 | allKeys = Object.keys(this.getTargetRepositoryDict);
|
42 | }
|
43 | else if (!polymorphicTypes || polymorphicTypes.length === 0) {
|
44 | console.warn('It is highly recommended to specify the polymorphicTypes param when using polymorphic types.');
|
45 | allKeys = Object.keys(this.getTargetRepositoryDict);
|
46 | }
|
47 | else {
|
48 | if (typeof polymorphicTypes === 'string') {
|
49 | polymorphicTypes = [polymorphicTypes];
|
50 | }
|
51 | allKeys = [];
|
52 | new Set(polymorphicTypes).forEach(element => {
|
53 | if (Object.keys(this.getTargetRepositoryDict).includes(element)) {
|
54 | allKeys.push(element);
|
55 | }
|
56 | });
|
57 | }
|
58 | let result = [];
|
59 | for (const key of allKeys) {
|
60 | const targetRepository = await this.getTargetRepositoryDict[key]();
|
61 | result = result.concat(await targetRepository.find((0, repositories_1.constrainFilter)(undefined, this.constraint), Object.assign((0, lodash_1.cloneDeep)(options !== null && options !== void 0 ? options : {}), { polymorphicType: key })));
|
62 | if (result.length >= 1) {
|
63 | return result[0];
|
64 | }
|
65 | }
|
66 |
|
67 | const id = 'constraint ' + JSON.stringify(this.constraint);
|
68 | throw new __1.EntityNotFoundError(this.targetResolver().name, id);
|
69 | }
|
70 | }
|
71 | exports.DefaultBelongsToRepository = DefaultBelongsToRepository;
|
72 |
|
\ | No newline at end of file |