UNPKG

3.82 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
3// Node module: @loopback/repository
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.resolveHasOneMetadata = void 0;
8const tslib_1 = require("tslib");
9const debug_1 = tslib_1.__importDefault(require("debug"));
10const lodash_1 = require("lodash");
11const errors_1 = require("../../errors");
12const type_resolver_1 = require("../../type-resolver");
13const relation_types_1 = require("../relation.types");
14const debug = (0, debug_1.default)('loopback:repository:relations:has-one:helpers');
15/**
16 * Resolves given hasOne metadata if target is specified to be a resolver.
17 * Mainly used to infer what the `keyTo` property should be from the target's
18 * hasOne metadata
19 * @param relationMeta - hasOne metadata to resolve
20 * @internal
21 */
22function 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 // keyFrom defaults to id property
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 // Make sure that if it already keys to the foreign key property,
50 // the key exists in the target model
51 if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
52 // The explicit cast is needed because of a limitation of type inference
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}
89exports.resolveHasOneMetadata = resolveHasOneMetadata;
90//# sourceMappingURL=has-one.helpers.js.map
\No newline at end of file