https://github.com/mongoosejs/mongoose-lean-virtuals
Attach virtuals to the results of mongoose lean() queries
https://github.com/mongoosejs/mongoose-lean-virtuals
Last synced: 2 months ago
JSON representation
Attach virtuals to the results of mongoose lean() queries
- Host: GitHub
- URL: https://github.com/mongoosejs/mongoose-lean-virtuals
- Owner: mongoosejs
- License: apache-2.0
- Created: 2017-05-27T17:27:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-08-17T19:09:48.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T14:59:54.949Z (about 1 year ago)
- Language: JavaScript
- Size: 150 KB
- Stars: 42
- Watchers: 3
- Forks: 24
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-lean-virtuals
Attach virtuals to the results of mongoose queries when using [`.lean()`](https://mongoosejs.com/docs/api.html#query_Query-lean).
[Read the docs here](http://plugins.mongoosejs.io/plugins/lean-virtuals).
# Usage
```javascript
const mongooseLeanVirtuals = require('mongoose-lean-virtuals');// Example schema
const userSchema = new mongoose.Schema({ name: String });userSchema.virtual('lowercase').get(function() {
return this.name.toLowerCase();
});// Now, the `lowercase` property will show up even if you do a lean query
userSchema.plugin(mongooseLeanVirtuals);// Later
// You **must** pass `virtuals: true` to `lean()`, otherwise `lowercase`
// won't be in `res`
const res = await UserModel.find().lean({ virtuals: true });
```# TypeScript
Mongoose's `lean()` function typings don't know about `virtuals: true`, so you need to explicitly set the type when calling `lean()`.
This module exports a convenient `VirtualsForModel` helper type that returns the virtual property types for a given model.
The below example shows using `VirtualsForModel` along with `lean()`.```ts
import mongooseLeanVirtuals, { VirtualsForModel } from "mongoose-lean-virtuals";interface ITest {
name: string
}const testSchema = new mongoose.Schema(
{ name: { type: String, required: true } },
{
virtuals: {
nameUpper: {
get() {
return this.name.toUpperCase();
}
}
}
}
);testSchema.plugin(mongooseLeanVirtuals);
const TestModel = mongoose.model('Test', testSchema);
TestModel.findOne().lean>({ virtuals: true }).orFail().then(doc => {
const name: string = doc.name;
const nameUpper: string = doc.nameUpper;
});
```