I’m looking for advice on how to models references in a mongoose / Feathers / DoneJS application.
Consider a schema that looks like:
const monthlyOsProjectSchema = new Schema({
osProjectId: { type: Schema.Types.ObjectId, ref: 'os_project' },
significance: Number
});
const contributionMonthSchema = new Schema({
date: { type: Date, default: Date.now },
monthlyOSProjects: [ monthlyOsProjectSchema ]
})
In short, a contribution month looks like:
{
date: "10/20/1982"
monthlyOSProjects: [
{osProjectId: "23nh2dso8s", significance: 100}
]
}
The problem I’m dealing with is that if you use $populate like $populate=monthlyOSProjects.osProjectId instead of osProjectId coming back as a string, it comes back as an osProject:
{
date: "10/20/1982"
monthlyOSProjects: [
{osProjectId: {_id: "23nh2dso8s", name: "CanJS"}, significance: 100}
]
}
If I’m trying to create useful models that work regardless if $populate is there or not, I might have something like:
MonthlyOSProjects = DefineList.extend({
indexOfProjectById: function(osProjectId){
for(var i = 0 ; i < this.length; i++){
if(this.get(i).osProjectId === osProjectId) {
return i;
}
}
return -1;
}
})
If suddenly osProjectId is an object instead of a string this doesn’t work. Ideally, there will be a:
-
monthlyOSProject[0].osProjectIdno matter what, and a -
monthlyOSProject[0].osProjectif someone wrote$populate=monthlyOSProjects.osProject
I could accomplish some of this right now by using parseInstanceData to check if osProjectId is an object, if it is, create an osProject on the response, and update osProjectId = osProjectId._id. However, I don’t writing: $populate=monthlyOSProjects.osProjectId. So I could rewrite my schema, and parseInstanceData to account for this.
I’m wondering if there are other solutions to this. I considered having a ref type where you could do:
-
monthlyOSProject[0].osProjectRef._idno matter what, and a -
monthlyOSProject[0].osProjectRef.osProjectif someone wrote$populate=monthlyOSProjects.osProjectRefor someone didn’t have it, but using a getter, it could be retrieved.
