1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.belongsTo = void 0;
|
8 | const core_1 = require("@loopback/core");
|
9 | const decorators_1 = require("../../decorators");
|
10 | const relation_decorator_1 = require("../relation.decorator");
|
11 | const relation_types_1 = require("../relation.types");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function belongsTo(targetResolver, definition, propertyDefinition) {
|
21 | return function (decoratedTarget, decoratedKey) {
|
22 | var _a;
|
23 | const propType = (_a = core_1.MetadataInspector.getDesignTypeForProperty(decoratedTarget, decoratedKey)) !== null && _a !== void 0 ? _a : propertyDefinition === null || propertyDefinition === void 0 ? void 0 : propertyDefinition.type;
|
24 | if (!propType) {
|
25 | const fullPropName = core_1.DecoratorFactory.getTargetName(decoratedTarget, decoratedKey);
|
26 | throw new Error(`Cannot infer type of model property ${fullPropName} because ` +
|
27 | 'TypeScript compiler option `emitDecoratorMetadata` is not set. ' +
|
28 | 'Please enable `emitDecoratorMetadata` or use the third argument of ' +
|
29 | '`@belongsTo` decorator to specify the property type explicitly.');
|
30 | }
|
31 | const propMeta = Object.assign({},
|
32 |
|
33 | propertyDefinition,
|
34 |
|
35 | {
|
36 | type: propType,
|
37 |
|
38 |
|
39 |
|
40 | });
|
41 | (0, decorators_1.property)(propMeta)(decoratedTarget, decoratedKey);
|
42 |
|
43 |
|
44 | const relationName = decoratedKey.replace(/Id$/, '');
|
45 | const meta = Object.assign(
|
46 |
|
47 | {
|
48 | keyFrom: decoratedKey,
|
49 | name: relationName,
|
50 | },
|
51 |
|
52 | definition,
|
53 |
|
54 | {
|
55 | type: relation_types_1.RelationType.belongsTo,
|
56 | targetsMany: false,
|
57 | source: decoratedTarget.constructor,
|
58 | target: targetResolver,
|
59 | });
|
60 | (0, relation_decorator_1.relation)(meta)(decoratedTarget, decoratedKey);
|
61 | };
|
62 | }
|
63 | exports.belongsTo = belongsTo;
|
64 |
|
\ | No newline at end of file |