hades-cli
Version:
Hades CLI developer tool
214 lines (213 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Property = void 0;
const types_1 = require("./../types");
const cliter_config_1 = require("./../config/cliter.config");
const faker = require("faker");
const _ = require("lodash");
class Property {
constructor(payload) {
// 'string' can't be used to index type 'Property'. No index signature with a
// parameter of type 'string' was found on type 'Property'.ts(7053)
this.config = cliter_config_1.cliterConfig;
this.id = faker.datatype.uuid();
this._name = payload.name;
this.type = payload.type;
this.primaryKey = payload.primaryKey;
this._enumOptions = payload.enumOptions;
this.decimals = payload.decimals;
this.length = payload.length;
this.minLength = payload.minLength;
this.maxLength = payload.maxLength;
this.nullable = payload.nullable;
this.defaultValue = payload.defaultValue;
this.relationship = payload.relationship;
this.relationshipSingularName = payload.relationshipSingularName;
this.relationshipAggregate = payload.relationshipAggregate;
this.relationshipModulePath = payload.relationshipModulePath;
this.relationshipKey = payload.relationshipKey;
this.relationshipField = payload.relationshipField;
this.intermediateTable = payload.intermediateTable;
this.intermediateModel = payload.intermediateModel;
this.intermediateModelModuleSection = payload.intermediateModelModuleSection;
this.intermediateModelFile = payload.intermediateModelFile;
this.index = payload.index;
this.example = payload.example;
this.faker = payload.faker;
if ((this.type === types_1.SqlType.VARCHAR ||
this.type === types_1.SqlType.INT ||
this.type === types_1.SqlType['INT.UNSIGNED'] ||
this.type === types_1.SqlType.SMALLINT ||
this.type === types_1.SqlType['SMALLINT.UNSIGNED']) &&
typeof this.length == 'number' && typeof this.maxLength !== 'number') {
this.maxLength = this.length;
this.length = undefined;
}
if (this.type === types_1.SqlType.DECIMAL) {
// set maxLength to validate in value object
this.maxLength = _.head(this.decimals);
}
}
// handlebars functions
get hasTimezone() {
return this.type === types_1.SqlType.TIMESTAMP;
}
get hasColumnDecorator() {
return this.relationship !== types_1.SqlRelationship.ONE_TO_MANY && this.relationship !== types_1.SqlRelationship.MANY_TO_MANY && !(this.relationship === types_1.SqlRelationship.ONE_TO_ONE && !this.relationshipField);
}
get hasHasOneDecorator() {
return this.relationship === types_1.SqlRelationship.ONE_TO_ONE && !this.relationshipField;
}
get hasBelongsToDecorator() {
return (this.relationship === types_1.SqlRelationship.MANY_TO_ONE && !!this.relationshipField) ||
(this.relationship === types_1.SqlRelationship.ONE_TO_ONE && !!this.relationshipField);
}
get hasHasManyDecorator() {
return this.relationship === types_1.SqlRelationship.ONE_TO_MANY;
}
get hasBelongsToManyDecorator() {
return this.relationship === types_1.SqlRelationship.MANY_TO_MANY;
}
get getDefaultValue() {
return typeof this.defaultValue === 'boolean' || typeof this.defaultValue === 'number' ? this.defaultValue : `'${this.defaultValue}'`;
}
get getReferenceKey() {
return this.relationshipKey ? this.relationshipKey : 'id';
}
// property names
get nativeName() {
return this._name;
}
get name() {
// properties that represent many to many relationships, are arrays of ids
if (this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return `${this.relationshipSingularName}Ids`;
return this._name;
}
get enumOptionsArrayItems() {
var _a;
return (_a = this.enumOptions) === null || _a === void 0 ? void 0 : _a.map(item => '\'' + item + '\'').join();
}
get enumOptions() {
return typeof this._enumOptions === 'string' ? this._enumOptions.split(',').map(item => item.trim().toUpperCase()) : undefined;
}
// morph properties
get nameGraphqlType() {
if (this.relationship === types_1.SqlRelationship.MANY_TO_ONE && this.relationshipField)
return this.relationshipField;
if (this.relationship === types_1.SqlRelationship.ONE_TO_ONE && this.relationshipField)
return this.relationshipField;
return this._name;
}
get getRelationshipBoundedContext() {
if (this.relationshipModulePath)
return this.parseModuleSection(this.relationshipModulePath).boundedContextName;
return null;
}
get getRelationshipModule() {
if (this.relationshipModulePath)
return this.parseModuleSection(this.relationshipModulePath).moduleName;
return null;
}
get getApiType() {
if (this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return this.config.sqlTypesEquivalenceApiTypes['manyToMany'];
return this.config.sqlTypesEquivalenceApiTypes[this.type];
}
get getJavascriptType() {
if (this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return this.config.sqlTypesEquivalenceJavascriptTypes['manyToMany'];
if (this.type === types_1.SqlType.RELATIONSHIP)
return `${this.relationshipAggregate}[]`;
return this.config.sqlTypesEquivalenceJavascriptTypes[this.type];
}
get getSequelizeType() {
let parameter;
if (this.type === types_1.SqlType.CHAR)
parameter = this.length; // parameter = length
if (this.type === types_1.SqlType.VARCHAR)
parameter = this.maxLength; // parameter = maxLength
if (this.type === types_1.SqlType.ENUM)
parameter = this.enumOptionsArrayItems; // parameter = values
if (this.type === types_1.SqlType.DECIMAL)
parameter = this.decimals; // parameter = decimals
return cliter_config_1.cliterConfig.sqlTypesEquivalenceSequelizeTypes[this.type](parameter);
}
get getGraphqlType() {
if (this.relationship === types_1.SqlRelationship.ONE_TO_MANY || this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return `[${this.relationshipAggregate}]`;
if (this.relationship === types_1.SqlRelationship.MANY_TO_ONE)
return `${this.relationshipAggregate}`;
if (this.relationship === types_1.SqlRelationship.ONE_TO_ONE)
return `${this.relationshipAggregate}`;
return this.config.sqlTypesEquivalenceQraphqlTypes[this.type];
}
get getGraphqlCreateType() {
var _a, _b;
if (this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return this.config.sqlTypesEquivalenceQraphqlTypes['manyToMany'];
if (this.relationship === types_1.SqlRelationship.ONE_TO_ONE && !this.relationshipField)
return `${(_a = this.getRelationshipBoundedContext) === null || _a === void 0 ? void 0 : _a.toPascalCase()}Create${(_b = this.getRelationshipModule) === null || _b === void 0 ? void 0 : _b.toPascalCase()}Input`;
return this.config.sqlTypesEquivalenceQraphqlTypes[this.type];
}
get getGraphqlUpdateType() {
var _a, _b;
if (this.relationship === types_1.SqlRelationship.MANY_TO_MANY)
return this.config.sqlTypesEquivalenceQraphqlTypes['manyToMany'];
if (this.relationship === types_1.SqlRelationship.ONE_TO_ONE && !this.relationshipField)
return `${(_a = this.getRelationshipBoundedContext) === null || _a === void 0 ? void 0 : _a.toPascalCase()}Update${(_b = this.getRelationshipModule) === null || _b === void 0 ? void 0 : _b.toPascalCase()}Input`;
return this.config.sqlTypesEquivalenceQraphqlTypes[this.type];
}
get hasQuotation() {
return this.config.quotationTypes[this.type];
}
fakerPostman(force = false) {
if (this.type === types_1.SqlType.ID && !force)
return this.id;
if (this.type === types_1.SqlType.RELATIONSHIP)
return '[]';
if (this.type === types_1.SqlType.JSON)
return this.config.fakerRelation[this.type]().replace(/\"/gi, '\\"');
if (this.type === types_1.SqlType.ENUM)
return this.enumOptions ? _.shuffle(this.enumOptions)[0] : null;
return this.config.fakerRelation[this.type] ? this.config.fakerRelation[this.type](this.maxLength ? this.maxLength : this.length) : '';
}
parseModuleSection(moduleSectionString) {
const moduleSection = moduleSectionString.split('/');
if (moduleSection.length !== 2)
throw new Error('Must input bounded context and module name, with format: bounded-context/module');
return {
boundedContextName: moduleSection[0],
moduleName: moduleSection[1]
};
}
toDto() {
return {
id: this.id,
name: this._name,
type: this.type,
primaryKey: this.primaryKey,
enumOptions: this.enumOptions,
decimals: this.decimals,
length: this.length,
minLength: this.minLength,
maxLength: this.maxLength,
nullable: this.nullable,
defaultValue: this.defaultValue,
relationship: this.relationship,
relationshipSingularName: this.relationshipSingularName,
relationshipAggregate: this.relationshipAggregate,
relationshipModulePath: this.relationshipModulePath,
relationshipKey: this.relationshipKey,
relationshipField: this.relationshipField,
intermediateTable: this.intermediateTable,
intermediateModel: this.intermediateModel,
intermediateModelModuleSection: this.intermediateModelModuleSection,
intermediateModelFile: this.intermediateModelFile,
index: this.index,
example: this.example,
faker: this.faker,
};
}
}
exports.Property = Property;