UNPKG

3.54 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.resolveHasManyMetaHelper = exports.resolveHasManyMetadata = 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-many:helpers');
15/**
16 * Resolves given hasMany 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 * belongsTo metadata
19 * @param relationMeta - hasMany metadata to resolve
20 * @internal
21 */
22function resolveHasManyMetadata(relationMeta) {
23 var _a;
24 // some checks and relationMeta.keyFrom are handled in here
25 relationMeta = resolveHasManyMetaHelper(relationMeta);
26 const targetModel = relationMeta.target();
27 const targetModelProperties = (_a = targetModel.definition) === null || _a === void 0 ? void 0 : _a.properties;
28 const sourceModel = relationMeta.source;
29 if (relationMeta.keyTo && targetModelProperties[relationMeta.keyTo]) {
30 // The explicit cast is needed because of a limitation of type inference
31 return relationMeta;
32 }
33 debug('Resolved model %s from given metadata: %o', targetModel.modelName, targetModel);
34 const defaultFkName = (0, lodash_1.camelCase)(sourceModel.modelName + '_id');
35 const hasDefaultFkProperty = targetModelProperties[defaultFkName];
36 if (!hasDefaultFkProperty) {
37 const reason = `target model ${targetModel.name} is missing definition of foreign key ${defaultFkName}`;
38 throw new errors_1.InvalidRelationError(reason, relationMeta);
39 }
40 return Object.assign(relationMeta, {
41 keyTo: defaultFkName,
42 });
43}
44exports.resolveHasManyMetadata = resolveHasManyMetadata;
45/**
46 * A helper to check relation type and the existence of the source/target models
47 * and set up keyFrom
48 * for HasMany(Through) relations
49 * @param relationMeta
50 *
51 * @returns relationMeta that has set up keyFrom
52 */
53function resolveHasManyMetaHelper(relationMeta) {
54 if (relationMeta.type !== relation_types_1.RelationType.hasMany) {
55 const reason = 'relation type must be HasMany';
56 throw new errors_1.InvalidRelationError(reason, relationMeta);
57 }
58 if (!(0, type_resolver_1.isTypeResolver)(relationMeta.target)) {
59 const reason = 'target must be a type resolver';
60 throw new errors_1.InvalidRelationError(reason, relationMeta);
61 }
62 const sourceModel = relationMeta.source;
63 if (!(sourceModel === null || sourceModel === void 0 ? void 0 : sourceModel.modelName)) {
64 const reason = 'source model must be defined';
65 throw new errors_1.InvalidRelationError(reason, relationMeta);
66 }
67 let keyFrom;
68 if (relationMeta.keyFrom &&
69 relationMeta.source.definition.properties[relationMeta.keyFrom]) {
70 keyFrom = relationMeta.keyFrom;
71 }
72 else {
73 keyFrom = sourceModel.getIdProperties()[0];
74 }
75 return Object.assign(relationMeta, { keyFrom });
76}
77exports.resolveHasManyMetaHelper = resolveHasManyMetaHelper;
78//# sourceMappingURL=has-many.helpers.js.map
\No newline at end of file