# @hodfords/nestjs-seeder

> A tool for seeding databases in NestJS apps

Latest version **12.3.1** (published 2026-10-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install @hodfords/nestjs-seeder
pnpm add @hodfords/nestjs-seeder
yarn add @hodfords/nestjs-seeder
bun add @hodfords/nestjs-seeder
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 12.3.1 |
| Published | 2026-10-15 |
| First published | 2022-11-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.19.0 |
| Dependencies | 1 |
| Unpacked size | 19.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 45 |
| Maintainers | nguyentiendung-dev |

## Links

- npm: https://www.npmjs.com/package/@hodfords/nestjs-seeder
- Repository: https://github.com/hodfords-solutions/nestjs-seeder
- Homepage: https://github.com/hodfords-solutions/nestjs-seeder#readme
- Issues: https://github.com/hodfords-solutions/nestjs-seeder/issues
- npm.io page: https://npm.io/package/@hodfords/nestjs-seeder

## Dependencies (1)

- [glob](https://npm.io/package/glob.md) ^13.0.6

## Recent versions

- 12.3.1 (latest) — 2026-10-15
- 12.3.0 — 2026-09-01
- 12.2.0 — 2026-08-31
- 12.1.0 — 2026-08-29
- 12.0.0 — 2026-08-28
- 11.0.3 — 2026-02-04
- 11.0.2 — 2025-03-19
- 11.0.1 — 2025-02-11
- 11.0.0 — 2025-02-10
- 10.1.3 — 2024-09-27
- 10.1.2 — 2024-09-26
- 10.1.1 — 2024-09-12
- 10.1.0 — 2024-08-27
- 10.0.0 — 2024-03-01
- 8.0.0 — 2024-01-04
- … 2 more at https://npm.io/package/@hodfords/nestjs-seeder/versions

## README

<p align="center">
  <a href="http://opensource.hodfords.uk" target="blank"><img src="https://opensource.hodfords.uk/img/logo.svg" width="320" alt="Hodfords Logo" /></a>
</p>

<p align="center"> <b>nestjs-seeder</b> 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.</p>

## Installation 🤖

Install the `nestjs-seeder` package with:

```
npm install @hodfords/nestjs-seeder --save
```

> [!IMPORTANT]
> This package is **ESM-only**. It must be loaded with `import` (`require()` is not supported)
> and needs Node.js `>=20.19` (or `>=22.12` / `>=24.15` / `>=26`).

### Compatibility

| `@hodfords/nestjs-seeder` | NestJS  |
| ------------------------- | ------- |
| `12.x`                    | `12.x`  |
| `11.x`                    | `11.x`  |

## 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<void>;
}
```

#### 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<Entity>;
saveMany(count: number, options?: any): Promise<Entity[]>;
```

##### 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

---
_Source: https://npm.io/package/@hodfords/nestjs-seeder · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
