feat(simple): simple CRUD REST API (no associations)

feat(associations): one-to-one associations
feat(associations): one-to-many associations
This commit is contained in:
Mahdi Dibaiee
2016-01-18 18:08:43 +03:30
commit bae6820e64
11 changed files with 464 additions and 0 deletions

View File

@ -0,0 +1,73 @@
import joi from 'joi';
import error from '../error';
let prefix;
export default (server, a, b, options) => {
prefix = options.prefix;
list(server, a, b);
destroy(server, a, b);
update(server, a, b);
}
export const list = (server, a, b) => {
server.route({
method: 'GET',
path: `${prefix}/${a._singular}/{aid}/${b._plural}`,
@error
async handler(request, reply) {
let list = await request.models[b.name].findAll({
where: {
...request.query,
[a.name + 'Id']: request.params.aid
}
});
reply(list);
}
})
}
export const destroy = (server, a, b) => {
server.route({
method: 'DELETE',
path: `${prefix}/${a._singular}/{aid}/${b._plural}`,
@error
async handler(request, reply) {
let list = await request.models[b.name].findAll({
where: {
...request.query,
[a.name + 'Id']: request.params.aid
}
});
await* list.map(instance => instance.destroy());
reply();
}
})
}
export const update = (server, a, b) => {
server.route({
method: 'PUT',
path: `${prefix}/${a._singular}/{aid}/${b._plural}`,
@error
async handler(request, reply) {
let list = await request.models[b.name].findOne({
where: {
...request.query,
[a.name + 'Id']: request.params.aid
}
});
await* list.map(instance => instance.update(request.payload));
reply(list);
}
})
}