Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hodfords-solutions/nestjs-seeder
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.
https://github.com/hodfords-solutions/nestjs-seeder
nestjs nodejs seeder
Last synced: about 20 hours ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/hodfords-solutions/nestjs-seeder
- Owner: hodfords-solutions
- Created: 2022-11-03T04:38:29.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-27T07:59:39.000Z (3 months ago)
- Last Synced: 2025-01-01T12:10:58.546Z (8 days ago)
- Topics: nestjs, nodejs, seeder
- Language: TypeScript
- Homepage: https://opensource.hodfords.uk/nestjs-seeder
- Size: 417 KB
- Stars: 46
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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.
## Installation 🤖
Install the `nestjs-seeder` package with:
```
npm install @hodfords/nestjs-seeder --save
```## Usage 🚀
To seed fake user data into your database, follow these 6 steps:
#### 1. Define the Factory
First, create a factory for UserEntity. This factory will generate fake data for user records.
##### user.factory.ts
```typescript
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 || {});
});
```#### 2. Create the BaseSeeder
Create a base seeder class that will be used to configure and run your seeding logic.
##### base.seeder.ts
```typescript
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;
}
```#### 3. Create the UserSeed
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**
```typescript
createOne(options?: any): Entity;
saveOne(options?: any): Promise;
saveMany(count: number, options?: any): Promise;
```##### user.seed.ts
```typescript
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
}
}
```#### 4. Create the seedConfig
Set up the seed configuration to include your seed classes.
```typescript
import { SeederModule } from '@hodfords/nestjs-seeder';
export const seedConfig = SeederModule.forRoot([UserSeed]);
```#### 5. Import seedConfig into AppModule
Integrate the seed configuration into your main application module.
```typescript
@Module({
imports: [seedConfig],
controllers: [AppController],
providers: []
})
export class AppModule {}
```#### 6. Run the seeder
Execute the seeder command to populate your database with the defined fake data.
```typescript
wz-command seeder
```## License 📝
This project is licensed under the MIT License