mongodb-js / mongoose-autopopulate

Always populate() certain fields in your mongoose schemas
Apache License 2.0
221 stars 36 forks source link

Add support for nested autopopulated virtuals #104

Closed Danchoys closed 1 year ago

Danchoys commented 1 year ago

Currently this library only populates the top-level virtuals. The following example would not work:

const cartItemSchema = new Schema({
  sku: { type: 'string', required: true },
  quantity: { type: 'number', required: true, default: 0 },
});

cartItemSchema.virtual('product', {
  ref: 'Product',
  localField: 'sku',
  foreignField: 'sku',
  justOne: true,
  autopopulate: true,
});

const cartSchema = new Schema({
  items: { type: [cartItemSchema] }
});

cartSchema.plugin(require('mongoose-autopopulate'));
takiuddinahmed commented 1 year ago

I also need this feature. I was trying to use nested autopopulated virtuals but it is not working.

Danchoys commented 1 year ago

@takiuddinahmed you can try the attached PR and either have a fork or apply it as a patch using the patch-package tool until this is incorporated into the library.

Danchoys commented 1 year ago

@vkarpov15 Валера, настало твое время 🙏