I am developing a Nodejs application and my database is Postgres and I am using Sequelize as my ORM because of its excellent support for migrations.
I am on the lookout for a good REST API generator based on the schema I have defined. There are two main hurdles I am facing and they are that the generators don't do a good job of creating association API routes and lack of ACL support. On the associations front, my schema has multiple levels of association i.e. for example..
Student.hasMany(Courses);
Courses.hasMany(Subjects);
So ideally the generated REST API should be something like
/student/:student_id/course/:course_id/subject/:subjectId
I found a few projects that are doing this, but are incomplete.
Is there any module that supports this?
I would recommend restify Its fairly simple to set up and routing is fairly easy to manage.
Somnething like the following would get you started.
server.get( '/student/:student_id/course/:course_id/subject/:subjectId', function(req, res, next) {
Subjects.find({
where: {
'id': req.params.subjectId,
'courses.id': req.params.course_id,
'student_id.id': req.params.student_id
},
include: [{
model: Courses,
include: [{
model: Student
}]
}]
}).success(function(results) {
console.log(results);
});
});
You could also cut down on the amount of routes you need to write with something like this. Personally I wouldn't recommend it however as you are sacrificing clarity and flexibility for the sake of some typing.
var models = {
'subjects': Subjects,
'courses': Courses,
'students': Students
};
server.get( '/:model/:id', function(req, res, next) {
models[req.params.model]
.find(req.params.id)
.success(function(results) {
console.log(results);
});
});