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