ts-mongoose
ts-mongoose copied to clipboard
Support for refPath and dynamic population
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 do you found a solution for this?
I gave up on using these sorts of libs and just went for straight mongoose with types 🤷🏻♂️
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)