fix(associations): request.query should apply to second model's where

feat(associations): scope queries on associations
This commit is contained in:
Mahdi Dibaiee 2016-01-19 10:37:56 +03:30
parent 6713ed0b0b
commit 14a36434f7
2 changed files with 81 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "hapi-sequelize-crud", "name": "hapi-sequelize-crud",
"version": "1.1.0", "version": "1.2.0",
"description": "Hapi plugin that automatically generates RESTful API for CRUD", "description": "Hapi plugin that automatically generates RESTful API for CRUD",
"main": "build/index.js", "main": "build/index.js",
"config": { "config": {

View File

@ -7,6 +7,8 @@ export default (server, a, b, options) => {
prefix = options.prefix; prefix = options.prefix;
list(server, a, b); list(server, a, b);
scope(server, a, b);
scopeScope(server, a, b);
destroy(server, a, b); destroy(server, a, b);
update(server, a, b); update(server, a, b);
} }
@ -19,14 +21,12 @@ export const list = (server, a, b) => {
@error @error
async handler(request, reply) { async handler(request, reply) {
let list = await b.findAll({ let list = await b.findAll({
where: {
...request.query,
},
include: [{ include: [{
model: a, model: a,
where: { 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) => { export const destroy = (server, a, b) => {
server.route({ server.route({
method: 'DELETE', method: 'DELETE',
@ -44,14 +113,12 @@ export const destroy = (server, a, b) => {
@error @error
async handler(request, reply) { async handler(request, reply) {
let list = await b.findAll({ let list = await b.findAll({
where: {
...request.query
},
include: [{ include: [{
model: a, model: a,
where: { where: {
id: request.params.aid id: request.params.aid,
...request.query
} }
}] }]
}); });
@ -71,14 +138,12 @@ export const update = (server, a, b) => {
@error @error
async handler(request, reply) { async handler(request, reply) {
let list = await b.findOne({ let list = await b.findOne({
where: {
...request.query
},
include: [{ include: [{
model: a, model: a,
where: { where: {
id: request.params.aid id: request.params.aid,
...request.query
} }
}] }]
}); });