UNPKG

typeorm-fixtures-cli

Version:

[![CircleCI](https://circleci.com/gh/RobinCK/typeorm-fixtures.svg?style=svg)](https://circleci.com/gh/RobinCK/typeorm-fixtures) ![GitHub CI](https://github.com/RobinCK/typeorm-fixtures/workflows/Build%20CI/badge.svg?branch=master) [![OpenCollective](https

85 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const fs = require("fs"); const path = require("path"); const lodash_1 = require("lodash"); const class_transformer_1 = require("class-transformer"); class Builder { constructor(dataSource, parser, ignoreDecorators) { this.dataSource = dataSource; this.parser = parser; this.ignoreDecorators = ignoreDecorators; this.entities = {}; this.processorCache = new Map(); } async callExecutors(entity, fixture, data) { /* istanbul ignore else */ for (const [method, values] of Object.entries(data.__call)) { /* istanbul ignore else */ if (entity[method]) { await entity[method].apply(entity, this.parser.parse(values instanceof Array ? values : [values], fixture, this.entities)); } } return entity; } async buildEntity(fixture, data) { const repository = this.dataSource.getRepository(fixture.entity); const entity = repository.create(); // exclude prefixes to ignore __call methods return (0, class_transformer_1.plainToClassFromExist)(entity, data, { excludePrefixes: ['__'], ignoreDecorators: this.ignoreDecorators, }); } async build(fixture) { let entity; let data = this.parser.parse(fixture.data, fixture, this.entities); let processorInstance = undefined; /* istanbul ignore else */ if (data.__call && (!(0, lodash_1.isObject)(data.__call) || (0, lodash_1.isArray)(data.__call))) { throw new Error('invalid "__call" parameter format'); } if (fixture.processor) { processorInstance = this.getProcessorInstance(fixture.processor); } /* istanbul ignore else */ if (processorInstance && typeof processorInstance.preProcess === 'function') { data = await processorInstance.preProcess(fixture.name, data); } entity = await this.buildEntity(fixture, data); if (data.__call) { entity = await this.callExecutors(entity, fixture, data); } /* istanbul ignore else */ if (processorInstance && typeof processorInstance.postProcess === 'function') { await processorInstance.postProcess(fixture.name, entity); } if (fixture.resolvedFields && Array.isArray(fixture.resolvedFields)) { fixture.resolvedFields.forEach((propertyName) => { entity[propertyName] = Promise.resolve(data[propertyName]); }); } this.entities[fixture.name] = entity; return entity; } getProcessorInstance(processor) { const processorPathWithoutExtension = path.join(path.dirname(processor), path.basename(processor, path.extname(processor))); if (!this.processorCache.has(processorPathWithoutExtension)) { const processorInstance = this.createProcessorInstance(processorPathWithoutExtension); this.processorCache.set(processorPathWithoutExtension, processorInstance); } return this.processorCache.get(processorPathWithoutExtension); } createProcessorInstance(processorPathWithoutExtension) { if (!fs.existsSync(processorPathWithoutExtension) && !fs.existsSync(processorPathWithoutExtension + '.ts') && !fs.existsSync(processorPathWithoutExtension + '.js')) { throw new Error(`Processor "${processorPathWithoutExtension}" not found`); } const processor = require(processorPathWithoutExtension).default; return new processor(); } } exports.Builder = Builder; //# sourceMappingURL=Builder.js.map