typeorm-faker
Version:
Generate mocks, stubs using fakers with your Entity Settings
169 lines (167 loc) • 5.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityPatcher = void 0;
const class_transformer_1 = require("class-transformer");
const typeorm_1 = require("typeorm");
const faker_1 = require("@faker-js/faker");
const util_service_1 = require("./util.service");
class EntityPatcher {
static patch(EntityClass, classTransformOptions = {}) {
let fakeEntity = {};
const filteredPropertyDescriptor = (0, typeorm_1.getMetadataArgsStorage)().filterColumns(EntityClass);
const SuperClass = Object.getPrototypeOf(EntityClass);
const hasSuperClass = util_service_1.UtilService.isEmptyObject(SuperClass) === false;
if (hasSuperClass) {
fakeEntity = EntityPatcher.patch(SuperClass);
}
for (const propertyDescriptor of filteredPropertyDescriptor) {
const { propertyName, options, mode } = propertyDescriptor;
fakeEntity[propertyName] = EntityPatcher.patchByMode(mode, options);
}
return (0, class_transformer_1.plainToInstance)(EntityClass, fakeEntity, {
ignoreDecorators: true,
...classTransformOptions
});
}
static patchByMode(mode, options) {
let value;
switch (mode) {
case 'regular':
value = EntityPatcher.patchValueByTypeormColumnOptions(options);
break;
default:
value = EntityPatcher.patchValueByTypeormColumnMode(mode);
break;
}
return value;
}
static patchValueByTypeormColumnOptions(options) {
const { type: propertyTypeOrTypeFunction, enum: enumValue, default: defaultValue } = options;
const propertyType = (propertyTypeOrTypeFunction instanceof Function
? typeof propertyTypeOrTypeFunction()
: propertyTypeOrTypeFunction);
let value = null;
if (defaultValue !== undefined) {
value = defaultValue;
}
else if (enumValue) {
const stringArrayEnums = enumValue;
const randomIndex = faker_1.faker.datatype.number({ min: 0, max: stringArrayEnums.length - 1 });
value = stringArrayEnums[randomIndex];
}
else if (propertyType) {
value = EntityPatcher.patchValueByTypeormColumnType(propertyType);
}
else {
value = null;
}
return value;
}
static patchValueByTypeormColumnMode(mode) {
let value = null;
switch (mode) {
case 'createDate':
case 'updateDate':
value = new Date();
break;
case 'deleteDate':
default:
value = null;
break;
}
return value;
}
static patchValueByTypeormColumnType(typeormColumnTypeString) {
let value;
switch (typeormColumnTypeString) {
case 'int':
case 'int2':
case 'int4':
case 'int8':
case 'int64':
case 'integer':
case 'unsigned big int':
case 'tinyint':
case 'smallint':
case 'mediumint':
case 'bigint':
case 'dec':
case 'decimal':
case 'smalldecimal':
case 'fixed':
case 'numeric':
case 'number':
case 'float':
case 'float4':
case 'float8':
case 'double':
case 'double precision':
case 'dec':
case 'decimal':
case 'smalldecimal':
case 'fixed':
case 'numeric':
case 'real':
value = faker_1.faker.datatype.number();
break;
case 'character varying':
case 'varying':
case 'nvarchar':
case 'national':
case 'character':
case 'native':
case 'varchar':
case 'char':
case 'nchar':
case 'national':
case 'varchar2':
case 'nvarchar2':
case 'alphanum':
case 'shorttext':
case 'raw':
case 'binary':
case 'varbinary':
case 'string':
case 'tinytext':
case 'mediumtext':
case 'text':
case 'ntext':
case 'citext':
case 'longtext':
case 'shorttext':
value = faker_1.faker.datatype.string();
break;
case 'date':
case 'datetime':
case 'datetime2':
case 'datetimeoffset':
case 'time':
case 'time with time zone':
case 'time without time zone':
case 'timestamp':
case 'timestamp without time zone':
case 'timestamp with time zone':
case 'timestamp with local time zone':
value = new Date();
break;
case 'boolean':
case 'bool':
value = faker_1.faker.datatype.boolean();
break;
case 'Object':
case 'smallmoney':
case 'money':
case 'simple-array':
case 'simple-json':
case 'simple-enum':
case 'json':
case 'array':
default:
value = null;
break;
}
return value;
}
}
exports.EntityPatcher = EntityPatcher;
//# sourceMappingURL=patcher.js.map