Search.../

beforeRemove( )

A hook that is called before a remove() operation.

Description

The beforeRemove() hook runs when:

  • The remove() function is called.
  • An action is performed on a dataset that removes an item from the collection.
  • An item is deleted using the CMS.

The hook also runs when an action is performed on a dataset that removes an item from the collection that the dataset is connected to.

Return a string or a Promise that resolves to a string from the beforeRemove() function. The returned string will be used as the itemId parameter for the remove() operation. The item with the new itemId will be removed instead of the item with the original itemId.

If the returned value is of the wrong type, the value is ignored.

A rejected Promise blocks the call to remove() and also calls the onFailure() hook if it has been registered.

Because the beforeRemove() hook is called before remove() is executed, it can affect the item that is removed from the collection or block the remove().

Syntax

function beforeRemove(itemId: string, context: UpdateHookContext): Promise<string> | string

beforeRemove Parameters

NAME
TYPE
DESCRIPTION
itemId
string

The ID of the original item to be removed.

context
UpdateHookContext

Contextual information about the hook.

Returns

The ID to be used for the remove() instead of the original itemId specified by the caller. Returning a rejected promise will block the operation and will return a rejected promise to the caller as well as trigger the onFailure() hook.

Return Type:

Promise<string>

 | 

string

Was this helpful?

A beforeRemove hook

Copy Code
1// In data.js
2
3export function myCollection_beforeRemove(itemId, context) {
4 let hookContext = context; // see below
5
6 // change to the item to remove
7
8 return itemId;
9}
10
11/*
12 * hookContext:
13 *
14 * {
15 * "collectionName": "myCollection",
16 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
17 * "userRole": "siteOwner"
18 * }
19 */
Change the item to remove using a beforeRemove hook

Copy Code
1// In data.js
2
3export function myCollection_beforeRemove(itemId, context) {
4 let hookContext = context; // see below
5
6 // change to the item to remove
7 let newItemId = "1234";
8
9 return newItemId;
10}
11
12/*
13 * hookContext:
14 *
15 * {
16 * "collectionName": "myCollection",
17 * "userId": "f45jf8d2-grkj-2opd-4ovk-9rfj4wo5tvj3",
18 * "userRole": "siteOwner"
19 * }
20 */