{"id":16429465,"url":"https://github.com/helveg/nestjs-multi-provider","last_synced_at":"2026-05-18T15:06:53.457Z","repository":{"id":205504728,"uuid":"714385412","full_name":"Helveg/nestjs-multi-provider","owner":"Helveg","description":"Patch for NestJS for the multi provider pattern","archived":false,"fork":false,"pushed_at":"2024-03-20T15:27:32.000Z","size":115,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T17:44:24.117Z","etag":null,"topics":["multi","multi-provider","nestjs","provider"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/nestjs-multi-provider","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Helveg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-11-04T18:47:58.000Z","updated_at":"2023-11-08T12:18:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"b8a9e3ad-e2a2-474b-bb59-276a1cf543bc","html_url":"https://github.com/Helveg/nestjs-multi-provider","commit_stats":null,"previous_names":["helveg/nestjs-multi-provider"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helveg%2Fnestjs-multi-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helveg%2Fnestjs-multi-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helveg%2Fnestjs-multi-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helveg%2Fnestjs-multi-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Helveg","download_url":"https://codeload.github.com/Helveg/nestjs-multi-provider/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helveg%2Fnestjs-multi-provider/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259096158,"owners_count":22804622,"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":["multi","multi-provider","nestjs","provider"],"created_at":"2024-10-11T08:23:02.983Z","updated_at":"2026-05-18T15:06:48.438Z","avatar_url":"https://github.com/Helveg.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NestJS patch for multi provider pattern\n\nThis package patches the [Nest.js](https://nestjs.com/) `Module` decorator so that you can\nsomewhat straightforwardly use the multi-provider pattern. This package lets you specify\nthe `multi` flag to providers. You can then collect these providers into an array using\nthe `collect` function in the `imports` attribute of a target module, and inject the\ncollected array of provided values anywhere in the target module.\n\n## Installation\n\n```\nnpm install nestjs-multi-provider\n```\n\n## Usage\n\nA full example is available [here](https://github.com/Helveg/nestjs-multi-provider/tree/main/example).\n\nOn the first line of your root module file (most likely `src/app.module.ts`), import the\npackage to activate the patch:\n\n```typescript\nimport 'nestjs-multi-provider';\n```\n\n**Note:** This package monkey-patches the `Module` decorator, if for some reason you\ncan't, see [the failsafe method](#Using-the-failsafe-decorator).\n\n### 1. Provide\n\nSimply use any of the provider patterns NestJS supports, and add the `multi` property. You\ncan do this from any module, any amount of times. All the providers will ultimately be\navailable under the same token as an array of provided values.\n\n```typescript\n@Module({\n    providers: [\n        {provide: SOME_TOKEN, useClass: SomeService, multi: true},\n        {provide: SOME_TOKEN, useClass: SomeServiceWithDeps, multi: true, standalone: false},\n    ]\n})\n```\n\n**Important:** If your provider has dependencies, you must set `standalone` to `false`.\n\n### 2. Collect\n\nTo access all your providers, use the `collect` function:\n\n```typescript\n@Module({\n    imports: [collect(SOME_TOKEN)]\n})\nexport class CollectingModule {}\n```\n\n### 3. Inject\n\nYou can now inject your array of providers anywhere inside of `CollectingModule`:\n\n```typescript\nexport class SomeServiceInCollectingModule {\n    constructor(@Inject(SOME_TOKEN) collection: any[]) {\n        console.log(\"Collected:\", collection)\n        // This will print `Collected: [SomeService {}, SomeServiceWithDeps {}]`\n    }\n}\n```\n\n## Using the failsafe decorator\n\nInstead of using the patched `Module` decorator, you can also decorate any modules that\nprovide multi-providers with the `ModuleWithMulti` decorator:\n\n```typescript\nimport { ModuleWithMulti } from \"nestjs-multi-provider\";\n\n@ModuleWithMulti({\n    providers: [{provide: \"something\", useValue: 1, multi: true}]\n})\nexport class MyModule {}\n```\n\n## During testing\n\nI have noticed during testing that the monkey patch doesn't always function because\ncertain statements may be hoisted above the patch. You can overcome this by creating\na statement that itself will be hoisted. For example, for Jest:\n\n```\nimport { ModuleWithMulti } from \"nestjs-multi-provider\";\n\njest.mock('@nestjs/common', () =\u003e {\n  const nestjsCommon = jest.requireActual('@nestjs/common');\n  return {\n    ...nestjsCommon,\n    Module: ModuleWithMulti,\n  };\n});\n```\n\n## Important notes\n\n* Your providers are not available unless you use `collect`.\n* `collect` creates a `DynamicModule` and therefor must be placed in the `imports` property.\n* Your providers are by default considered `standalone` (not having any dependencies),\n  and are not actually provided by the module they are declared in, but by the collecting\n  `DynamicModule`. This is done to avoid creating needless dependencies between modules.\n* If your provider has dependencies, set `standalone` to `false` to have access to the\n  declaring module's providers. Doing this will result in a `forwardRef` of the declaring\n  module by the collecting module.\n* Multi providers are not supported in `DynamicModule`s.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelveg%2Fnestjs-multi-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelveg%2Fnestjs-multi-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelveg%2Fnestjs-multi-provider/lists"}