ts-mongoose icon indicating copy to clipboard operation
ts-mongoose copied to clipboard

Support for refPath and dynamic population

Open hennessyevan opened this issue 6 years ago • 3 comments

Is there a way to implement a refPath for the Type.ref() pattern?

I'm talking about this https://mongoosejs.com/docs/populate.html#dynamic-ref

hennessyevan avatar May 31 '19 22:05 hennessyevan

@hennessyevan do you found a solution for this?

ManAnRuck avatar Mar 20 '20 13:03 ManAnRuck

I gave up on using these sorts of libs and just went for straight mongoose with types 🤷🏻‍♂️

hennessyevan avatar Mar 20 '20 15:03 hennessyevan

You can just declare your field with the regular mongoose syntax, inside your ts-mongoose schema:

For example:

const MyModelSchema = createSchema(
  {
    type: Type.string({
      required: true,
      index: true,
      enum: Object.values(ItemType)
    }),
    items: { type: Schema.Types.ObjectId, refPath: "type" },
  }
)

const MyModel = typedModel("MyModel", MyModelSchema)

// ...

await MyModel.find().populate("items)

tezqa avatar May 16 '22 14:05 tezqa