{"id":19077889,"url":"https://github.com/backendstack21/item-store-redis","last_synced_at":"2025-04-30T04:34:16.874Z","repository":{"id":80728606,"uuid":"606036036","full_name":"BackendStack21/item-store-redis","owner":"BackendStack21","description":"This module provides classes that implement repositories for storing and retrieving data in a Redis database","archived":false,"fork":false,"pushed_at":"2023-04-02T22:37:47.000Z","size":98,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-25T12:05:00.763Z","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/BackendStack21.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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,"zenodo":null},"funding":{"custom":"https://www.paypal.me/kyberneees"}},"created_at":"2023-02-24T13:04:08.000Z","updated_at":"2024-08-20T23:23:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"6f401d63-818a-45c2-b534-019b3e0158de","html_url":"https://github.com/BackendStack21/item-store-redis","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BackendStack21%2Fitem-store-redis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BackendStack21%2Fitem-store-redis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BackendStack21%2Fitem-store-redis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BackendStack21%2Fitem-store-redis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BackendStack21","download_url":"https://codeload.github.com/BackendStack21/item-store-redis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251644178,"owners_count":21620615,"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-11-09T02:03:46.448Z","updated_at":"2025-04-30T04:34:16.869Z","avatar_url":"https://github.com/BackendStack21.png","language":"TypeScript","funding_links":["https://www.paypal.me/kyberneees"],"categories":[],"sub_categories":[],"readme":"# Introduction\n[![NPM version](https://badgen.net/npm/v/item-store-redis)](https://www.npmjs.com/package/item-store-redis)\n[![NPM Total Downloads](https://badgen.net/npm/dt/item-store-redis)](https://www.npmjs.com/package/item-store-redis)\n[![License](https://badgen.net/npm/license/item-store-redis)](https://www.npmjs.com/package/item-store-redis)\n[![TypeScript support](https://badgen.net/npm/types/item-store-redis)](https://www.npmjs.com/package/item-store-redis)\n[![Github stars](https://badgen.net/github/stars/BackendStack21/item-store-redis?icon=github)](https://github.com/BackendStack21/item-store-redis.git)\n\n\u003cimg src=\"illustration.svg\" width=\"400\"\u003e  \n\n\u003e Explanatory article: https://medium.com/@kyberneees/introducing-item-store-redis-a-high-performance-redis-based-item-store-for-efficient-data-4510d4e9fcf9\n\nThis module provides classes that implement repositories for storing and retrieving data in a Redis database:\n\n- `ItemRepository`: The class provides basic functionality for storing and retrieving individual items, as well as querying all items or a subset of items in a paginated manner. The items are stored in Redis using a key-value data model with expiration support. \n- `SortedItemRepository`: The class provides a more advanced functionality for storing and retrieving items in a sorted manner. The items are sorted based on a numeric score, and can be queried in a paginated manner as well. The items are stored in Redis using a sorted set data model, making it more performant and efficient at scale (see `demos/benchmark.ts`). However, this model does not supports automatic expiration.\n\n```ts\nimport { ItemRepository, SortedItemRepository, IItem } from 'item-store-redis'\n```\n\n## Class ItemRepository\n\n### Constructor\n```ts\nconstructor (name: string, redis?: Redis | Cluster)\n```\n\n### Usage\n```ts\nimport { ItemRepository, IItem } from 'item-store-redis'\nimport Redis from 'ioredis'\n\nconst redis = new Redis({ host: 'localhost', port: 6379 })\nconst repository = new ItemRepository\u003cT\u003e('my-unsorted-repo', redis)\n...\n```\n\n### Interface\n```ts\ninterface IItemRepository\u003cT\u003e {\n  name: string\n  redis: Redis | Cluster\n  set: (item: IItem\u003cT\u003e, expirationInSeconds?: number) =\u003e Promise\u003cvoid\u003e\n  getById: (id: string) =\u003e Promise\u003cIItem\u003cT\u003e | null\u003e\n  getAll: () =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n  getPaginated: (page: number, pageSize: number) =\u003e Promise\u003cIPaginatedItems\u003cT\u003e\u003e\n  hasItem: (id: string) =\u003e Promise\u003cboolean\u003e\n  deleteById: (id: string) =\u003e Promise\u003cvoid\u003e\n  deleteAll: () =\u003e Promise\u003cvoid\u003e\n  count: () =\u003e Promise\u003cnumber\u003e\n}\n```\n\n## Class SortedItemRepository\n\n### Constructor\n```ts\nconstructor (name: string, redis?: Redis | Cluster)\n```\n\n### Usage\n```ts\nimport { SortedItemRepository, IItem } from 'item-store-redis'\nimport Redis from 'ioredis'\n\nconst redis = new Redis({ host: 'localhost', port: 6379 })\nconst repository = new SortedItemRepository\u003cT\u003e('my-sorted-repo', redis)\n...\n```\n\n### Interface\n```ts\n/**\n * ISortedItemRepository is an interface that defines the methods for interacting with a\n * sorted item repository that uses Redis as the storage system.\n */\nexport interface ISortedItemRepository\u003cT\u003e {\n  name: string\n  redis: Redis | Cluster\n\n  /**\n   * Sets an item in the repository.\n   * @param item - The item to be added.\n   */\n  set: (item: IItem\u003cT\u003e) =\u003e Promise\u003cvoid\u003e\n\n  /**\n   * Retrieves an item from the repository by its ID.\n   * @param id - The ID of the item to retrieve.\n   * @returns The item if found, null otherwise.\n   */\n  getById: (id: string) =\u003e Promise\u003cIItem\u003cT\u003e | null\u003e\n\n  /**\n   * Retrieves all items from the repository.\n   * @returns An array of all items in the repository.\n   */\n  getAll: () =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n\n  /**\n   * Retrieves a paginated set of items from the repository.\n   * @param page - The page number to retrieve.\n   * @param pageSize - The number of items per page.\n   * @returns An object containing the paginated items and the total count of items.\n   */\n  getPaginated: (page: number, pageSize: number) =\u003e Promise\u003cIPaginatedItems\u003cT\u003e\u003e\n\n  /**\n   * Retrieves the score of an item by its ID.\n   * @param id - The ID of the item.\n   * @returns The score of the item if found, null otherwise.\n   */\n  getItemScoreById: (id: string) =\u003e Promise\u003cnumber | null\u003e\n\n  /**\n   * Retrieves items by their scores within a specified range.\n   * @param min - The minimum score value.\n   * @param max - The maximum score value.\n   * @returns An array of items with scores within the specified range.\n   */\n  getItemsByScore: (min: number, max: number) =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n\n  /**\n   * Deletes a page of items from the repository.\n   * @param page - The page number of items to delete.\n   * @param pageSize - The number of items per page.\n   */\n  deletePage: (page: number, pageSize: number) =\u003e Promise\u003cvoid\u003e\n\n  /**\n   * Checks if an item exists in the repository by its ID.\n   * @param id - The ID of the item.\n   * @returns A boolean indicating whether the item exists.\n   */\n  hasItem: (id: string) =\u003e Promise\u003cboolean\u003e\n\n  /**\n   * Deletes an item from the repository by its ID.\n   * @param id - The ID of the item to delete.\n   */\n  deleteById: (id: string) =\u003e Promise\u003cvoid\u003e\n\n  /**\n   * Deletes all items from the repository.\n   */\n  deleteAll: () =\u003e Promise\u003cvoid\u003e\n\n  /**\n   * Retrieves the count of items in the repository.\n   * @returns The count of items.\n   */\n  count: () =\u003e Promise\u003cnumber\u003e\n\n  /**\n   * Retrieves the first N items from the repository.\n   * @param n - The number of items to retrieve.\n   * @returns An array of the first N items.\n   */\n  getFirstNItems: (n: number) =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n\n  /**\n   * Retrieves the last N items from the repository.\n   * @param n - The number of items to retrieve.\n   * @returns An array of the last N items.\n   */\n  getLastNItems: (n: number) =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n\n  /**\n   * Retrieves items within a specified index range.\n   * @param start - The starting index.\n   * @param end - The ending index.\n   *\n   * @returns An array of items within the specified index range.\n   */\n  getItemsInRange: (start: number, end: number) =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n\n  /**\n   *\n   * Checks if any items exist within a specified score range.\n   * @param min - The minimum score value.\n   * @param max - The maximum score value.\n   * @returns A boolean indicating whether any items exist within the specified score range.\n   */\n  existsInRange: (min: number, max: number) =\u003e Promise\u003cboolean\u003e\n\n  /**\n   *\n   * Retrieves the next N items with scores greater than a specified score.\n   * @param score - The score to compare.\n   * @param n - The number of items to retrieve.\n   * @returns An array of the next N items with scores greater than the specified score.\n   */\n  getNextNItemsGreaterThanScore: (score: number, n: number) =\u003e Promise\u003cIItem\u003cT\u003e[]\u003e\n}\n\n```\n\n# License\n\n```\nMIT License\n\nCopyright (c) 2023 21no.de\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbackendstack21%2Fitem-store-redis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbackendstack21%2Fitem-store-redis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbackendstack21%2Fitem-store-redis/lists"}