UNPKG

2.84 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.referencesOne = exports.embedsMany = exports.embedsOne = exports.getModelRelations = exports.relation = exports.RELATIONS_KEY = void 0;
8const core_1 = require("@loopback/core");
9const decorators_1 = require("../decorators");
10const relation_types_1 = require("./relation.types");
11exports.RELATIONS_KEY = 'loopback:relations';
12/**
13 * Decorator for relations
14 * @param definition
15 * @returns A property decorator
16 */
17function relation(definition) {
18 // Apply relation definition to the model class
19 return core_1.PropertyDecoratorFactory.createDecorator(exports.RELATIONS_KEY, definition, {
20 decoratorName: '@relation',
21 });
22}
23exports.relation = relation;
24/**
25 * Get metadata of all relations defined on a given model class.
26 *
27 * @param modelCtor - The model class (the constructor function).
28 * @returns A map of relation definitions
29 */
30function getModelRelations(modelCtor) {
31 // Build model definitions if `@model` is missing
32 const modelDef = (0, decorators_1.buildModelDefinition)(modelCtor);
33 return (modelDef === null || modelDef === void 0 ? void 0 : modelDef.relations) || {};
34}
35exports.getModelRelations = getModelRelations;
36//
37// placeholder decorators for relations that are not implemented yet
38// TODO: move these decorators to per-relation subdirectories
39//
40/**
41 * Decorator for embedsOne
42 * @param definition
43 * @returns A property decorator
44 */
45function embedsOne(definition) {
46 const rel = Object.assign({ type: relation_types_1.RelationType.embedsOne }, definition);
47 return core_1.PropertyDecoratorFactory.createDecorator(exports.RELATIONS_KEY, rel, {
48 decoratorName: '@embedsOne',
49 });
50}
51exports.embedsOne = embedsOne;
52/**
53 * Decorator for embedsMany
54 * @param definition
55 * @returns A property decorator
56 */
57function embedsMany(definition) {
58 const rel = Object.assign({ type: relation_types_1.RelationType.embedsMany }, definition);
59 return core_1.PropertyDecoratorFactory.createDecorator(exports.RELATIONS_KEY, rel, {
60 decoratorName: '@embedsMany',
61 });
62}
63exports.embedsMany = embedsMany;
64/**
65 * Decorator for referencesOne
66 * @param definition
67 * @returns A property decorator
68 */
69function referencesOne(definition) {
70 const rel = Object.assign({ type: relation_types_1.RelationType.referencesOne }, definition);
71 return core_1.PropertyDecoratorFactory.createDecorator(exports.RELATIONS_KEY, rel, {
72 decoratorName: '@referencesOne',
73 });
74}
75exports.referencesOne = referencesOne;
76//# sourceMappingURL=relation.decorator.js.map
\No newline at end of file