fix(primaryKey): use model.primaryKey
instead of hardcoded id
for id
routes, fixes #9
This commit is contained in:
parent
29ee49fc62
commit
6c46ff68d0
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hapi-sequelize-crud",
|
"name": "hapi-sequelize-crud",
|
||||||
"version": "2.3.1",
|
"version": "2.4.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": {
|
||||||
|
@ -16,13 +16,13 @@ export default (server, a, b, names, options) => {
|
|||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
const instanceb = await b.findOne({
|
const instanceb = await b.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.bid,
|
[b.primaryKeyField]: request.params.bid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const instancea = await a.findOne({
|
const instancea = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,18 +28,23 @@ export const get = (server, a, b, names) => {
|
|||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
const include = parseInclude(request);
|
const include = parseInclude(request);
|
||||||
|
|
||||||
const base = a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const method = getMethod(base, names.b);
|
const method = getMethod(base, names.b);
|
||||||
|
|
||||||
const list = await method({ where: {
|
const list = await method({ where: {
|
||||||
id: request.params.bid,
|
[b.primaryKeyField]: request.params.bid,
|
||||||
}, include });
|
}, include });
|
||||||
|
|
||||||
|
if (Array.isArray(list)) {
|
||||||
|
reply(list[0]);
|
||||||
|
} else {
|
||||||
reply(list);
|
reply(list);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
config: defaultConfig,
|
config: defaultConfig,
|
||||||
@ -58,7 +63,7 @@ export const list = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -86,7 +91,7 @@ export const scope = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -159,7 +164,7 @@ export const destroy = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -188,7 +193,7 @@ export const destroyScope = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primarykeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -228,7 +233,7 @@ export const update = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ export const get = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const method = getMethod(base, names.b, false);
|
const method = getMethod(base, names.b, false);
|
||||||
@ -53,7 +53,7 @@ export const create = (server, a, b, names) => {
|
|||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.id,
|
[a.primaryKeyField]: request.params.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -79,10 +79,12 @@ export const destroy = (server, a, b, names) => {
|
|||||||
|
|
||||||
const base = await a.findOne({
|
const base = await a.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.aid,
|
[a.primaryKeyField]: request.params.aid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
where[b.primaryKeyField] = request.params.bid;
|
||||||
|
|
||||||
const method = getMethod(base, names.b, false, 'get');
|
const method = getMethod(base, names.b, false, 'get');
|
||||||
const instance = await method({ where, include });
|
const instance = await method({ where, include });
|
||||||
await instance.destroy();
|
await instance.destroy();
|
||||||
@ -110,6 +112,8 @@ export const update = (server, a, b, names) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
where[b.primaryKeyField] = request.params.bid;
|
||||||
|
|
||||||
const method = getMethod(base, names.b, false);
|
const method = getMethod(base, names.b, false);
|
||||||
|
|
||||||
const instance = await method({ where, include });
|
const instance = await method({ where, include });
|
||||||
|
@ -54,7 +54,7 @@ export const get = (server, model) => {
|
|||||||
const include = parseInclude(request);
|
const include = parseInclude(request);
|
||||||
const where = parseWhere(request);
|
const where = parseWhere(request);
|
||||||
const { id } = request.params;
|
const { id } = request.params;
|
||||||
if (id) where.id = id;
|
if (id) where[model.primaryKeyField] = id;
|
||||||
|
|
||||||
const instance = await model.findOne({ where, include });
|
const instance = await model.findOne({ where, include });
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ export const destroy = (server, model) => {
|
|||||||
@error
|
@error
|
||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
const where = parseWhere(request);
|
const where = parseWhere(request);
|
||||||
if (request.params.id) where.id = request.params.id;
|
if (request.params.id) where[model.primaryKeyField] = request.params.id;
|
||||||
|
|
||||||
const list = await model.findAll({ where });
|
const list = await model.findAll({ where });
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user