Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enabling Virtuals In Mongoose Lean Mode (in a FeathersJs service)
import mongooseLeanVirtuals from 'mongoose-lean-virtuals';
export default (app) => {
const mongooseClient = app.get('mongooseClient');
const {Schema} = mongooseClient;
const {ObjectId} = Schema.Types;
const user = new Schema({
// ...
});
// Define a virtual attribute `UID`
user.virtual('uid').get(function () {
return this._id;
});
user.set('toObject', { virtuals: true }); // add this
user.set('toJSON', { virtuals: true }); // add this
user.plugin(mongooseLeanVirtuals); // add this
return mongooseClient.model('user', user);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment