diff --git a/README.md b/README.md index f9bb5a58..c7ff9d98 100755 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ define(Pet, (faker: typeof Faker, settings: undefined) => { const pet = new Pet() pet.name = name pet.age = faker.random.number() - pet.user = factory(User)({ roles: ['admin'] }) // not yet implemented + pet.user = factory(User)({ roles: ['admin'] }) as any return pet }) ``` @@ -230,8 +230,8 @@ Now you are able to execute your seeds with this command `npm run seed`. | Option | Default | Description | | ---------------------- | --------------- | --------------------------------------------------------------------------- | | `--seed` or `-s` | null | Option to specify a specific seeder class to run individually. | -| `--name` or `-n` | null | Name of the typeorm connection. Required if there are multiple connections. | -| `--configName` or `-c` | `ormconfig.js` | Name to the typeorm config file. | +| `--connection` or `-c` | null | Name of the typeorm connection. Required if there are multiple connections. | +| `--configName` or `-n` | `ormconfig.js` | Name to the typeorm config file. | | `--root` or `-r` | `process.cwd()` | Path to the typeorm config file. | ## ❯ License diff --git a/package.json b/package.json index 380c96d9..72206491 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,11 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "semantic-release": "semantic-release", - "schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop", - "schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync", - "schema:log": "ts-node ./node_modules/typeorm/cli.js schema:log", - "seed:run": "ts-node ./src/cli.ts seed", - "seed:config": "ts-node ./src/cli.ts config" + "schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop -c sample", + "schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync -c sample", + "schema:log": "ts-node ./node_modules/typeorm/cli.js schema:log -c sample", + "seed:run": "ts-node ./src/cli.ts seed -c sample", + "seed:config": "ts-node ./src/cli.ts config -c sample" }, "license": "MIT", "author": "w3tec.ch ", diff --git a/sample/factories/pet.factory.ts b/sample/factories/pet.factory.ts index 8bc34347..e479daf9 100644 --- a/sample/factories/pet.factory.ts +++ b/sample/factories/pet.factory.ts @@ -10,6 +10,6 @@ define(Pet, (faker: typeof Faker) => { const pet = new Pet() pet.name = name pet.age = faker.random.number() - // pet.user = factory(User)({ roles: ['admin'] }) + pet.user = factory(User)({ roles: ['admin'] }) as any return pet }) diff --git a/sample/seeds/create-pets.seed.ts b/sample/seeds/create-pets.seed.ts index 24177b4f..b290631e 100644 --- a/sample/seeds/create-pets.seed.ts +++ b/sample/seeds/create-pets.seed.ts @@ -6,15 +6,16 @@ import { User } from '../entities/User.entity' export default class CreatePets implements Seeder { public async run(factory: Factory, connection: Connection): Promise { - const em = connection.createEntityManager() + await factory(Pet)().seed() - await times(10, async (n) => { - // This creates a pet in the database - const pet = await factory(Pet)().seed() - // This only returns a entity with fake data - const user = await factory(User)().make() - user.pets = [pet] - await em.save(user) - }) + // const em = connection.createEntityManager() + // await times(1, async (n) => { + // This creates a pet in the database + // const pet = await factory(Pet)().seed() + // This only returns a entity with fake data + // const user = await factory(User)().make() + // user.pets = [pet] + // await em.save(user) + // }) } } diff --git a/src/cli.ts b/src/cli.ts index 346b9eac..8bf16ec0 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,7 +4,6 @@ import * as yargs from 'yargs' import { SeedCommand } from './commands/seed.command' import { ConfigCommand } from './commands/config.command' -// tslint:disable-next-line yargs .usage('Usage: $0 [options]') .command(new ConfigCommand()) diff --git a/src/commands/config.command.ts b/src/commands/config.command.ts index 5eede1bc..4ba48b51 100644 --- a/src/commands/config.command.ts +++ b/src/commands/config.command.ts @@ -9,13 +9,13 @@ export class ConfigCommand implements yargs.CommandModule { builder(args: yargs.Argv) { return args - .option('c', { + .option('n', { alias: 'configName', default: '', describe: 'Name of the typeorm config file (json or js).', }) - .option('n', { - alias: 'name', + .option('c', { + alias: 'connection', default: '', describe: 'Name of the typeorm connection', }) @@ -36,7 +36,7 @@ export class ConfigCommand implements yargs.CommandModule { root: args.root as string, configName: args.configName as string, }, - args.name as string, + args.connection as string, ) log(option) } catch (error) { diff --git a/src/commands/seed.command.ts b/src/commands/seed.command.ts index 4fe439fd..61372ecd 100644 --- a/src/commands/seed.command.ts +++ b/src/commands/seed.command.ts @@ -13,13 +13,13 @@ export class SeedCommand implements yargs.CommandModule { builder(args: yargs.Argv) { return args - .option('c', { + .option('n', { alias: 'configName', default: '', describe: 'Name of the typeorm config file (json or js).', }) - .option('n', { - alias: 'name', + .option('c', { + alias: 'connection', default: '', describe: 'Name of the typeorm connection', }) @@ -36,10 +36,7 @@ export class SeedCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { // Disable logging for the seeders, but keep it alive for our cli - // tslint:disable-next-line const log = console.log - // // tslint:disable-next-line - // console.log = () => void 0 const pkg = require('../../package.json') log(chalk.bold(`typeorm-seeding v${(pkg as any).version}`)) @@ -53,7 +50,7 @@ export class SeedCommand implements yargs.CommandModule { root: args.root as string, configName: args.configName as string, }, - args.name as string, + args.connection as string, ) spinner.succeed('ORM Config loaded') } catch (error) { @@ -111,7 +108,6 @@ export class SeedCommand implements yargs.CommandModule { function panic(spinner: ora.Ora, error: Error, message: string) { spinner.fail(message) - // tslint:disable-next-line console.error(error) process.exit(1) } diff --git a/src/connection.ts b/src/connection.ts index 1afd6703..33d3e201 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -20,29 +20,39 @@ export declare type ConnectionOptions = TypeORMConnectionOptions & SeedingOption const attachSeedingOptions = (option: ConnectionOptions): ConnectionOptions => { if (!option.factories) { - option.factories = [process.env.TYPEORM_SEEDING_FACTORIES as string] + const envFactoriesPath = process.env.TYPEORM_SEEDING_FACTORIES + if (envFactoriesPath) { + option.factories = [envFactoriesPath] + } else { + option.factories = ['src/database/factories/**/*{.ts,.js}'] + } } if (!option.seeds) { - option.seeds = [process.env.TYPEORM_SEEDING_SEEDS as string] + const envSeedsPath = process.env.TYPEORM_SEEDING_SEEDS + if (envSeedsPath) { + option.seeds = [envSeedsPath] + } else { + option.seeds = ['src/database/seeds/**/*{.ts,.js}'] + } } return option } export const getConnectionOption = async ( option: ConnectionOptionArguments, - name: string, + connection: string, ): Promise => { const reader = new ConnectionOptionsReader(option) const options = (await reader.all()) as any[] if (options.length === 1) { return attachSeedingOptions(options[0]) } - if (name !== undefined && name !== '') { - const filteredOptions = options.filter((o) => o.name === name) + if (connection !== undefined && connection !== '') { + const filteredOptions = options.filter((o) => o.name === connection) if (filteredOptions.length === 1) { return attachSeedingOptions(options[0]) } else { - printError('Could not find any connection with the name=', name) + printError('Could not find any connection with the name=', connection) } } printError('There are multiple connections please provide a connection name') diff --git a/src/entity-factory.test.ts b/src/entity-factory.test.ts index 3e6a8bb3..5a266820 100644 --- a/src/entity-factory.test.ts +++ b/src/entity-factory.test.ts @@ -1,11 +1,9 @@ import { EntityFactory } from './entity-factory' describe('make', () => { - // tslint:disable-next-line class User { constructor(public name: string) {} } - // tslint:disable-next-line class Pet { constructor(public name: string, public user: User) {} } diff --git a/src/entity-factory.ts b/src/entity-factory.ts index de675403..f9b62ea8 100644 --- a/src/entity-factory.ts +++ b/src/entity-factory.ts @@ -31,21 +31,7 @@ export class EntityFactory { * Make a new entity, but does not persist it */ public async make(overrideParams: EntityProperty = {}): Promise { - if (this.factory) { - let entity = await this.resolveEntity(this.factory(Faker, this.settings)) - if (this.mapFunction) { - entity = await this.mapFunction(entity) - } - - for (const key in overrideParams) { - if (overrideParams.hasOwnProperty(key)) { - entity[key] = overrideParams[key] - } - } - - return entity - } - throw new Error('Could not found entity') + return this.makeEnity(overrideParams, false) } /** @@ -56,7 +42,7 @@ export class EntityFactory { if (connection) { const em = connection.createEntityManager() try { - const entity = await this.make(overrideParams) + const entity = await this.makeEnity(overrideParams, true) return await em.save(entity) } catch (error) { const message = 'Could not save entity' @@ -90,18 +76,41 @@ export class EntityFactory { // Prrivat Helpers // ------------------------------------------------------------------------- - private async resolveEntity(entity: Entity): Promise { + private async makeEnity(overrideParams: EntityProperty = {}, isSeeding = false): Promise { + if (this.factory) { + let entity = await this.resolveEntity(this.factory(Faker, this.settings), isSeeding) + if (this.mapFunction) { + entity = await this.mapFunction(entity) + } + + for (const key in overrideParams) { + if (overrideParams.hasOwnProperty(key)) { + entity[key] = overrideParams[key] + } + } + + return entity + } + throw new Error('Could not found entity') + } + + private async resolveEntity(entity: Entity, isSeeding = false): Promise { for (const attribute in entity) { if (entity.hasOwnProperty(attribute)) { if (isPromiseLike(entity[attribute])) { entity[attribute] = entity[attribute] } - if (entity[attribute] && typeof entity[attribute] === 'object' && !(entity[attribute] instanceof Date)) { const subEntityFactory = entity[attribute] try { - if (typeof (subEntityFactory as any).make === 'function') { - entity[attribute] = await (subEntityFactory as any).make() + if (isSeeding) { + if (typeof (subEntityFactory as any).seed === 'function') { + entity[attribute] = await (subEntityFactory as any).seed() + } + } else { + if (typeof (subEntityFactory as any).make === 'function') { + entity[attribute] = await (subEntityFactory as any).make() + } } } catch (error) { const message = `Could not make ${(subEntityFactory as any).name}` diff --git a/src/utils/factory.test.ts b/src/utils/factory.test.ts index 08969044..4752203a 100644 --- a/src/utils/factory.test.ts +++ b/src/utils/factory.test.ts @@ -32,7 +32,6 @@ describe('isPromiseLike', () => { expect(isPromiseLike([])).toBeFalsy() expect(isPromiseLike({})).toBeFalsy() expect(isPromiseLike((): any => void 0)).toBeFalsy() - // tslint:disable-next-line class UserEntity {} expect(isPromiseLike(new UserEntity())).toBeFalsy() expect(isPromiseLike(new Date())).toBeFalsy() diff --git a/src/utils/log.util.ts b/src/utils/log.util.ts index 5b243ec8..0849dbf4 100644 --- a/src/utils/log.util.ts +++ b/src/utils/log.util.ts @@ -4,10 +4,8 @@ import * as chalk from 'chalk' * Prints the error to the console */ export const printError = (message: string, error?: any) => { - // tslint:disable-next-line console.log('\n❌ ', chalk.red(message)) if (error) { - // tslint:disable-next-line console.error(error) } }