{"id":13679755,"url":"https://github.com/krislefeber/nestjs-dataloader","last_synced_at":"2025-04-29T19:31:50.802Z","repository":{"id":47055865,"uuid":"214328768","full_name":"krislefeber/nestjs-dataloader","owner":"krislefeber","description":"Dataloader plugin for NestJS","archived":false,"fork":false,"pushed_at":"2024-01-17T05:15:02.000Z","size":361,"stargazers_count":149,"open_issues_count":23,"forks_count":47,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-19T14:52:04.563Z","etag":null,"topics":["dataloader","nestjs"],"latest_commit_sha":null,"homepage":"https://krislefeber.github.io/nestjs-dataloader/","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/krislefeber.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":"2019-10-11T02:46:33.000Z","updated_at":"2024-12-24T13:11:23.000Z","dependencies_parsed_at":"2024-06-18T16:42:52.364Z","dependency_job_id":"79f4a864-6145-4641-a22c-4177cc64c80e","html_url":"https://github.com/krislefeber/nestjs-dataloader","commit_stats":{"total_commits":72,"total_committers":13,"mean_commits":5.538461538461538,"dds":0.6527777777777778,"last_synced_commit":"f3acaf5570d74782d8e3035acd7c7210097a1ad7"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krislefeber%2Fnestjs-dataloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krislefeber%2Fnestjs-dataloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krislefeber%2Fnestjs-dataloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krislefeber%2Fnestjs-dataloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krislefeber","download_url":"https://codeload.github.com/krislefeber/nestjs-dataloader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251569549,"owners_count":21610575,"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":["dataloader","nestjs"],"created_at":"2024-08-02T13:01:09.094Z","updated_at":"2025-04-29T19:31:45.788Z","avatar_url":"https://github.com/krislefeber.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# NestJS Dataloader\n\n![Node.js CI](https://github.com/krislefeber/nestjs-dataloader/workflows/Node.js%20CI/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/krislefeber/nestjs-dataloader/badge.svg?branch=master)](https://coveralls.io/github/krislefeber/nestjs-dataloader?branch=master)\n\nNestJS dataloader simplifies adding [graphql/dataloader](https://github.com/graphql/dataloader) to your NestJS project. DataLoader aims to solve the common N+1 loading problem.\n\n## Installation\n\nInstall with yarn\n\n```bash\nyarn add nestjs-dataloader\n```\n\nInstall with npm\n\n```bash\nnpm install --save nestjs-dataloader\n```\n\n## Usage\n\n### NestDataLoader Creation\n\nWe start by implementing the `NestDataLoader` interface. This tells `DataLoader` how to load our objects.\n\n```typescript\nimport * as DataLoader from 'dataloader';\nimport { Injectable } from '@nestjs/common';\nimport { NestDataLoader } from 'nestjs-dataloader';\n...\n\n@Injectable()\nexport class AccountLoader implements NestDataLoader\u003cstring, Account\u003e {\n  constructor(private readonly accountService: AccountService) { }\n\n  generateDataLoader(): DataLoader\u003cstring, Account\u003e {\n    return new DataLoader\u003cstring, Account\u003e(keys =\u003e this.accountService.findByIds(keys));\n  }\n}\n```\n\nThe first generic of the interface is the type of ID the datastore uses. The second generic is the type of object that will be returned. In the above instance, we want `DataLoader` to return instances of the `Account` class.\n\n### Providing the NestDataLoader\n\nFor each NestDataLoader we create, we need to provide it to our module.\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { APP_INTERCEPTOR } from '@nestjs/core';\nimport {DataLoaderInterceptor} from 'nestjs-dataloader'\n...\n\n@Module({\n  providers: [\n    AccountResolver,\n    AccountLoader,\n    {\n      provide: APP_INTERCEPTOR,\n      useClass: DataLoaderInterceptor,\n    },\n  ],\n\n})\nexport class ResolversModule { }\n```\n\n### Using the NestDataLoader\n\nNow that we have a dataloader and our module is aware of it, we need to pass it as a parameter to an endpoint in our graphQL resolver.\n\n```typescript\nimport * as DataLoader from 'dataloader';\nimport { Loader } from 'nestjs-dataloader';\n...\n\n@Resolver(Account)\nexport class AccountResolver {\n\n    @Query(() =\u003e [Account])\n    public getAccounts(\n        @Args({ name: 'ids', type: () =\u003e [String] }) ids: string[],\n        @Loader(AccountLoader) accountLoader: DataLoader\u003cAccount['id'], Account\u003e): Promise\u003cAccount[]\u003e {\n        return accountLoader.loadMany(ids);\n    }\n}\n```\n\nThe important thing to note is that the parameter of the `@Loader` decorator is the entity/class of the `NestDataLoader` we want to be injected to the method. The DataLoader library will handle bulk retrieval and caching of our requests. Note that the caching is stored on a per-request basis.\n\n## Contributing\n\nPull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrislefeber%2Fnestjs-dataloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrislefeber%2Fnestjs-dataloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrislefeber%2Fnestjs-dataloader/lists"}