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:
4
src/associations/index.js
Normal file
4
src/associations/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
import oneToOne from './one-to-one';
|
||||
import oneToMany from './one-to-many';
|
||||
|
||||
export { oneToOne, oneToMany };
|
0
src/associations/many-to-many.js
Normal file
0
src/associations/many-to-many.js
Normal file
73
src/associations/one-to-many.js
Normal file
73
src/associations/one-to-many.js
Normal 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);
|
||||
}
|
||||
})
|
||||
}
|
89
src/associations/one-to-one.js
Normal file
89
src/associations/one-to-one.js
Normal file
@ -0,0 +1,89 @@
|
||||
import joi from 'joi';
|
||||
import error from '../error';
|
||||
|
||||
let prefix;
|
||||
|
||||
export default (server, a, b, options) => {
|
||||
prefix = options.prefix;
|
||||
|
||||
get(server, a, b);
|
||||
create(server, a, b);
|
||||
destroy(server, a, b);
|
||||
update(server, a, b);
|
||||
}
|
||||
|
||||
export const get = (server, a, b) => {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: `${prefix}/${a._singular}/{aid}/${b._singular}/{bid}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let instance = await request.models[b.name].findOne({
|
||||
where: {
|
||||
id: request.params.bid,
|
||||
[a.name + 'Id']: request.params.aid
|
||||
}
|
||||
});
|
||||
|
||||
reply(instance);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const create = (server, a, b) => {
|
||||
server.route({
|
||||
method: 'POST',
|
||||
path: `${prefix}/${a._singular}/{id}/${b._singular}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
request.payload[a.name + 'Id'] = request.params.id;
|
||||
let instance = await request.models[b.name].create(request.payload);
|
||||
|
||||
reply(instance);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const destroy = (server, a, b) => {
|
||||
server.route({
|
||||
method: 'DELETE',
|
||||
path: `${prefix}/${a._singular}/{aid}/${b._singular}/{bid}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let instance = await request.models[b.name].findOne({
|
||||
where: {
|
||||
id: request.params.bid,
|
||||
[a.name + 'Id']: request.params.aid
|
||||
}
|
||||
});
|
||||
|
||||
await instance.destroy();
|
||||
|
||||
reply();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const update = (server, a, b) => {
|
||||
server.route({
|
||||
method: 'PUT',
|
||||
path: `${prefix}/${a._singular}/{aid}/${b._singular}/{bid}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let instance = await request.models[b.name].findOne({
|
||||
where: {
|
||||
id: request.params.bid,
|
||||
[a.name + 'Id']: request.params.aid
|
||||
}
|
||||
});
|
||||
|
||||
await instance.update(request.payload);
|
||||
|
||||
reply(instance);
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user