Skip to content

data

Reads or rewrites context.data for create/update/patch.

NameCategoryDescription
checkRequired
hooks

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.

createRelated
hooks

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. They are created in a single multi-create if the related service allows it, otherwise with one call per item.

findOrCreate
hooks

A before:create (or around:create) hook that looks for an existing record before creating one.

It builds a query from the uniqueBy paths read out of context.data, runs find({ paginate: false }) on the target service, and if exactly one record matches, sets context.result to that record — short-circuiting the create. With zero matches (or array data) the create proceeds; with multiple matches the onMultiple option decides.

preventChanges
hooks

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.

Supports dot.notation for nested fields.

setData
hooks

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
hooks

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).

softDelete
hooks

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.

transformData
hooks

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.

traverse
hooks

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.

createMany
utils

Creates all given items with a single create([...]) call.

If the service does not allow multi creating (its multi option, or an explicit multi option), the items are created with one call per item instead - so it works regardless of the service's multi configuration. A single item is always created with a single call.

getDataIsArray
utils

Normalizes context.data into an array for uniform processing. Returns { data, isArray } where data is always an array and isArray indicates whether the original value was already an array.

mutateData
utils

Applies a transformer function to each item in context.data, updating it in place. If the transformer returns a new object, it replaces the original item. Correctly handles both single-item and array data, preserving the original shape.

patchBatch
utils

Batch patching utility that takes an array of items to be changed and returns an array of arguments to be called with the patch method.

This utility is useful when you need to patch multiple items with varying data in as few requests as possible.

patchMany
utils

Patches all items matching params with a single patch(null, ...) call.

If the service does not allow multi patching (its multi option, or an explicit multi option), the matching items are fetched with find and patched with one call per item instead - so it works regardless of the service's multi configuration.

The params only select which items are affected: the query is consumed by the find - which only collects the ids - and is not applied again for the per-item calls, except $select.

zipDataResult
utils

Pairs each item in context.data with its corresponding item in context.result by index. Handles both single-item and array data, normalizing them into an array of { data, result } pairs. Only works in after/around hooks for create, update, and patch methods.

resolve
resolvers

Combines data, query, and result resolvers into a single around hook. Data and query resolvers run before next(), while the result resolver runs after. At least one resolver must be provided.

resolveData
resolvers

Resolves and transforms context.data using a map of resolver functions. Each property in the resolver object receives the current value and can return a transformed value. Runs before next() in the hook pipeline.

defaults
transformers

Sets default values on an item for fields that are undefined. Values can be static or functions that return a value. Supports dot.notation for nested fields.

lowercase
transformers

Transforms the specified fields of an item to lowercase.

omit
transformers

Omit the specified fields from an item.

parseDate
transformers

Parses the specified fields of an item into Date objects.

pick
transformers

Picks the specified fields from an item.

setNow
transformers

Sets the specified fields of an item to the current date and time.

trim
transformers

Trims the specified fields of an item.

See all tags for the full vocabulary.

Released under the MIT License.