1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.resolveHasManyMetaHelper = exports.resolveHasManyMetadata = 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-many:helpers');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function resolveHasManyMetadata(relationMeta) {
|
23 | var _a;
|
24 |
|
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 |
|
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 | }
|
44 | exports.resolveHasManyMetadata = resolveHasManyMetadata;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function 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 | }
|
77 | exports.resolveHasManyMetaHelper = resolveHasManyMetaHelper;
|
78 |
|
\ | No newline at end of file |