-
Hey, there. Thanks for this awesome lib, saved me some time. I'm having a issue when trying to seed the database in test environment. I'm probably missing something. // this is only for testing, we inject configs via nest decorators
import { DataSource, DataSourceOptions } from 'typeorm';
import * as path from 'path';
import { SeederOptions } from 'typeorm-extension';
import UserSeeder from './seeds/user.seeder';
import userFactory from './factories/user.factory';
const options: DataSourceOptions & SeederOptions = {
type: 'postgres',
host: 'localhost',
port: 5433,
username: 'postgres',
password: 'postgres',
database: 'test_postgres',
synchronize: true,
entities: [path.join(__dirname, '/../**/*.entity{.ts,.js}')],
migrations: [],
seeds: [UserSeeder],
factories: [userFactory],
};
export const dataSource = new DataSource(options); Here's the seeder factory: import { User } from 'src/users/entities/user.entity';
import { Role } from 'src/core/models/role';
import { setSeederFactory } from 'typeorm-extension';
export default setSeederFactory(User, (faker) => {
const user = new User();
user.firstName = faker.name.firstName('male');
user.lastName = faker.name.lastName('male');
user.email = faker.internet.email(user.firstName, user.lastName);
user.passwordHash = faker.random.alphaNumeric(16);
user.gender = 'Male';
user.birthDate = new Date();
user.state = faker.address.state();
user.city = faker.address.city();
user.neighborhood = faker.address.city();
user.role = Role.USER;
return user;
}); Here's the Seeder class: import { Seeder, SeederFactoryManager } from 'typeorm-extension';
import { DataSource } from 'typeorm';
import { User } from 'src/users/entities/user.entity';
export default class UserSeeder implements Seeder {
public async run(
dataSource: DataSource,
factoryManager: SeederFactoryManager,
): Promise<any> {
const userFactory = factoryManager.get(User);
await userFactory.saveMany(5);
}
} and here's the execution of seeder: import UserSeeder from 'src/database/seeds/user.seeder';
import { User } from 'src/users/entities/user.entity';
import { DataSource, DataSourceOptions } from 'typeorm';
import {
createDatabase,
dropDatabase,
runSeeder,
runSeeders,
} from 'typeorm-extension';
import UserFactory from 'src/database/factories/user.factory';
import { dataSource } from 'src/database/data-source';
export const prepareDatabase = async (connection: any): Promise<void> => {
await dropDatabase({
options: connection as DataSourceOptions,
ifExist: true,
});
await createDatabase({
options: connection as DataSourceOptions,
ifNotExist: true,
});
await dataSource.initialize();
await runSeeders(dataSource);
const repo = await dataSource.getRepository(User);
const users = await repo.find();
console.log(users);
}; I'm always getting this error: const repository = dataSource.getRepository(User);
await repository.insert([
{
firstName: 'Caleb',
lastName: 'Barrows',
email: '[email protected]',
},
]); it goes fine. However for the amount data I need for testing, this is really cumbersome, the seeder factory would be really useful for me in this project. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Glad, you like it and happy to hear😇 . |
Beta Was this translation helpful? Give feedback.
-
@glothos Can you check if version |
Beta Was this translation helpful? Give feedback.
-
your welcome 😊 |
Beta Was this translation helpful? Give feedback.
@glothos Can you check if version
v2.1.2
is working for you?