onDelete
hook to manipulate related items on delete.
This can be handled by your database, but this hook allows you to do it in your application logic. Then you get service events and hooks for the related items.
ts
import { onDelete } from 'feathers-utils/hooks';Type declaration
ts
export type OnDeleteAction = "cascade" | "set null"
export interface OnDeleteOptions<Path extends string = string> {
/**
* The related service where related items should be manipulated
*/
service: Path
/**
* The propertyKey in the related service
*/
keyThere: string
/**
* The propertyKey in the current service.
*/
keyHere: string
/**
* The action to perform on the related items.
*
* - `cascade`: remove related items
* - `set null`: set the related property to null
*/
onDelete: OnDeleteAction
/**
* If true, the hook will wait for the service to finish before continuing
*
* @default false
*/
blocking?: boolean
}
/**
* hook to manipulate related items on delete.
*
* This can be handled by your database, but this hook allows you to do it in your application logic.
* Then you get service events and hooks for the related items.
*
* @see https://utils.feathersjs.com/hooks/on-delete.html
*/
export declare const onDelete: <
S = Record<string, any>,
H extends HookContext = HookContext,
>(
options: MaybeArray<OnDeleteOptions<KeyOf<S>>>,
) => (context: H, next?: NextFunction) => Promise<void>| Argument | Type | Description |
|---|---|---|
| options | MaybeArray<OnDeleteOptions<KeyOf<S>>> |
| type | methods | multi |
|---|---|---|
| after, around | remove | yes |
The onDelete hook is used to manipulate related data when a record is deleted. This can include setting a foreign key to null or deleting related records.
Example
ts
// users.hooks.ts
import { onDelete } from "feathers-utils/hooks";
export default {
// ...
after: {
// ...
remove: [
onDelete({
service: "posts",
keyThere: "userId",
keyHere: "id",
onDelete: "setNull",
}),
],
},
};