README
Mongoose Populate
Mini population hook for Mongoose so you don't need to keep rewriting this.populate
and next
.
Installation
$ npm install mongoose-populate
CommonJS
const { Schema } = require('mongoose')
const { createPopulateHook } = require('mongoose-populate')
ESM
import { Schema } from 'mongoose'
import { createPopulateHook } from 'mongoose-populate'
Schema
const ExampleSchema = new Schema({
example: {
type: Schema.Types.ObjectId,
ref: 'OtherModel',
},
})
Hook replacement
- ExampleSchema.pre('find', function populateExampleHook(next) {
- this.populate('example');
- next();
- });
+ ExampleSchema.pre('find', createPopulateHook('example'));