forest-express-mongoose
forest-express-mongoose copied to clipboard
[+] Polymorphism Mongoose refPath Support
cancelledBy: { type: mongoose.Schema.Types.ObjectId, refPath: 'cancelledByUserType' }
This kind of refPath instead of ref is not working. I know it's a new feature. It would be great if this feature is provided.
Hi @ItsMrAkhil, thank you for the idea!
Can you provide a small piece of code to help us reproduce and implement the refPath support?
🌲🌲🌲
Hi, @arnaudbesnier Let me explain it in a clear way.
Let's say I have three mongoose schemas like below
AdminSchema:
{
name: { type: String },
email: { type: String },
}
UserSchema:
{
name: { type: String },
email: { type: String },
}
BookingSchema:
{
item: { type: mongoose.Schema.Types.ObjectId, ref: 'item' }, // Currently supported by forest admin
createdBy: { type: mongoose.Schema.Types.ObjectId, refPath: 'createdByUserType' }, // Currently not supported by forest admin
createdByUserType: { type: String, enum: ['Admin', 'User'] },
}
If we want to create a booking and we have only one user schema we all set to go with current forest admin. But if we do not know who is going to create a booking and you want mongoose population to work properly you need to use refPath provided by the mongoose.
And now the problem with the current forest admin is -> it is not showing the related details of createdBy field in the document. In this place, it is showing the object id which is not so great. Whereas I am expecting the populated document details (So, that I can choose whatever field I want. In my case it is name in both schemas AdminSchema as well as in UserSchema ). I am attaching an image below as an example.

In the image, paymentBy has to be populated based on paymentByUserType.
Best regards, Akhil
Thanks @ItsMrAkhil, it is crystal clear. This is a feature we really need to have. I'll add a story for this in our backlog but I cannot provide any ETA at this time.
Hi @arnaudbesnier Any updates on this ? Is it still planned in your backlog ?
waiting for this
Hey @BenjaminG @akashdeepsinghal This feature is not yet planned in our 6-weeks roadmap. This is something more and more customers are asking for (accross the stacks). I cannot provide any ETA but it could be prioritised in a few months.
Hi @akashdeepsinghal, can you precise me your Forest Admin project name to note your request in our roadmap? You can contact me at [email protected]. Thanks
I have got a workaround without referring the object: Following is an example:
const mongoose = require('mongoose'); const schema = mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, // refer as the mongoose ObjectID type // ref: 'users', // avoid referring the object to any other collection required: true }, planName: { type: String ....................
@arnaudbesnier It's been a couple of years since I open this issue. Still looking forward to the solution. Any ETA on this?
@ItsMrAkhil, I am sorry to tell you that there is no ETA for this feature request. We'd love to have the time to implement this feature, but we're still a small team focused on other top priorities.
Thanks for your patience.
@arnaudbesnier Can I create my own PR for this? Because it will help me a lot if I have this feature.
Yes you can fork the repo and suggest changes that would support this feature.