dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

associations: subResourceName is set by association.as if available #234

Closed wroberts closed 1 year ago

wroberts commented 6 years ago

I think it makes sense to be able to decide what the subResourceName of an association is. For instance, imagine that I have:

var Person = sequelize.define('person', { name: { type: DataTypes.STRING } }),
    Course = sequelize.define('course', { name: { type: DataTypes.STRING } });
Course.hasMany(Person, {as: 'students'});
Course.hasOne(Person, { as: 'teacher' });
epilogue.resource({model: Course, 
    endpoints: ['/courses', '/courses/:id'], 
    associations: true});

Under the current logic, the association Resources are named according to the model name, inflected for the number of the association. I think with my example here, we would get /courses/:id/person and /courses/:id/people/. Intuitively, though, it seems like we have gone to the trouble of naming the association in the model, and so we should get routes like /courses/:id/students/ and /courses/:id/teacher.

This patch checks the association.as field before setting the subResourceName.