{"id":18662580,"url":"https://github.com/zcong1993/db-cache","last_synced_at":"2025-10-04T11:55:43.413Z","repository":{"id":37848130,"uuid":"391000587","full_name":"zcong1993/db-cache","owner":"zcong1993","description":null,"archived":false,"fork":false,"pushed_at":"2025-08-11T18:06:20.000Z","size":409,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-11T08:51:38.467Z","etag":null,"topics":[],"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/zcong1993.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-07-30T09:05:30.000Z","updated_at":"2022-06-23T10:58:08.000Z","dependencies_parsed_at":"2024-07-31T22:25:32.161Z","dependency_job_id":"891c722d-64f2-402d-94c6-2c227990dfea","html_url":"https://github.com/zcong1993/db-cache","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/zcong1993/db-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fdb-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fdb-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fdb-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fdb-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcong1993","download_url":"https://codeload.github.com/zcong1993/db-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fdb-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278308624,"owners_count":25965654,"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-10-04T02:00:05.491Z","response_time":63,"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":[],"created_at":"2024-11-07T08:13:03.319Z","updated_at":"2025-10-04T11:55:43.397Z","avatar_url":"https://github.com/zcong1993.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# db-cache\n\n[![NPM version](https://img.shields.io/npm/v/@zcong/db-cache.svg?style=flat)](https://npmjs.com/package/@zcong/db-cache)\n[![NPM downloads](https://img.shields.io/npm/dm/@zcong/db-cache.svg?style=flat)](https://npmjs.com/package/@zcong/db-cache)\n[![JS Test](https://github.com/zcong1993/db-cache/actions/workflows/js-test.yml/badge.svg)](https://github.com/zcong1993/db-cache/actions/workflows/js-test.yml)\n[![codecov](https://codecov.io/gh/zcong1993/db-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/zcong1993/db-cache)\n\n\u003e like [go-zero cache](https://go-zero.dev/cn/redis-cache.html), but for NodeJS\n\n## Features\n\n- **universal** Support [typeorm](https://github.com/typeorm/typeorm) and [sequelize-typescript](https://github.com/RobinBuschmann/sequelize-typescript) by default, alsoe support extensions\n- **singleflight** Concurrent requests for the same instance will only call the database once\n- **cache nonexist** Records that do not exist in the database will also be cached for a period of time\n- **memory efficient** A record will only cache one complete data, and a unique key will cache one primary key reference\n- **sharding** Out-of-the-box support for Redis cluster sharding by [@zcong/node-redis-cache](https://github.com/zcong1993/node-redis-cache)\n- **metrics** Out-of-the-box support prometheus metrics by [@zcong/node-redis-cache](https://github.com/zcong1993/node-redis-cache)\n\n## Install\n\n```bash\n$ yarn add @zcong/db-cache\n# or npm\n$ npm i @zcong/db-cache --save\n```\n\n## Usage\n\n### Typeorm\n\n```ts\n@Entity()\nexport class Student {\n  @PrimaryGeneratedColumn()\n  studentId: number\n\n  @Column({\n    unique: true,\n  })\n  cardId: string\n\n  @Column()\n  age: number\n}\n\nconst redis = new Redis()\nconst cache = new RedisCache({ redis, prefix: 'typeorm' })\n\nconst student = new TypeormCache(new TypeormAdaptor(StudentRepository), cache, {\n  disable: false,\n  expire: 60, // cache expire seconds\n  uniqueFields: ['cardId'], // cacheFindByUniqueKey method fields allowlist, filed must be unique\n})\n\n// findone with cache\nconsole.log(await student.cacheFindByPk(1))\nconsole.log(await student.cacheFindByUniqueKey('cardId', 'card-01'))\n// updateone, will clear record cache\nconsole.log(await student.cacheUpdateByPk(record))\n// deleteone, will clear record cache\nconsole.log(await student.deleteByPk(1))\n// only clear record cache\nconsole.log(await student.deleteCache(real))\n```\n\n### Sequelize-Typescript\n\n```ts\n@Table\nexport class Student extends Model\u003cStudent, Partial\u003cStudent\u003e\u003e {\n  @Column({\n    primaryKey: true,\n    autoIncrement: true,\n  })\n  studentId: number\n\n  @Column({\n    unique: true,\n  })\n  cardId: string\n\n  @Column\n  age: number\n}\n\nconst redis = new Redis()\nconst cache = new RedisCache({ redis, prefix: 'sequelize' })\n\nconst student = new TypeormCache(\n  new SequelizeTypescriptAdaptor(StudentRepository),\n  cache,\n  {\n    disable: false,\n    expire: 60, // cache expire seconds\n    uniqueFields: ['cardId'], // cacheFindByUniqueKey method fields allowlist, filed must be unique\n  }\n)\n\n// findone with cache\nconsole.log(await student.cacheFindByPk(1))\nconsole.log(await student.cacheFindByUniqueKey('cardId', 'card-01'))\n// updateone, will clear record cache\nconsole.log(await student.cacheUpdateByPk(record))\n// deleteone, will clear record cache\nconsole.log(await student.deleteByPk(1))\n// only clear record cache\nconsole.log(await student.deleteCache(real))\n```\n\n## License\n\nMIT \u0026copy; zcong1993\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcong1993%2Fdb-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcong1993%2Fdb-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcong1993%2Fdb-cache/lists"}