1 | ;
|
2 | // Copyright IBM Corp. and LoopBack contributors 2017,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
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.ModelMetadataHelper = void 0;
|
8 | const core_1 = require("@loopback/core");
|
9 | const model_1 = require("../model");
|
10 | const relations_1 = require("../relations");
|
11 | const model_decorator_1 = require("./model.decorator");
|
12 | class ModelMetadataHelper {
|
13 | /**
|
14 | * A utility function to simplify retrieving metadata from a target model and
|
15 | * its properties.
|
16 | * @param target - The class from which to retrieve metadata.
|
17 | * @param options - An options object for the MetadataInspector to customize
|
18 | * the output of the metadata retrieval functions.
|
19 | */
|
20 | static getModelMetadata(target, options) {
|
21 | const classDef = core_1.MetadataInspector.getClassMetadata(model_decorator_1.MODEL_WITH_PROPERTIES_KEY, target,
|
22 | // https://github.com/loopbackio/loopback-next/issues/4721
|
23 | // The `target` can be a subclass for a base model
|
24 | { ...options, ownMetadataOnly: true });
|
25 | // Return the cached value, if it exists.
|
26 | // XXX(kjdelisle): If we're going to support dynamic updates, then this
|
27 | // will be problematic in the future, since it will never update.
|
28 | if (classDef) {
|
29 | return classDef;
|
30 | }
|
31 | else {
|
32 | const modelMeta = core_1.MetadataInspector.getClassMetadata(model_decorator_1.MODEL_KEY, target, options);
|
33 | if (!modelMeta) {
|
34 | return {};
|
35 | }
|
36 | else {
|
37 | // sets the metadata to a dedicated key if cached value does not exist
|
38 | // set ModelDefinition properties if they don't already exist
|
39 | const meta = new model_1.ModelDefinition({ ...modelMeta });
|
40 | // set properties lost from creating instance of ModelDefinition
|
41 | Object.assign(meta, modelMeta);
|
42 | meta.properties = Object.assign(meta.properties, core_1.MetadataInspector.getAllPropertyMetadata(model_decorator_1.MODEL_PROPERTIES_KEY, target.prototype, options));
|
43 | meta.relations = Object.assign(meta.relations, core_1.MetadataInspector.getAllPropertyMetadata(relations_1.RELATIONS_KEY, target.prototype, options));
|
44 | core_1.MetadataInspector.defineMetadata(model_decorator_1.MODEL_WITH_PROPERTIES_KEY.key, meta, target);
|
45 | return meta;
|
46 | }
|
47 | }
|
48 | }
|
49 | }
|
50 | exports.ModelMetadataHelper = ModelMetadataHelper;
|
51 | //# sourceMappingURL=metadata.js.map |
\ | No newline at end of file |