Skip to content

Commit

Permalink
Add beforeDelete transform
Browse files Browse the repository at this point in the history
The id list is deliberately not replaced with the value returned from
the transform as the user should not be able to silently remove ids from
the id list. Therefore, this 'transform' should be used purely as a way
to throw errors when the requested deletion is not allowed.

Closes #66
  • Loading branch information
carlbennettnz committed Aug 31, 2015
1 parent 60e6931 commit 22fbcc5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ResourceTypeRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* whose property is being retrieved/set, and the value to set it to, if any.
*/
const autoGetterSetterProps = ["dbAdapter", "beforeSave", "beforeRender",
"labelMappers", "defaultIncludes", "info", "parentType"];
"beforeDelete", "labelMappers", "defaultIncludes", "info", "parentType"];

/**
* To fulfill a JSON API request, you often need to know about all the resources
Expand Down
23 changes: 23 additions & 0 deletions src/controllers/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import co from "co";

import Response from "../types/HTTP/Response";
import Document from "../types/Document";
import Resource from "../types/Resource";
import Collection from "../types/Collection";
import APIError from "../types/APIError";

Expand Down Expand Up @@ -114,6 +115,28 @@ class APIController {
}
}

if (request.method === "delete") {
let toTransform;

if (Array.isArray(request.idOrIds)) {
toTransform = new Collection(
request.idOrIds.map((id) => {
if (typeof id === "string") {
return new Resource(request.type, id);
}
})
);
}

else if (typeof request.idOrIds === "string") {
toTransform = new Resource(request.type, request.idOrIds);
}

yield applyTransform(
toTransform, "beforeDelete", registry, frameworkReq, frameworkRes
);
}

// Actually fulfill the request!
// If we've already populated the primary resources, which is possible
// because the label may have mapped to no id(s), we don't need to query.
Expand Down

0 comments on commit 22fbcc5

Please sign in to comment.