UNPKG

5.18 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.property = exports.buildModelDefinition = exports.model = exports.MODEL_WITH_PROPERTIES_KEY = exports.MODEL_PROPERTIES_KEY = exports.MODEL_KEY = void 0;
8const core_1 = require("@loopback/core");
9const model_1 = require("../model");
10const relation_decorator_1 = require("../relations/relation.decorator");
11exports.MODEL_KEY = core_1.MetadataAccessor.create('loopback:model');
12exports.MODEL_PROPERTIES_KEY = core_1.MetadataAccessor.create('loopback:model-properties');
13exports.MODEL_WITH_PROPERTIES_KEY = core_1.MetadataAccessor.create('loopback:model-and-properties');
14/**
15 * Decorator for model definitions
16 * @param definition
17 * @returns A class decorator for `model`
18 */
19function model(definition) {
20 return function (target) {
21 var _a;
22 definition = definition !== null && definition !== void 0 ? definition : {};
23 const def = Object.assign(definition, {
24 name: (_a = definition.name) !== null && _a !== void 0 ? _a : target.name,
25 });
26 const decorator = core_1.ClassDecoratorFactory.createDecorator(exports.MODEL_KEY, definition, { decoratorName: '@model' });
27 decorator(target);
28 // Build "ModelDefinition" and store it on model constructor
29 buildModelDefinition(target, def);
30 };
31}
32exports.model = model;
33/**
34 * Build model definition from decorations
35 * @param target - Target model class
36 * @param def - Model definition spec
37 */
38function buildModelDefinition(target, def) {
39 var _a, _b, _c, _d;
40 // Check if the definition for this class has been built (not from the super
41 // class)
42 const baseClass = Object.getPrototypeOf(target);
43 if (!def &&
44 target.definition &&
45 baseClass &&
46 target.definition !== baseClass.definition) {
47 return target.definition;
48 }
49 const modelDef = new model_1.ModelDefinition(def !== null && def !== void 0 ? def : { name: target.name });
50 const prototype = target.prototype;
51 const propertyMap = (_a = core_1.MetadataInspector.getAllPropertyMetadata(exports.MODEL_PROPERTIES_KEY, prototype)) !== null && _a !== void 0 ? _a : {};
52 for (const [propName, propDef] of Object.entries(propertyMap)) {
53 const designType = (_b = propDef.type) !== null && _b !== void 0 ? _b : core_1.MetadataInspector.getDesignTypeForProperty(prototype, propName);
54 if (!designType) {
55 const err = new Error(`The definition of model property ${modelDef.name}.${propName} is missing ` +
56 '`type` field and TypeScript did not provide any design-time type. ' +
57 'Learn more at https://loopback.io/doc/en/lb4/Error-codes.html#cannot_infer_property_type');
58 err.code = 'CANNOT_INFER_PROPERTY_TYPE';
59 throw err;
60 }
61 if (propDef.hidden) {
62 modelDef.settings.hiddenProperties =
63 (_c = modelDef.settings.hiddenProperties) !== null && _c !== void 0 ? _c : [];
64 modelDef.settings.hiddenProperties.push(propName);
65 }
66 propDef.type = designType;
67 modelDef.addProperty(propName, propDef);
68 }
69 target.definition = modelDef;
70 const relationMeta = (_d = core_1.MetadataInspector.getAllPropertyMetadata(relation_decorator_1.RELATIONS_KEY, prototype)) !== null && _d !== void 0 ? _d : {};
71 const relations = {};
72 // Build an object keyed by relation names
73 Object.values(relationMeta).forEach(r => {
74 relations[r.name] = r;
75 });
76 target.definition.relations = relations;
77 return modelDef;
78}
79exports.buildModelDefinition = buildModelDefinition;
80/**
81 * Decorator for model properties
82 * @param definition
83 * @returns A property decorator
84 */
85function property(definition) {
86 return core_1.PropertyDecoratorFactory.createDecorator(exports.MODEL_PROPERTIES_KEY, Object.assign({}, definition), { decoratorName: '@property' });
87}
88exports.property = property;
89(function (property) {
90 property.ERR_PROP_NOT_ARRAY = '@property.array can only decorate array properties!';
91 property.ERR_NO_ARGS = 'decorator received less than two parameters';
92 /**
93 *
94 * @param itemType - The type of array items.
95 * Examples: `number`, `Product`, `() => Order`.
96 * @param definition - Optional PropertyDefinition object for additional
97 * metadata
98 */
99 function array(itemType, definition) {
100 return function (target, propertyName) {
101 const propType = core_1.MetadataInspector.getDesignTypeForProperty(target, propertyName);
102 if (propType !== Array) {
103 throw new Error(property.ERR_PROP_NOT_ARRAY);
104 }
105 else {
106 property(Object.assign({ type: Array, itemType }, definition))(target, propertyName);
107 }
108 };
109 }
110 property.array = array;
111})(property = exports.property || (exports.property = {}));
112//# sourceMappingURL=model.decorator.js.map
\No newline at end of file