typeorm-fixtures-cli
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -47,2 +47,3 @@ #!/usr/bin/env node | ||
const entity = resolver.resolve(fixture); | ||
console.log(entity); | ||
} | ||
@@ -49,0 +50,0 @@ yield connection.close(); |
@@ -14,3 +14,3 @@ import { IFixture } from './interface'; | ||
/** | ||
* @param {{[p: string]: any}[]} fixtures | ||
* @param fixtures | ||
* @return {IFixture[]} | ||
@@ -20,7 +20,14 @@ */ | ||
/** | ||
* @param {string} parentReferenceName | ||
* @param {any[] | object} propertyList | ||
* @return {any[]} | ||
*/ | ||
findDependencies(propertyList: any[] | object): any[]; | ||
findDependencies(parentReferenceName: string, propertyList: any): any[]; | ||
/** | ||
* @param {string} parentReferenceName | ||
* @param {string} reference | ||
* @return {any} | ||
*/ | ||
private parseReference; | ||
/** | ||
* @param item | ||
@@ -27,0 +34,0 @@ * @return {any[]} |
@@ -10,3 +10,2 @@ "use strict"; | ||
const schema_1 = require("./schema"); | ||
const parser_1 = require("./parser"); | ||
class Loader { | ||
@@ -38,3 +37,3 @@ /** | ||
/** | ||
* @param {{[p: string]: any}[]} fixtures | ||
* @param fixtures | ||
* @return {IFixture[]} | ||
@@ -44,20 +43,16 @@ */ | ||
for (const { entity, items, parameters, processor } of fixtures) { | ||
for (const [referenceName, propertyList] of Object.entries(items)) { | ||
for (const [mainReferenceName, propertyList] of Object.entries(items)) { | ||
const rangeRegExp = /^([\w-_]+)\{(\d+)\.\.(\d+)\}$/gm; | ||
if (rangeRegExp.test(referenceName)) { | ||
const result = referenceName.split(rangeRegExp); | ||
let referenceNames = []; | ||
if (rangeRegExp.test(mainReferenceName)) { | ||
const result = mainReferenceName.split(rangeRegExp); | ||
if (result) { | ||
for (const rangeNumber of lodash_1.range(+result[2], +(+result[3]) + 1)) { | ||
this.stack.push({ | ||
parameters: parameters || {}, | ||
processor, | ||
entity: entity, | ||
name: `${result[1]}${rangeNumber}`, | ||
dependencies: this.findDependencies(Object.assign({}, propertyList)), | ||
data: Object.assign({}, propertyList), | ||
}); | ||
} | ||
referenceNames = lodash_1.range(+result[2], +(+result[3]) + 1) | ||
.map(rangeNumber => `${result[1]}${rangeNumber}`); | ||
} | ||
} | ||
else { | ||
referenceNames = [mainReferenceName]; | ||
} | ||
for (const name of referenceNames) { | ||
this.stack.push({ | ||
@@ -67,5 +62,5 @@ parameters: parameters || {}, | ||
entity: entity, | ||
name: referenceName, | ||
dependencies: this.findDependencies(Object.assign({}, propertyList)), | ||
data: Object.assign({}, propertyList), | ||
name: name, | ||
dependencies: this.findDependencies(mainReferenceName, propertyList), | ||
data: propertyList, | ||
}); | ||
@@ -80,13 +75,16 @@ } | ||
/** | ||
* @param {string} parentReferenceName | ||
* @param {any[] | object} propertyList | ||
* @return {any[]} | ||
*/ | ||
findDependencies(propertyList) { | ||
findDependencies(parentReferenceName, propertyList) { | ||
const dependencies = []; | ||
for (const value of Object.values(propertyList)) { | ||
for (const [key, value] of Object.entries(propertyList)) { | ||
if (typeof value === 'string' && value.indexOf('@') === 0) { | ||
dependencies.push(value.substr(1)); | ||
const reference = this.parseReference(parentReferenceName, value.substr(1)); | ||
propertyList[key] = `@${reference}`; | ||
dependencies.push(reference); | ||
} | ||
else if (typeof value === 'object') { | ||
dependencies.push(...this.findDependencies(value)); | ||
dependencies.push(...this.findDependencies(parentReferenceName, value)); | ||
} | ||
@@ -97,2 +95,23 @@ } | ||
/** | ||
* @param {string} parentReferenceName | ||
* @param {string} reference | ||
* @return {any} | ||
*/ | ||
parseReference(parentReferenceName, reference) { | ||
const currentRegExp = /^([\w-_]+)\(\$current\)$/gm; | ||
const rangeRegExp = /^([\w-_]+)\{(\d+)\.\.(\d+)\}$/gm; | ||
if (currentRegExp.test(reference)) { | ||
const currentIndexRegExp = /^[a-z\_\-]+(\d+)$/gi; | ||
const splitting = parentReferenceName.split(currentIndexRegExp); | ||
const index = splitting[1] || ''; | ||
return reference.replace('($current)', index); | ||
} | ||
else if (rangeRegExp.test(reference)) { | ||
const splitting = reference.split(rangeRegExp); | ||
lodash_1.sample(lodash_1.range(+splitting[2], +(+splitting[3]) + 1)); | ||
return `${splitting[1]}${lodash_1.sample(lodash_1.range(+splitting[2], +(+splitting[3]) + 1))}`; | ||
} | ||
return reference; | ||
} | ||
/** | ||
* @param item | ||
@@ -106,22 +125,10 @@ * @return {any[]} | ||
if (!dependencyElement) { | ||
if (dependencyName.substr(dependencyName.length - 1) !== '*' && | ||
dependencyName.indexOf('<($current)>') === -1) { | ||
if (dependencyName.substr(dependencyName.length - 1) !== '*') { | ||
throw new Error(`Reference "${dependencyName}" not found`); | ||
} | ||
if (dependencyName.indexOf('<($current)>') === -1) { | ||
const prefix = dependencyName.substr(0, dependencyName.length - 1); | ||
const regex = new RegExp(`^${prefix}([0-9]+)$`); | ||
for (const dependencyMaskElement of this.stack.filter(s => regex.test(s.name))) { | ||
dependencies.push(dependencyMaskElement.name, ...this.deepDependenciesResolver(dependencyMaskElement)); | ||
} | ||
const prefix = dependencyName.substr(0, dependencyName.length - 1); | ||
const regex = new RegExp(`^${prefix}([0-9]+)$`); | ||
for (const dependencyMaskElement of this.stack.filter(s => regex.test(s.name))) { | ||
dependencies.push(dependencyMaskElement.name, ...this.deepDependenciesResolver(dependencyMaskElement)); | ||
} | ||
else { | ||
const splitting = item.name.split(parser_1.CurrentParser.currentIndexRegExp); | ||
const resolvedDependencyName = dependencyName.replace(/<\(\$current\)>/g, splitting[1] || 1); | ||
const dependency = this.stack.find(s => s.name === resolvedDependencyName); | ||
if (!dependency) { | ||
throw new Error(`Dependency ${resolvedDependencyName} not found`); | ||
} | ||
dependencies.push(resolvedDependencyName, ...this.deepDependenciesResolver(dependency)); | ||
} | ||
} | ||
@@ -128,0 +135,0 @@ else { |
export * from './EjsParser'; | ||
export * from './FakerParser'; | ||
export * from './CurrentParser'; | ||
export * from './ParameterParser'; | ||
export * from './ReferenceParser'; |
@@ -8,5 +8,4 @@ "use strict"; | ||
__export(require("./FakerParser")); | ||
__export(require("./CurrentParser")); | ||
__export(require("./ParameterParser")); | ||
__export(require("./ReferenceParser")); | ||
//# sourceMappingURL=index.js.map |
@@ -45,9 +45,10 @@ "use strict"; | ||
} | ||
const processor = require(processorPath); | ||
if (typeof processor.preProcess === 'function') { | ||
data = processor.preProcess(fixture.name, data); | ||
const processor = require(processorPath).default; | ||
const processorInstance = new processor(); | ||
if (typeof processorInstance.preProcess === 'function') { | ||
data = processorInstance.preProcess(fixture.name, data); | ||
} | ||
Object.assign(entity, data); | ||
if (typeof processor.postProcess === 'function') { | ||
processor.postProcess(fixture.name, entity); | ||
if (typeof processorInstance.postProcess === 'function') { | ||
processorInstance.postProcess(fixture.name, entity); | ||
} | ||
@@ -54,0 +55,0 @@ } |
{ | ||
"name": "typeorm-fixtures-cli", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# typeorm-fixtures | ||
Fixtures loader for typeorm | ||
## Install | ||
#### NPM | ||
```bash | ||
npm install typeorm-fixtures-cli --save-dev | ||
``` | ||
#### Yarn | ||
```bash | ||
yarn add typeorm-fixtures-cli --dev | ||
``` | ||
## Development Setup | ||
```bash | ||
# install dependencies | ||
npm install | ||
# build dist files | ||
npm run build | ||
``` | ||
## Usage | ||
``` | ||
Usage: fixtures [options] <path> Fixtures folder path | ||
Options: | ||
-v, --version output the version number | ||
-c, --config <path> TypeORM config path (default: "ormconfig.yml") | ||
-cn, --connection [value] TypeORM connection name (default: "default") | ||
--no-color Disable color | ||
-h, --help output usage information | ||
``` | ||
## Configure | ||
MIT © [Igor Ognichenko](https://github.com/RobinCK) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
45
194156
62
686