{"id":18523605,"url":"https://github.com/mongoosejs/mongoose-lean-virtuals","last_synced_at":"2025-04-09T11:31:42.602Z","repository":{"id":41798338,"uuid":"92613640","full_name":"mongoosejs/mongoose-lean-virtuals","owner":"mongoosejs","description":"Attach virtuals to the results of mongoose lean() queries","archived":false,"fork":false,"pushed_at":"2023-08-17T19:09:48.000Z","size":154,"stargazers_count":42,"open_issues_count":7,"forks_count":24,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T14:59:54.949Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongoosejs.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}},"created_at":"2017-05-27T17:27:23.000Z","updated_at":"2024-06-07T23:37:07.370Z","dependencies_parsed_at":"2024-06-07T23:51:56.708Z","dependency_job_id":null,"html_url":"https://github.com/mongoosejs/mongoose-lean-virtuals","commit_stats":null,"previous_names":["vkarpov15/mongoose-lean-virtuals"],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-lean-virtuals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-lean-virtuals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-lean-virtuals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoosejs%2Fmongoose-lean-virtuals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongoosejs","download_url":"https://codeload.github.com/mongoosejs/mongoose-lean-virtuals/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247856545,"owners_count":21007621,"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-06T17:36:28.499Z","updated_at":"2025-04-09T11:31:42.597Z","avatar_url":"https://github.com/mongoosejs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-lean-virtuals\n\nAttach virtuals to the results of mongoose queries when using [`.lean()`](https://mongoosejs.com/docs/api.html#query_Query-lean).\n\n[Read the docs here](http://plugins.mongoosejs.io/plugins/lean-virtuals).\n\n# Usage\n\n```javascript\nconst mongooseLeanVirtuals = require('mongoose-lean-virtuals');\n\n// Example schema\nconst userSchema = new mongoose.Schema({ name: String });\n\nuserSchema.virtual('lowercase').get(function() {\n  return this.name.toLowerCase();\n});\n\n// Now, the `lowercase` property will show up even if you do a lean query\nuserSchema.plugin(mongooseLeanVirtuals);\n\n// Later\n\n// You **must** pass `virtuals: true` to `lean()`, otherwise `lowercase`\n// won't be in `res`\nconst res = await UserModel.find().lean({ virtuals: true });\n```\n\n# TypeScript\n\nMongoose's `lean()` function typings don't know about `virtuals: true`, so you need to explicitly set the type when calling `lean()`.\nThis module exports a convenient `VirtualsForModel` helper type that returns the virtual property types for a given model.\nThe below example shows using `VirtualsForModel` along with `lean\u003cTypeOverride\u003e()`.\n\n```ts\nimport mongooseLeanVirtuals, { VirtualsForModel } from \"mongoose-lean-virtuals\";\n\ninterface ITest {\n  name: string\n}\n\nconst testSchema = new mongoose.Schema(\n  { name: { type: String, required: true } },\n  {\n    virtuals: {\n      nameUpper: {\n        get() {\n          return this.name.toUpperCase();\n        }\n      }\n    }\n  }\n);\n\ntestSchema.plugin(mongooseLeanVirtuals);\n\nconst TestModel = mongoose.model('Test', testSchema);\n\nTestModel.findOne().lean\u003cITest \u0026 VirtualsForModel\u003ctypeof TestModel\u003e\u003e({ virtuals: true }).orFail().then(doc =\u003e {\n  const name: string = doc.name;\n  const nameUpper: string = doc.nameUpper;\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fmongoose-lean-virtuals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoosejs%2Fmongoose-lean-virtuals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoosejs%2Fmongoose-lean-virtuals/lists"}