Hey there! I'm currently exploring how to set up a factory and establish relationships between models.
For instance, I have a UserFactory that corresponds to the User entity which is connected to the userType table. However, in the factory, I'm unable to access the EntityManager to locate any existing entries.
export class UserFactory extends Factory<User> {
model = User
definition(faker: Faker): Partial<User> {
const user = {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
...
userType: // My challenge here is something like this:
// EntityManager.findOne(UserType, {id: 1}}
// But since EntityManager is private in Factory class, I'm stuck
}
return user
}
}
I also attempted the following approach but encountered an error message: ValidationError: Value for User.type is required, 'undefined' found DatabaseSeeder
export class DatabaseSeeder extends Seeder {
async run(em: EntityManager): Promise<void> {
const users: User[] = new UserFactory(em).each(async user => {
const userType : UserType| null = await em.findOne(UserType, 1)
console.log(tenant)
const userType = await em.findOne(UserType, 1)
if (userType !== null) {
user.type = userType
} else {
user.type = em.create(UserType, {
type: 'test'
})
}
}).make(10)
}
}
What would be the correct way to resolve this issue? Your insights are greatly appreciated!