Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mongoose-autopopulate populate with typegoose

I am using nest js with mongodb and for the mongodb modelling i am using Typegoose . they did not have autopopulate functionality yet.

I am using

https://github.com/szokodiakos/typegoose

and just want to use auto populate

https://www.npmjs.com/package/mongoose-autopopulate

like image 619
Akash Gupta Avatar asked Sep 06 '25 17:09

Akash Gupta


1 Answers

There is an autopopulate option for the prop decorator, which is used like so:

import { prop, Ref } from '@typegoose/typegoose';

class Person {
  @prop()
  name: string
}

class User {
  @prop({ autopopulate: true, ref: Person })
  person: Ref<Person>;

  @prop()
  password: string;
}

like image 93
Mateja Petrovic Avatar answered Sep 09 '25 08:09

Mateja Petrovic