Skip to content

Commit f35d8b6

Browse files
authored
Merge pull request #13 from fratzinger/feat/onDelete
feat: onDelete
2 parents 193ae3a + 316ecd7 commit f35d8b6

10 files changed

Lines changed: 1536 additions & 543 deletions

File tree

package-lock.json

Lines changed: 916 additions & 512 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,30 @@
3939
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
4040
},
4141
"dependencies": {
42-
"@feathersjs/adapter-commons": "^4.5.12",
43-
"@feathersjs/errors": "^4.5.12",
44-
"@feathersjs/feathers": "^4.5.12",
45-
"feathers-hooks-common": "^5.0.6",
42+
"@feathersjs/adapter-commons": "^4.5.15",
43+
"@feathersjs/errors": "^4.5.15",
44+
"@feathersjs/feathers": "^4.5.15",
45+
"feathers-hooks-common": "^6.1.5",
4646
"lodash": "^4.17.21"
4747
},
4848
"devDependencies": {
4949
"@istanbuljs/nyc-config-typescript": "^1.0.2",
50-
"@types/lodash": "^4.14.179",
51-
"@types/mocha": "^9.1.0",
52-
"@types/node": "^17.0.21",
53-
"@typescript-eslint/eslint-plugin": "^5.13.0",
54-
"@typescript-eslint/parser": "^5.13.0",
50+
"@types/lodash": "^4.14.184",
51+
"@types/mocha": "^9.1.1",
52+
"@types/node": "^18.7.14",
53+
"@typescript-eslint/eslint-plugin": "^5.36.0",
54+
"@typescript-eslint/parser": "^5.36.0",
5555
"cross-env": "^7.0.3",
56-
"eslint": "^8.10.0",
57-
"eslint-import-resolver-typescript": "^2.5.0",
58-
"eslint-plugin-import": "^2.25.4",
59-
"eslint-plugin-security": "^1.4.0",
56+
"eslint": "^8.23.0",
57+
"eslint-import-resolver-typescript": "^3.5.0",
58+
"eslint-plugin-import": "^2.26.0",
59+
"eslint-plugin-security": "^1.5.0",
6060
"feathers-memory": "^4.1.0",
61-
"mocha": "^9.2.1",
62-
"np": "^7.6.0",
61+
"mocha": "^10.0.0",
62+
"np": "^7.6.2",
6363
"nyc": "^15.1.0",
6464
"shx": "^0.3.4",
65-
"ts-node": "^10.5.0",
66-
"typescript": "^4.6.2"
65+
"ts-node": "^10.9.1",
66+
"typescript": "^4.8.2"
6767
}
6868
}

src/hooks/onDelete.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { HookContext } from "@feathersjs/feathers";
2+
import { checkContext } from "feathers-hooks-common";
3+
import type { OnDeleteOptions } from "../types";
4+
import { getItemsIsArray } from "../utils/getItemsIsArray";
5+
6+
export function onDelete<S = Record<string, any>>(
7+
service: keyof S,
8+
{
9+
keyThere,
10+
keyHere = "id",
11+
onDelete = "cascade",
12+
blocking = true
13+
}: OnDeleteOptions) {
14+
if (!service || !keyThere) {
15+
throw "initialize hook 'removeRelated' completely!";
16+
}
17+
if (!["cascade", "set null"].includes(onDelete)) {
18+
throw "onDelete must be 'cascade' or 'set null'";
19+
}
20+
21+
return async (context: HookContext): Promise<HookContext> => {
22+
checkContext(context, "after", "remove", "onDelete");
23+
24+
const { items } = getItemsIsArray(context);
25+
26+
let ids = items.map(x => x[keyHere]).filter(x => !!x);
27+
ids = [...new Set(ids)];
28+
29+
if (!ids || ids.length <= 0) { return context; }
30+
31+
const params = {
32+
query: {
33+
[keyThere]: {
34+
$in: ids
35+
}
36+
},
37+
paginate: false
38+
};
39+
40+
let promise;
41+
42+
if (onDelete === "cascade") {
43+
promise = context.app.service(service as string).remove(null, params);
44+
} else if (onDelete === "set null") {
45+
const data = { [keyThere]: null };
46+
promise = context.app.service(service as string).patch(null, data, params);
47+
}
48+
49+
if (blocking) {
50+
await promise;
51+
}
52+
53+
return context;
54+
};
55+
}

src/hooks/setData.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import _get from "lodash/get";
2-
import _set from "lodash/set";
3-
import _has from "lodash/has";
1+
import _get from "lodash/get.js";
2+
import _set from "lodash/set.js";
3+
import _has from "lodash/has.js";
44

55
import { Forbidden } from "@feathersjs/errors";
66
import { getItemsIsArray } from "../utils/getItemsIsArray";

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export { setResultEmpty } from "./utils/setResultEmpty";
3838
export { markHookForSkip } from "./utils/markHookForSkip";
3939
export { filterQuery } from "./utils/filterQuery";
4040
export { getItemsIsArray } from "./utils/getItemsIsArray";
41+
export { onDelete } from "./hooks/onDelete";
4142
export { shouldSkip } from "./utils/shouldSkip";
4243

4344

src/mixins/debounce-mixin/DebouncedStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _debounce from "lodash/debounce";
1+
import _debounce from "lodash/debounce.js";
22

33
import type { DebouncedFunc } from "lodash";
44
import type { Application, Id } from "@feathersjs/feathers";

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export interface CreateRelatedOptions<S = Record<string, any>> {
5050
createItemsInDataArraySeparately?: boolean
5151
}
5252

53+
export type OnDeleteAction = "cascade" | "set null";
54+
55+
export interface OnDeleteOptions {
56+
keyThere: string
57+
keyHere: string
58+
onDelete: OnDeleteAction
59+
blocking?: boolean
60+
}
61+
5362
//#endregion
5463

5564
//#region mixins

src/utils/mergeQuery/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import _get from "lodash/get";
2-
import _has from "lodash/has";
3-
import _isEmpty from "lodash/isEmpty";
4-
import _isEqual from "lodash/isEqual";
5-
import _merge from "lodash/merge";
6-
import _set from "lodash/set";
7-
import _uniqWith from "lodash/uniqWith";
1+
import _get from "lodash/get.js";
2+
import _has from "lodash/has.js";
3+
import _isEmpty from "lodash/isEmpty.js";
4+
import _isEqual from "lodash/isEqual.js";
5+
import _merge from "lodash/merge.js";
6+
import _set from "lodash/set.js";
7+
import _uniqWith from "lodash/uniqWith.js";
88

99
import { mergeArrays } from "./mergeArrays";
1010
import { filterQuery } from "../filterQuery";

src/utils/pushSet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import _isEqual from "lodash/isEqual";
2-
import _get from "lodash/get";
3-
import _set from "lodash/set";
1+
import _isEqual from "lodash/isEqual.js";
2+
import _get from "lodash/get.js";
3+
import _set from "lodash/set.js";
44

55
import type {
66
Path,

0 commit comments

Comments
 (0)