1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.property = exports.buildModelDefinition = exports.model = exports.MODEL_WITH_PROPERTIES_KEY = exports.MODEL_PROPERTIES_KEY = exports.MODEL_KEY = void 0;
|
8 | const core_1 = require("@loopback/core");
|
9 | const model_1 = require("../model");
|
10 | const relation_decorator_1 = require("../relations/relation.decorator");
|
11 | exports.MODEL_KEY = core_1.MetadataAccessor.create('loopback:model');
|
12 | exports.MODEL_PROPERTIES_KEY = core_1.MetadataAccessor.create('loopback:model-properties');
|
13 | exports.MODEL_WITH_PROPERTIES_KEY = core_1.MetadataAccessor.create('loopback:model-and-properties');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function 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 |
|
29 | buildModelDefinition(target, def);
|
30 | };
|
31 | }
|
32 | exports.model = model;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function buildModelDefinition(target, def) {
|
39 | var _a, _b, _c, _d;
|
40 |
|
41 |
|
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 |
|
73 | Object.values(relationMeta).forEach(r => {
|
74 | relations[r.name] = r;
|
75 | });
|
76 | target.definition.relations = relations;
|
77 | return modelDef;
|
78 | }
|
79 | exports.buildModelDefinition = buildModelDefinition;
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function property(definition) {
|
86 | return core_1.PropertyDecoratorFactory.createDecorator(exports.MODEL_PROPERTIES_KEY, Object.assign({}, definition), { decoratorName: '@property' });
|
87 | }
|
88 | exports.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 |
|
95 |
|
96 |
|
97 |
|
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 = property = {}));
|
112 |
|
\ | No newline at end of file |