UNPKG

1.47 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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.hasOne = void 0;
8const relation_decorator_1 = require("../relation.decorator");
9const relation_types_1 = require("../relation.types");
10/*
11 * Decorator for hasOne
12 * infers foreign key name from target model name unless explicitly specified
13 * @param targetResolver - Target model for hasOne relation
14 * @param definition - Optional metadata for setting up hasOne relation
15 * @returns A property decorator
16 */
17function hasOne(targetResolver, definition) {
18 return function (decoratedTarget, key) {
19 // property.array(targetResolver)(decoratedTarget, key);
20 const meta = Object.assign(
21 // default values, can be customized by the caller
22 {},
23 // properties provided by the caller
24 definition,
25 // properties enforced by the decorator
26 {
27 type: relation_types_1.RelationType.hasOne,
28 targetsMany: false,
29 name: key,
30 source: decoratedTarget.constructor,
31 target: targetResolver,
32 });
33 (0, relation_decorator_1.relation)(meta)(decoratedTarget, key);
34 };
35}
36exports.hasOne = hasOne;
37//# sourceMappingURL=has-one.decorator.js.map
\No newline at end of file