{"id":16351529,"url":"https://github.com/onhate/inbatches","last_synced_at":"2025-03-21T00:31:09.239Z","repository":{"id":188120423,"uuid":"678132243","full_name":"onhate/inbatches","owner":"onhate","description":"@InBatches(): A developer-friendly zero-dependency TypeScript library for efficient batching the execution of asynchronous parallel operations.","archived":false,"fork":false,"pushed_at":"2024-08-07T16:49:30.000Z","size":109,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-12T01:09:38.791Z","etag":null,"topics":["async","asynchronous-programming","batch","batches","dataloader","decorators","event-loop","graphql","javascript","performance","typescript"],"latest_commit_sha":null,"homepage":"","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/onhate.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-08-13T19:34:54.000Z","updated_at":"2024-08-16T14:26:09.000Z","dependencies_parsed_at":"2023-10-14T20:46:21.216Z","dependency_job_id":null,"html_url":"https://github.com/onhate/inbatches","commit_stats":null,"previous_names":["onhate/inbatches"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onhate%2Finbatches","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onhate%2Finbatches/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onhate%2Finbatches/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onhate%2Finbatches/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onhate","download_url":"https://codeload.github.com/onhate/inbatches/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221810387,"owners_count":16884105,"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":["async","asynchronous-programming","batch","batches","dataloader","decorators","event-loop","graphql","javascript","performance","typescript"],"created_at":"2024-10-11T01:09:35.163Z","updated_at":"2024-10-28T08:55:35.197Z","avatar_url":"https://github.com/onhate.png","language":"TypeScript","readme":"# @InBatches(📦,📦,📦,...)\n\nInBatches is a zero-dependency generic TypeScript library that provides a convenient way to batch executions that runs\nasynchronous.\n\nIt is designed to be used as part of your application's data fetching layer to provide a consistent API over various\nbackends and reduce requests to those backends via batching.\n\nThis library is especially useful for scenarios where you need to perform multiple asynchronous operations efficiently,\nsuch as when making network requests or performing database queries.\n\nHeavily inspired by [graphql/dataloader](https://github.com/graphql/dataloader) but simpler using decorators (😜 really\ndecoupled). Because of that the\nrest of your application doesn't event need to know about the batching/dataloader, it just works!\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Basic usage with `@InBatches` Decorator](#basic-usage-with-inbatches-decorator)\n    - [Advanced usage with custom `Batcher` class](#advanced-usage-with-custom-batcher-class)\n- [API](#api)\n    - [`BatcherOptions`](#batcheroptions)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Installation\n\n```bash\nnpm install inbatches\n```\n\nor\n\n```bash\nyarn add inbatches\n```\n\n## Usage\n\n### Basic usage with `@InBatches` Decorator\n\nThe simplest way to get the grown running is to use the `@InBatches` decorator. This decorator will wrap your method\nand will batch-enable it, like magic! 🧙‍♂️\n\n```typescript\nimport { InBatches } from 'inbatches';\n\nclass MyService {\n\n  // (optional) overloaded method, where you define the keys as `number` and the return type as `User` for typings\n  async fetch(key: number): Promise\u003cUser\u003e;\n\n  // This method is now batch-enabled\n  @InBatches()\n  async fetch(keys: number | number[]): Promise\u003cUser | User[]\u003e {\n    if (Array.isArray(keys)) return await this.db.getMany(keys);\n\n    // in reality the Decorator will wrap this method and it will never be called with a single key :)\n    throw new Error('It will never be called with a single key 😉');\n  }\n}\n```\n\nProfit! 🤑\n\n```typescript\nconst service = new MyService();\n\nconst result = [1, 2, 3, 4, 5].map(async id =\u003e {\n  return await service.fetch(id);\n});\n\n// The result will be an array of results in the same order as the keys\nresult.then(results =\u003e {\n  console.log(results); // Output: [{ id: 1, name: 'Result for key 1' }, ...]\n});\n```\n\n### Advanced usage with custom `Batcher` class\n\nAnother way to use the library is to create a class that extends the `Batcher` class and implement the `run` method.\nThis class will provide a `enqueue` method that you can use to enqueue keys for batched execution.\n\n```typescript\nimport { Batcher } from 'inbatches';\n\n// The `run` method will be called with an array of keys collected from the `enqueue` method\nclass MyBatcher extends Batcher\u003cnumber, string\u003e {\n  async run(ids: number[]): Promise\u003cstring[]\u003e {\n    // Perform asynchronous operations using the keys\n    // you must return an array of results in the same order as the keys\n    return this.db.getMany(ids);\n  }\n}\n```\n\nthen\n\n```typescript\n// Create an instance of your batcher\nconst batcher = new MyBatcher();\n\n// Enqueue keys for batched execution\nconst result = [1, 2, 3, 4, 5].map(async id =\u003e {\n  return await batcher.enqueue(id);\n});\n\n// The result will be an array of results in the same order as the keys\nresult.then(results =\u003e {\n  console.log(results); // Output: [{ id: 1, name: 'Result for key 1' }, ...]\n});\n```\n\n## API\n\n### `BatcherOptions`\n\nAn interface to specify options for the batcher.\n\n- `maxBatchSize`: The maximum number of keys to batch together. Default is `25`.\n- `delayWindowInMs`: (not recommended) The delay window in milliseconds before dispatching the batch. Default\n  is `undefined` and will use `process.nextTick` to dispatch the batch, which is highly efficient and fast. Only use\n  this if you really want to accumulate promises calls in a window of time before dispatching the batch.\n\n## Contributing\n\nContributions are welcome! Feel free to open issues or submit pull requests on\nthe [GitHub repository](https://github.com/onhate/inbatches).\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonhate%2Finbatches","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonhate%2Finbatches","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonhate%2Finbatches/lists"}