w3tecch / typeorm-seeding

🌱 A delightful way to seed test data into your database.
https://www.npmjs.com/package/typeorm-seeding
MIT License
887 stars 132 forks source link

TypeError: Cannot read property 'factory' of undefined #108

Closed JoshuaM1995 closed 3 years ago

JoshuaM1995 commented 3 years ago

I got the following error when trying to run a seed. In the CLI, it's saying the ORMConfig has been loaded, factories and seeders are imported, database connected and the seeder itself has been executed.

Could not run the seed CreateUsers!
TypeError: Cannot read property 'factory' of undefined
    at /Users/****/Personal Projects/****/server/node_modules/typeorm-seeding/src/typeorm-seeding.ts:40:79
    at CreateUsers.run (/Users/****/Personal Projects/****/server/src/seeds/create-users.seed.ts:8:24)
    at /Users/****/Personal Projects/****/server/node_modules/typeorm-seeding/src/typeorm-seeding.ts:46:17
    at step (/Users/****/Personal Projects/****/server/node_modules/tslib/tslib.js:141:27)
    at Object.next (/Users/****/Personal Projects/****/server/node_modules/tslib/tslib.js:122:57)
    at fulfilled (/Users/****/Personal Projects/****/server/node_modules/tslib/tslib.js:112:62)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Here's user.factory.ts:

import { define } from 'typeorm-seeding';
import Faker from 'faker';
import User from '../modules/user/entities/user.entity';

define(User, (faker: typeof Faker) => {
  const user = new User();
  user.uuid = faker.random.uuid();
  user.email = faker.internet.email();
  user.username = faker.internet.userName();
  user.firstName = faker.name.firstName();
  user.birthday = faker.date.between(new Date('2000-01-01'), new Date());
  user.password = faker.internet.password(16);

  return user;
});

create-users.seed.ts:

import { Factory, Seeder } from 'typeorm-seeding';
import { Connection } from 'typeorm';
import User from '../modules/user/entities/user.entity';

export default class CreateUsers implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await factory(User)().createMany(5);
  }
}

ormconfig.js:

module.exports = {
  type: 'postgres',
  host: 'localhost',
  username: '****',
  password: '****',
  logging: true,
  database: '****',
  port: 5432,
  entities: ['./src/**/*.entity.ts'],
  migrations: ['./src/migrations/*.ts'],
  seeds: ['./src/seeds/**/*.ts'],
  factories: ['./src/factories/**/*.ts}'],
  cli: {
    migrationsDir: './src/migrations',
  },
};
JoshuaM1995 commented 3 years ago

Changing my ORMConfig to this worked: factories: [``${__dirname}/src/factories/**/*{.ts,.js}``],

It would be nice if there was some type of error like "no factories found" or something along those lines.

robophil commented 3 years ago

Changing my ORMConfig to this worked: factories: [${__dirname}/src/factories/*/{.ts,.js}],

It would be nice if there was some type of error like "no factories found" or something along those lines.

I can't seem to find this option here for factories https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md

valdineifer commented 3 years ago

@robophil That's because it's from typeorm-seeding package, not from TypeORM. Here is the section.