Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created January 14, 2019 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvinkcx/2ab664b88daa1a7c377e33d59f9b0736 to your computer and use it in GitHub Desktop.
Save melvinkcx/2ab664b88daa1a7c377e33d59f9b0736 to your computer and use it in GitHub Desktop.
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