@allgemein/schema-api
Version:
Library for schema api
154 lines • 5.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractRef = void 0;
const lodash_1 = require("lodash");
const Constants_1 = require("./Constants");
const IClassRef_1 = require("../api/IClassRef");
const MetadataRegistry_1 = require("./registry/MetadataRegistry");
class AbstractRef {
constructor(type, name, object = null, namespace = Constants_1.DEFAULT_NAMESPACE) {
/**
* Namespace better registry of this entry
*/
this.namespace = Constants_1.DEFAULT_NAMESPACE;
this.namespace = namespace;
this.metaType = type;
this.name = name;
if ((0, IClassRef_1.isClassRef)(object)) {
this.object = object;
}
else {
this.object = object ? this.getClassRefFor(object, type) : null;
}
}
getNamespace() {
return this.namespace;
}
getSourceRef() {
return this.object;
}
getOptionsEntry() {
if (!this._cachedOptions) {
if (this.metaType === Constants_1.METATYPE_PROPERTY) {
this._cachedOptions = MetadataRegistry_1.MetadataRegistry.$().findCached(this.metaType, (x) => x.target === this.getClass(true) &&
x.propertyName === this.name &&
x.namespace === this.getNamespace());
if (!this._cachedOptions) {
this._cachedOptions = { target: this.getClass(true), propertyName: this.name, namespace: this.getNamespace() };
MetadataRegistry_1.MetadataRegistry.$().addCached(this.metaType, this._cachedOptions);
}
}
else if (this.metaType === Constants_1.METATYPE_SCHEMA) {
this._cachedOptions = MetadataRegistry_1.MetadataRegistry.$().findCached(this.metaType, (x) => x.name === this.name &&
x.namespace === this.getNamespace());
if (!this._cachedOptions) {
this._cachedOptions = { name: this.name, namespace: this.getNamespace() };
MetadataRegistry_1.MetadataRegistry.$().addCached(this.metaType, this._cachedOptions);
}
}
else {
this._cachedOptions = MetadataRegistry_1.MetadataRegistry.$().findCached(this.metaType, (x) => x.target === this.getClass(true) &&
x.namespace === this.getNamespace());
if (!this._cachedOptions) {
this._cachedOptions = { target: this.getClass(true), namespace: this.getNamespace() };
MetadataRegistry_1.MetadataRegistry.$().addCached(this.metaType, this._cachedOptions);
}
}
}
return this._cachedOptions;
}
getOptions(key, defaultValue = null) {
if (key) {
return (0, lodash_1.get)(this.getOptionsEntry(), key, defaultValue);
}
return this.getOptionsEntry();
}
setOptions(options) {
if (options && !(0, lodash_1.isEmpty)((0, lodash_1.keys)(options))) {
const opts = this.getOptionsEntry();
// if same object cause taken from MetadataRegistry then ignore setting
if (opts !== options) {
for (const k of (0, lodash_1.keys)(opts)) {
delete opts[k];
}
(0, lodash_1.assign)(opts, options);
}
}
}
setOption(key, value) {
const opts = this.getOptionsEntry();
(0, lodash_1.set)(opts, key, value);
}
hasOption(key) {
const opts = this.getOptionsEntry();
return (0, lodash_1.has)(opts, key);
}
/**
* Return class ref
*/
getClassRef() {
return this.object;
}
/**
* Get class for the entry
*
* @param create: create anonymous placeholder if no class exists
*/
getClass(create = false) {
return this.getClassRef().getClass(create);
}
/**
* Return the name of the class ref, if not exits then return null
*/
get originalName() {
if (this.object) {
return this.object.name;
}
return null;
}
/**
* Return internal name (same as calling storingName)
*/
get internalName() {
return this.storingName;
}
/**
* Return name in snake case notation
*/
get machineName() {
return (0, lodash_1.snakeCase)(this.name);
}
/**
* Return internal name, check if internalName is set else check if name options is present
*
*/
get storingName() {
let name = this.getOptions(Constants_1.C_INTERNAL_NAME, null);
if (name) {
return name;
}
if (this.metaType === Constants_1.METATYPE_PROPERTY) {
name = this.getOptions(Constants_1.C_NAME, null);
if (!name) {
name = (0, lodash_1.snakeCase)(this.name);
}
}
else {
if (this.name !== this.originalName) {
name = this.name;
}
else {
name = (0, lodash_1.snakeCase)(this.name);
}
}
return name;
}
/**
* Return supported primitive data types
*/
getSupportedDataTypes() {
return Constants_1.JS_DATA_TYPES;
}
}
exports.AbstractRef = AbstractRef;
//# sourceMappingURL=AbstractRef.js.map