From 544dc2372396e75f09b156a45f85d1c1f0a756a1 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Thu, 31 Mar 2016 20:40:01 +0430 Subject: [PATCH] fix(associate): associating one way is enough fix(destroy): directly get a `destroy` method instead of find and destroy --- package.json | 2 +- src/associations/associate.js | 10 +++------- src/associations/one-to-many.js | 6 ++---- src/associations/one-to-one.js | 3 +-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 020dea3..9030f0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hapi-sequelize-crud", - "version": "2.0.4", + "version": "2.0.5", "description": "Hapi plugin that automatically generates RESTful API for CRUD", "main": "build/index.js", "config": { diff --git a/src/associations/associate.js b/src/associations/associate.js index 5514ea5..92667b5 100644 --- a/src/associations/associate.js +++ b/src/associations/associate.js @@ -26,13 +26,9 @@ export default (server, a, b, names, options) => { } }); - const fna = getMethod(instancea, names.b, false, 'add') || - getMethod(instancea, names.b, false, 'set'); - const fnb = getMethod(instanceb, names.a, false, 'add') || - getMethod(instanceb, names.a, false, 'set'); - - fnb(instancea); - fna(instanceb); + const fn = getMethod(instancea, names.b, false, 'add') || + getMethod(instancea, names.b, false, 'set'); + await fn(instanceb); reply([instancea, instanceb]); } diff --git a/src/associations/one-to-many.js b/src/associations/one-to-many.js index 36798d8..c4aff1c 100644 --- a/src/associations/one-to-many.js +++ b/src/associations/one-to-many.js @@ -157,11 +157,9 @@ export const destroy = (server, a, b, names) => { } }); - const method = getMethod(base, names.b); + const method = getMethod(base, names.b, true, 'destroy'); const list = await method({ where, include }); - await* list.map(instance => instance.destroy()); - reply(list); } }) @@ -185,7 +183,7 @@ export const destroyScope = (server, a, b, names) => { } }); - const method = getMethod(base, names.b); + const method = getMethod(base, names.b, true, 'destroy'); const list = await method({ scope: request.params.scope, where, diff --git a/src/associations/one-to-one.js b/src/associations/one-to-one.js index 6994667..5702270 100644 --- a/src/associations/one-to-one.js +++ b/src/associations/one-to-one.js @@ -79,10 +79,9 @@ export const destroy = (server, a, b, names) => { } }); - const method = getMethod(base, names.b, false); + const method = getMethod(base, names.b, false, 'destroy'); const instance = await method({ where, include }); - await instance.destroy(); reply(instance); }