From 973f63640cc49f8e205d0d952f088376a30b2b78 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 19 Apr 2016 11:39:59 +0430 Subject: [PATCH] fix(destroy): associative `remove` methods require the instance, they don't accept queries, so fallback to manual destroy --- package.json | 2 +- src/associations/one-to-many.js | 8 ++++++-- src/associations/one-to-one.js | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 9030f0e..57b76a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hapi-sequelize-crud", - "version": "2.0.5", + "version": "2.0.6", "description": "Hapi plugin that automatically generates RESTful API for CRUD", "main": "build/index.js", "config": { diff --git a/src/associations/one-to-many.js b/src/associations/one-to-many.js index c4aff1c..70b8edd 100644 --- a/src/associations/one-to-many.js +++ b/src/associations/one-to-many.js @@ -157,8 +157,11 @@ export const destroy = (server, a, b, names) => { } }); - const method = getMethod(base, names.b, true, 'destroy'); + const method = getMethod(base, names.b, true, 'get'); const list = await method({ where, include }); + await Promise.all(list.map(item => + item.destroy() + )); reply(list); } @@ -183,7 +186,8 @@ export const destroyScope = (server, a, b, names) => { } }); - const method = getMethod(base, names.b, true, 'destroy'); + const method = getMethod(base, names.b, true, 'get'); + 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 5702270..eb7f564 100644 --- a/src/associations/one-to-one.js +++ b/src/associations/one-to-one.js @@ -79,9 +79,9 @@ export const destroy = (server, a, b, names) => { } }); - const method = getMethod(base, names.b, false, 'destroy'); - + const method = getMethod(base, names.b, false, 'get'); const instance = await method({ where, include }); + await instance.destroy(); reply(instance); }