@goatlab/fluent
Version:
Readable query Interface & API generator for TS and Node
49 lines • 2.81 kB
JavaScript
;
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelMetadataHelper = exports.MODEL_WITH_PROPERTIES_KEY = exports.MODEL_PROPERTIES_KEY = exports.MODEL_KEY = void 0;
const metadata_1 = require("@loopback/metadata");
const model_1 = require("./model");
const RELATIONS_KEY = 'loopback:relations';
exports.MODEL_KEY = metadata_1.MetadataAccessor.create('loopback:model');
exports.MODEL_PROPERTIES_KEY = metadata_1.MetadataAccessor.create('loopback:model-properties');
exports.MODEL_WITH_PROPERTIES_KEY = metadata_1.MetadataAccessor.create('loopback:model-and-properties');
class ModelMetadataHelper {
/**
* A utility function to simplify retrieving metadata from a target model and
* its properties.
* @param target - The class from which to retrieve metadata.
* @param options - An options object for the MetadataInspector to customize
* the output of the metadata retrieval functions.
*/
static getModelMetadata(target, options) {
const classDef = metadata_1.MetadataInspector.getClassMetadata(exports.MODEL_WITH_PROPERTIES_KEY, target,
// https://github.com/loopbackio/loopback-next/issues/4721
// The `target` can be a subclass for a base model
{ ...options, ownMetadataOnly: true });
// Return the cached value, if it exists.
// XXX(kjdelisle): If we're going to support dynamic updates, then this
// will be problematic in the future, since it will never update.
if (classDef) {
return classDef;
}
const modelMeta = metadata_1.MetadataInspector.getClassMetadata(exports.MODEL_KEY, target, options);
if (!modelMeta) {
return {};
}
// sets the metadata to a dedicated key if cached value does not exist
// set ModelDefinition properties if they don't already exist
const meta = new model_1.ModelDefinition({ ...modelMeta });
// set properties lost from creating instance of ModelDefinition
Object.assign(meta, modelMeta);
meta.properties = Object.assign(meta.properties, metadata_1.MetadataInspector.getAllPropertyMetadata(exports.MODEL_PROPERTIES_KEY, target.prototype, options));
meta.relations = Object.assign(meta.relations, metadata_1.MetadataInspector.getAllPropertyMetadata(RELATIONS_KEY, target.prototype, options));
metadata_1.MetadataInspector.defineMetadata(exports.MODEL_WITH_PROPERTIES_KEY.key, meta, target);
return meta;
}
}
exports.ModelMetadataHelper = ModelMetadataHelper;
//# sourceMappingURL=metadata.js.map