1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.resolveHasOneMetadata = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const debug_1 = tslib_1.__importDefault(require("debug"));
|
10 | const lodash_1 = require("lodash");
|
11 | const errors_1 = require("../../errors");
|
12 | const type_resolver_1 = require("../../type-resolver");
|
13 | const relation_types_1 = require("../relation.types");
|
14 | const debug = (0, debug_1.default)('loopback:repository:relations:has-one:helpers');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function resolveHasOneMetadata(relationMeta) {
|
23 | var _a;
|
24 | if (relationMeta.type !== relation_types_1.RelationType.hasOne) {
|
25 | const reason = 'relation type must be HasOne';
|
26 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
27 | }
|
28 | if (!(0, type_resolver_1.isTypeResolver)(relationMeta.target)) {
|
29 | const reason = 'target must be a type resolver';
|
30 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
31 | }
|
32 | const targetModel = relationMeta.target();
|
33 | const targetModelProperties = (_a = targetModel.definition) === null || _a === void 0 ? void 0 : _a.properties;
|
34 | const sourceModel = relationMeta.source;
|
35 | if (!(sourceModel === null || sourceModel === void 0 ? void 0 : sourceModel.modelName)) {
|
36 | const reason = 'source model must be defined';
|
37 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
38 | }
|
39 |
|
40 | let keyFrom;
|
41 | if (relationMeta.keyFrom &&
|
42 | relationMeta.source.definition.properties[relationMeta.keyFrom]) {
|
43 | keyFrom = relationMeta.keyFrom;
|
44 | }
|
45 | else {
|
46 | keyFrom = sourceModel.getIdProperties()[0];
|
47 | }
|
48 | let keyTo;
|
49 |
|
50 |
|
51 | if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
|
52 |
|
53 | keyTo = relationMeta.keyTo;
|
54 | }
|
55 | else {
|
56 | debug('Resolved model %s from given metadata: %o', targetModel.modelName, targetModel);
|
57 | keyTo = (0, lodash_1.camelCase)(sourceModel.modelName + '_id');
|
58 | const hasDefaultFkProperty = targetModelProperties[keyTo];
|
59 | if (!hasDefaultFkProperty) {
|
60 | const reason = `target model ${targetModel.name} is missing definition of foreign key ${keyTo}`;
|
61 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
62 | }
|
63 | }
|
64 | let polymorphic;
|
65 | if (relationMeta.polymorphic === undefined ||
|
66 | relationMeta.polymorphic === false ||
|
67 | !relationMeta.polymorphic) {
|
68 | const polymorphicFalse = false;
|
69 | polymorphic = polymorphicFalse;
|
70 | }
|
71 | else {
|
72 | if (relationMeta.polymorphic === true) {
|
73 | const polymorphicObject = {
|
74 | discriminator: (0, lodash_1.camelCase)(relationMeta.target().name + '_type'),
|
75 | };
|
76 | polymorphic = polymorphicObject;
|
77 | }
|
78 | else {
|
79 | const polymorphicObject = relationMeta.polymorphic;
|
80 | polymorphic = polymorphicObject;
|
81 | }
|
82 | }
|
83 | return Object.assign(relationMeta, {
|
84 | keyFrom: keyFrom,
|
85 | keyTo: keyTo,
|
86 | polymorphic: polymorphic,
|
87 | });
|
88 | }
|
89 | exports.resolveHasOneMetadata = resolveHasOneMetadata;
|
90 |
|
\ | No newline at end of file |