nestjs-seeder streamlines the process of populating your NestJS application with mock data. It makes it easy to generate and manage seed data, ideal for testing and simulating API responses.
Install the nestjs-seeder
package with:
npm install @hodfords/nestjs-seeder --save
To seed fake user data into your database, follow these 6 steps:
First, create a factory for UserEntity. This factory will generate fake data for user records.
import { define } from '@hodfords/nestjs-seeder';
interface SeedUserOptions {
countryId: string;
}
class UserEntity {
name: string;
age: string;
countryId: string;
createdAt: Date;
}
define(UserEntity, (options: SeedUserOptions) => {
const user = new UserEntity();
user.name = faker.name.title();
user.age = faker.datatype.number(100);
user.createdAt = faker.date.future();
return plainToClassFromExist(user, options || {});
});
Create a base seeder class that will be used to configure and run your seeding logic.
import { Test } from '@nestjs/testing';
import { AppModule } from '~app.module';
import { databaseConfig } from '~config/database.config';
import { BaseSeeder as AbstractSeeder } from '@hodfords/nestjs-seeder';
export abstract class BaseSeeder extends AbstractSeeder {
createModule() {
return Test.createTestingModule({
imports: [AppModule, databaseConfig]
}).compile();
}
abstract run(): Promise<void>;
}
Implement a seeder class that extends BaseSeeder. Use the factory methods to generate and save data.
There are 3 methods to seed a fake data from factory method
createOne(options?: any): Entity;
saveOne(options?: any): Promise<Entity>;
saveMany(count: number, options?: any): Promise<Entity[]>;
import { BaseSeeder } from '~core/seeders/base-seeder';
import { factory } from '@hodfords/nestjs-seeder';
import faker from 'faker';
export class UserSeed extends BaseSeeder {
async run() {
const countryId = (await factory(CountryEntity)).id;
await factory(UserEntity).saveOne({ countryId }); // 1
factory(UserEntity).createOne({ countryId }); // 2
await factory(UserEntity).saveMany(100, { countryId }); // 3
}
}
Set up the seed configuration to include your seed classes.
import { SeederModule } from '@hodfords/nestjs-seeder';
export const seedConfig = SeederModule.forRoot([UserSeed]);
Integrate the seed configuration into your main application module.
@Module({
imports: [seedConfig],
controllers: [AppController],
providers: []
})
export class AppModule {}
Execute the seeder command to populate your database with the defined fake data.
wz-command seeder
This project is licensed under the MIT License