1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.resolveBelongsToMetadata = 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:belongs-to:helpers');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function resolveBelongsToMetadata(relationMeta) {
|
23 | if (relationMeta.type !== relation_types_1.RelationType.belongsTo) {
|
24 | const reason = 'relation type must be BelongsTo';
|
25 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
26 | }
|
27 | if (!(0, type_resolver_1.isTypeResolver)(relationMeta.target)) {
|
28 | const reason = 'target must be a type resolver';
|
29 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
30 | }
|
31 | const sourceModel = relationMeta.source;
|
32 | if (!(sourceModel === null || sourceModel === void 0 ? void 0 : sourceModel.modelName)) {
|
33 | const reason = 'source model must be defined';
|
34 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
35 | }
|
36 | const targetModel = relationMeta.target();
|
37 | const targetName = targetModel.modelName;
|
38 | debug('Resolved model %s from given metadata: %o', targetName, targetModel);
|
39 | let keyFrom;
|
40 | if (relationMeta.keyFrom &&
|
41 | relationMeta.source.definition.properties[relationMeta.keyFrom]) {
|
42 | keyFrom = relationMeta.keyFrom;
|
43 | }
|
44 | else {
|
45 | keyFrom = (0, lodash_1.camelCase)(targetName + '_id');
|
46 | }
|
47 | const targetProperties = targetModel.definition.properties;
|
48 | debug('relation metadata from %o: %o', targetName, targetProperties);
|
49 | let keyTo;
|
50 | if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
51 |
|
52 | keyTo = relationMeta.keyTo;
|
53 | }
|
54 | else {
|
55 | keyTo = targetModel.definition.idProperties()[0];
|
56 | if (!keyTo) {
|
57 | const reason = `${targetName} does not have any primary key (id property)`;
|
58 | throw new errors_1.InvalidRelationError(reason, relationMeta);
|
59 | }
|
60 | }
|
61 | let polymorphic;
|
62 | if (relationMeta.polymorphic === undefined ||
|
63 | relationMeta.polymorphic === false ||
|
64 | !relationMeta.polymorphic) {
|
65 | const polymorphicFalse = false;
|
66 | polymorphic = polymorphicFalse;
|
67 | }
|
68 | else {
|
69 | if (relationMeta.polymorphic === true) {
|
70 | const polymorphicObject = {
|
71 | discriminator: (0, lodash_1.camelCase)(relationMeta.target().name + '_type'),
|
72 | };
|
73 | polymorphic = polymorphicObject;
|
74 | }
|
75 | else {
|
76 | const polymorphicObject = relationMeta.polymorphic;
|
77 | polymorphic = polymorphicObject;
|
78 | }
|
79 | }
|
80 | return Object.assign(relationMeta, {
|
81 | keyFrom,
|
82 | keyTo: keyTo,
|
83 | polymorphic: polymorphic,
|
84 | });
|
85 | }
|
86 | exports.resolveBelongsToMetadata = resolveBelongsToMetadata;
|
87 |
|
\ | No newline at end of file |