Skip to content

Hooks

HookDescription
cache

Caches get and find results based on params. On mutating methods (create, update, patch, remove), affected cache entries are automatically invalidated. Works as a before, after, or around hook.

checkMulti

Checks if the multi option is enabled for the current method and throws a MethodNotAllowed error if multi operations are not permitted. Useful to guard against accidental bulk create, patch, or remove calls.

checkRequired

Validates that the specified fields exist on context.data and are not falsy. Numeric 0 and boolean false are treated as valid values. Throws a BadRequest error if any required field is missing or null.

combine

Sequentially executes multiple hooks, passing the updated context from one to the next. Returns a single hook function that runs the entire chain. If any hook throws, the error is annotated with the current hook context.

createRelated

Creates related records in other services after a successful create call. For each result item, a data function produces the record to create in the target service. Supports creating records one-by-one or in a single multi-create when multi: true.

debug

Logs the current hook context to the console for debugging purposes. Displays timestamp, service path, method, type, id, data, query, result, and any additional param fields you specify.

disablePagination

Disables pagination when query.$limit is -1 or '-1'. Removes the $limit from the query and sets params.paginate = false. Must be used as a before or around hook on the find method.

disallow

Prevents access to a service method completely or for specific transports. When called without arguments, the method is blocked for all callers. When called with transport names, only those transports are blocked.

iff

Conditionally executes a series of hooks when the predicate is truthy. The predicate can be a boolean value or a sync/async function. Supports an .else(...) chain for the falsy branch. Also exported as when.

iffElse

Executes one array of hooks when the predicate is truthy, or another array when it is falsy. The predicate can be a boolean or a sync/async function. Unlike iff, both branches are provided upfront without chaining.

onDelete
paramsForServer

Client-side hook that moves whitelisted params properties into query._$client so they survive the client-to-server transport. The server only receives query from params — use paramsFromClient on the server to restore them.

paramsFromClient

Server-side hook that extracts whitelisted properties from query._$client back into context.params. This is the counterpart to paramsForServer, which encodes params on the client side for transport.

preventChanges

Prevents patch calls from modifying certain fields. By default, the protected fields are silently removed from context.data. When error is set, a BadRequest is thrown if any protected field is present.

setData

Sets a property on each item in context.data from another property on the hook context. Supports dot-notation paths for both source and target. Throws a Forbidden error if the source field is missing (unless allowUndefined is true).

setField

Sets a field on the hook context (e.g. params.query) based on the value of another context field (e.g. params.user.id). Useful for scoping queries to the authenticated user. Throws a Forbidden error if the source field is missing (unless allowUndefined is true).

setResult

Sets a property on each item in context.result from another property on the hook context. Supports dot-notation paths for both source and target, and can optionally operate on context.dispatch as well.

setSlug

Extracts URL route parameters (slugs) and sets them on params.query. For example, given a route /stores/:storeId, this hook copies the resolved storeId value from params.route into the query. Only applies to the rest provider.

skippable

Wraps a hook so it can be conditionally skipped based on a predicate. When the predicate returns true, the wrapped hook is skipped entirely. Commonly used with shouldSkip and addSkip for runtime hook control.

softDelete

Marks items as deleted instead of physically removing them. On remove, the hook patches the record with removeData (e.g. { deletedAt: new Date() }). On all other methods, it appends deletedQuery (e.g. { deletedAt: null }) to filter out soft-deleted items.

stashBefore

Stashes the current value of a record into params.before prior to mutation. Performs a get (single item) or find (multi) call to retrieve the existing state. Useful in update, patch, or remove hooks when you need the original data for comparison.

throwIf

Throws a BadRequest error when the given predicate function returns true. The predicate receives the hook context and can be async. Useful for validating conditions before proceeding with a request.

throwIfIsMulti

Throws a BadRequest error when the current operation is a multi operation (array data on create, or id === null on patch/remove). Useful to prevent bulk operations on services that should only handle single items.

throwIfIsProvider

Throws a MethodNotAllowed error when the request comes from one of the specified transports. Combines throwIf with the isProvider predicate for a convenient one-liner. Use this to restrict methods to server-only or specific transport types.

transformData

Transforms each item in context.data using the provided transformer function. The transformer receives each item and can mutate it in place or return a new object. Commonly used with built-in transformers like lowercase, trim, or setNow.

transformQuery

Transforms context.params.query using the provided transformer function. The transformer receives the current query and can return a modified version. Useful for normalizing, sanitizing, or enriching queries before they hit the database.

transformResult

Transforms each item in context.result using the provided transformer function. The transformer receives each item and can mutate it in place or return a new object. Optionally operates on context.dispatch via the dispatch option.

traverse

Recursively walks and transforms fields in record(s) using neotraverse. The getObject function extracts the target from the context, and transformer is called for every node during traversal --- ideal for deep, structural transformations.

unless

Executes a series of hooks when the predicate is falsy --- the inverse of iff. The predicate can be a boolean or a sync/async function. Useful for applying hooks to all contexts except those matching a condition.

Released under the MIT License.