{"id":18867930,"url":"https://github.com/hodfords-solutions/nestjs-seeder","last_synced_at":"2025-04-07T14:12:08.044Z","repository":{"id":257802452,"uuid":"561160660","full_name":"hodfords-solutions/nestjs-seeder","owner":"hodfords-solutions","description":"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.","archived":false,"fork":false,"pushed_at":"2025-03-19T07:02:47.000Z","size":512,"stargazers_count":46,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-31T13:17:52.012Z","etag":null,"topics":["nestjs","nodejs","seeder"],"latest_commit_sha":null,"homepage":"https://opensource.hodfords.uk/nestjs-seeder","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hodfords-solutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-03T04:38:29.000Z","updated_at":"2025-03-19T07:02:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"3c042d32-cf98-49ab-b350-b8922787549d","html_url":"https://github.com/hodfords-solutions/nestjs-seeder","commit_stats":null,"previous_names":["hodfords-solutions/nestjs-seeder"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-seeder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-seeder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-seeder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-seeder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hodfords-solutions","download_url":"https://codeload.github.com/hodfords-solutions/nestjs-seeder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247666014,"owners_count":20975788,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nestjs","nodejs","seeder"],"created_at":"2024-11-08T05:11:53.978Z","updated_at":"2025-04-07T14:12:08.016Z","avatar_url":"https://github.com/hodfords-solutions.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://opensource.hodfords.uk\" target=\"blank\"\u003e\u003cimg src=\"https://opensource.hodfords.uk/img/logo.svg\" width=\"320\" alt=\"Hodfords Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e \u003cb\u003enestjs-seeder\u003c/b\u003e 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.\u003c/p\u003e\n\n## Installation 🤖\n\nInstall the `nestjs-seeder` package with:\n\n```\nnpm install @hodfords/nestjs-seeder --save\n```\n\n## Usage 🚀\n\nTo seed fake user data into your database, follow these 6 steps:\n\n#### 1. Define the Factory\n\nFirst, create a factory for UserEntity. This factory will generate fake data for user records.\n\n##### user.factory.ts\n\n```typescript\nimport { define } from '@hodfords/nestjs-seeder';\n\ninterface SeedUserOptions {\n    countryId: string;\n}\n\nclass UserEntity {\n    name: string;\n    age: string;\n    countryId: string;\n    createdAt: Date;\n}\n\ndefine(UserEntity, (options: SeedUserOptions) =\u003e {\n    const user = new UserEntity();\n\n    user.name = faker.name.title();\n    user.age = faker.datatype.number(100);\n    user.createdAt = faker.date.future();\n\n    return plainToClassFromExist(user, options || {});\n});\n```\n\n#### 2. Create the BaseSeeder\n\nCreate a base seeder class that will be used to configure and run your seeding logic.\n\n##### base.seeder.ts\n\n```typescript\nimport { Test } from '@nestjs/testing';\nimport { AppModule } from '~app.module';\nimport { databaseConfig } from '~config/database.config';\nimport { BaseSeeder as AbstractSeeder } from '@hodfords/nestjs-seeder';\n\nexport abstract class BaseSeeder extends AbstractSeeder {\n    createModule() {\n        return Test.createTestingModule({\n            imports: [AppModule, databaseConfig]\n        }).compile();\n    }\n\n    abstract run(): Promise\u003cvoid\u003e;\n}\n```\n\n#### 3. Create the UserSeed\n\nImplement a seeder class that extends BaseSeeder. Use the factory methods to generate and save data.\n\n**There are 3 methods to seed a fake data from factory method**\n\n```typescript\ncreateOne(options?: any): Entity;\nsaveOne(options?: any): Promise\u003cEntity\u003e;\nsaveMany(count: number, options?: any): Promise\u003cEntity[]\u003e;\n```\n\n##### user.seed.ts\n\n```typescript\nimport { BaseSeeder } from '~core/seeders/base-seeder';\nimport { factory } from '@hodfords/nestjs-seeder';\nimport faker from 'faker';\n\nexport class UserSeed extends BaseSeeder {\n    async run() {\n        const countryId = (await factory(CountryEntity)).id;\n\n        await factory(UserEntity).saveOne({ countryId }); // 1\n        factory(UserEntity).createOne({ countryId }); // 2\n        await factory(UserEntity).saveMany(100, { countryId }); // 3\n    }\n}\n```\n\n#### 4. Create the seedConfig\n\nSet up the seed configuration to include your seed classes.\n\n```typescript\nimport { SeederModule } from '@hodfords/nestjs-seeder';\nexport const seedConfig = SeederModule.forRoot([UserSeed]);\n```\n\n#### 5. Import seedConfig into AppModule\n\nIntegrate the seed configuration into your main application module.\n\n```typescript\n@Module({\n    imports: [seedConfig],\n    controllers: [AppController],\n    providers: []\n})\nexport class AppModule {}\n```\n\n#### 6. Run the seeder\n\nExecute the seeder command to populate your database with the defined fake data.\n\n```typescript\nwz-command seeder\n```\n\n## License 📝\n\nThis project is licensed under the MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodfords-solutions%2Fnestjs-seeder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhodfords-solutions%2Fnestjs-seeder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodfords-solutions%2Fnestjs-seeder/lists"}