{"id":22435512,"url":"https://github.com/timeraa/mongodb-caching","last_synced_at":"2025-04-13T17:41:26.263Z","repository":{"id":41492667,"uuid":"509883941","full_name":"Timeraa/MongoDB-Caching","owner":"Timeraa","description":"MongoDB with automatic Caching functionality using Keyv.","archived":false,"fork":false,"pushed_at":"2023-02-24T18:04:07.000Z","size":27,"stargazers_count":4,"open_issues_count":4,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-06T04:32:24.075Z","etag":null,"topics":["automatic","cache","keyv","mongodb"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Timeraa.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":"2022-07-02T23:48:40.000Z","updated_at":"2025-02-10T16:07:15.000Z","dependencies_parsed_at":"2024-10-03T12:32:32.581Z","dependency_job_id":"99de225c-7665-49f0-a865-7d614c177bde","html_url":"https://github.com/Timeraa/MongoDB-Caching","commit_stats":{"total_commits":18,"total_committers":2,"mean_commits":9.0,"dds":0.2222222222222222,"last_synced_commit":"05585894f421046fdda496dc369d75fab857c675"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Timeraa%2FMongoDB-Caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Timeraa%2FMongoDB-Caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Timeraa%2FMongoDB-Caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Timeraa%2FMongoDB-Caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Timeraa","download_url":"https://codeload.github.com/Timeraa/MongoDB-Caching/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248755392,"owners_count":21156587,"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":["automatic","cache","keyv","mongodb"],"created_at":"2024-12-05T23:14:09.701Z","updated_at":"2025-04-13T17:41:26.212Z","avatar_url":"https://github.com/Timeraa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MongoDB-Caching [![Version](https://img.shields.io/npm/v/mongodb-caching.svg)](https://www.npmjs.com/package/mongodb-caching)\n\nMongoDB with automatic Caching functionality using Keyv.\n\n## Installation\n\n```bash\n# npm\nnpm install mongodb-caching\n\n# yarn\nyarn add mongodb-caching\n\n# pnpm\npnpm i mongodb-caching\n```\n\n## Cached functions\n\n_These functions automatically handle caching_\n\n- find\n- findOne\n- countDocuments\n- distinct\n\n**NOTE: When you update/insert/delete a document, the cache is not updated, you'll need to manually clear the cache using `customClass.clear()` or `customClass.keyv.clear()`**\n\n## Usage\n\n### Main\n\n```ts\nimport { MongoClient } from \"mongodb\";\n\nimport MongoDBCaching from \"mongodb-caching\";\n\ninterface UserCollectionSchema {\n\tusername: string;\n}\n\nclass User extends MongoDBCaching\u003cUserCollectionSchema\u003e {\n\tcreate(username: string) {\n\t\t//* this.collection refers to MongoDB.Db.Collection\n\t\treturn this.collection.insertOne({ username });\n\t}\n\n\tgetUser(username: string) {\n\t\t//* Internally handles caching\n\t\treturn this.findOne({ username });\n\t}\n}\n\nasync function run() {\n\tconst mongodb = new MongoClient(\"mongoUri\");\n\n\tconst userCollection = new User(mongodb.db(\"main\").collection(\"users\"));\n\n\tawait userCollection.create(\"Timeraa\");\n\n\tconsole.log(await userCollection.getUser(\"Timeraa\"));\n}\n\nrun();\n```\n\n### With context\n\n```ts\nimport { MongoClient } from \"mongodb\";\n\nimport MongoDBCaching from \"mongodb-caching\";\n\ninterface UserCollectionSchema {\n\tusername: string;\n\tip: string;\n}\n\ninterface Context {\n\tip: string;\n}\n\nclass User extends MongoDBCaching\u003cUserCollectionSchema, Context\u003e {\n\tcreate(username: string) {\n\t\t//* this.collection refers to MongoDB.Db.Collection\n\t\treturn this.collection.insertOne({\n\t\t\tusername,\n\t\t\tip: this.context!.ip\n\t\t});\n\t}\n\n\tgetUser(username: string) {\n\t\t//* Internally handles caching\n\t\treturn this.findOne({ username });\n\t}\n}\n\nasync function run() {\n\tconst mongodb = new MongoClient(\"mongoUri\");\n\n\tconst userCollection = new User(mongodb.db(\"main\").collection(\"users\"));\n\n\t//* Context would be set in let's say a middleware\n\tuserCollection.setContext({ ip: \"someIp\" });\n\n\tawait userCollection.create(\"Timeraa\");\n\n\tconsole.log((await userCollection.getUser(\"Timeraa\"))?.ip);\n}\n\nrun();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimeraa%2Fmongodb-caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimeraa%2Fmongodb-caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimeraa%2Fmongodb-caching/lists"}