{"_id":"@double-spent/nestjs-dataloader","_rev":"2-c7c122cc91bd0cb58125f4d6a85d46eb","name":"@double-spent/nestjs-dataloader","dist-tags":{"pre":"0.1.0-pre.0","latest":"0.1.0"},"versions":{"0.1.0-pre.0":{"name":"@double-spent/nestjs-dataloader","version":"0.1.0-pre.0","description":"TBD ...","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"build":"tsup","test":"jest","lint":"eslint \"src/**/*.ts*\""},"repository":{"type":"git","url":"git+https://github.com/double-spent/nestjs-dataloader.git"},"overrides":{"nest-request-context":{"@nestjs/common":"~10.0.0"}},"dependencies":{"dataloader":"^2.2.2","uuid":"^9.0.1","nest-request-context":"^1.0.0"},"devDependencies":{"@changesets/cli":"^2.26.2","@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0","@nestjs/testing":"^10.2.7","@types/jest":"^29.5.6","@types/node":"^18.18.6","@types/uuid":"^9.0.6","@typescript-eslint/eslint-plugin":"^6.8.0","eslint-config-prettier":"^9.0.0","eslint-plugin-simple-import-sort":"^10.0.0","jest":"^29.7.0","nest-request-context":"^1.0.0","ts-jest":"^29.1.1","tsup":"^7.2.0","typescript":"^5.2.2"},"peerDependencies":{"@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0"},"optionalDependencies":{"nest-request-context":"^1.0.0"},"gitHead":"0627fb83c472d07a0368e6d6480fa4465e8569c5","bugs":{"url":"https://github.com/double-spent/nestjs-dataloader/issues"},"homepage":"https://github.com/double-spent/nestjs-dataloader#readme","_id":"@double-spent/nestjs-dataloader@0.1.0-pre.0","_nodeVersion":"16.19.0","_npmVersion":"8.19.3","dist":{"integrity":"sha512-E24ERPCS/gGuByAYz9Gr7+yIVYuT3pPp3+HccQ74DSC/rTm6KY2R36M7ogDd4zz7yru5A613ArZNMSri08o02g==","shasum":"f11fad60c35a6d40c97a8c733e95436e03ab5d52","tarball":"https://registry.npmjs.org/@double-spent/nestjs-dataloader/-/nestjs-dataloader-0.1.0-pre.0.tgz","fileCount":4,"unpackedSize":21669,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCvR1zX3F/M8R28E7RHs0LI5AJHJmVOw55JdU+qZLVCNQIhAJ58s/rUbZvN4bIOoVgJF/8KClxKut/7RDNQyCtyS957"}]},"_npmUser":{"name":"npmdash","email":"ldiego08@gmail.com"},"directories":{},"maintainers":[{"name":"npmdash","email":"ldiego08@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-dataloader_0.1.0-pre.0_1698297102881_0.102374480741801"},"_hasShrinkwrap":false},"0.1.0-pre.1":{"name":"@double-spent/nestjs-dataloader","version":"0.1.0-pre.1","description":"A dataloader implementation for NestJS including request-scoped cache and more","license":"MIT","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"build":"tsup","test":"jest","lint":"eslint \"src/**/*.ts*\""},"repository":{"type":"git","url":"git+https://github.com/double-spent/nestjs-dataloader.git"},"overrides":{"nest-request-context":{"@nestjs/common":"~10.0.0"}},"dependencies":{"dataloader":"^2.2.2","uuid":"^9.0.1","nest-request-context":"^1.0.0"},"devDependencies":{"@changesets/cli":"^2.26.2","@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0","@nestjs/testing":"^10.2.7","@types/jest":"^29.5.6","@types/node":"^18.18.6","@types/uuid":"^9.0.6","@typescript-eslint/eslint-plugin":"^6.8.0","eslint-config-prettier":"^9.0.0","eslint-plugin-simple-import-sort":"^10.0.0","jest":"^29.7.0","nest-request-context":"^1.0.0","ts-jest":"^29.1.1","tsup":"^7.2.0","typescript":"^5.2.2"},"peerDependencies":{"@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0"},"optionalDependencies":{"nest-request-context":"^1.0.0"},"gitHead":"bbce0b6b6c43d49c90a4215212fc8a533f1227d1","bugs":{"url":"https://github.com/double-spent/nestjs-dataloader/issues"},"homepage":"https://github.com/double-spent/nestjs-dataloader#readme","_id":"@double-spent/nestjs-dataloader@0.1.0-pre.1","_nodeVersion":"16.19.0","_npmVersion":"8.19.3","dist":{"integrity":"sha512-k3qNusR0j0iZ1VRbbrNFZxy3v3ChtvySve8jRv6tCaPw/GaOx9ocq3NmYPwZldEhmTlWtvsJaQcAoN7prN4INw==","shasum":"5b378bc335b9538834d78e1d6eadecc90555e775","tarball":"https://registry.npmjs.org/@double-spent/nestjs-dataloader/-/nestjs-dataloader-0.1.0-pre.1.tgz","fileCount":4,"unpackedSize":21895,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDJKyCyCuBe8lWt4QzKbUEhxERlUiU/UJNHzG4Vz0m56wIhALpg1rJ+NptxrYk1r2Wb858EMXlUm272v/VpfH+5qntb"}]},"_npmUser":{"name":"npmdash","email":"ldiego08@gmail.com"},"directories":{},"maintainers":[{"name":"npmdash","email":"ldiego08@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-dataloader_0.1.0-pre.1_1698299767066_0.6887056049173805"},"_hasShrinkwrap":false},"0.1.0":{"name":"@double-spent/nestjs-dataloader","version":"0.1.0","description":"A dataloader implementation for NestJS including request-scoped cache and more","license":"MIT","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"build":"tsup","test":"jest","lint":"eslint \"src/**/*.ts*\""},"repository":{"type":"git","url":"git+https://github.com/double-spent/nestjs-dataloader.git"},"overrides":{"nest-request-context":{"@nestjs/common":"~10.0.0"}},"dependencies":{"dataloader":"^2.2.2","uuid":"^9.0.1","nest-request-context":"^1.0.0"},"devDependencies":{"@changesets/cli":"^2.26.2","@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0","@nestjs/testing":"^10.2.7","@types/jest":"^29.5.6","@types/node":"^18.18.6","@types/uuid":"^9.0.6","@typescript-eslint/eslint-plugin":"^6.8.0","eslint-config-prettier":"^9.0.0","eslint-plugin-simple-import-sort":"^10.0.0","jest":"^29.7.0","nest-request-context":"^1.0.0","ts-jest":"^29.1.1","tsup":"^7.2.0","typescript":"^5.2.2"},"peerDependencies":{"@nestjs/common":"~10.0.0","@nestjs/core":"~10.0.0"},"optionalDependencies":{"nest-request-context":"^1.0.0"},"gitHead":"880a4f08424f7bf8d462adb545b144ccb30da08c","bugs":{"url":"https://github.com/double-spent/nestjs-dataloader/issues"},"homepage":"https://github.com/double-spent/nestjs-dataloader#readme","_id":"@double-spent/nestjs-dataloader@0.1.0","_nodeVersion":"18.17.0","_npmVersion":"9.6.7","dist":{"integrity":"sha512-JY40jzbU6JFutoyKG+HKUiup11MHQGp7vNXB1xXkGA0YdLAPJDEWi07yZaqv2WmkAmm4u0JbLDtrti2AKqSRTg==","shasum":"128bb3c23c4f89e2c633365f71b7c25338adb94d","tarball":"https://registry.npmjs.org/@double-spent/nestjs-dataloader/-/nestjs-dataloader-0.1.0.tgz","fileCount":4,"unpackedSize":26229,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDBdZZtFUGGG2HwGjupwXaJgv5KzO4XhBBjwrL/II8pnQIgSaWcT+D95BGqoOem3cBmgfsx8vB2hg71+FiA1uhLN/Y="}]},"_npmUser":{"name":"npmdash","email":"ldiego08@gmail.com"},"directories":{},"maintainers":[{"name":"npmdash","email":"ldiego08@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-dataloader_0.1.0_1698638748568_0.9869305125239511"},"_hasShrinkwrap":false}},"time":{"created":"2023-10-26T05:11:42.746Z","0.1.0-pre.0":"2023-10-26T05:11:43.061Z","modified":"2023-10-30T04:05:49.027Z","0.1.0-pre.1":"2023-10-26T05:56:07.375Z","0.1.0":"2023-10-30T04:05:48.818Z"},"maintainers":[{"name":"npmdash","email":"ldiego08@gmail.com"}],"description":"A dataloader implementation for NestJS including request-scoped cache and more","homepage":"https://github.com/double-spent/nestjs-dataloader#readme","repository":{"type":"git","url":"git+https://github.com/double-spent/nestjs-dataloader.git"},"bugs":{"url":"https://github.com/double-spent/nestjs-dataloader/issues"},"readme":"<h1 align=\"center\">⚡️ NestJS Dataloader</h1>\n<p align=\"center\">A simple dataloader implementation for NestJS including request-scoped caching.</p>\n\n<p align=\"center\">\n  <a href=\"https://badge.fury.io/js/@double-spent%2Fnestjs-dataloader\">\n    <img src=\"https://badge.fury.io/js/@double-spent%2Fnestjs-dataloader.svg\" alt=\"package npm version\" height=\"18\" />\n  </a>\n  <img src=\"https://img.shields.io/npm/l/%40double-spent%2Fnestjs-dataloader\" alt=\"package license\" height=\"18\">\n  <img src=\"https://img.shields.io/npm/dw/%40double-spent%2Fnestjs-dataloader\" alt=\"package weekly downloads\" height=\"18\" />\n</p>\n\n## About\n\n> [DataLoader](https://www.npmjs.com/package/dataloader) is a generic utility to be used as part of your application's\n> data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or\n> web services via batching and caching.\n\nThis library simplifies the integration of [DataLoader](https://www.npmjs.com/package/dataloader) within NestJS\napplications by providing a friendly wrapper around the original implementation. It also introduces a request-scoped\nDataLoader, enabling efficient data fetching while ensuring the isolation of requests, especially useful in GraphQL\nscenarios and services that doesn't support request-scope like [NestJS CQRS.](https://github.com/nestjs/cqrs)\n\n```ts\n// before:\n\nimport DataLoader from 'dataloader';\n\nconst userLoader = new DataLoader(async (keys) => {\n  // load users ..\n});\n\n// after:\n\nimport { DataLoader } from '@double-spent/nestjs-dataloader';\n\n@Injectable()\nclass UserLoader extends DataLoader<User['id'], User> {\n  async batchLoad(ids): User[] {\n    // load users ..\n  }\n}\n```\n\n## Installation\n\n```bash\nnpm install @double-spent/nestjs-dataloader\n```\n\n## Usage\n\n### Default DataLoader\n\nThe default DataLoader base class offers all the functionality the original implementation does by accepting the same\noptions in the constructor. To use it in a NestJS module, extend the base class, decorate it with `@Injectable()`, and\nadd it to the module providers and exports:\n\n```ts\n// 💾 UserLoader.ts\n\nimport { DataLoader } from '@double-spent/nestjs-dataloader';\n\n@Injectable()\nexport class UserLoader extends DataLoader<User['id'], User> {\n  async batchLoad(ids): User[] {\n    // load users ..\n  }\n}\n\n// 💾 UserController.ts\n\n@Controller()\nexport class UserController() {\n  constructor(userLoader: UserLoader) {}\n\n  @Get()\n  async user(@Param('id') id: User['id']) {\n    return this.userLoader.load(id);\n  }\n}\n\n// 💾 UserModule.ts\n\n@Module({\n  providers: [UserLoader],\n  controllers: [UserController],\n})\nexport class UserModule {}\n```\n\n### Request-scoped DataLoader\n\nThe default DataLoader uses a memoized cache by default that is shared across all requests. On the other hand, the\nrequest-scoped DataLoader leverages the [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#introduction)\nclass to initialize a cache that expires at the end of the request. This makes it safe to use in NestJS services that\nonly supports global scope but must handle request-scoped (or user-scoped) data, like\n[CQRS.](https://github.com/nestjs/cqrs)\n\nIn order for this loader to work, you must import the `RequestScopedDataLoaderModule` into your application which\ninitializes the cache storage. An error is thrown otherwise as there's no sense in using this DataLoader if no\nrequest-scoped storage is available.\n\n```ts\n// 💾 UserLoader.ts\n\nimport { RequestScopedDataLoader } from '@double-spent/nestjs-dataloader';\n\n@Injectable()\nexport class UserLoader extends RequestScopedDataLoader<User['id'], User> {\n  async batchLoad(ids): User[] {\n    // load users ..\n  }\n}\n\n// 💾 UserController.ts\n\n@Controller()\nexport class UserController() {\n  constructor(userLoader: UserLoader) {}\n\n  @Get()\n  async user(@Param('id') id: User['id']) {\n    return this.userLoader.load(id);\n  }\n}\n\n// 💾 UserModule.ts\n\n@Module({\n  providers: [UserLoader],\n  controllers: [UserController],\n  imports: [RequestScopedDataLoaderModule],\n})\nexport class UserModule {}\n```\n\n## Troubleshooting\n\n### I'm getting a runtime error when extending the `RequestScopedDataLoader` class.\n\nMake sure you're importing the `RequestScopedDataLoaderModule` into your application.\n\n## License\n\n[MIT License](https://andreasonny.mit-license.org/2019) © Andrea SonnY\n","readmeFilename":"README.md","license":"MIT"}