{"id":16736157,"url":"https://github.com/flexdinesh/next-batch","last_synced_at":"2025-04-10T12:21:16.613Z","repository":{"id":152397772,"uuid":"625818157","full_name":"flexdinesh/next-batch","owner":"flexdinesh","description":"A promise batching utility mostly used in GraphQL resolvers to avoid N + 1 data fetching","archived":false,"fork":false,"pushed_at":"2023-04-13T01:47:15.000Z","size":345,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T11:08:10.676Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/flexdinesh.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-04-10T07:00:20.000Z","updated_at":"2025-01-04T08:21:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"d3bcafec-668c-4da9-a12b-65f61aa70c37","html_url":"https://github.com/flexdinesh/next-batch","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Fnext-batch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Fnext-batch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Fnext-batch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flexdinesh%2Fnext-batch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flexdinesh","download_url":"https://codeload.github.com/flexdinesh/next-batch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217179,"owners_count":21066633,"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":[],"created_at":"2024-10-13T00:08:30.832Z","updated_at":"2025-04-10T12:21:16.602Z","avatar_url":"https://github.com/flexdinesh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# next-batch\n\nA promise batching utility mostly used in GraphQL resolvers to avoid N + 1 data fetching\n\n## Install\n\n```sh\nnpm add next-batch\n```\n\n## Usage\n\n```ts\nimport { nextBatch } from \"next-batch\";\n\n// in your resolvers\nUser: {\n  tasks: async ({ id: userId }, _args, context) =\u003e {\n    // batch to collect all resolver requests that are flushed\n    // in the next execution frame using process.nextTick()\n    const tasksBatch = nextBatch({\n      // unique string key to collect all promises into a single batch\n      key: \"tasks\",\n      // batchHandler is the callback that will be invoked when the batch\n      // is flushed in the next tick. All keys from taskBatch.add(key)\n      // will be collected as an array and sent as an argument to this\n      // batchHandler callback\n      batchHandler: async (keys: { id: number }[]) =\u003e {\n        const tasks = await taskDB.findByUserIds({\n          ids: keys.map((key) =\u003e key.id),\n        });\n        // batch handler should return a Map with every key in keys arg\n        // and its corresponding value as a map entry.\n        // remember to use the same key reference and don't deconstruct\n        // and construct a new object as map key\n        const result = new Map\u003ctypeof keys[number], { title: string }\u003e();\n        keys.forEach((key) =\u003e {\n          const task = tasks.filter((task) =\u003e task.id === key.id);\n          result.set(key, task);\n        });\n        return result;\n      },\n    });\n    // tasks are requested per user but resolved in batch for all users\n    // via the JavaScript magic of our next-batch util\n    const tasks = await tasksBatch.add({ id: userId });\n    return tasks;\n  };\n}\n```\n\n## License\n\nMIT © [Dinesh Pandiyan](https://github.com/flexdinesh)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflexdinesh%2Fnext-batch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflexdinesh%2Fnext-batch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflexdinesh%2Fnext-batch/lists"}