fix(associations): request.query should apply to second model's where
feat(associations): scope queries on associations
This commit is contained in:
parent
6713ed0b0b
commit
14a36434f7
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hapi-sequelize-crud",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "Hapi plugin that automatically generates RESTful API for CRUD",
|
||||
"main": "build/index.js",
|
||||
"config": {
|
||||
|
@ -7,6 +7,8 @@ export default (server, a, b, options) => {
|
||||
prefix = options.prefix;
|
||||
|
||||
list(server, a, b);
|
||||
scope(server, a, b);
|
||||
scopeScope(server, a, b);
|
||||
destroy(server, a, b);
|
||||
update(server, a, b);
|
||||
}
|
||||
@ -19,14 +21,12 @@ export const list = (server, a, b) => {
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let list = await b.findAll({
|
||||
where: {
|
||||
...request.query,
|
||||
},
|
||||
|
||||
include: [{
|
||||
model: a,
|
||||
where: {
|
||||
id: request.params.aid
|
||||
id: request.params.aid,
|
||||
|
||||
...request.query
|
||||
}
|
||||
}]
|
||||
});
|
||||
@ -36,6 +36,75 @@ export const list = (server, a, b) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const scope = (server, a, b) => {
|
||||
let scopes = Object.keys(b.options.scopes);
|
||||
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: `${prefix}/${a._singular}/{aid}/${b._plural}/{scope}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let list = await b.scope(request.params.scope).findAll({
|
||||
include: [{
|
||||
model: a,
|
||||
where: {
|
||||
id: request.params.aid,
|
||||
|
||||
...request.query
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
||||
reply(list);
|
||||
},
|
||||
|
||||
config: {
|
||||
validate: {
|
||||
params: joi.object().keys({
|
||||
scope: joi.string().valid(...scopes),
|
||||
aid: joi.number().integer().required()
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const scopeScope = (server, a, b) => {
|
||||
let scopes = {
|
||||
a: Object.keys(a.options.scopes),
|
||||
b: Object.keys(b.options.scopes)
|
||||
};
|
||||
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: `${prefix}/${a._plural}/{scopea}/${b._plural}/{scopeb}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let list = await b.scope(request.params.scopeb).findAll({
|
||||
include: [{
|
||||
model: a.scope(request.params.scopea),
|
||||
where: {
|
||||
...request.query
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
reply(list);
|
||||
},
|
||||
|
||||
config: {
|
||||
validate: {
|
||||
params: joi.object().keys({
|
||||
scopea: joi.string().valid(...scopes.a),
|
||||
scopeb: joi.string().valid(...scopes.b)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const destroy = (server, a, b) => {
|
||||
server.route({
|
||||
method: 'DELETE',
|
||||
@ -44,14 +113,12 @@ export const destroy = (server, a, b) => {
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let list = await b.findAll({
|
||||
where: {
|
||||
...request.query
|
||||
},
|
||||
|
||||
include: [{
|
||||
model: a,
|
||||
where: {
|
||||
id: request.params.aid
|
||||
id: request.params.aid,
|
||||
|
||||
...request.query
|
||||
}
|
||||
}]
|
||||
});
|
||||
@ -71,14 +138,12 @@ export const update = (server, a, b) => {
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let list = await b.findOne({
|
||||
where: {
|
||||
...request.query
|
||||
},
|
||||
|
||||
include: [{
|
||||
model: a,
|
||||
where: {
|
||||
id: request.params.aid
|
||||
id: request.params.aid,
|
||||
|
||||
...request.query
|
||||
}
|
||||
}]
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user