{"id":34787169,"url":"https://github.com/darky/keyv-secondary","last_synced_at":"2025-12-25T09:30:33.754Z","repository":{"id":323216714,"uuid":"1092462424","full_name":"darky/keyv-secondary","owner":"darky","description":"Keyv with secondary indexes","archived":false,"fork":false,"pushed_at":"2025-12-13T18:18:21.000Z","size":86,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-15T06:34:38.202Z","etag":null,"topics":["index","keyv","secondary"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-08T17:10:29.000Z","updated_at":"2025-12-13T18:18:24.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/darky/keyv-secondary","commit_stats":null,"previous_names":["darky/keyv-secondary"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/darky/keyv-secondary","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fkeyv-secondary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fkeyv-secondary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fkeyv-secondary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fkeyv-secondary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darky","download_url":"https://codeload.github.com/darky/keyv-secondary/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darky%2Fkeyv-secondary/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28025541,"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","status":"online","status_checked_at":"2025-12-25T02:00:05.988Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["index","keyv","secondary"],"created_at":"2025-12-25T09:30:31.418Z","updated_at":"2025-12-25T09:30:33.744Z","avatar_url":"https://github.com/darky.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keyv-secondary\n\n![NPM Version](https://img.shields.io/npm/v/keyv-secondary)\n\n[Keyv](https://keyv.org) with secondary indexes\n\n## Example\n\n```typescript\nimport assert from 'node:assert'\nimport { KeyvSecondary } from 'keyv-secondary'\n\ntype Id = number \u0026 {__brand: 'id'}\ntype Person = { age: number; firstName: string; lastName: string }\ntype Indexes = 'byYoungAge' | 'byOldAge'\n\nconst kv = new KeyvSecondary\u003cId, Person, Indexes\u003e(\n  {}, // Keyv options\n  {\n    indexes: [\n      {\n        field: 'age',\n        filter: ({ age }) =\u003e age \u003e= 40,\n        name: 'byOldAge',\n      },\n      {\n        field: 'age',\n        filter: ({ age }) =\u003e age \u003c 40,\n        name: 'byYoungAge',\n      },\n    ],\n  }\n)\n\nawait kv.set(1 as Id, { age: 30, firstName: 'Galina', id: 1 as Id, lastName: 'Ivanova' })\nawait kv.set(2 as Id, { age: 59, firstName: 'Zinaida', id: 2 as Id, lastName: 'Petrovna' })\nawait kv.set(3 as Id, { age: 17, firstName: 'Stepan', id: 3 as Id, lastName: 'Lukov' })\nawait kv.set(4 as Id, { age: 59, firstName: 'Ibragim', id: 4 as Id, lastName: 'Lukov' })\n\nassert.deepStrictEqual(await kv.getByIndex('byOldAge', 59), [\n  {\n    age: 59,\n    firstName: 'Zinaida',\n    id: 2,\n    lastName: 'Petrovna',\n  },\n  {\n    age: 59,\n    firstName: 'Ibragim',\n    id: 4,\n    lastName: 'Lukov',\n  },\n])\n\nassert.deepStrictEqual(await kv.getByIndex('byYoungAge', 30), [{ age: 30, firstName: 'Galina', id: 1, lastName: 'Ivanova' }])\n\nassert.deepStrictEqual(await kv.getByIndex('byYoungAge', 17), [{ age: 17, firstName: 'Stepan', id: 3, lastName: 'Lukov' }])\n\nassert.deepStrictEqual(await kv.getByIndex('byYoungAge', 59), [])\n\nassert.deepStrictEqual(await kv.getByIndex('byOldAge', 17), [])\n```\n\n## Concurrency Handling\n\nInternally, `KeyvSecondary` uses a `p-queue` with a concurrency limit of 1 to ensure that operations such as `set` and `delete` are processed in a serialized manner. This avoids race conditions when multiple asynchronous operations are performed on the same instance.\n\nIf you want to provide your own concurrency control mechanism, you can use the `options.locker` parameter:\n\n```typescript\nconst customLocker = async \u003cT\u003e(cb: () =\u003e T): T =\u003e {\n  // Custom concurrency logic\n  return cb()\n}\n\nconst kv = new KeyvSecondary\u003cId, Person, Indexes\u003e(\n  {}, // Keyv options\n  {\n    locker: customLocker, // Pass your custom locker function\n    // ... indexes\n  }\n)\n```\n\nBy default, if no custom `locker` is provided, the internal `p-queue` will be used.\n\n## Index generation by field example for Postgres\n\n```sql\nselect\n  'keyv:$secondary-index:byCountry:'\n    || (value-\u003e'value'-\u003e\u003e'country')::text as key,\n  jsonb_build_object(\n    'value',\n    array_agg(trim('keyv:' from key)::int)\n  ) as value\nfrom keyv_user \nwhere not key like '%$secondary-index%'\ngroup by value-\u003e'value'-\u003e\u003e'country'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fkeyv-secondary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarky%2Fkeyv-secondary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarky%2Fkeyv-secondary/lists"}