UNPKG

4.03 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2018,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.referencesMany = void 0;
8const core_1 = require("@loopback/core");
9const decorators_1 = require("../../decorators");
10const relation_decorator_1 = require("../relation.decorator");
11const relation_types_1 = require("../relation.types");
12/**
13 * Decorator for referencesMany
14 * @param targetResolver - A resolver function that returns the target model for
15 * a referencesMany relation
16 * @param definition - Optional metadata for setting up a referencesMany relation
17 * @param propertyDefinition - Optional metadata for setting up the property
18 * @returns A property decorator
19 */
20function referencesMany(targetResolver, definition, propertyDefinition) {
21 return function (decoratedTarget, decoratedKey) {
22 var _a, _b, _c;
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 '`@referencesMany` decorator to specify the property type explicitly.');
30 }
31 const sourceKeyType = core_1.MetadataInspector.getDesignTypeForProperty(targetResolver().prototype, (_b = definition === null || definition === void 0 ? void 0 : definition.keyTo) !== null && _b !== void 0 ? _b : 'id');
32 if (!sourceKeyType) {
33 const fullPropName = core_1.DecoratorFactory.getTargetName(targetResolver().prototype, (_c = definition === null || definition === void 0 ? void 0 : definition.keyTo) !== null && _c !== void 0 ? _c : 'id');
34 throw new Error(`Cannot infer type of model property ${fullPropName} because ` +
35 'TypeScript compiler option `emitDecoratorMetadata` is not set. ' +
36 'Please enable `emitDecoratorMetadata` or use the second argument of ' +
37 '`@referencesMany` decorator to specify the property type explicitly.');
38 }
39 const propMeta = Object.assign({},
40 // properties provided by the caller
41 propertyDefinition,
42 // properties enforced by the decorator
43 {
44 type: propType,
45 itemType: sourceKeyType,
46 // TODO(bajtos) Make the foreign key required once our REST API layer
47 // allows controller methods to exclude required properties
48 // required: true,
49 });
50 (0, decorators_1.property)(propMeta)(decoratedTarget, decoratedKey);
51 // @referencesMany() is typically decorating the foreign key property,
52 // e.g. customerIds. We need to strip the trailing "Ids" suffix from the name.
53 const relationName = decoratedKey.replace(/Ids$/, 's');
54 const meta = Object.assign(
55 // default values, can be customized by the caller
56 {
57 keyFrom: decoratedKey,
58 name: relationName,
59 },
60 // properties provided by the caller
61 definition,
62 // properties enforced by the decorator
63 {
64 type: relation_types_1.RelationType.referencesMany,
65 targetsMany: true,
66 source: decoratedTarget.constructor,
67 target: targetResolver,
68 });
69 (0, relation_decorator_1.relation)(meta)(decoratedTarget, decoratedKey);
70 };
71}
72exports.referencesMany = referencesMany;
73//# sourceMappingURL=references-many.decorator.js.map
\No newline at end of file